Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improvements on views. Now user can prepend a CTE (Commom table

expression) on view's definition.

User now can create a single reference containing a expression that
defines the entire view. Once defined the definition expression the view
cannot receive any other references.

Minor improvements on permissions.
  • Loading branch information...
commit 45163a3af39bf24299fe0ee09a8e363ee4db666d 1 parent feb2531
@rkhaotix rkhaotix authored
Showing with 463 additions and 221 deletions.
  1. +1 −0  conf/defaults/sql-highlight.conf
  2. +1 −0  conf/sql-highlight.conf
  3. +9 −2 libobjrenderer/src/graphicalview.cpp
  4. +49 −16 libpgmodeler/src/databasemodel.cpp
  5. +1 −0  libpgmodeler/src/permission.cpp
  6. +19 −11 libpgmodeler/src/reference.cpp
  7. +12 −1 libpgmodeler/src/reference.h
  8. +152 −70 libpgmodeler/src/view.cpp
  9. +16 −2 libpgmodeler/src/view.h
  10. BIN  libpgmodeler_ui/res/icones/permission_grp.png
  11. +1 −0  libpgmodeler_ui/res/resources.qrc
  12. +4 −4 libpgmodeler_ui/src/constraintwidget.cpp
  13. +14 −12 libpgmodeler_ui/src/modelobjectswidget.cpp
  14. +12 −12 libpgmodeler_ui/src/objecttablewidget.cpp
  15. +2 −2 libpgmodeler_ui/src/objecttablewidget.h
  16. +3 −3 libpgmodeler_ui/src/relationshipwidget.cpp
  17. +1 −1  libpgmodeler_ui/src/syntaxhighlighter.cpp
  18. +3 −3 libpgmodeler_ui/src/tablewidget.cpp
  19. +3 −3 libpgmodeler_ui/src/triggerwidget.cpp
  20. +41 −16 libpgmodeler_ui/src/viewwidget.cpp
  21. +3 −2 libpgmodeler_ui/src/viewwidget.h
  22. +86 −45 libpgmodeler_ui/ui/viewwidget.ui
  23. +4 −1 libutil/src/exception.cpp
  24. +5 −2 libutil/src/exception.h
  25. +3 −2 libutil/src/parsersattributes.h
  26. +9 −2 schemas/sql/common/view.sch
  27. +2 −2 schemas/xml/dtd/baseelements.dtd
  28. +1 −6 schemas/xml/reference.sch
  29. +6 −1 schemas/xml/view.sch
View
1  conf/defaults/sql-highlight.conf
@@ -245,6 +245,7 @@
<element value="PRIMARY"/>
<element value="PROCEDURAL"/>
<element value="PROCEDURE"/>
+ <element value="PUBLIC"/>
<element value="RANGE"/>
<element value="RECEIVE"/>
<element value="RECHECK"/>
View
1  conf/sql-highlight.conf
@@ -245,6 +245,7 @@
<element value="PRIMARY"/>
<element value="PROCEDURAL"/>
<element value="PROCEDURE"/>
+ <element value="PUBLIC"/>
<element value="RANGE"/>
<element value="RECEIVE"/>
<element value="RECHECK"/>
View
11 libobjrenderer/src/graphicalview.cpp
@@ -38,7 +38,7 @@ void GraphicalView::configureObject(void)
{
View *view=dynamic_cast<View *>(this->getSourceObject());
QPolygonF pol;
- int i, count;
+ int i, count, count1=0;
Reference ref;
float width;
QPen pen;
@@ -51,6 +51,10 @@ void GraphicalView::configureObject(void)
//Gets the reference count on SELECT part of the SQL definition
count=view->getReferenceCount(Reference::SQL_REFER_SELECT);
+ if(count==0)
+ count=count1=view->getReferenceCount(Reference::SQL_VIEW_DEFINITION);
+
+
//Moves the references group to the origin to be moved latter
references->moveBy(-references->scenePos().x(),
-references->scenePos().y());
@@ -59,7 +63,10 @@ void GraphicalView::configureObject(void)
for(i=0; i < count; i++)
{
- ref=view->getReference(i, Reference::SQL_REFER_SELECT);
+ if(count1==0)
+ ref=view->getReference(i, Reference::SQL_REFER_SELECT);
+ else
+ ref=view->getReference(i, Reference::SQL_VIEW_DEFINITION);
//Reuses the subitem if it was allocated before
if(!subitems.isEmpty() && i < subitems.size())
View
65 libpgmodeler/src/databasemodel.cpp
@@ -662,21 +662,21 @@ void DatabaseModel::setProtected(bool value)
void DatabaseModel::destroyObjects(void)
{
- ObjectType types[20]={
+ ObjectType types[]={
BASE_RELATIONSHIP,OBJ_RELATIONSHIP, OBJ_TABLE, OBJ_VIEW,
OBJ_AGGREGATE, OBJ_OPERATOR,
OBJ_SEQUENCE, OBJ_CONVERSION,
OBJ_CAST, OBJ_OPFAMILY, OBJ_OPCLASS,
BASE_RELATIONSHIP, OBJ_TEXTBOX,
OBJ_DOMAIN, OBJ_TYPE, OBJ_FUNCTION, OBJ_SCHEMA,
- OBJ_LANGUAGE, OBJ_TABLESPACE, OBJ_ROLE };
+ OBJ_LANGUAGE, OBJ_TABLESPACE, OBJ_ROLE, OBJ_PERMISSION };
vector<BaseObject *> *list=NULL;
BaseObject *object=NULL;
- unsigned i;
+ unsigned i, cnt=sizeof(types)/sizeof(ObjectType);
disconnectRelationships();
- for(i=0; i < 20; i++)
+ for(i=0; i < cnt; i++)
{
list=getObjectList(types[i]);
@@ -4757,6 +4757,7 @@ View *DatabaseModel::createView(void)
vector<Reference> refs;
unsigned type;
int ref_idx, i, count;
+ bool refs_in_expr=false;
try
{
@@ -4835,22 +4836,32 @@ View *DatabaseModel::createView(void)
{
XMLParser::savePosition();
XMLParser::getElementAttributes(attribs);
+ XMLParser::accessElement(XMLParser::CHILD_ELEMENT);
- if(attribs[ParsersAttributes::TYPE]==ParsersAttributes::SELECT_EXP)
- type=Reference::SQL_REFER_SELECT;
- else if(attribs[ParsersAttributes::TYPE]==ParsersAttributes::FROM_EXP)
- type=Reference::SQL_REFER_FROM;
+ if(attribs[ParsersAttributes::TYPE]==ParsersAttributes::CTE_EXPRESSION)
+ view->setCommomTableExpression(XMLParser::getElementContent());
else
- type=Reference::SQL_REFER_WHERE;
+ {
+ if(attribs[ParsersAttributes::TYPE]==ParsersAttributes::SELECT_EXP)
+ type=Reference::SQL_REFER_SELECT;
+ else if(attribs[ParsersAttributes::TYPE]==ParsersAttributes::FROM_EXP)
+ type=Reference::SQL_REFER_FROM;
+ else
+ type=Reference::SQL_REFER_WHERE;
- XMLParser::accessElement(XMLParser::CHILD_ELEMENT);
- list_aux=XMLParser::getElementContent().split(',');
- count=list_aux.size();
- for(i=0; i < count; i++)
- {
- ref_idx=list_aux[i].toInt();
- view->addReference(refs[ref_idx],type);
+ list_aux=XMLParser::getElementContent().split(',');
+ count=list_aux.size();
+
+ //Indicates that some of the references were used in the expressions
+ if(cout > 0 && !refs_in_expr)
+ refs_in_expr=true;
+
+ for(i=0; i < count; i++)
+ {
+ ref_idx=list_aux[i].toInt();
+ view->addReference(refs[ref_idx],type);
+ }
}
XMLParser::restorePosition();
@@ -4859,6 +4870,28 @@ View *DatabaseModel::createView(void)
}
while(XMLParser::accessElement(XMLParser::NEXT_ELEMENT));
}
+
+ /** Special case for refereces used as view definition **
+
+ If the flag 'refs_in_expr' isn't set indicates that the user defined a reference
+ but no used to define the view declaration, this way pgModeler will consider these
+ references as View definition expressions and will force the insertion them as
+ Reference::SQL_VIEW_DEFINITION.
+
+ This process can raise errors because if the user defined more than one reference the view
+ cannot accept the two as it's SQL definition, also the defined references MUST be expressions in
+ order to be used as view definition */
+ if(!refs.empty() && !refs_in_expr)
+ {
+ vector<Reference>::iterator itr;
+
+ itr=refs.begin();
+ while(itr!=refs.end())
+ {
+ view->addReference(*itr, Reference::SQL_VIEW_DEFINITION);
+ itr++;
+ }
+ }
}
catch(Exception &e)
{
View
1  libpgmodeler/src/permission.cpp
@@ -145,6 +145,7 @@ void Permission::setPrivilege(unsigned priv_id, bool value, bool grant_op)
privileges[priv_id]=value;
this->grant_option[priv_id]=grant_op;
+ generatePermissionId();
}
void Permission::setRevoke(bool value)
View
30 libpgmodeler/src/reference.cpp
@@ -22,6 +22,7 @@ Reference::Reference(void)
{
this->table=NULL;
this->column=NULL;
+ this->is_def_expr=false;
}
Reference::Reference(Table *table, Column *column, const QString &tab_alias, const QString &col_alias)
@@ -43,6 +44,7 @@ Reference::Reference(Table *table, Column *column, const QString &tab_alias, con
this->column=column;
this->alias=tab_alias;
this->column_alias=col_alias;
+ this->is_def_expr=false;
}
Reference::Reference(const QString &expression, const QString &expr_alias)
@@ -51,13 +53,24 @@ Reference::Reference(const QString &expression, const QString &expr_alias)
if(expression=="")
throw Exception(ERR_ASG_INV_EXPR_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
//Raises an error if the expression alias has an invalid name
- else if(!BaseObject::isValidName(expr_alias))
+ else if(!expr_alias.isEmpty() && !BaseObject::isValidName(expr_alias))
throw Exception(ERR_ASG_INV_NAME_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
table=NULL;
column=NULL;
alias=expr_alias;
this->expression=expression;
+ this->is_def_expr=false;
+}
+
+void Reference::setDefinitionExpression(bool value)
+{
+ is_def_expr=value;
+}
+
+bool Reference::isDefinitionExpression(void)
+{
+ return(is_def_expr);
}
Table *Reference::getTable(void)
@@ -107,16 +120,10 @@ QString Reference::getSQLDefinition(unsigned sql_type)
if(refer_type==REFER_COLUMN)
{
/* Generated SQL definition:
- {TABLE_NAME|TABLE_ALIAS}.{COLUMN_NAME | *} [AS COLUMN_ALIAS] */
+ [TABLE_ALIAS.]{COLUMN_NAME | *} [AS COLUMN_ALIAS] */
- //Use the real table name when its alias isn't defined
- if(alias=="")
- tab_name=table->getName(true);
- else
- //Use the table alias when its not empty
- tab_name=BaseObject::formatName(alias);
-
- tab_name+=".";
+ if(!alias.isEmpty())
+ tab_name=BaseObject::formatName(alias) + ".";
/* Case there is no column definede the default behavior is consider
all the table columns (e.g. table.*) */
@@ -227,7 +234,8 @@ bool Reference::operator == (Reference &refer)
else
{
return(this->expression==refer.expression &&
- this->alias==refer.alias);
+ this->alias==refer.alias &&
+ this->is_def_expr==refer.is_def_expr);
}
}
else
View
13 libpgmodeler/src/reference.h
@@ -44,6 +44,9 @@ class Reference {
//! \brief Stores only the alias for the column
column_alias;
+ //! \brief Indicates if the expression is used as entire view definition
+ bool is_def_expr;
+
public:
//! \brief Constants used to define the reference type
static const unsigned REFER_COLUMN=0, //! \brief The reference is based on a table column
@@ -52,7 +55,8 @@ class Reference {
//! \brief Constants used on the view code generation
static const unsigned SQL_REFER_WHERE=10,
SQL_REFER_SELECT=20,
- SQL_REFER_FROM=30;
+ SQL_REFER_FROM=30,
+ SQL_VIEW_DEFINITION=40;
Reference(void);
@@ -62,6 +66,10 @@ class Reference {
//! \brief Creates a reference based on a expression
Reference(const QString &expression, const QString &expr_alias);
+ /*! \brief Changes the behavior of the expression. Calling this method will cause the
+ reference to be used as the entire view SQL definition */
+ void setDefinitionExpression(bool value);
+
//! \brief Gets the referenced table
Table *getTable(void);
@@ -86,6 +94,9 @@ class Reference {
//! \brief Returns the XML code definition
QString getXMLDefinition(void);
+ //! \brief Indicates if the reference when used as expression defines the entire view (raw sql definition)
+ bool isDefinitionExpression(void);
+
/*! \brief Compare the attributes of two references returning true
when they have the same values */
bool operator == (Reference &refer);
View
222 libpgmodeler/src/view.cpp
@@ -25,7 +25,33 @@ View::View(void) : BaseTable()
attributes[ParsersAttributes::REFERENCES]="";
attributes[ParsersAttributes::SELECT_EXP]="";
attributes[ParsersAttributes::FROM_EXP]="";
- attributes[ParsersAttributes::EXP_SIMPLES]="";
+ attributes[ParsersAttributes::SIMPLE_EXP]="";
+ attributes[ParsersAttributes::CTE_EXPRESSION]="";
+}
+
+void View::setCommomTableExpression(const QString &expr)
+{
+ cte_expression=expr;
+}
+
+bool View::hasDefinitionExpression(void)
+{
+ vector<Reference>::iterator itr;
+ bool found=false;
+
+ itr=references.begin();
+ while(itr!=references.end() && !found)
+ {
+ found=((*itr).isDefinitionExpression());
+ itr++;
+ }
+
+ return(found);
+}
+
+QString View::getCommomTableExpression(void)
+{
+ return(cte_expression);
}
int View::getReferenceIndex(Reference &refer)
@@ -54,8 +80,10 @@ vector<unsigned> *View::getExpressionList(unsigned sql_type)
return(&exp_select);
else if(sql_type==Reference::SQL_REFER_FROM)
return(&exp_from);
- else
+ else if(sql_type==Reference::SQL_REFER_WHERE)
return(&exp_where);
+ else
+ return(NULL);
}
void View::addReference(Reference &refer, unsigned sql_type, int expr_id)
@@ -64,6 +92,23 @@ void View::addReference(Reference &refer, unsigned sql_type, int expr_id)
vector<unsigned> *expr_list=NULL;
Column *col=NULL;
+ //Specific tests for expressions used as view definition
+ if(sql_type==Reference::SQL_VIEW_DEFINITION)
+ {
+ //Raises an error if the expression is empty
+ if(refer.getExpression().isEmpty())
+ throw Exception(ERR_INV_VIEW_DEF_EXPRESSION,__PRETTY_FUNCTION__,__FILE__,__LINE__);
+ //Raises an error if already exists a definition expression
+ else if(hasDefinitionExpression())
+ throw Exception(ERR_ASG_SEC_VIEW_DEF_EXPRESSION,__PRETTY_FUNCTION__,__FILE__,__LINE__);
+ //Raises an error if the user try to add a definition expression when already exists another references
+ else if(!references.empty())
+ throw Exception(ERR_MIX_VIEW_DEF_EXPR_REFS,__PRETTY_FUNCTION__,__FILE__,__LINE__);
+ }
+ //Raises an error if the user try to add a ordinary reference when there is a reference used as definition expression
+ else if(hasDefinitionExpression())
+ throw Exception(ERR_MIX_VIEW_DEF_EXPR_REFS,__PRETTY_FUNCTION__,__FILE__,__LINE__);
+
//Checks if the reference already exists
idx=getReferenceIndex(refer);
@@ -71,26 +116,30 @@ void View::addReference(Reference &refer, unsigned sql_type, int expr_id)
if(idx < 0)
{
//Inserts the reference on the view
+ refer.setDefinitionExpression(sql_type==Reference::SQL_VIEW_DEFINITION);
references.push_back(refer);
idx=references.size()-1;
}
- //Gets the expression list
- expr_list=getExpressionList(sql_type);
-
- //Inserts the reference id on the expression list
- if(expr_id >= 0 && expr_id < static_cast<int>(expr_list->size()))
- expr_list->insert(expr_list->begin() + expr_id, static_cast<unsigned>(idx));
- //Raises an error if the expression id is invalid
- else if(expr_id >= 0 && expr_id >= static_cast<int>(expr_list->size()))
- throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
- else
- expr_list->push_back(static_cast<unsigned>(idx));
-
- col=refer.getColumn();
- if(col && col->isAddedByRelationship() &&
- col->getObjectId() > this->object_id)
- this->object_id=BaseObject::getGlobalId();
+ if(sql_type!=Reference::SQL_VIEW_DEFINITION)
+ {
+ //Gets the expression list
+ expr_list=getExpressionList(sql_type);
+
+ //Inserts the reference id on the expression list
+ if(expr_id >= 0 && expr_id < static_cast<int>(expr_list->size()))
+ expr_list->insert(expr_list->begin() + expr_id, static_cast<unsigned>(idx));
+ //Raises an error if the expression id is invalid
+ else if(expr_id >= 0 && expr_id >= static_cast<int>(expr_list->size()))
+ throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
+ else
+ expr_list->push_back(static_cast<unsigned>(idx));
+
+ col=refer.getColumn();
+ if(col && col->isAddedByRelationship() &&
+ col->getObjectId() > this->object_id)
+ this->object_id=BaseObject::getGlobalId();
+ }
}
unsigned View::getReferenceCount(void)
@@ -102,23 +151,33 @@ unsigned View::getReferenceCount(unsigned sql_type, int ref_type)
{
vector<unsigned> *vect_idref=getExpressionList(sql_type);
- if(ref_type < 0)
- return(vect_idref->size());
+ if(!vect_idref)
+ {
+ if(sql_type==Reference::SQL_VIEW_DEFINITION)
+ return(references.size());
+ else
+ return(0);
+ }
else
{
- vector<unsigned>::iterator itr, itr_end;
- unsigned qtd;
-
- qtd=0;
- itr=vect_idref->begin();
- itr_end=vect_idref->end();
- while(itr!=itr_end)
+ if(ref_type < 0)
+ return(vect_idref->size());
+ else
{
- if(references[(*itr)].getReferenceType()==static_cast<unsigned>(ref_type)) qtd++;
- itr++;
- }
+ vector<unsigned>::iterator itr, itr_end;
+ unsigned count=0;
+
+
+ itr=vect_idref->begin();
+ itr_end=vect_idref->end();
+ while(itr!=itr_end)
+ {
+ if(references[(*itr)].getReferenceType()==static_cast<unsigned>(ref_type)) count++;
+ itr++;
+ }
- return(qtd);
+ return(count);
+ }
}
}
@@ -139,7 +198,10 @@ Reference View::getReference(unsigned ref_id, unsigned sql_type)
if(ref_id >= references.size())
throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
- return(references[vect_idref->at(ref_id)]);
+ if(sql_type==Reference::SQL_VIEW_DEFINITION || vect_idref)
+ return(references[ref_id]);
+ else
+ return(references[vect_idref->at(ref_id)]);
}
void View::removeReference(unsigned ref_id)
@@ -192,65 +254,82 @@ void View::removeReference(unsigned expr_id, unsigned sql_type)
int View::getReferenceIndex(Reference &ref, unsigned sql_type)
{
vector<unsigned> *vet_idref=getExpressionList(sql_type);
- vector<unsigned>::iterator itr, itr_aux, itr_end;
+ vector<unsigned>::iterator itr, itr_end;
int idx_ref;
bool found=false;
idx_ref=getReferenceIndex(ref);
- itr=vet_idref->begin();
- itr_end=vet_idref->end();
- while(itr!=itr_end && !found)
+ if(sql_type==Reference::SQL_VIEW_DEFINITION &&
+ idx_ref >=0 && ref.isDefinitionExpression())
+ return(idx_ref);
+ else if(sql_type!=Reference::SQL_VIEW_DEFINITION)
{
- found=(static_cast<int>(*itr)==idx_ref);
- if(!found) itr++;
- }
+ itr=vet_idref->begin();
+ itr_end=vet_idref->end();
- if(!found)
- return(-1);
+ while(itr!=itr_end && !found)
+ {
+ found=(static_cast<int>(*itr)==idx_ref);
+ if(!found) itr++;
+ }
+
+ if(!found)
+ return(-1);
+ else
+ return(itr-vet_idref->begin());
+ }
else
- return(itr-vet_idref->begin());
+ return(-1);
}
void View::setDeclarationAttribute(void)
{
QString decl;
- if(exp_select.size() > 0)
+ if(!references.empty())
{
- vector<unsigned> *vet_ref[3]={&exp_select, &exp_from, &exp_where};
- vector<unsigned>::iterator itr, itr_end;
- QString palavras[3]={"SELECT ", "\n FROM ", "\n WHERE "};
- unsigned i, qtd, idx, tipo_sql[3]={Reference::SQL_REFER_SELECT,
- Reference::SQL_REFER_FROM,
- Reference::SQL_REFER_WHERE};
-
- for(i=0; i < 3; i++)
+ if(exp_select.empty())
+ {
+ decl=references[0].getExpression();
+ }
+ else
{
- if(vet_ref[i]->size() > 0)
+ vector<unsigned> *refs_vect[3]={&exp_select, &exp_from, &exp_where};
+ vector<unsigned>::iterator itr, itr_end;
+ QString palavras[3]={"SELECT ", "\n FROM ", "\n WHERE "};
+ unsigned i, qtd, idx, tipo_sql[3]={Reference::SQL_REFER_SELECT,
+ Reference::SQL_REFER_FROM,
+ Reference::SQL_REFER_WHERE};
+
+ for(i=0; i < 3; i++)
{
- decl+=palavras[i];
-
- itr=vet_ref[i]->begin();
- itr_end=vet_ref[i]->end();
- while(itr!=itr_end)
+ if(refs_vect[i]->size() > 0)
{
- idx=(*itr);
- decl+=references[idx].getSQLDefinition(tipo_sql[i]);
- itr++;
- }
-
- if(tipo_sql[i]==Reference::SQL_REFER_SELECT ||
- tipo_sql[i]==Reference::SQL_REFER_FROM)
- {
- //Removing the final comma from SELECT / FROM declarations
- qtd=decl.size();
- if(decl[qtd-2]==',')
- decl.remove(qtd-2,2);
+ decl+=palavras[i];
+
+ itr=refs_vect[i]->begin();
+ itr_end=refs_vect[i]->end();
+ while(itr!=itr_end)
+ {
+ idx=(*itr);
+ decl+=references[idx].getSQLDefinition(tipo_sql[i]);
+ itr++;
+ }
+
+ if(tipo_sql[i]==Reference::SQL_REFER_SELECT ||
+ tipo_sql[i]==Reference::SQL_REFER_FROM)
+ {
+ //Removing the final comma from SELECT / FROM declarations
+ qtd=decl.size();
+ if(decl[qtd-2]==',')
+ decl.remove(qtd-2,2);
+ }
}
}
}
}
+
attributes[ParsersAttributes::DECLARATION]=decl;
}
@@ -259,7 +338,7 @@ void View::setReferencesAttribute(void)
QString str_aux;
QString attribs[]={ ParsersAttributes::SELECT_EXP,
ParsersAttributes::FROM_EXP,
- ParsersAttributes::EXP_SIMPLES };
+ ParsersAttributes::SIMPLE_EXP };
vector<unsigned> *vect_exp[]={&exp_select, &exp_from, &exp_where};
int qtd, i, i1;
@@ -331,6 +410,8 @@ bool View::isReferencingColumn(Column *col)
QString View::getCodeDefinition(unsigned def_type)
{
+ attributes[ParsersAttributes::CTE_EXPRESSION]=cte_expression;
+
if(def_type==SchemaParser::SQL_DEFINITION)
setDeclarationAttribute();
else
@@ -350,5 +431,6 @@ void View::operator = (View &view)
this->exp_select=view.exp_select;
this->exp_from=view.exp_from;
this->exp_where=view.exp_where;
+ this->cte_expression=view.cte_expression;
}
View
18 libpgmodeler/src/view.h
@@ -34,11 +34,16 @@ class View: public BaseTable {
vector<Reference> references;
/*! \brief Vectors that stores indexes to the view references in each
- SQL part: SELECT-FROM, FROM-WHERE and after WHERE */
+ SQL part: SELECT-FROM, FROM-WHERE, after WHERE*/
vector<unsigned> exp_select,
exp_from,
exp_where;
+ /*! \brief Commom table expression. This is prepend on the views definition.
+ CTE's are available since PostgreSQL 8.4:
+ > http://www.postgresql.org/docs/8.4/interactive/queries-with.html */
+ QString cte_expression;
+
//! \brief Sets the declaration attribute used by the SchemaParser
void setDeclarationAttribute(void);
@@ -61,7 +66,10 @@ class View: public BaseTable {
new references at the end of the list */
void addReference(Reference &refer, unsigned sql_type, int expr_id=-1);
- /*! \brief Remove the reference from the view using its index, removing all the elements
+ //! \brief Sets the commom table expression for the view
+ void setCommomTableExpression(const QString &expr);
+
+ /*! \brief Remove the reference from the view using its index, removing all the elements
from the exp_??? vectors when they make use of the deleted reference. */
void removeReference(unsigned ref_id);
@@ -71,6 +79,9 @@ class View: public BaseTable {
//! \brief Removes an element from the expression list specified by the 'sql_type' parameter
void removeReference(unsigned expr_id, unsigned sql_type);
+ //! \brief Returns the commom table expression
+ QString getCommomTableExpression(void);
+
//! \brief Returns the reference count from view
unsigned getReferenceCount(void);
@@ -105,6 +116,9 @@ class View: public BaseTable {
//! \brief Returns if the view is referencing the specified column
bool isReferencingColumn(Column *col);
+ //! \brief Returns if the view has an reference expression that is used as view definition
+ bool hasDefinitionExpression(void);
+
//! \brief Copy the attributes between two views
void operator = (View &visao);
};
View
BIN  libpgmodeler_ui/res/icones/permission_grp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  libpgmodeler_ui/res/resources.qrc
@@ -141,6 +141,7 @@
<file>icones/relationshipfk.png</file>
<file>icones/help.png</file>
<file>icones/permission.png</file>
+ <file>icones/permission_grp.png</file>
</qresource>
<qresource prefix="/imagens">
<file>imagens/model2sql.png</file>
View
8 libpgmodeler_ui/src/constraintwidget.cpp
@@ -132,7 +132,7 @@ void ConstraintWidget::addColumn(int row)
addColumn(column, col_id, row);
//When there is no items con the combo the insert button of the table is disabled
- aux_col_tab->enableButtons(ObjectTableWidget::ADD_BUTTON, (combo->count()!=0));
+ aux_col_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON, (combo->count()!=0));
}
catch(Exception &e)
{
@@ -240,7 +240,7 @@ void ConstraintWidget::updateColumnsCombo(unsigned col_id)
combo->addItem(Utf8String::create(column->getName()) + " (" + ~column->getType() +")", QVariant::fromValue<void *>(column));
}
- aux_col_tab->enableButtons(ObjectTableWidget::ADD_BUTTON, (combo->count()!=0));
+ aux_col_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON, (combo->count()!=0));
}
catch(Exception &e)
{
@@ -368,7 +368,7 @@ void ConstraintWidget::setAttributes(DatabaseModel *model, BaseObject *parent_ob
}
updateColumnsCombo(Constraint::SOURCE_COLS);
- columns_tab->enableButtons(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
+ columns_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
columns_tab->blockSignals(false);
if(constr)
@@ -404,7 +404,7 @@ void ConstraintWidget::setAttributes(DatabaseModel *model, BaseObject *parent_ob
}
updateColumnsCombo(Constraint::REFERENCED_COLS);
- ref_columns_tab->enableButtons(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
+ ref_columns_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
ref_columns_tab->blockSignals(false);
}
}
View
26 libpgmodeler_ui/src/modelobjectswidget.cpp
@@ -26,7 +26,7 @@ ModelObjectsWidget::ModelObjectsWidget(bool simplified_view, QWidget *parent, Qt
OBJ_TYPE, OBJ_TABLESPACE, OBJ_OPFAMILY, OBJ_OPCLASS,
OBJ_RELATIONSHIP, OBJ_TEXTBOX, OBJ_COLUMN, OBJ_CONSTRAINT,
OBJ_TRIGGER, OBJ_INDEX, OBJ_RULE };
- int type_id, type_count=24;
+ int type_id, type_count=sizeof(type)/sizeof(ObjectType);
QListWidgetItem *item=NULL;
QPixmap icon;
QString str_aux;
@@ -299,11 +299,13 @@ void ModelObjectsWidget::updateObjectsList(void)
OBJ_SCHEMA, OBJ_AGGREGATE, OBJ_OPERATOR, OBJ_SEQUENCE,
OBJ_ROLE, OBJ_CONVERSION, OBJ_CAST, OBJ_LANGUAGE,
OBJ_TYPE, OBJ_TABLESPACE, OBJ_OPFAMILY, OBJ_OPCLASS,
- OBJ_RELATIONSHIP, OBJ_TEXTBOX/*, BASE_RELATIONSHIP */ },
+ OBJ_RELATIONSHIP, OBJ_TEXTBOX },
subtypes[]={ OBJ_COLUMN, OBJ_CONSTRAINT,
OBJ_TRIGGER, OBJ_INDEX, OBJ_RULE };
- int type_cnt=19, subtype_cnt=5, type_id, count, count1, idx, tab_id;
+ int type_cnt=sizeof(types)/sizeof(ObjectType),
+ subtype_cnt=sizeof(subtypes)/sizeof(ObjectType),
+ type_id, count, count1, idx, tab_id;
try
{
@@ -591,11 +593,11 @@ void ModelObjectsWidget::updatedSchemaTree(QTreeWidgetItem *root)
vector<BaseObject *> obj_list;
QFont font;
QTreeWidgetItem *item=NULL, *item1=NULL, *item2=NULL, *item3=NULL, *item4=NULL;
- int count, count2, i, i1, i2;
ObjectType types[]={ OBJ_VIEW, OBJ_FUNCTION, OBJ_AGGREGATE,
OBJ_DOMAIN, OBJ_TYPE, OBJ_CONVERSION,
OBJ_OPERATOR, OBJ_OPFAMILY, OBJ_OPCLASS,
OBJ_SEQUENCE };
+ int count, count2, type_cnt=sizeof(types)/sizeof(ObjectType), i, i1, i2;
QPixmap sch_icon=QPixmap(QString(":/icones/icones/") +
QString(BaseObject::getSchemaName(OBJ_SCHEMA)) +
@@ -653,7 +655,7 @@ void ModelObjectsWidget::updatedSchemaTree(QTreeWidgetItem *root)
updateTableTree(item2, schema);
//Creates the object group at schema level (function, domain, sequences, etc)
- for(i1=0; i1 < 10; i1++)
+ for(i1=0; i1 < type_cnt; i1++)
{
if(visible_objs_map[types[i1]])
{
@@ -733,12 +735,12 @@ void ModelObjectsWidget::updateTableTree(QTreeWidgetItem *root, BaseObject *sche
vector<BaseObject *> obj_list;
Table *table=NULL;
QTreeWidgetItem *item=NULL, *item1=NULL, *item2=NULL, *item3=NULL;
- int count, count1, i, i1, i2;
QString str_aux;
QFont font;
ConstraintType constr_type;
ObjectType types[]={ OBJ_COLUMN, OBJ_CONSTRAINT, OBJ_RULE,
OBJ_TRIGGER, OBJ_INDEX };
+ int count, count1, type_cnt=sizeof(types)/sizeof(ObjectType), i, i1, i2;
QPixmap group_icon=QPixmap(QString(":/icones/icones/") +
QString(BaseObject::getSchemaName(OBJ_TABLE)) +
QString("_grp") + QString(".png"));
@@ -782,7 +784,7 @@ void ModelObjectsWidget::updateTableTree(QTreeWidgetItem *root, BaseObject *sche
}
//Creating the group for the child objects (column, rules, triggers, indexes and constraints)
- for(i1=0; i1 < 5; i1++)
+ for(i1=0; i1 < type_cnt; i1++)
{
if(visible_objs_map[types[i1]])
{
@@ -865,13 +867,13 @@ void ModelObjectsWidget::updateDatabaseTree(void)
{
QString str_aux;
BaseObject *object=NULL;
- unsigned count, i, i1, rel_type;
QTreeWidgetItem *root=NULL,*item1=NULL, *item2=NULL;
QFont font;
vector<BaseObject *> tree_state, obj_list;
ObjectType types[]={ OBJ_ROLE, OBJ_TABLESPACE,
OBJ_LANGUAGE, OBJ_CAST, OBJ_TEXTBOX,
OBJ_RELATIONSHIP };
+ unsigned count, i, i1, rel_type, type_cnt=sizeof(types)/sizeof(ObjectType);
try
{
@@ -888,9 +890,9 @@ void ModelObjectsWidget::updateDatabaseTree(void)
QString(".png")));
objectstree_tw->insertTopLevelItem(0,root);
- root->setText(0,Utf8String::create(/*modelo_wgt->*/db_model->getName()));
- root->setToolTip(0,Utf8String::create(/*modelo_wgt->*/db_model->getName()));
- root->setData(0, Qt::UserRole, generateItemValue(/*modelo_wgt->*/db_model));
+ root->setText(0,Utf8String::create(db_model->getName()));
+ root->setToolTip(0,Utf8String::create(db_model->getName()));
+ root->setData(0, Qt::UserRole, generateItemValue(db_model));
if(db_model->isProtected())
{
@@ -902,7 +904,7 @@ void ModelObjectsWidget::updateDatabaseTree(void)
updatedSchemaTree(root);
- for(i=0; i < 6; i++)
+ for(i=0; i < type_cnt; i++)
{
if(visible_objs_map[types[i]])
{
View
24 libpgmodeler_ui/src/objecttablewidget.cpp
@@ -30,7 +30,7 @@ ObjectTableWidget::ObjectTableWidget(unsigned button_conf, bool conf_exclusion,
connect(edit_tb, SIGNAL(clicked(bool)), this, SLOT(editRow(void)));
connect(update_tb, SIGNAL(clicked(bool)), this, SLOT(updateRow(void)));
connect(remove_all_tb, SIGNAL(clicked(bool)), this, SLOT(removeRows(void)));
- connect(table_tbw, SIGNAL(cellClicked(int,int)), this, SLOT(enableButtons(void)));
+ connect(table_tbw, SIGNAL(cellClicked(int,int)), this, SLOT(setButtonsEnabled(void)));
connect(table_tbw, SIGNAL(cellDoubleClicked(int,int)), this, SLOT(editRow(void)));
this->conf_exclusion=conf_exclusion;
@@ -258,7 +258,7 @@ void ObjectTableWidget::addColumn(unsigned col_idx)
table_tbw->insertColumn(col_idx);
table_tbw->clearSelection();
- enableButtons();
+ setButtonsEnabled();
emit s_columnAdded(col_idx);
}
@@ -274,7 +274,7 @@ void ObjectTableWidget::selectRow(int lin_idx)
item=table_tbw->item(lin_idx,0);
item->setSelected(true);
table_tbw->setCurrentItem(item);
- enableButtons();
+ setButtonsEnabled();
}
}
@@ -303,7 +303,7 @@ void ObjectTableWidget::addRow(unsigned lin_idx)
void ObjectTableWidget::addRow(void)
{
this->addRow(table_tbw->rowCount());
- enableButtons();
+ setButtonsEnabled();
emit s_rowAdded(table_tbw->rowCount()-1);
}
@@ -351,7 +351,7 @@ void ObjectTableWidget::removeRow(void)
{
table_tbw->removeRow(row_idx);
table_tbw->setCurrentItem(NULL);
- enableButtons();
+ setButtonsEnabled();
emit s_rowRemoved(row_idx);
}
@@ -378,7 +378,7 @@ void ObjectTableWidget::removeRows(void)
while(table_tbw->rowCount() > 0)
table_tbw->removeRow(0);
- enableButtons();
+ setButtonsEnabled();
emit s_rowsRemoved();
}
@@ -392,7 +392,7 @@ void ObjectTableWidget::removeColumn(unsigned col_idx)
table_tbw->removeColumn(col_idx);
table_tbw->clearSelection();
- enableButtons();
+ setButtonsEnabled();
emit s_columnRemoved(col_idx);
}
@@ -476,7 +476,7 @@ void ObjectTableWidget::moveRows(void)
}
}
- enableButtons();
+ setButtonsEnabled();
emit s_rowsMoved(row, row1);
}
}
@@ -495,10 +495,10 @@ void ObjectTableWidget::clearSelection(void)
{
table_tbw->clearSelection();
table_tbw->setCurrentItem(NULL);
- enableButtons();
+ setButtonsEnabled();
}
-void ObjectTableWidget::enableButtons(unsigned button_conf, bool value)
+void ObjectTableWidget::setButtonsEnabled(unsigned button_conf, bool value)
{
int lin=-1;
QTableWidgetItem *item=table_tbw->currentItem();
@@ -530,11 +530,11 @@ void ObjectTableWidget::enableButtons(unsigned button_conf, bool value)
update_tb->setEnabled(value && lin >= 0);
}
-void ObjectTableWidget::enableButtons(void)
+void ObjectTableWidget::setButtonsEnabled(void)
{
QTableWidgetItem *item=table_tbw->currentItem();
- enableButtons(ALL_BUTTONS, true);
+ setButtonsEnabled(ALL_BUTTONS, true);
if(item && item->row() >= 0)
emit s_rowSelected(item->row());
View
4 libpgmodeler_ui/src/objecttablewidget.h
@@ -127,7 +127,7 @@ class ObjectTableWidget: public QWidget, public Ui::ObjectTableWidget {
void updateRow(void);
//! \brief Enables the handle buttons according to the selected row
- void enableButtons(void);
+ void setButtonsEnabled(void);
public slots:
//! \brief Adds a new row at the end of the table
@@ -146,7 +146,7 @@ class ObjectTableWidget: public QWidget, public Ui::ObjectTableWidget {
void selectRow(int lin_idx);
//! \brief Controls the enable state of each button
- void enableButtons(unsigned button_conf, bool value);
+ void setButtonsEnabled(unsigned button_conf, bool value);
signals:
//! \brief Signal emitted when a new row is added. The new row index is send with the signal
View
6 libpgmodeler_ui/src/relationshipwidget.cpp
@@ -275,8 +275,8 @@ void RelationshipWidget::setAttributes(DatabaseModel *model, OperationList *op_l
deferrable_chk->setChecked(aux_rel->isDeferrable());
relnn_tab_name_edt->setText(aux_rel->getTableNameRelNN());
- attributes_tab->enableButtons(ObjectTableWidget::ALL_BUTTONS, !aux_rel->isProtected());
- constraints_tab->enableButtons(ObjectTableWidget::ALL_BUTTONS, !aux_rel->isProtected());
+ attributes_tab->setButtonsEnabled(ObjectTableWidget::ALL_BUTTONS, !aux_rel->isProtected());
+ constraints_tab->setButtonsEnabled(ObjectTableWidget::ALL_BUTTONS, !aux_rel->isProtected());
//Lists the relationship attributes
listObjects(OBJ_COLUMN);
@@ -397,7 +397,7 @@ void RelationshipWidget::listObjects(ObjectType obj_type)
tab->clearSelection();
tab->blockSignals(false);
- constraints_tab->enableButtons(ObjectTableWidget::ADD_BUTTON,
+ constraints_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON,
attributes_tab->getRowCount() > 0);
}
catch(Exception &e)
View
2  libpgmodeler_ui/src/syntaxhighlighter.cpp
@@ -582,7 +582,7 @@ void SyntaxHighlighter::loadConfiguration(const QString &filename)
regexp.setPatternSyntax(QRegExp::FixedString);
if(expr_type=="" ||
- expr_type==ParsersAttributes::EXP_SIMPLES ||
+ expr_type==ParsersAttributes::SIMPLE_EXP ||
expr_type==ParsersAttributes::INITIAL_EXP)
initial_exprs[group].push_back(regexp);
else
View
6 libpgmodeler_ui/src/tablewidget.cpp
@@ -301,11 +301,11 @@ void TableWidget::listObjects(ObjectType obj_type)
//Enables the add button on the constraints, triggers and index tab only when there is columns created
if(obj_type==OBJ_COLUMN)
{
- objects_tab_map[OBJ_CONSTRAINT]->enableButtons(ObjectTableWidget::ADD_BUTTON,
+ objects_tab_map[OBJ_CONSTRAINT]->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON,
objects_tab_map[OBJ_COLUMN]->getRowCount() > 0);
- objects_tab_map[OBJ_TRIGGER]->enableButtons(ObjectTableWidget::ADD_BUTTON,
+ objects_tab_map[OBJ_TRIGGER]->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON,
objects_tab_map[OBJ_COLUMN]->getRowCount() > 0);
- objects_tab_map[OBJ_INDEX]->enableButtons(ObjectTableWidget::ADD_BUTTON,
+ objects_tab_map[OBJ_INDEX]->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON,
objects_tab_map[OBJ_COLUMN]->getRowCount() > 0);
}
}
View
6 libpgmodeler_ui/src/triggerwidget.cpp
@@ -110,7 +110,7 @@ void TriggerWidget::addColumn(int lin_idx)
column=reinterpret_cast<Column *>(column_cmb->itemData(column_cmb->currentIndex(),Qt::UserRole).value<void *>());
column_cmb->removeItem(column_cmb->currentIndex());
addColumn(column, lin_idx);
- columns_tab->enableButtons(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
+ columns_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
}
catch(Exception &e)
{
@@ -150,7 +150,7 @@ void TriggerWidget::updateColumnsCombo(void)
}
}
- columns_tab->enableButtons(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
+ columns_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
}
catch(Exception &e)
{
@@ -248,7 +248,7 @@ void TriggerWidget::setAttributes(DatabaseModel *model, Table *parent_table, Ope
arguments_tab->setCellText(trigger->getArgument(i), i, 0);
}
- columns_tab->enableButtons(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
+ columns_tab->setButtonsEnabled(ObjectTableWidget::ADD_BUTTON, (column_cmb->count()!=0));
arguments_tab->blockSignals(false);
columns_tab->blockSignals(false);
}
View
57 libpgmodeler_ui/src/viewwidget.cpp
@@ -36,6 +36,12 @@ ViewWidget::ViewWidget(QWidget *parent): BaseObjectWidget(parent, OBJ_VIEW)
GlobalAttributes::SQL_HIGHLIGHT_CONF +
GlobalAttributes::CONFIGURATION_EXT);
+ cte_expression_hl=new SyntaxHighlighter(cte_expression_txt, false);
+ cte_expression_hl->loadConfiguration(GlobalAttributes::CONFIGURATIONS_DIR +
+ GlobalAttributes::DIR_SEPARATOR +
+ GlobalAttributes::SQL_HIGHLIGHT_CONF +
+ GlobalAttributes::CONFIGURATION_EXT);
+
table_sel=new ObjectSelectorWidget(OBJ_TABLE, true, this);
column_sel=new ObjectSelectorWidget(OBJ_COLUMN, true, this);
@@ -44,7 +50,7 @@ ViewWidget::ViewWidget(QWidget *parent): BaseObjectWidget(parent, OBJ_VIEW)
references_tab->setHeaderLabel(trUtf8("Col./Expr."),0);
references_tab->setHeaderLabel(trUtf8("Alias"),1);
references_tab->setHeaderLabel(trUtf8("Alias Col."),2);
- references_tab->setHeaderLabel(trUtf8("SF FW AW"),3);
+ references_tab->setHeaderLabel(trUtf8("Flags: SF FW AW VD"),3);
frame_info=generateInformationFrame(trUtf8("To reference all columns in a table (*) just do not fill the field <strong>Column</strong>, this is the same as write <em><strong>[schema].[tablel].*</strong></em>"));
@@ -65,6 +71,13 @@ ViewWidget::ViewWidget(QWidget *parent): BaseObjectWidget(parent, OBJ_VIEW)
connect(references_tab, SIGNAL(s_rowEdited(int)), this, SLOT(editReference(int)));
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateCodePreview(void)));
+ connect(view_def_chk, SIGNAL(toggled(bool)), select_from_chk, SLOT(setDisabled(bool)));
+ connect(view_def_chk, SIGNAL(toggled(bool)), from_where_chk, SLOT(setDisabled(bool)));
+ connect(view_def_chk, SIGNAL(toggled(bool)), after_where_chk, SLOT(setDisabled(bool)));
+ connect(view_def_chk, SIGNAL(toggled(bool)), expr_alias_edt, SLOT(setDisabled(bool)));
+ connect(view_def_chk, SIGNAL(toggled(bool)), expr_alias_lbl, SLOT(setDisabled(bool)));
+
+
parent_form->setMinimumSize(650, 630);
selectReferenceType();
}
@@ -77,6 +90,9 @@ ViewWidget::ViewWidget(QWidget *parent): BaseObjectWidget(parent, OBJ_VIEW)
void ViewWidget::hideEvent(QHideEvent *evento)
{
references_tab->removeRows();
+ tabWidget->setCurrentIndex(0);
+ cte_expression_txt->clear();
+ clearReferenceForm();
BaseObjectWidget::hideEvent(evento);
}
@@ -108,10 +124,12 @@ void ViewWidget::selectReferenceType(void)
tab_alias_lbl->setVisible(ref_obj);
frame_info->setVisible(ref_obj);
+ view_def_chk->setChecked(false);
expression_lbl->setVisible(!ref_obj);
expression_txt->setVisible(!ref_obj);
expr_alias_edt->setVisible(!ref_obj);
expr_alias_lbl->setVisible(!ref_obj);
+ view_def_chk->setVisible(!ref_obj);
}
void ViewWidget::handleReference(int ref_idx)
@@ -138,11 +156,12 @@ void ViewWidget::handleReference(int ref_idx)
if the user do not check some of these attributes raises an error */
if(!select_from_chk->isChecked() &&
!from_where_chk->isChecked() &&
- !after_where_chk->isChecked())
+ !after_where_chk->isChecked() &&
+ !view_def_chk->isChecked())
throw Exception(ERR_SQL_SCOPE_INV_VIEW_REF,__PRETTY_FUNCTION__,__FILE__,__LINE__);
showReferenceData(ref, select_from_chk->isChecked(), from_where_chk->isChecked(),
- after_where_chk->isChecked(), ref_idx);
+ after_where_chk->isChecked(), view_def_chk->isChecked() ,ref_idx);
clearReferenceForm();
references_tab->clearSelection();
@@ -185,6 +204,7 @@ void ViewWidget::editReference(int ref_idx)
select_from_chk->setChecked(str_aux[0]=='1');
from_where_chk->setChecked(str_aux[1]=='1');
after_where_chk->setChecked(str_aux[2]=='1');
+ view_def_chk->setChecked(str_aux[3]=='1');
}
void ViewWidget::showObjectName(void)
@@ -217,7 +237,7 @@ void ViewWidget::showObjectName(void)
}
}
-void ViewWidget::showReferenceData(Reference refer, bool selec_from, bool from_where, bool after_where, unsigned row)
+void ViewWidget::showReferenceData(Reference refer, bool selec_from, bool from_where, bool after_where, bool view_def, unsigned row)
{
Table *tab=NULL;
Column *col=NULL;
@@ -241,22 +261,21 @@ void ViewWidget::showReferenceData(Reference refer, bool selec_from, bool from_w
if(col)
references_tab->setCellText(Utf8String::create(refer.getColumnAlias()),row,2);
- else
- references_tab->setCellText(QString("-"),row,2);
}
else
{
references_tab->setCellText(Utf8String::create(refer.getExpression()),row,0);
references_tab->setCellText(Utf8String::create(refer.getAlias()),row,1);
- references_tab->setCellText(QString("-"),row,2);
}
//Configures the string that denotes the SQL application for the reference
str_aux+=(selec_from ? "1" : "0");
str_aux+=(from_where ? "1" : "0");
str_aux+=(after_where ? "1" : "0");
+ str_aux+=(view_def ? "1" : "0");
references_tab->setCellText(str_aux,row,3);
+ refer.setDefinitionExpression(view_def);
references_tab->setRowData(QVariant::fromValue<Reference>(refer), row);
updateCodePreview();
}
@@ -265,16 +284,17 @@ void ViewWidget::updateCodePreview(void)
{
Reference refer;
QString str_aux;
- unsigned i, count, i1, expr_type[3]={Reference::SQL_REFER_SELECT,
- Reference::SQL_REFER_FROM,
- Reference::SQL_REFER_WHERE};
+ unsigned i, count, i1, expr_type[4]={Reference::SQL_REFER_SELECT,
+ Reference::SQL_REFER_FROM,
+ Reference::SQL_REFER_WHERE,
+ Reference::SQL_VIEW_DEFINITION};
try
{
//Clears the auxiliary view
aux_view.removeReferences();
-
aux_view.BaseObject::setName(name_edt->text().toUtf8());
aux_view.setSchema(schema_sel->getSelectedObject());
+ aux_view.setCommomTableExpression(cte_expression_txt->toPlainText().toUtf8());
count=references_tab->getRowCount();
for(i=0; i < count; i++)
@@ -284,7 +304,7 @@ void ViewWidget::updateCodePreview(void)
//Get the SQL application string for the current reference
str_aux=references_tab->getCellText(i,3);
- for(i1=0; i1 < 3; i1++)
+ for(i1=0; i1 < 4; i1++)
{
if(str_aux[i1]=='1')
aux_view.addReference(refer, expr_type[i1]);
@@ -296,14 +316,16 @@ void ViewWidget::updateCodePreview(void)
catch(Exception &e)
{
//In case of error no code is outputed, showing a error message in the code preview widget
- code_txt->setPlainText(trUtf8("-- Could not generate the code. Make sure all attributes are correctly filled! --"));
+ str_aux=trUtf8("/* Could not generate the SQL code. Make sure all attributes are correctly filled! ");
+ str_aux+=QString("\n\n>> Returned error(s): \n\n%1*/").arg(e.getExceptionsText());
+ code_txt->setPlainText(str_aux);
}
}
void ViewWidget::setAttributes(DatabaseModel *model, OperationList *op_list, Schema *schema, View *view, float px, float py)
{
unsigned i, count;
- bool sel_from, from_where, after_where;
+ bool sel_from, from_where, after_where, view_def;
Reference refer;
BaseObjectWidget::setAttributes(model,op_list, view, schema, px, py);
@@ -313,9 +335,11 @@ void ViewWidget::setAttributes(DatabaseModel *model, OperationList *op_list, Sch
if(view)
{
- count=view->getReferenceCount();
+ cte_expression_txt->setPlainText(Utf8String::create(view->getCommomTableExpression()));
+ count=view->getReferenceCount();
references_tab->blockSignals(true);
+
for(i=0; i < count; i++)
{
references_tab->addRow();
@@ -324,8 +348,9 @@ void ViewWidget::setAttributes(DatabaseModel *model, OperationList *op_list, Sch
sel_from=(view->getReferenceIndex(refer,Reference::SQL_REFER_SELECT) >= 0);
from_where=(view->getReferenceIndex(refer,Reference::SQL_REFER_FROM) >= 0);
after_where=(view->getReferenceIndex(refer,Reference::SQL_REFER_WHERE)>= 0);
+ view_def=(view->getReferenceIndex(refer,Reference::SQL_VIEW_DEFINITION)>= 0);
- showReferenceData(refer, sel_from, from_where, after_where, i);
+ showReferenceData(refer, sel_from, from_where, after_where, view_def, i);
}
references_tab->blockSignals(false);
View
5 libpgmodeler_ui/src/viewwidget.h
@@ -48,14 +48,15 @@ class ViewWidget: public BaseObjectWidget, public Ui::ViewWidget {
ObjectTableWidget *references_tab;
SyntaxHighlighter *expression_hl,
- *code_hl;
+ *code_hl,
+ *cte_expression_hl;
ObjectSelectorWidget *table_sel,
*column_sel;
//! \brief Shows the reference at the reference's table
void showReferenceData(Reference refer, bool selec_from, bool from_where,
- bool after_where, unsigned row);
+ bool after_where, bool view_def, unsigned row);
void clearReferenceForm(void);
void hideEvent(QHideEvent *);
View
131 libpgmodeler_ui/ui/viewwidget.ui
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
- <width>473</width>
- <height>251</height>
+ <width>632</width>
+ <height>462</height>
</rect>
</property>
<property name="windowTitle">
@@ -36,40 +36,32 @@
<property name="spacing">
<number>6</number>
</property>
- <item row="0" column="0">
- <widget class="QLabel" name="ref_type_lbl">
- <property name="text">
- <string>Type:</string>
- </property>
- </widget>
- </item>
- <item row="0" column="1">
- <widget class="QComboBox" name="ref_type_cmb">
- <item>
- <property name="text">
- <string>Column</string>
- </property>
- </item>
- <item>
- <property name="text">
- <string>Expression</string>
- </property>
- </item>
- </widget>
- </item>
- <item row="0" column="2" colspan="3">
+ <item row="0" column="2" colspan="4">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
- <width>249</width>
- <height>21</height>
+ <width>40</width>
+ <height>20</height>
</size>
</property>
</spacer>
</item>
+ <item row="0" column="0">
+ <widget class="QLabel" name="ref_type_lbl">
+ <property name="minimumSize">
+ <size>
+ <width>100</width>
+ <height>0</height>
+ </size>
+ </property>
+ <property name="text">
+ <string>Type:</string>
+ </property>
+ </widget>
+ </item>
<item row="1" column="0">
<widget class="QLabel" name="used_in_lbl">
<property name="text">
@@ -77,10 +69,16 @@
</property>
</widget>
</item>
- <item row="1" column="1" colspan="4">
+ <item row="1" column="1" colspan="5">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QCheckBox" name="select_from_chk">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
<property name="text">
<string>SELECT-FROM</string>
</property>
@@ -88,6 +86,12 @@
</item>
<item>
<widget class="QCheckBox" name="from_where_chk">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
<property name="text">
<string>FROM-WHERE</string>
</property>
@@ -95,11 +99,30 @@
</item>
<item>
<widget class="QCheckBox" name="after_where_chk">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
<property name="text">
<string>After WHERE</string>
</property>
</widget>
</item>
+ <item>
+ <widget class="QCheckBox" name="view_def_chk">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>View Definition</string>
+ </property>
+ </widget>
+ </item>
</layout>
</item>
<item row="2" column="0">
@@ -109,7 +132,7 @@
</property>
</widget>
</item>
- <item row="2" column="3">
+ <item row="2" column="4">
<widget class="QLabel" name="tab_alias_lbl">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
@@ -122,7 +145,7 @@
</property>
</widget>
</item>
- <item row="2" column="4">
+ <item row="2" column="5">
<widget class="QLineEdit" name="tab_alias_edt">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
@@ -139,7 +162,7 @@
</property>
</widget>
</item>
- <item row="3" column="3">
+ <item row="3" column="4">
<widget class="QLabel" name="col_alias_lbl">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
@@ -152,7 +175,7 @@
</property>
</widget>
</item>
- <item row="3" column="4">
+ <item row="3" column="5">
<widget class="QLineEdit" name="col_alias_edt">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
@@ -169,7 +192,7 @@
</property>
</widget>
</item>
- <item row="4" column="1" colspan="4">
+ <item row="4" column="1" colspan="5">
<widget class="QTextEdit" name="expression_txt">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
@@ -197,7 +220,7 @@
</property>
</widget>
</item>
- <item row="5" column="0">
+ <item row="6" column="0">
<widget class="QLabel" name="expr_alias_lbl">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
@@ -210,7 +233,21 @@
</property>
</widget>
</item>
- <item row="5" column="1" colspan="2">
+ <item row="0" column="1">
+ <widget class="QComboBox" name="ref_type_cmb">
+ <item>
+ <property name="text">
+ <string>Column</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Expression</string>
+ </property>
+ </item>
+ </widget>
+ </item>
+ <item row="6" column="1" colspan="2">
<widget class="QLineEdit" name="expr_alias_edt">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
@@ -220,18 +257,22 @@
</property>
</widget>
</item>
- <item row="5" column="3" colspan="2">
- <spacer name="horizontalSpacer_2">
- <property name="orientation">
- <enum>Qt::Horizontal</enum>
- </property>
- <property name="sizeHint" stdset="0">
- <size>
- <width>203</width>
- <height>20</height>
- </size>
+ </layout>
+ </widget>
+ <widget class="QWidget" name="CTE">
+ <attribute name="title">
+ <string>Table Expression</string>
+ </attribute>
+ <layout class="QGridLayout" name="gridLayout">
+ <item row="0" column="0">
+ <widget class="QTextEdit" name="cte_expression_txt">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
</property>
- </spacer>
+ </widget>
</item>
</layout>
</widget>
View
5 libutil/src/exception.cpp
@@ -161,7 +161,7 @@ QString Exception::messages[ERROR_COUNT][2]={
{"ERR_REF_LIN_OBJTAB_INV_INDEX", QT_TR_NOOP("Reference to a row of the objects table with invalid index!")},
{"ERR_OPR_RESERVED_OBJECT", QT_TR_NOOP("The schema public and the languages plpgsql, c and sql can not be manipulated because they are reserved to PostgreSQL! They are present in the model database only as a reference!")},
{"ERR_FUNC_CONFIG_INV_OBJECT", QT_TR_NOOP("The new configuration function invalidates the object '%1' (%2)! In this case it is needed to undo the relationship between the affected object and function so that the new configuration of the latter to take effect!")},
- {"ERR_SQL_SCOPE_INV_VIEW_REF", QT_TR_NOOP("A vision reference must have at least one SQL scope: SELECT, FROM, FROM-WHERE or After WHERE!")},
+ {"ERR_SQL_SCOPE_INV_VIEW_REF", QT_TR_NOOP("A view reference must be used in at least one these SQL scopes: View Definition, SELECT-FROM, FROM-WHERE or After WHERE!")},
{"ERR_CONSTR_NO_COLUMNS", QT_TR_NOOP("Constraints like primary key, foreign key or unique must have at least one column related to them! For foreign keys must be selected, in addition, the referenced columns!")},
{"ERR_CONFIG_NOT_LOADED", QT_TR_NOOP("Unable to load one or more configuration files! Please check if files exists in the configuration folder and if they are not corrupted to preventing this error to occur again on the next startup!")},
{"ERR_DEFAULT_CONFIG_NOT_REST", QT_TR_NOOP("Could not find the default settings file '%1'! To restore default settings check the existence of the file and try again!")},
@@ -194,6 +194,9 @@ QString Exception::messages[ERROR_COUNT][2]={
{"ERR_REF_TUPLE_COL_INV_NAME", QT_TR_NOOP("Reference to a column of tuple with invalid name!")},
{"ERR_REF_TUPLANAOEXISTE", QT_TR_NOOP("Reference to a tuple with index invalid or the result is empty (no tuples)!")},
{"ERR_CMD_SQL_NOT_EXECUTED", QT_TR_NOOP("Could not execute the SQL command.\n Message returned: %1")},
+ {"ERR_INV_VIEW_DEF_EXPRESSION", QT_TR_NOOP("Invalid use of a view referece as whole SQL definition! The assigned reference must be an expression!")},
+ {"ERR_ASG_SEC_VIEW_DEF_EXPRESSION", QT_TR_NOOP("Assignment of a second definition expression to the view!")},
+ {"ERR_MIX_VIEW_DEF_EXPR_REFS", QT_TR_NOOP("It's not possible mix ordinary references (SELECT-FROM, FROM-WHERE, After WHERE) with references used as view SQL definition!")},
};
Exception::Exception(void)
View
7 libutil/src/exception.h
@@ -36,7 +36,7 @@
using namespace std;
-const int ERROR_COUNT=175;
+const int ERROR_COUNT=178;
/*
ErrorType enum format: ERR_[LIBRARY]_[[OPERATION_CODE][ERROR_CODE]] where:
@@ -225,7 +225,10 @@ enum ErrorType {
ERR_REF_TUPLE_COL_INV_INDEX,
ERR_REF_TUPLE_COL_INV_NAME,
ERR_REF_TUPLANAOEXISTE,
- ERR_CMD_SQL_NOT_EXECUTED
+ ERR_CMD_SQL_NOT_EXECUTED,
+ ERR_INV_VIEW_DEF_EXPRESSION,
+ ERR_ASG_SEC_VIEW_DEF_EXPRESSION,
+ ERR_MIX_VIEW_DEF_EXPR_REFS
};
class Exception {
View
5 libutil/src/parsersattributes.h
@@ -200,7 +200,7 @@ namespace ParsersAttributes {
REFERENCE="reference",
SELECT_EXP="select-exp",
FROM_EXP="from-exp",
- EXP_SIMPLES="simple-exp",
+ SIMPLE_EXP="simple-exp",
ALIAS="alias",
COLUMN_ALIAS="column-alias",
SRC_REQUIRED="src-required",
@@ -348,7 +348,8 @@ namespace ParsersAttributes {
DDL_END_TOKEN="-- ddl-end --",
SQL_DISABLED="sql-disabled",
REVOKE="revoke",
- CASCADE="cascade";
+ CASCADE="cascade",
+ CTE_EXPRESSION="cte-exp";
}
#endif
View
11 schemas/sql/common/view.sch
@@ -6,10 +6,17 @@
[-- object: ] @{name} [ | type: ] @{sql-object} [ -- ] $br
[CREATE OR REPLACE VIEW ] @{name} $br
-[AS ] @{declaration}; $br
+[AS ]
+
+#Commom table expression (CTE)
+%if @{cte-exp} %then
+ [WITH ] @{cte-exp}
+%end
+
+@{declaration}; $br
%if @{comment} %then @{comment} %end
# This is a special token that pgModeler recognizes as end of DDL command
# when exporting models directly to DBMS. DO NOT REMOVE THIS TOKEN!
-[-- ddl-end --] $br $br
+[-- ddl-end --] $br $br
View
4 schemas/xml/dtd/baseelements.dtd
@@ -10,7 +10,7 @@
<!ELEMENT condition (#PCDATA)>
<!ELEMENT expression (#PCDATA)>
-<!ATTLIST expression type (none|select-exp|from-exp|simple-exp) "none">
+<!ATTLIST expression type (none|select-exp|from-exp|simple-exp|cte-exp) "none">
<!ELEMENT position EMPTY>
<!ATTLIST position x CDATA "0">
@@ -18,4 +18,4 @@
<!ELEMENT columns EMPTY>
<!ATTLIST columns names CDATA #REQUIRED>
-<!ATTLIST columns ref-type (src-columns|dst-columns) "src-columns">
+<!ATTLIST columns ref-type (src-columns|dst-columns) "src-columns">
View
7 schemas/xml/reference.sch
@@ -3,18 +3,13 @@
# Code generation can be broken if incorrect changes are made.
$tb <reference
-
-#%if @{view} %then
-# [ view=] "@{view}"
-#%end
-
%if @{table} %then
[ table=] "@{table}"
%if @{column} %then [ column=] "@{column}" %end
%if @{alias} %then [ alias=] "@{alias}" %end
%if @{column-alias} %then [ column-alias=] "@{column-alias}" %end
%else
- [ alias=] "@{alias}"
+ %if @{alias} %then [ alias=] "@{alias}" %end
%end
%if @{expression} %then
View
7 schemas/xml/view.sch
@@ -25,4 +25,9 @@
%if @{simple-exp} %then
[<expression type="simple-exp">] @{simple-exp} </expression> $br
%end
-</view> $br $br
+
+ %if @{cte-exp} %then
+ [<expression type="cte-exp">] <! $ob CDATA $ob @{cte-exp} $cb $cb ></expression> $br
+ %end
+
+</view> $br $br
Please sign in to comment.
Something went wrong with that request. Please try again.