[Uml-devel] KDE/kdesdk/umbrello/umbrello/codegenerators
Sharan Rao
sharanrao at gmail.com
Sun Jun 24 10:42:21 UTC 2007
SVN commit 679581 by sharan:
Autoincrement support in SQL Code generation
M +45 -2 mysqlwriter.cpp
M +6 -0 mysqlwriter.h
M +45 -0 postgresqlwriter.cpp
M +7 -0 postgresqlwriter.h
M +15 -3 sqlwriter.cpp
M +8 -1 sqlwriter.h
--- trunk/KDE/kdesdk/umbrello/umbrello/codegenerators/mysqlwriter.cpp #679580:679581
@@ -16,11 +16,13 @@
#include <klocale.h>
#include <qlist.h>
+#include "../entity.h"
+#include "../entityattribute.h"
#include "../foreignkeyconstraint.h"
-#include "../entityattribute.h"
+#include "../umlclassifierlistitemlist.h"
#include "../umlentityattributelist.h"
-#include "../umlclassifierlistitemlist.h"
+
MySQLWriter::MySQLWriter() {
}
@@ -97,4 +99,45 @@
SQLWriter::printForeignKeyConstraints( sql, constrList );
}
+void MySQLWriter::printAutoIncrements(QTextStream& sql, UMLEntityAttributeList entAttList) {
+
+ // rules
+ // only one attribute can have an auto increment in a table in MySQL
+ // and that attribute should have an index on it :/
+
+ // get the first attribute of list with auto increment
+ UMLEntityAttribute* ea, *autoIncrementEntAtt = NULL;
+ foreach( ea, entAttList ) {
+ if ( ea->getAutoIncrement() ) {
+ autoIncrementEntAtt = ea;
+ break;
+ }
+ }
+
+ if ( autoIncrementEntAtt == NULL ) {
+ return;
+ }
+
+ // create an index on this attribute
+ UMLEntityAttributeList indexList;
+ indexList.append( autoIncrementEntAtt );
+
+ printIndex( sql, m_pEntity, indexList );
+
+ // now alter the table and this column to add the auto increment
+ sql<<"ALTER TABLE "<<cleanName( m_pEntity->getName() )
+ <<" CHANGE "<<cleanName( autoIncrementEntAtt->getName() )
+ <<" "<<cleanName( autoIncrementEntAtt->getName() )
+ <<" "<<cleanName( autoIncrementEntAtt->getTypeName() )
+ <<" "<<cleanName( autoIncrementEntAtt->getAttributes() )
+ <<" "<<" NOT NULL AUTO_INCREMENT ; ";
+
+ sql<<m_endl;
+
+ // we don't support start values currently, but when we do, uncomment the following
+ //sql<<" ALTER TABLE "<<cleanName( m_pEntity->getName() )
+ // <<" AUTO_INCREMENT = "<<theValue;
+ //sql<<m_endl;
+}
+
#include "mysqlwriter.moc"
--- trunk/KDE/kdesdk/umbrello/umbrello/codegenerators/mysqlwriter.h #679580:679581
@@ -46,7 +46,13 @@
*/
void printForeignKeyConstraints(QTextStream& sql, UMLClassifierListItemList constrList);
+protected:
+ /**
+ * Reimplement printAutoIncrements from Base Class for MySQL
+ */
+ void printAutoIncrements(QTextStream& sql, UMLEntityAttributeList entAttList);
+
};
#endif // MySQLWRITER_H
--- trunk/KDE/kdesdk/umbrello/umbrello/codegenerators/postgresqlwriter.cpp #679580:679581
@@ -16,6 +16,9 @@
#include <klocale.h>
#include <qlist.h>
+#include "../entity.h"
+#include "../umlentityattributelist.h"
+
PostgreSQLWriter::PostgreSQLWriter() {
}
@@ -66,4 +69,46 @@
}
+void PostgreSQLWriter::printAutoIncrements(QTextStream& sql, UMLEntityAttributeList entAttList) {
+
+ // rules
+ // postgres has no such thing as auto increment
+ // instead it uses sequences. For simulating auto increment, set default value of
+ // each attribute to the nextval() of it's very own sequence
+
+ foreach( UMLEntityAttribute* ea, entAttList ) {
+ if ( !ea->getAutoIncrement() )
+ continue;
+
+ QString sequenceName;
+ // we keep the sequence name as entityName + '_' + entityAttributeName
+ sequenceName = m_pEntity->getName() + '_' + ea->getName();
+
+ // we assume the sequence count starts with 1 and interval is 1 too
+ // change the values when we start suporting different start values and
+ // interval values
+
+ sql<<"CREATE SEQUENCE "<<cleanName( sequenceName )
+ <<" START 1 INCREMENT 1 ;";
+
+ sql<<m_endl;
+
+ // alter the table column ( set not null )
+ sql<<"ALTER TABLE "<<cleanName( m_pEntity->getName() )
+ <<" ALTER COLUMN "<<cleanName( ea->getName() )
+ <<" SET NOT NULL ";
+
+ sql<<m_endl;
+
+ // alter the table column
+ sql<<"ALTER TABLE "<<cleanName( m_pEntity->getName() )
+ <<" ALTER COLUMN "<<cleanName( ea->getName() )
+ <<" SET DEFAULT nextval('"<<cleanName( sequenceName )
+ <<"');";
+
+ sql<<m_endl;
+ }
+
+}
+
#include "postgresqlwriter.moc"
--- trunk/KDE/kdesdk/umbrello/umbrello/codegenerators/postgresqlwriter.h #679580:679581
@@ -37,6 +37,13 @@
*/
QStringList defaultDatatypes();
+protected:
+
+ /**
+ * Reimplement printAutoIncrement statements from Base Class for PostgreSQL
+ */
+ void printAutoIncrements(QTextStream& sql, UMLEntityAttributeList entAttList);
+
};
#endif // PostgreSQLWRITER_H
--- trunk/KDE/kdesdk/umbrello/umbrello/codegenerators/sqlwriter.cpp #679580:679581
@@ -88,17 +88,25 @@
sql << "-- " << m_endl << m_endl;
}
- sql << "CREATE TABLE "<< entityname << " ( " << m_endl;
-
// write all entity attributes
UMLEntityAttributeList entAttList = m_pEntity->getEntityAttributes();
+ sql << "CREATE TABLE "<< entityname << " ( " << m_endl;
+
printEntityAttributes(sql, entAttList);
sql << m_endl << ");" << m_endl;
+ // auto increments
+ UMLEntityAttributeList autoIncrementList;
+ foreach( UMLEntityAttribute* entAtt, entAttList ) {
+ autoIncrementList.append(entAtt);
+ }
- // write all unqiue constraint ( including primary key )
+ printAutoIncrements( sql, autoIncrementList );
+
+
+ // write all unique constraint ( including primary key )
UMLClassifierListItemList constrList = m_pEntity->getFilteredList(Uml::ot_UniqueConstraint);
printUniqueConstraints(sql, constrList);
@@ -524,4 +532,8 @@
}
+void SQLWriter::printAutoIncrements(QTextStream& sql, UMLEntityAttributeList entAttList ) {
+ // defer to derived classes
+}
+
#include "sqlwriter.moc"
--- trunk/KDE/kdesdk/umbrello/umbrello/codegenerators/sqlwriter.h #679580:679581
@@ -90,10 +90,17 @@
* Prints out Indexes as "CREATE INDEX " statements
* @param sql The Stream we should print to
* @param ent The Entity's attributes on which we want to create an Index
- * @param entAttList The list of entityattributes to create and index upon
+ * @param entAttList The list of entityattributes to create an index upon
*/
virtual void printIndex(QTextStream& sql, UMLEntity* ent, UMLEntityAttributeList entAttList);
+ /**
+ * Handles AutoIncrements
+ * The derived classes provide the actual body
+ * @param sql The Stream we should print to
+ * @param entAttList The List of Entity Attributes that we want to auto increment
+ */
+ virtual void printAutoIncrements(QTextStream& sql, UMLEntityAttributeList entAttList );
};
#endif // SQLWRITER_H
More information about the umbrello-devel
mailing list