38
46
void SQLWriter::writeClass(UMLClassifier *c) {
41
kDebug()<<"Cannot write class of NULL concept!" << endl;
48
UMLEntity* e = static_cast<UMLEntity*>(c);
51
uDebug()<<"Cannot write entity of NULL concept!";
45
const bool isClass = !c->isInterface();
46
QString classname = cleanName(c->getName());
57
QString entityname = cleanName(m_pEntity->getName());
48
59
//find an appropriate name for our file
49
QString fileName = findFileName(c, ".sql");
60
QString fileName = findFileName(m_pEntity, ".sql");
50
61
if (fileName.isEmpty()) {
51
emit codeGenerated(c, false);
62
emit codeGenerated(m_pEntity, false);
56
67
if( !openFile(file, fileName) ) {
57
emit codeGenerated(c, false);
68
emit codeGenerated(m_pEntity, false);
66
77
str = getHeadingFile(".sql");
67
78
if(!str.isEmpty()) {
68
79
str.replace(QRegExp("%filename%"),fileName);
69
str.replace(QRegExp("%filepath%"),file.name());
80
str.replace(QRegExp("%filepath%"),file.fileName());
73
84
//Write class Documentation if there is somthing or if force option
74
if(forceDoc() || !c->getDoc().isEmpty()) {
85
if(forceDoc() || !m_pEntity->getDoc().isEmpty()) {
75
86
sql << m_endl << "--" << m_endl;
76
sql<<"-- TABLE: "<<classname<<m_endl;
77
sql<<formatDoc(c->getDoc(),"-- ");
87
sql<<"-- TABLE: "<<entityname<<m_endl;
88
sql<<formatDoc(m_pEntity->getDoc(),"-- ");
78
89
sql << "-- " << m_endl << m_endl;
81
sql << "CREATE TABLE "<< classname << " ( " << m_endl;
84
writeAttributes(c, sql);
92
// write all entity attributes
93
UMLEntityAttributeList entAttList = m_pEntity->getEntityAttributes();
95
sql << "CREATE TABLE "<< entityname << " ( " << m_endl;
97
printEntityAttributes(sql, entAttList);
86
99
sql << m_endl << ");" << m_endl;
88
QMap<UMLAssociation*,UMLAssociation*> constraintMap; // so we don't repeat constraint
89
UMLAssociationList aggregations = c->getAggregations();
90
if( forceSections() || !aggregations.isEmpty() ) {
91
for(UMLAssociation* a = aggregations.first(); a; a = aggregations.next()) {
102
UMLEntityAttributeList autoIncrementList;
103
foreach( UMLEntityAttribute* entAtt, entAttList ) {
104
autoIncrementList.append(entAtt);
107
printAutoIncrements( sql, autoIncrementList );
110
// write all unique constraint ( including primary key )
111
UMLClassifierListItemList constrList = m_pEntity->getFilteredList(Uml::ot_UniqueConstraint);
112
printUniqueConstraints(sql, constrList);
115
// write all foreign key constraints
116
constrList = m_pEntity->getFilteredList(Uml::ot_ForeignKeyConstraint);
117
printForeignKeyConstraints(sql, constrList);
119
// write all check constraints
120
constrList = m_pEntity->getFilteredList(Uml::ot_CheckConstraint);
121
printCheckConstraints(sql,constrList);
123
// write all other indexes
124
foreach( UMLEntityAttribute* ea, entAttList ) {
125
if ( ea->getIndexType() != Uml::Index )
127
UMLEntityAttributeList tempList;
128
tempList.append( ea );
129
printIndex( sql, m_pEntity, tempList );
133
QMap<UMLAssociation*,UMLAssociation*> constraintMap; // so we don't repeat constraint
134
UMLAssociationList relationships = m_pEntity->getRelationships();
135
if( forceSections() || !relationships.isEmpty() ) {
136
foreach ( UMLAssociation* a , relationships ) {
92
137
UMLObject *objA = a->getObject(Uml::A);
93
138
UMLObject *objB = a->getObject(Uml::B);
94
if (objA->getID() == c->getID() && objB->getID() != c->getID())
139
if (objA->getID() == m_pEntity->getID() && objB->getID() != m_pEntity->getID())
96
141
constraintMap[a] = a;
100
145
QMap<UMLAssociation*,UMLAssociation*>::Iterator itor = constraintMap.begin();
101
146
for (;itor != constraintMap.end();itor++) {
102
UMLAssociation* a = itor.data();
103
sql << "ALTER TABLE "<< classname
147
UMLAssociation* a = itor.value();
148
sql << "ALTER TABLE "<< entityname
104
149
<< " ADD CONSTRAINT " << a->getName() << " FOREIGN KEY ("
105
150
<< a->getRoleName(Uml::B) << ") REFERENCES "
106
151
<< a->getObject(Uml::A)->getName()
107
152
<< " (" << a->getRoleName(Uml::A) << ");" << m_endl;
112
emit codeGenerated(c, true);
116
void SQLWriter::writeAttributes(UMLClassifier *c, QTextStream &sql) {
117
UMLAttributeList atpub, atprot, atpriv, atimp;
118
atpub.setAutoDelete(false);
119
atprot.setAutoDelete(false);
120
atpriv.setAutoDelete(false);
121
atimp.setAutoDelete(false);
123
//sort attributes by scope and see if they have a default value
124
UMLAttributeList atl = c->getAttributeList();
125
for(UMLAttribute* at=atl.first(); at ; at=atl.next()) {
126
switch(at->getVisibility()) {
127
case Uml::Visibility::Public:
130
case Uml::Visibility::Protected:
133
case Uml::Visibility::Private:
136
case Uml::Visibility::Implementation:
142
// now print the attributes; they are sorted by there scope
143
// in front of the first attribute shouldn't be a , -> so we need to find
144
// out, when the first attribute was added
147
if (atpub.count() > 0)
149
printAttributes(sql, atpub, first);
153
if (atprot.count() > 0)
155
printAttributes(sql, atprot, first);
159
if (atpriv.count() > 0)
161
printAttributes(sql, atpriv, first);
165
if (atimp.count() > 0)
167
printAttributes(sql, atimp, first);
174
void SQLWriter::printAttributes(QTextStream& sql, UMLAttributeList attributeList, bool first) {
175
QString attrDoc = "";
178
for (at=attributeList.first();at;at=attributeList.next())
180
// print , after attribute
181
if (first == false) {
187
// print documentation/comment of last attribute at end of line
188
if (attrDoc.isEmpty() == false)
190
sql << " -- " << attrDoc << m_endl;
195
// write the attribute
196
sql << m_indentation << cleanName(at->getName()) << " " << at->getTypeName() << " "
197
<< (at->getInitialValue().isEmpty()?QString(""):QString(" DEFAULT ")+at->getInitialValue());
199
// now get documentation/comment of current attribute
200
attrDoc = at->getDoc();
203
// print documentation/comment at end of line
204
if (attrDoc.isEmpty() == false)
206
sql << " -- " << attrDoc << m_endl;
158
emit codeGenerated(m_pEntity, true);
214
162
Uml::Programming_Language SQLWriter::getLanguage() {
215
163
return Uml::pl_SQL;
342
void SQLWriter::printEntityAttributes(QTextStream& sql, UMLEntityAttributeList entityAttributeList ) {
343
QString attrDoc = "";
347
foreach ( UMLEntityAttribute* at, entityAttributeList ) {
348
// print , after attribute
349
if (first == false) {
355
// print documentation/comment of last attribute at end of line
356
if (attrDoc.isEmpty() == false) {
357
sql << " -- " << attrDoc << m_endl;
362
// write the attribute name
363
sql << m_indentation << cleanName(at->getName()) ;
366
sql<< ' ' << at->getTypeName();
368
// the length ( if there's some value)
369
QString lengthStr = at->getValues().trimmed();
371
uint length = lengthStr.toUInt(&ok);
374
sql<<'('<<length<<')';
376
// write the attributes ( unsigned, zerofill etc)
377
QString attributes = at->getAttributes().trimmed();
378
if ( !attributes.isEmpty() ) {
379
sql<<' '<<attributes;
382
if ( !at->getNull() ) {
386
// write any default values
387
sql<< (at->getInitialValue().isEmpty()?QString(""):QString(" DEFAULT ")+at->getInitialValue());
389
// now get documentation/comment of current attribute
390
attrDoc = at->getDoc();
393
// print documentation/comment at end of line
394
if (attrDoc.isEmpty() == false) {
395
sql << " -- " << attrDoc << m_endl;
404
void SQLWriter::printUniqueConstraints(QTextStream& sql, UMLClassifierListItemList constrList){
406
foreach( UMLClassifierListItem* cli, constrList ) {
409
UMLUniqueConstraint* uuc = static_cast<UMLUniqueConstraint*>(cli);
411
UMLEntityAttributeList attList = uuc->getEntityAttributeList();
413
// print documentation
414
sql<<"-- "<<uuc->getDoc();
417
sql<<"ALTER TABLE "<<cleanName(m_pEntity->getName())
418
<<" ADD CONSTRAINT "<<cleanName(uuc->getName());
420
if ( m_pEntity->isPrimaryKey( uuc ) )
421
sql<<" PRIMARY KEY ";
428
foreach( UMLEntityAttribute* entAtt, attList ) {
434
sql<<cleanName(entAtt->getName());
444
void SQLWriter::printForeignKeyConstraints(QTextStream& sql, UMLClassifierListItemList constrList) {
445
foreach( UMLClassifierListItem* cli, constrList ) {
448
UMLForeignKeyConstraint* fkc = static_cast<UMLForeignKeyConstraint*>(cli);
450
QMap<UMLEntityAttribute*,UMLEntityAttribute*> attributeMap;
451
attributeMap = fkc->getEntityAttributePairs();
453
// print documentation
454
sql<<"-- "<<fkc->getDoc();
457
sql<<"ALTER TABLE "<<cleanName(m_pEntity->getName())
458
<<" ADD CONSTRAINT "<<cleanName(fkc->getName());
460
sql<<" FOREIGN KEY (";
462
QList<UMLEntityAttribute*> entAttList = attributeMap.keys();
465
// the local attributes which refer the attributes of the referenced entity
466
foreach( UMLEntityAttribute* entAtt, entAttList ) {
472
sql<<cleanName( entAtt->getName() );
479
UMLEntity* referencedEntity = fkc->getReferencedEntity();
480
sql<<cleanName( referencedEntity->getName() );
484
QList<UMLEntityAttribute*> refEntAttList = attributeMap.values();
487
// the attributes of the referenced entity which are being referenced
488
foreach( UMLEntityAttribute* refEntAtt, refEntAttList ) {
494
sql<<cleanName( refEntAtt->getName() );
499
UMLForeignKeyConstraint::UpdateDeleteAction updateAction, deleteAction;
500
updateAction = fkc->getUpdateAction();
501
deleteAction = fkc->getDeleteAction();
503
sql<<" ON UPDATE "<<Model_Utils::updateDeleteActionToString(updateAction);
504
sql<<" ON DELETE "<<Model_Utils::updateDeleteActionToString(deleteAction);
513
void SQLWriter::printIndex(QTextStream& sql, UMLEntity* ent , UMLEntityAttributeList entAttList) {
516
sql<<"CREATE INDEX ";
518
// we don;t have any name, so we just merge the names of all attributes along with their entity name
520
sql<<cleanName( ent->getName() )<<'_';
521
foreach( UMLEntityAttribute* entAtt, entAttList ) {
522
sql<<cleanName( entAtt->getName() )<<'_';
526
sql<<" ON "<<cleanName( m_pEntity->getName() )<<'(';
530
foreach( UMLEntityAttribute* entAtt, entAttList ) {
536
sql<<cleanName( entAtt->getName() );
546
void SQLWriter::printAutoIncrements(QTextStream& /*sql*/, UMLEntityAttributeList /*entAttList*/ ) {
547
// defer to derived classes
550
void SQLWriter::printCheckConstraints(QTextStream& sql,UMLClassifierListItemList constrList) {
552
foreach( UMLClassifierListItem* cli, constrList ) {
553
UMLCheckConstraint* chConstr = static_cast<UMLCheckConstraint*>(cli);
556
// print documentation
557
sql<<"-- "<<chConstr->getDoc();
561
sql<<"ALTER TABLE "<<cleanName( m_pEntity->getName() )
562
<<" ADD CONSTRAINT "<<cleanName( chConstr->getName() )
563
<<" CHECK ("<<chConstr->getCheckCondition()
394
572
#include "sqlwriter.moc"