void Table::setColumnsAttribute(unsigned def_type) { QString str_cols; unsigned i, count; //Concatena a definiação SQL das colunas count=columns.size(); for(i=0; i < count; i++) { /* Do not generates the column code definition when it is not included by relatoinship, in case of XML definition. */ if((def_type==SchemaParser::SQL_DEFINITION && !columns[i]->isAddedByCopy())|| (def_type==SchemaParser::XML_DEFINITION && !columns[i]->isAddedByRelationship())) { str_cols+=columns[i]->getCodeDefinition(def_type); if(def_type==SchemaParser::SQL_DEFINITION) setCommentAttribute(columns[i]); } } if(def_type==SchemaParser::SQL_DEFINITION) { if(str_cols!="") { count=str_cols.size(); if(str_cols[count-2]==',' || str_cols[count-2]=='\n') str_cols.remove(count-2,2); } } attributes[ParsersAttributes::COLUMNS]=str_cols; }
void Table::setConstraintsAttribute(unsigned def_type) { QString str_constr; unsigned i, count; bool inc_added_by_rel; Constraint *constr=nullptr; count=constraints.size(); for(i=0; i < count; i++) { constr=dynamic_cast<Constraint *>(constraints[i]); if((def_type==SchemaParser::SQL_DEFINITION && (!constr->isReferRelationshipAddedColumn() || constr->getConstraintType()==ConstraintType::primary_key)) || (def_type==SchemaParser::XML_DEFINITION && !constr->isAddedByRelationship() && ((constr->getConstraintType()!=ConstraintType::primary_key && !constr->isReferRelationshipAddedColumn()) || (constr->getConstraintType()==ConstraintType::primary_key)))) { inc_added_by_rel=(def_type==SchemaParser::SQL_DEFINITION); str_constr+=constr->getCodeDefinition(def_type,inc_added_by_rel); if(def_type==SchemaParser::SQL_DEFINITION) setCommentAttribute(constr); } } if(def_type==SchemaParser::SQL_DEFINITION) { if(str_constr!="") { count=str_constr.size(); if(str_constr[count-2]==',' || str_constr[count-2]=='\n') str_constr.remove(count-2,2); } } attributes[ParsersAttributes::CONSTRAINTS]=str_constr; }
void Table::setColumnsAttribute(unsigned def_type) { QString str_cols, inh_cols; unsigned i, count; count=columns.size(); for(i=0; i < count; i++) { /* Do not generates the column code definition when it is not included by relatoinship, in case of XML definition. */ if((def_type==SchemaParser::SQL_DEFINITION && !columns[i]->isAddedByCopy() && !columns[i]->isAddedByGeneralization())|| (def_type==SchemaParser::XML_DEFINITION && !columns[i]->isAddedByRelationship())) { str_cols+=columns[i]->getCodeDefinition(def_type); if(def_type==SchemaParser::SQL_DEFINITION) setCommentAttribute(columns[i]); } else if(def_type==SchemaParser::SQL_DEFINITION && columns[i]->isAddedByGeneralization() && !gen_alter_cmds) { inh_cols+=QString("-- ") + columns[i]->getCodeDefinition(def_type); } } if(def_type==SchemaParser::SQL_DEFINITION) { if(!str_cols.isEmpty()) { count=str_cols.size(); if(str_cols[count-2]==',' || str_cols[count-2]=='\n') str_cols.remove(count-2,2); } attributes[ParsersAttributes::INH_COLUMNS]=inh_cols; } attributes[ParsersAttributes::COLUMNS]=str_cols; }
void Table::setConstraintsAttribute(unsigned def_type) { QString str_constr; unsigned i, count; bool inc_added_by_rel; Constraint *constr=nullptr; vector<QString> lines; count=constraints.size(); for(i=0; i < count; i++) { constr=dynamic_cast<Constraint *>(constraints[i]); if(constr->getConstraintType()!=ConstraintType::foreign_key && ((def_type==SchemaParser::SQL_DEFINITION && ((!constr->isReferRelationshipAddedColumn() && constr->getConstraintType()!=ConstraintType::check) || (constr->getConstraintType()==ConstraintType::check && !constr->isAddedByGeneralization()) || constr->getConstraintType()==ConstraintType::primary_key)) || (def_type==SchemaParser::XML_DEFINITION && !constr->isAddedByRelationship() && ((constr->getConstraintType()!=ConstraintType::primary_key && !constr->isReferRelationshipAddedColumn()) || (constr->getConstraintType()==ConstraintType::primary_key))))) { inc_added_by_rel=(def_type==SchemaParser::SQL_DEFINITION); if(def_type==SchemaParser::XML_DEFINITION) str_constr+=constr->getCodeDefinition(def_type,inc_added_by_rel); else //For sql definition the generated constraints are stored in a vector to be treated below lines.push_back(constr->getCodeDefinition(def_type,inc_added_by_rel)); if(def_type==SchemaParser::SQL_DEFINITION) setCommentAttribute(constr); } } if(def_type==SchemaParser::SQL_DEFINITION && !lines.empty()) { /* When the coistraints are being generated in form of ALTER commands simply concatenates all the lines */ if(gen_alter_cmds) { for(i=0; i < lines.size(); i++) str_constr+=lines[i]; } else { /* Check if some constraint has its sql disabled. If so, it necessary to make some tweaks in order to not generate bad sql code */ i=lines.size()-1; unsigned dis_sql_cnt=0; //If the last line starts with -- indicates that sql code for the constraint is disable if(lines[i].startsWith(QLatin1String("--")) && i > 0) //Removes the comma from the above line in order to avoid bad sql lines[i-1].remove(lines[i-1].lastIndexOf(','),1); else //Otherwise removes the comma from the last line lines[i].remove(lines[i].lastIndexOf(','),1); for(i=0; i < lines.size(); i++) { if(lines[i].startsWith(QLatin1String("--"))) dis_sql_cnt++; str_constr+=lines[i]; } attributes[ParsersAttributes::CONSTR_SQL_DISABLED]=(dis_sql_cnt==lines.size() ? ParsersAttributes::_TRUE_ : QString()); } } attributes[ParsersAttributes::CONSTRAINTS]=str_constr; }