From 0d601c03f1ed96637328215e5a138986594d6b25 Mon Sep 17 00:00:00 2001 From: Peinthor Rene Date: Sun, 24 Mar 2013 21:20:49 +0100 Subject: [PATCH] add emptyinsertstm function to table object creates an empty insert statement for the table fields --- src/sqlitetypes.cpp | 23 +++++++++++++++++++++++ src/sqlitetypes.h | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/src/sqlitetypes.cpp b/src/sqlitetypes.cpp index 78cfdf8a..f465b68a 100644 --- a/src/sqlitetypes.cpp +++ b/src/sqlitetypes.cpp @@ -165,6 +165,29 @@ Table Table::parseSQL(const QString &sSQL) return Table(""); } +QString Table::emptyInsertStmt() const +{ + QString stmt = QString("INSERT INTO `%1` VALUES(").arg(m_name); + + QStringList vals; + foreach(FieldPtr f, m_fields) { + if(f->notnull()) + { + if(f->isInteger()) + vals << "0"; + else + vals << "''"; + } + else + vals << "NULL"; + } + + stmt.append(vals.join(",")); + stmt.append(");"); + + return stmt; +} + QString Table::sql() const { diff --git a/src/sqlitetypes.h b/src/sqlitetypes.h index 91ea976a..61c31db1 100644 --- a/src/sqlitetypes.h +++ b/src/sqlitetypes.h @@ -68,12 +68,19 @@ public: const QString& name() const { return m_name; } const FieldVector& fields() const { return m_fields; } const FieldVector& primarykey() const { return m_primarykey; } + + /** + * @brief Creates an empty insert statement. + * @return An sqlite conform INSERT INTO statement with empty values. (NULL,'',0) + */ + QString emptyInsertStmt() const; QString sql() const; void addField(const FieldPtr& f); bool removeField(const QString& sFieldName); void setFields(const FieldVector& fields); void clear(); + /** * @brief findField Finds a field and returns the index. * @param sname