sqlwriter.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "sqlwriter.h"
00018
00019 #include <kdebug.h>
00020
00021 #include <klocale.h>
00022 #include <kmessagebox.h>
00023 #include <qfile.h>
00024 #include <qtextstream.h>
00025 #include <qregexp.h>
00026
00027 #include "../classifier.h"
00028 #include "../operation.h"
00029 #include "../umlnamespace.h"
00030 #include "../association.h"
00031 #include "../attribute.h"
00032
00033 SQLWriter::SQLWriter() {
00034 }
00035
00036 SQLWriter::~SQLWriter() {}
00037
00038 void SQLWriter::writeClass(UMLClassifier *c) {
00039
00040 if(!c) {
00041 kDebug()<<"Cannot write class of NULL concept!" << endl;
00042 return;
00043 }
00044
00045 const bool isClass = !c->isInterface();
00046 QString classname = cleanName(c->getName());
00047
00048
00049 QString fileName = findFileName(c, ".sql");
00050 if (fileName.isEmpty()) {
00051 emit codeGenerated(c, false);
00052 return;
00053 }
00054
00055 QFile file;
00056 if( !openFile(file, fileName) ) {
00057 emit codeGenerated(c, false);
00058 return;
00059 }
00060
00061
00062
00063 QTextStream sql(&file);
00064
00065 QString str;
00066 str = getHeadingFile(".sql");
00067 if(!str.isEmpty()) {
00068 str.replace(QRegExp("%filename%"),fileName);
00069 str.replace(QRegExp("%filepath%"),file.name());
00070 sql<<str<<m_endl;
00071 }
00072
00073
00074 if(forceDoc() || !c->getDoc().isEmpty()) {
00075 sql << m_endl << "--" << m_endl;
00076 sql<<"-- TABLE: "<<classname<<m_endl;
00077 sql<<formatDoc(c->getDoc(),"-- ");
00078 sql << "-- " << m_endl << m_endl;
00079 }
00080
00081 sql << "CREATE TABLE "<< classname << " ( " << m_endl;
00082
00083 if (isClass)
00084 writeAttributes(c, sql);
00085
00086 sql << m_endl << ");" << m_endl;
00087
00088 QMap<UMLAssociation*,UMLAssociation*> constraintMap;
00089 UMLAssociationList aggregations = c->getAggregations();
00090 if( forceSections() || !aggregations.isEmpty() ) {
00091 for(UMLAssociation* a = aggregations.first(); a; a = aggregations.next()) {
00092 UMLObject *objA = a->getObject(Uml::A);
00093 UMLObject *objB = a->getObject(Uml::B);
00094 if (objA->getID() == c->getID() && objB->getID() != c->getID())
00095 continue;
00096 constraintMap[a] = a;
00097 }
00098 }
00099
00100 QMap<UMLAssociation*,UMLAssociation*>::Iterator itor = constraintMap.begin();
00101 for (;itor != constraintMap.end();itor++) {
00102 UMLAssociation* a = itor.data();
00103 sql << "ALTER TABLE "<< classname
00104 << " ADD CONSTRAINT " << a->getName() << " FOREIGN KEY ("
00105 << a->getRoleName(Uml::B) << ") REFERENCES "
00106 << a->getObject(Uml::A)->getName()
00107 << " (" << a->getRoleName(Uml::A) << ");" << m_endl;
00108 }
00109
00110
00111 file.close();
00112 emit codeGenerated(c, true);
00113 }
00114
00115
00116 void SQLWriter::writeAttributes(UMLClassifier *c, QTextStream &sql) {
00117 UMLAttributeList atpub, atprot, atpriv, atimp;
00118 atpub.setAutoDelete(false);
00119 atprot.setAutoDelete(false);
00120 atpriv.setAutoDelete(false);
00121 atimp.setAutoDelete(false);
00122
00123
00124 UMLAttributeList atl = c->getAttributeList();
00125 for(UMLAttribute* at=atl.first(); at ; at=atl.next()) {
00126 switch(at->getVisibility()) {
00127 case Uml::Visibility::Public:
00128 atpub.append(at);
00129 break;
00130 case Uml::Visibility::Protected:
00131 atprot.append(at);
00132 break;
00133 case Uml::Visibility::Private:
00134 atpriv.append(at);
00135 break;
00136 case Uml::Visibility::Implementation:
00137 atimp.append(at);
00138 break;
00139 }
00140 }
00141
00142
00143
00144
00145 bool first = true;
00146
00147 if (atpub.count() > 0)
00148 {
00149 printAttributes(sql, atpub, first);
00150 first = false;
00151 }
00152
00153 if (atprot.count() > 0)
00154 {
00155 printAttributes(sql, atprot, first);
00156 first = false;
00157 }
00158
00159 if (atpriv.count() > 0)
00160 {
00161 printAttributes(sql, atpriv, first);
00162 first = false;
00163 }
00164
00165 if (atimp.count() > 0)
00166 {
00167 printAttributes(sql, atimp, first);
00168 first = false;
00169 }
00170
00171 return;
00172 }
00173
00174 void SQLWriter::printAttributes(QTextStream& sql, UMLAttributeList attributeList, bool first) {
00175 QString attrDoc = "";
00176 UMLAttribute* at;
00177
00178 for (at=attributeList.first();at;at=attributeList.next())
00179 {
00180
00181 if (first == false) {
00182 sql <<",";
00183 } else {
00184 first = false;
00185 }
00186
00187
00188 if (attrDoc.isEmpty() == false)
00189 {
00190 sql << " -- " << attrDoc << m_endl;
00191 } else {
00192 sql << m_endl;
00193 }
00194
00195
00196 sql << m_indentation << cleanName(at->getName()) << " " << at->getTypeName() << " "
00197 << (at->getInitialValue().isEmpty()?QString(""):QString(" DEFAULT ")+at->getInitialValue());
00198
00199
00200 attrDoc = at->getDoc();
00201 }
00202
00203
00204 if (attrDoc.isEmpty() == false)
00205 {
00206 sql << " -- " << attrDoc << m_endl;
00207 } else {
00208 sql << m_endl;
00209 }
00210
00211 return;
00212 }
00213
00214 Uml::Programming_Language SQLWriter::getLanguage() {
00215 return Uml::pl_SQL;
00216 }
00217
00218 QStringList SQLWriter::defaultDatatypes() {
00219 QStringList l;
00220 l.append("varchar");
00221 l.append("tinyint");
00222 l.append("smallint");
00223 l.append("mediumint");
00224 l.append("bigint");
00225 l.append("float");
00226 l.append("double");
00227 l.append("decimal");
00228 l.append("date");
00229 l.append("datetime");
00230 l.append("time");
00231 l.append("timestamp");
00232 l.append("year");
00233 l.append("char");
00234 l.append("tinyblob");
00235 l.append("blob");
00236 l.append("mediumblob");
00237 l.append("longblob");
00238 l.append("tinytext");
00239 l.append("text");
00240 l.append("mediumtext");
00241 l.append("longtext");
00242 l.append("enum");
00243 l.append("set");
00244 return l;
00245 }
00246
00247 const QStringList SQLWriter::reservedKeywords() const {
00248
00249 static QStringList keywords;
00250
00251 if (keywords.isEmpty()) {
00252 keywords << "access"
00253 << "add"
00254 << "all"
00255 << "alter"
00256 << "analyze"
00257 << "and"
00258 << "any"
00259 << "as"
00260 << "asc"
00261 << "audit"
00262 << "begin"
00263 << "between"
00264 << "boolean"
00265 << "by"
00266 << "char"
00267 << "character"
00268 << "check"
00269 << "cluster"
00270 << "column"
00271 << "comment"
00272 << "commit"
00273 << "compress"
00274 << "connect"
00275 << "create"
00276 << "current"
00277 << "cursor"
00278 << "date"
00279 << "decimal"
00280 << "default"
00281 << "delete"
00282 << "desc"
00283 << "distinct"
00284 << "drop"
00285 << "else"
00286 << "elsif"
00287 << "end"
00288 << "escape"
00289 << "exception"
00290 << "exclusive"
00291 << "execute"
00292 << "exists"
00293 << "explain"
00294 << "false"
00295 << "file"
00296 << "float"
00297 << "for"
00298 << "from"
00299 << "function"
00300 << "grant"
00301 << "group"
00302 << "having"
00303 << "identified"
00304 << "if"
00305 << "immediate"
00306 << "in"
00307 << "increment"
00308 << "index"
00309 << "initial"
00310 << "insert"
00311 << "integer"
00312 << "intersect"
00313 << "into"
00314 << "is"
00315 << "level"
00316 << "like"
00317 << "lock"
00318 << "long"
00319 << "loop"
00320 << "maxextents"
00321 << "minus"
00322 << "mlslabel"
00323 << "mode"
00324 << "modify"
00325 << "noaudit"
00326 << "nocompress"
00327 << "not"
00328 << "nowait"
00329 << "null"
00330 << "number"
00331 << "of"
00332 << "offline"
00333 << "on"
00334 << "online"
00335 << "option"
00336 << "or"
00337 << "order"
00338 << "out"
00339 << "pctfree"
00340 << "prior"
00341 << "privileges"
00342 << "procedure"
00343 << "public"
00344 << "raw"
00345 << "rename"
00346 << "resource"
00347 << "return"
00348 << "revoke"
00349 << "rollback"
00350 << "row"
00351 << "rowid"
00352 << "rowlabel"
00353 << "rownum"
00354 << "rows"
00355 << "savepoint"
00356 << "select"
00357 << "session"
00358 << "set"
00359 << "share"
00360 << "size"
00361 << "smallint"
00362 << "some"
00363 << "start"
00364 << "successful"
00365 << "synonym"
00366 << "sysdate"
00367 << "table"
00368 << "then"
00369 << "to"
00370 << "trigger"
00371 << "true"
00372 << "truncate"
00373 << "type"
00374 << "uid"
00375 << "union"
00376 << "unique"
00377 << "update"
00378 << "user"
00379 << "using"
00380 << "validate"
00381 << "values"
00382 << "varchar"
00383 << "varchar2"
00384 << "varray"
00385 << "view"
00386 << "whenever"
00387 << "where"
00388 << "with";
00389 }
00390
00391 return keywords;
00392 }
00393
00394 #include "sqlwriter.moc"
This file is part of the documentation for umbrello Version 3.1.0.