Permalink
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...
1 parent feb2531 commit 45163a3af39bf24299fe0ee09a8e363ee4db666d @rkhaotix rkhaotix committed Mar 19, 2013
@@ -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
@@ -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"/>
@@ -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())
@@ -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)
{
@@ -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)
@@ -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
@@ -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);
Oops, something went wrong.

0 comments on commit 45163a3

Please sign in to comment.