/** * Call this method to generate sql code for a UMLClassifier. * @param c the class to generate code for */ void SQLWriter::writeClass(UMLClassifier *c) { UMLEntity* e = c->asUMLEntity(); if (!e) { uError() << "Invalid cast from" << c->baseTypeStr() << "'" << c->name() << "' to UMLEntity*"; return; } m_pEntity = e; QString entityname = cleanName(m_pEntity->name()); //find an appropriate name for our file QString fileName = findFileName(m_pEntity, QLatin1String(".sql")); if (fileName.isEmpty()) { emit codeGenerated(m_pEntity, false); return; } QFile file; if (!openFile(file, fileName)) { emit codeGenerated(m_pEntity, false); return; } //Start generating the code!! QTextStream sql(&file); //try to find a heading file (license, coments, etc) QString str; str = getHeadingFile(QLatin1String(".sql")); if (!str.isEmpty()) { str.replace(QRegExp(QLatin1String("%filename%")), fileName); str.replace(QRegExp(QLatin1String("%filepath%")), file.fileName()); sql << str << m_endl; } //Write class Documentation if there is somthing or if force option if (forceDoc() || !m_pEntity->doc().isEmpty()) { sql << m_endl << "--" << m_endl; sql << "-- TABLE: " << entityname << m_endl; sql << formatDoc(m_pEntity->doc(),QLatin1String("-- ")); sql << "-- " << m_endl << m_endl; } // write all entity attributes UMLEntityAttributeList entAttList = m_pEntity->getEntityAttributes(); sql << "CREATE TABLE " << entityname << " ("; printEntityAttributes(sql, entAttList); sql << m_endl << ");" << m_endl; // auto increments UMLEntityAttributeList autoIncrementList; foreach(UMLEntityAttribute* entAtt, entAttList) { autoIncrementList.append(entAtt); }
// write all other indexes foreach(UMLEntityAttribute* ea, entAttList) { if (ea->indexType() != UMLEntityAttribute::Index) continue; UMLEntityAttributeList tempList; tempList.append(ea); printIndex(sql, m_pEntity, tempList); tempList.clear(); }
foreach( UMLClassifierListItem* cli, constrList ) { UMLForeignKeyConstraint* fkc = static_cast<UMLForeignKeyConstraint*>(cli); QMap<UMLEntityAttribute*, UMLEntityAttribute*> attributeMap = fkc->getEntityAttributePairs(); // get the referenced attributes QList<UMLEntityAttribute*> eaList = attributeMap.values(); // convert to UMLEntityAttributeList UMLEntityAttributeList refAttList; foreach( UMLEntityAttribute* ea, eaList ) { refAttList.append( ea ); }