commit 3b5d8beb6f851d64a672f45f0a84f789fd8e5a31
Author: Christian Ehringfeld <c.ehringfeld@t-online.de>
Date:   Sat Aug 8 17:52:01 2015 +0200

    building column schema

diff --git a/src/schema.cpp b/src/schema.cpp
index af2ac16..de7abac 100644
--- a/src/schema.cpp
+++ b/src/schema.cpp
@@ -34,6 +34,99 @@ Schema::Schema(QSharedPointer<Database> database,
 Schema::~Schema() {
 }
 
+QString Schema::primaryKey(int length) const {
+    return this->buildColumnSchema(TYPE_PK, this->lengthToString(length));
+}
+
+QString Schema::bigPrimaryKey(int length) const {
+    return this->buildColumnSchema(TYPE_BIGPK, this->lengthToString(length));
+}
+
+QString Schema::string(int length, bool notNull, QString defaultValue,
+                       bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_STRING, this->lengthToString(length),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::text(bool notNull, QString defaultValue, bool unique,
+                     QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_TEXT, "", notNull, defaultValue, unique,
+                                   checkConstraint);
+}
+
+QString Schema::smallInteger(int length, bool notNull, QString defaultValue,
+                             bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_SMALLINT, this->lengthToString(length),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::integer(int length, bool notNull, QString defaultValue,
+                        bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_INTEGER, this->lengthToString(length),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::bigInteger(int length, bool notNull, QString defaultValue,
+                           bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_BIGINT, this->lengthToString(length),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::floatColumn(int precision, bool notNull, QString defaultValue,
+                            bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_FLOAT, this->lengthToString(precision),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::doubleColumn(int precision, bool notNull, QString defaultValue,
+                             bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_DOUBLE, this->lengthToString(precision),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::decimal(int precision, int scale, bool notNull,
+                        QString defaultValue, bool unique, QString checkConstraint) const {
+
+    return this->buildColumnSchema(TYPE_DECIMAL,
+                                   this->combineScaleAndPrecision(precision, scale), notNull, defaultValue,
+                                   unique, checkConstraint);
+}
+
+QString Schema::dateTime(int precision, bool notNull, QString defaultValue,
+                         bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_DATETIME, this->lengthToString(precision),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::timestamp(int precision, bool notNull, QString defaultValue,
+                          bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_TIMESTAMP, this->lengthToString(precision),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::time(int precision, bool notNull, QString defaultValue,
+                     bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_TIME, this->lengthToString(precision),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
+QString Schema::binary(int length, bool notNull, bool unique,
+                       QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_BINARY, this->lengthToString(length),
+                                   notNull, "", unique, checkConstraint);
+}
+
+QString Schema::boolean(QString defaultValue, bool notNull) const {
+    return this->buildColumnSchema(TYPE_DOUBLE, "", notNull, defaultValue);
+}
+
+QString Schema::money(int precision, int scale, bool notNull,
+                      QString defaultValue, bool unique, QString checkConstraint) const {
+    return this->buildColumnSchema(TYPE_MONEY,
+                                   this->combineScaleAndPrecision(precision, scale),
+                                   notNull, defaultValue, unique, checkConstraint);
+}
+
 void Schema::initAbstractDatabaseTypes() {
     this->abstractTypeMap->insert("bool", TYPE_SMALLINT);
     this->abstractTypeMap->insert("short", TYPE_SMALLINT);
@@ -198,13 +291,48 @@ QString Schema::buildUniqueString(bool unique) const {
 }
 
 QString Schema::buildDefaultString(QString def) const {
-
+    if (def.isEmpty()) {
+        return "";
+    }
+    QString defaultValue = " DEFAULT ";
+    bool ok;
+    def.toInt(&ok);
+    if (ok) {
+        return (defaultValue + def);
+    }
+    if (def == "true" || def == "false") {
+        return (defaultValue + def.toUpper());
+    }
+    QString copy = def.replace(",", ".");
+    copy.toDouble(&ok);
+    if (ok) {
+        return (defaultValue + copy);
+    }
+    return ("\'" + defaultValue + def + "\'");
 }
 
 QString Schema::buildCheckString(QString check) const {
     return check.isEmpty() ? "" : (" CHECK (" + check + ")");
 }
 
+QString Schema::lengthToString(int length) const {
+    return length != 0 ? QString::number(length) : "";
+}
+
+QString Schema::combineScaleAndPrecision(int precision, int scale) const {
+    QString length = "";
+    if (precision != 0) {
+        length += QString::number(precision);
+    }
+    if (scale != 0) {
+        if (precision != 0) {
+            length += ", ";
+        }
+        length += QString::number(scale);
+    }
+    return length;
+}
+
 
 QHash<QString, QSharedPointer<TableSchema> > Schema::getTables() const {
     return this->tables;
diff --git a/src/schema.h b/src/schema.h
index 2d117f2..db3e304 100644
--- a/src/schema.h
+++ b/src/schema.h
@@ -47,47 +47,47 @@ class Schema {
     const QString TYPE_BOOLEAN = "boolean";
     const QString TYPE_MONEY = "money";
 
-//    QString primaryKey(int length = 0) const;
-//    QString bigPrimaryKey(int length = 0) const;
-//    QString string(int length = 0, bool notNull = false,
-//                   QString defaultValue = "", bool unique = false,
-//                   QString checkConstraint = "") const;
-//    QString text(bool notNull = false, QString defaultValue = "",
-//                 bool unique = false, QString checkConstraint = "") const;
-//    QString smallInteger(int length = 0, bool notNull = false,
-//                         QString defaultValue = "", bool unique = false,
-//                         QString checkConstraint = "") const;
-//    QString integer(int length = 0, bool notNull = false,
-//                    QString defaultValue = "", bool unique = false,
-//                    QString checkConstraint = "") const;
-//    QString bigInteger(int length = 0, bool notNull = false,
-//                       QString defaultValue = "", bool unique = false,
-//                       QString checkConstraint = "") const;
-//    QString floatColumn(int precision = 0, bool notNull = false,
-//                        QString defaultValue = "", bool unique = false,
-//                        QString checkConstraint = "") const;
-//    QString doubleColumn(int precision = 0, bool notNull = false,
-//                         QString defaultValue = "", bool unique = false,
-//                         QString checkConstraint = "") const;
-//    QString decimal(int precision = 0, int scale = 0, bool notNull = false,
-//                    QString defaultValue = "", bool unique = false,
-//                    QString checkConstraint = "") const;
-//    QString dateTime(int precision = 0, bool notNull = false,
-//                     QString defaultValue = "", bool unique = false,
-//                     QString checkConstraint = "") const;
-//    QString timestamp(int precision = 0, bool notNull = false,
-//                      QString defaultValue = "", bool unique = false,
-//                      QString checkConstraint = "") const;
-//    QString time(int precision = 0, bool notNull = false,
-//                 QString defaultValue = "", bool unique = false,
-//                 QString checkConstraint = "") const;
-//    QString date( bool notNull = false, QString defaultValue = "") const;
-//    QString binary(int length = 0, bool notNull = false, bool unique = false,
-//                   QString checkConstraint = "") const;
-//    QString boolean(bool defaultValue = 0) const;
-//    QString money(int precision = 0, int scale = 0, bool notNull = false,
-//                  QString defaultValue = "", bool unique = false,
-//                  QString checkConstraint = "") const;
+    QString primaryKey(int length = 0) const;
+    QString bigPrimaryKey(int length = 0) const;
+    QString string(int length = 0, bool notNull = false,
+                   QString defaultValue = "", bool unique = false,
+                   QString checkConstraint = "") const;
+    QString text(bool notNull = false, QString defaultValue = "",
+                 bool unique = false, QString checkConstraint = "") const;
+    QString smallInteger(int length = 0, bool notNull = false,
+                         QString defaultValue = "", bool unique = false,
+                         QString checkConstraint = "") const;
+    QString integer(int length = 0, bool notNull = false,
+                    QString defaultValue = "", bool unique = false,
+                    QString checkConstraint = "") const;
+    QString bigInteger(int length = 0, bool notNull = false,
+                       QString defaultValue = "", bool unique = false,
+                       QString checkConstraint = "") const;
+    QString floatColumn(int precision = 0, bool notNull = false,
+                        QString defaultValue = "", bool unique = false,
+                        QString checkConstraint = "") const;
+    QString doubleColumn(int precision = 0, bool notNull = false,
+                         QString defaultValue = "", bool unique = false,
+                         QString checkConstraint = "") const;
+    QString decimal(int precision = 0, int scale = 0, bool notNull = false,
+                    QString defaultValue = "", bool unique = false,
+                    QString checkConstraint = "") const;
+    QString dateTime(int precision = 0, bool notNull = false,
+                     QString defaultValue = "", bool unique = false,
+                     QString checkConstraint = "") const;
+    QString timestamp(int precision = 0, bool notNull = false,
+                      QString defaultValue = "", bool unique = false,
+                      QString checkConstraint = "") const;
+    QString time(int precision = 0, bool notNull = false,
+                 QString defaultValue = "", bool unique = false,
+                 QString checkConstraint = "") const;
+    QString date( bool notNull = false, QString defaultValue = "") const;
+    QString binary(int length = 0, bool notNull = false, bool unique = false,
+                   QString checkConstraint = "") const;
+    QString boolean(QString defaultValue = "", bool notNull = false) const;
+    QString money(int precision = 0, int scale = 0, bool notNull = false,
+                  QString defaultValue = "", bool unique = false,
+                  QString checkConstraint = "") const;
 
 
 
@@ -119,14 +119,16 @@ class Schema {
     void setAbstractTypeMap(const QSharedPointer<QHash<QString, QString> > &value);
 
   protected:
-    virtual QString buildColumnSchema(QString type, QString length, bool notNull = false, QString defaultValue = "",
-                                               bool unique = false, QString checkConstraint = "") const;
+    virtual QString buildColumnSchema(QString type, QString length,
+                                      bool notNull = false, QString defaultValue = "",
+                                      bool unique = false, QString checkConstraint = "") const;
     virtual QString buildLengthString(QString length) const;
     virtual QString buildNotNullString(bool notNull) const;
     virtual QString buildUniqueString(bool unique) const;
     virtual QString buildDefaultString(QString def) const;
     virtual QString buildCheckString(QString check) const;
-
+    virtual QString lengthToString(int length) const;
+virtual QString combineScaleAndPrecision(int precision, int scale) const;
 
 
     virtual QStringList findTableNames(QString schema = "") = 0;
