QString Trigger::getCodeDefinition(unsigned def_type) { setBasicAttributes(def_type); /* Case the trigger doesn't referece some column added by relationship it will be declared inside the parent table construction by the use of 'decl-in-table' schema attribute */ if(!isReferRelationshipAddedColumn()) attributes[ParsersAttributes::DECL_IN_TABLE]="1"; if(getParentTable()) attributes[ParsersAttributes::TABLE]=getParentTable()->getName(true); attributes[ParsersAttributes::CONSTRAINT]=(is_constraint ? "1" : ""); attributes[ParsersAttributes::FIRING_TYPE]=(~firing_type); //** Constraint trigger MUST execute per row ** attributes[ParsersAttributes::PER_ROW]=((is_exec_per_row && !is_constraint) || is_constraint ? "1" : ""); attributes[ParsersAttributes::CONDITION]=condition; if(referenced_table) { attributes[ParsersAttributes::REF_TABLE]=referenced_table->getName(true); attributes[ParsersAttributes::DEFERRABLE]=(is_deferrable ? "1" : ""); attributes[ParsersAttributes::DEFER_TYPE]=(~deferral_type); } return(BaseObject::__getCodeDefinition(def_type)); }
QString BaseObject::getDropDefinition(bool cascade) { try { if(acceptsDropCommand()) { attribs_map attribs; setBasicAttributes(true); schparser.setPgSQLVersion(BaseObject::pgsql_ver); schparser.ignoreUnkownAttributes(true); schparser.ignoreEmptyAttributes(true); attribs=attributes; /* Creating an attribute that identifies the object type in order to permit conditional code generation inside the DROP script */ if(attribs.count(this->getSchemaName())==0) attribs[this->getSchemaName()]=ParsersAttributes::_TRUE_; attribs[ParsersAttributes::CASCADE]=(cascade ? ParsersAttributes::_TRUE_ : QString()); return(schparser.getCodeDefinition(ParsersAttributes::DROP, attribs, SchemaParser::SQL_DEFINITION)); } else return(QString()); } catch(Exception &e) { throw Exception(e.getErrorMessage(), e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e); } }
QString BaseObject::getAlterDefinition(BaseObject *object, bool ignore_name_diff) { if(!object) throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__); QString alter; if(object->obj_type!=this->obj_type) throw Exception(ERR_OPR_OBJ_INV_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__); setBasicAttributes(true); try { QStringList attribs={ ParsersAttributes::OWNER, ParsersAttributes::SCHEMA, ParsersAttributes::TABLESPACE }; bool accepts_obj[3]={ acceptsOwner(), acceptsSchema(), acceptsTablespace() }; BaseObject *dep_objs[3]={ this->getOwner(), this->getSchema(), this->getTablespace() }, *aux_dep_objs[3]={ object->getOwner(), object->getSchema(), object->getTablespace() }; if(!ignore_name_diff && this->getName()!=object->getName()) { attributes[ParsersAttributes::NEW_NAME]=object->getName(true, false); alter+=BaseObject::getAlterDefinition(ParsersAttributes::RENAME, attributes, true); attributes[ParsersAttributes::NAME]=attributes[ParsersAttributes::NEW_NAME]; attributes[ParsersAttributes::SIGNATURE]=object->getSignature(true); } for(unsigned i=0; i < 3; i++) { if(accepts_obj[i] && dep_objs[i] && aux_dep_objs[i] && dep_objs[i]->getName(true)!=aux_dep_objs[i]->getName(true)) { attributes[attribs[i]]=aux_dep_objs[i]->getName(true); alter+=BaseObject::getAlterDefinition(attribs[i], attributes, true); } } if(this->getComment()!=object->getComment()) { if(object->getComment().isEmpty()) attributes[ParsersAttributes::COMMENT]=ParsersAttributes::UNSET; else attributes[ParsersAttributes::COMMENT]=object->getComment(); schparser.ignoreUnkownAttributes(true); schparser.ignoreEmptyAttributes(true); alter+=schparser.getCodeDefinition(ParsersAttributes::COMMENT, attributes, SchemaParser::SQL_DEFINITION); } } catch(Exception &e) { throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e); } return(alter); }
QString BaseObject::getCodeDefinition(unsigned def_type, bool reduced_form) { QString code_def; if((def_type==SchemaParser::SQL_DEFINITION && obj_type!=BASE_OBJECT && obj_type!=BASE_RELATIONSHIP && obj_type!=BASE_TABLE && obj_type!=OBJ_TEXTBOX) || (def_type==SchemaParser::XML_DEFINITION && obj_type!=BASE_OBJECT && obj_type!=BASE_TABLE)) { bool format=false; schparser.setPgSQLVersion(BaseObject::pgsql_ver); attributes[ParsersAttributes::SQL_DISABLED]=(sql_disabled ? ParsersAttributes::_TRUE_ : QString()); //Formats the object's name in case the SQL definition is being generated format=((def_type==SchemaParser::SQL_DEFINITION) || (def_type==SchemaParser::XML_DEFINITION && reduced_form && obj_type!=OBJ_TEXTBOX && obj_type!=OBJ_RELATIONSHIP)); setBasicAttributes(format); if(schema) { if(def_type==SchemaParser::XML_DEFINITION) attributes[ParsersAttributes::SCHEMA]=schema->getCodeDefinition(def_type, true); else attributes[ParsersAttributes::SCHEMA]=schema->getName(format); } if(def_type==SchemaParser::XML_DEFINITION) attributes[ParsersAttributes::PROTECTED]=(is_protected ? ParsersAttributes::_TRUE_ : QString()); if(tablespace) { if(def_type==SchemaParser::SQL_DEFINITION) attributes[ParsersAttributes::TABLESPACE]=tablespace->getName(format); else attributes[ParsersAttributes::TABLESPACE]=tablespace->getCodeDefinition(def_type, true); } if(collation && attributes[ParsersAttributes::COLLATION].isEmpty()) { if(def_type==SchemaParser::SQL_DEFINITION) attributes[ParsersAttributes::COLLATION]=collation->getName(format); else attributes[ParsersAttributes::COLLATION]=collation->getCodeDefinition(def_type, true); } if(owner) { if(def_type==SchemaParser::SQL_DEFINITION) { attributes[ParsersAttributes::OWNER]=owner->getName(format); /** Only tablespaces and database do not have an ALTER OWNER SET because the rule says that PostgreSQL tablespaces and database should be created with just a command line isolated from the others **/ if(obj_type!=OBJ_TABLESPACE && obj_type!=OBJ_DATABASE) { SchemaParser sch_parser; QString filename=GlobalAttributes::SCHEMAS_ROOT_DIR + GlobalAttributes::DIR_SEPARATOR + GlobalAttributes::ALTER_SCHEMA_DIR + GlobalAttributes::DIR_SEPARATOR + ParsersAttributes::OWNER + GlobalAttributes::SCHEMA_EXT; sch_parser.ignoreUnkownAttributes(true); attributes[ParsersAttributes::OWNER]=sch_parser.getCodeDefinition(filename, attributes); } } else attributes[ParsersAttributes::OWNER]=owner->getCodeDefinition(def_type, true); } if(!comment.isEmpty()) { if(def_type==SchemaParser::SQL_DEFINITION) attributes[ParsersAttributes::COMMENT]=QString(comment).replace(QString("'"), QString("''")); else attributes[ParsersAttributes::COMMENT]=comment; if((def_type==SchemaParser::SQL_DEFINITION && obj_type!=OBJ_TABLESPACE && obj_type!=OBJ_DATABASE) || def_type==SchemaParser::XML_DEFINITION) { schparser.ignoreUnkownAttributes(true); attributes[ParsersAttributes::COMMENT]= schparser.getCodeDefinition(ParsersAttributes::COMMENT, attributes, def_type); } } if(!appended_sql.isEmpty()) { attributes[ParsersAttributes::APPENDED_SQL]=appended_sql; if(def_type==SchemaParser::XML_DEFINITION) { schparser.ignoreUnkownAttributes(true); attributes[ParsersAttributes::APPENDED_SQL]= schparser.getCodeDefinition(QString(ParsersAttributes::APPENDED_SQL).remove('-'), attributes, def_type); } else { attributes[ParsersAttributes::APPENDED_SQL]=QString("\n-- Appended SQL commands --\n") + appended_sql; } } if(!prepended_sql.isEmpty()) { attributes[ParsersAttributes::PREPENDED_SQL]=prepended_sql; if(def_type==SchemaParser::XML_DEFINITION) { schparser.ignoreUnkownAttributes(true); attributes[ParsersAttributes::PREPENDED_SQL]= schparser.getCodeDefinition(QString(ParsersAttributes::PREPENDED_SQL).remove('-'), attributes, def_type); } else { attributes[ParsersAttributes::PREPENDED_SQL]=QString("\n-- Prepended SQL commands --\n") + prepended_sql; } } if(def_type==SchemaParser::SQL_DEFINITION && this->acceptsDropCommand()) { attributes[ParsersAttributes::DROP]=getDropDefinition(true); attributes[ParsersAttributes::DROP].remove(ParsersAttributes::DDL_END_TOKEN + '\n'); } attributes[ParsersAttributes::REDUCED_FORM]=(reduced_form ? ParsersAttributes::_TRUE_ : QString()); try { code_def+=schparser.getCodeDefinition(objs_schemas[obj_type], attributes, def_type); //Internally disabling the SQL definition if(sql_disabled && def_type==SchemaParser::SQL_DEFINITION) { //Creates a text stream and insert an comment start token on each line QTextStream ts(&code_def); QString buf; while(!ts.atEnd()) buf+=QString("-- %1\n").arg(ts.readLine()); //The entire commented buffer will be returned code_def=buf; } clearAttributes(); //Database object doesn't handles cached code. if(use_cached_code && obj_type!=OBJ_DATABASE) { if(def_type==SchemaParser::SQL_DEFINITION || (!reduced_form && def_type==SchemaParser::XML_DEFINITION)) cached_code[def_type]=code_def; else if(reduced_form) cached_reduced_code=code_def; } code_invalidated=false; } catch(Exception &e) { schparser.restartParser(); clearAttributes(); if(e.getErrorType()==ERR_UNDEF_ATTRIB_VALUE) throw Exception(Exception::getErrorMessage(ERR_ASG_OBJ_INV_DEFINITION) .arg(this->getName(true)) .arg(this->getTypeName()), ERR_ASG_OBJ_INV_DEFINITION,__PRETTY_FUNCTION__,__FILE__,__LINE__,&e); else throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e); } } return(code_def); }