QString Column::getTypeReference(void) { if(getParentTable()) return(getParentTable()->getName(true) + QString(".") + this->getName(true) + QString("%TYPE")); else return(QString()); }
QString Index::getCodeDefinition(unsigned tipo_def) { setIndexElementsAttribute(tipo_def); attributes[ParsersAttributes::UNIQUE]=(index_attribs[UNIQUE] ? "1" : ""); attributes[ParsersAttributes::CONCURRENT]=(index_attribs[CONCURRENT] ? "1" : ""); attributes[ParsersAttributes::INDEX_TYPE]=(~indexing_type); attributes[ParsersAttributes::CONDITION]=conditional_expr; attributes[ParsersAttributes::STORAGE_PARAMS]=""; if(getParentTable()) { attributes[ParsersAttributes::TABLE]=getParentTable()->getName(true); if(tipo_def==SchemaParser::SQL_DEFINITION && getParentTable()->getSchema()) attributes[ParsersAttributes::SCHEMA]=getParentTable()->getSchema()->getName(true); } if(this->indexing_type==IndexingType::gin) attributes[ParsersAttributes::STORAGE_PARAMS]=attributes[ParsersAttributes::FAST_UPDATE]=(index_attribs[FAST_UPDATE] ? "1" : ""); if(this->indexing_type==IndexingType::btree && fill_factor >= 10) { attributes[ParsersAttributes::FACTOR]=QString("%1").arg(fill_factor); attributes[ParsersAttributes::STORAGE_PARAMS]="1"; } else if(tipo_def==SchemaParser::XML_DEFINITION) attributes[ParsersAttributes::FACTOR]="0"; /* Case the index 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"; return(BaseObject::__getCodeDefinition(tipo_def)); }
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 Column::getCodeDefinition(unsigned def_type) { QString code_def=getCachedCode(def_type, false); if(!code_def.isEmpty()) return(code_def); if(getParentTable()) attributes[ParsersAttributes::TABLE]=getParentTable()->getName(true); attributes[ParsersAttributes::TYPE]=type.getCodeDefinition(def_type); attributes[ParsersAttributes::DEFAULT_VALUE]=QString(); if(!sequence) attributes[ParsersAttributes::DEFAULT_VALUE]=default_value; else { //Configuring the default value of the column to get the next value of the sequence if(def_type==SchemaParser::SQL_DEFINITION) attributes[ParsersAttributes::DEFAULT_VALUE]=QString("nextval('%1'::regclass)").arg(sequence->getSignature());//.remove("\"")); attributes[ParsersAttributes::SEQUENCE]=sequence->getName(true); } attributes[ParsersAttributes::NOT_NULL]=(!not_null ? QString() : ParsersAttributes::_TRUE_); attributes[ParsersAttributes::DECL_IN_TABLE]=(isDeclaredInTable() ? ParsersAttributes::_TRUE_ : QString()); return(BaseObject::__getCodeDefinition(def_type)); }
QString Constraint::getSignature(bool format) { if(!getParentTable()) return(BaseObject::getSignature(format)); return(QString("%1 ON %2 ").arg(this->getName(format)).arg(getParentTable()->getSignature(true))); }
QString Column::getCodeDefinition(unsigned def_type) { if(getParentTable()) attributes[ParsersAttributes::TABLE]=getParentTable()->getName(true); attributes[ParsersAttributes::TYPE]=type.getCodeDefinition(def_type); attributes[ParsersAttributes::DEFAULT_VALUE]=default_value; attributes[ParsersAttributes::NOT_NULL]=(!not_null ? "" : "1"); attributes[ParsersAttributes::DECL_IN_TABLE]=(isDeclaredInTable() ? "1" : ""); return(BaseObject::__getCodeDefinition(def_type)); }
QString Column::getAlterDefinition(BaseObject *object) { Column *col=dynamic_cast<Column *>(object); if(!col) throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__); try { attribs_map attribs; QString def_val; BaseObject::setBasicAttributes(true); if(getParentTable()) attribs[ParsersAttributes::TABLE]=getParentTable()->getName(true); if(!this->type.isEquivalentTo(col->type) || (this->type.isEquivalentTo(col->type) && ((this->type.hasVariableLength() && (this->type.getLength()!=col->type.getLength())) || (this->type.acceptsPrecision() && (this->type.getPrecision()!=col->type.getPrecision()))))) attribs[ParsersAttributes::TYPE]=col->type.getCodeDefinition(SchemaParser::SQL_DEFINITION); if(col->sequence) def_val=QString("nextval('%1'::regclass)").arg(col->sequence->getSignature()); else def_val=col->default_value; if(this->default_value!=def_val) attribs[ParsersAttributes::DEFAULT_VALUE]=(def_val.isEmpty() ? ParsersAttributes::UNSET : def_val); if(this->not_null!=col->not_null) attribs[ParsersAttributes::NOT_NULL]=(!col->not_null ? ParsersAttributes::UNSET : ParsersAttributes::_TRUE_); copyAttributes(attribs); return(BaseObject::getAlterDefinition(this->getSchemaName(), attributes, false, true)); } catch(Exception &e) { throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e); } }
void Trigger::validateTrigger(void) { if(getParentTable()) { ObjectType parent_type=getParentTable()->getObjectType(); if(!is_constraint) { //The INSTEAD OF mode cannot be used on triggers that belongs to tables! This is available only for view triggers if(firing_type==FiringType::instead_of && parent_type==OBJ_TABLE) throw Exception(ERR_TABLE_TRIG_INSTEADOF_FIRING,__PRETTY_FUNCTION__,__FILE__,__LINE__); //The INSTEAD OF mode cannot be used on view triggers that executes for each statement else if(firing_type==FiringType::instead_of && parent_type==OBJ_VIEW && !is_exec_per_row) throw Exception(ERR_TRIGGER_INV_INSTEADOF_USAGE,__PRETTY_FUNCTION__,__FILE__,__LINE__); //A trigger cannot make reference to columns when using INSTEAD OF mode and UPDATE event else if(firing_type==FiringType::instead_of && events[EventType::on_update] && !upd_columns.empty()) throw Exception(ERR_TRIGGER_INV_INSTEADOF_UPDATE,__PRETTY_FUNCTION__,__FILE__,__LINE__); //The TRUNCATE event can only be used when the trigger executes for each statement and belongs to a table else if(events[EventType::on_truncate] && (is_exec_per_row || parent_type==OBJ_VIEW)) throw Exception(ERR_TRIGGER_INV_TRUNCATE_USAGE,__PRETTY_FUNCTION__,__FILE__,__LINE__); //A view trigger cannot be AFTER/BEFORE when it executes for each row else if(parent_type==OBJ_VIEW && is_exec_per_row && (firing_type==FiringType::after || firing_type==FiringType::before)) throw Exception(ERR_VIEW_TRIG_INV_AFTBFR_USAGE,__PRETTY_FUNCTION__,__FILE__,__LINE__); //Only constraint triggers can be deferrable or reference another table else if(referenced_table || is_deferrable) throw Exception(ERR_TRIG_USING_CONSTRIG_ATRIBS,__PRETTY_FUNCTION__,__FILE__,__LINE__); } //Constraint triggers can only be executed on AFTER events and for each row else { if(firing_type!=FiringType::after && !is_exec_per_row) throw Exception(ERR_CONST_TRIG_NOT_AFTER_ROW,__PRETTY_FUNCTION__,__FILE__,__LINE__); } } }
QString Constraint::getCodeDefinition(unsigned def_type, bool inc_addedbyrel) { QString code_def=getCachedCode(def_type, false); if(!inc_addedbyrel && !code_def.isEmpty()) return(code_def); QString attrib; attributes[ParsersAttributes::PK_CONSTR]=QString(); attributes[ParsersAttributes::FK_CONSTR]=QString(); attributes[ParsersAttributes::CK_CONSTR]=QString(); attributes[ParsersAttributes::UQ_CONSTR]=QString(); attributes[ParsersAttributes::EX_CONSTR]=QString(); switch(!constr_type) { case ConstraintType::check: attrib=ParsersAttributes::CK_CONSTR; break; case ConstraintType::primary_key: attrib=ParsersAttributes::PK_CONSTR; break; case ConstraintType::foreign_key: attrib=ParsersAttributes::FK_CONSTR; break; case ConstraintType::unique: attrib=ParsersAttributes::UQ_CONSTR; break; default: attrib=ParsersAttributes::EX_CONSTR; break; } attributes[attrib]=ParsersAttributes::_TRUE_; attributes[ParsersAttributes::TYPE]=attrib; attributes[ParsersAttributes::UPD_ACTION]=(~upd_action); attributes[ParsersAttributes::DEL_ACTION]=(~del_action); attributes[ParsersAttributes::EXPRESSION]=expression; if(constr_type!=ConstraintType::check) { if(constr_type!=ConstraintType::exclude) setColumnsAttribute(SOURCE_COLS, def_type, inc_addedbyrel); else setExcludeElementsAttribute(def_type); /* Only generates the definition of the foreign key referenced columns if the number of columns of the source and referenced cols list are equal, this means the constraint is configured correctly, otherwise don't generates the attribute forcing the schema parser to return an error because the foreign key is misconfigured. */ if(constr_type==ConstraintType::foreign_key && columns.size() == ref_columns.size()) setColumnsAttribute(REFERENCED_COLS, def_type, inc_addedbyrel); } attributes[ParsersAttributes::REF_TABLE]=(ref_table ? ref_table->getName(true) : QString()); attributes[ParsersAttributes::DEFERRABLE]=(deferrable ? ParsersAttributes::_TRUE_ : QString()); attributes[ParsersAttributes::NO_INHERIT]=(no_inherit ? ParsersAttributes::_TRUE_ : QString()); attributes[ParsersAttributes::COMPARISON_TYPE]=(~match_type); attributes[ParsersAttributes::DEFER_TYPE]=(~deferral_type); attributes[ParsersAttributes::INDEX_TYPE]=(~ index_type); if(getParentTable()) attributes[ParsersAttributes::TABLE]=getParentTable()->getName(true); setDeclInTableAttribute(); if(fill_factor!=0 && (constr_type==ConstraintType::primary_key || constr_type==ConstraintType::unique)) attributes[ParsersAttributes::FACTOR]=QString("%1").arg(fill_factor); else attributes[ParsersAttributes::FACTOR]=QString(); return(BaseObject::__getCodeDefinition(def_type)); }