From c29702a1b97e6fcc30651973591b7b565746a90d Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Wed, 15 Oct 2014 12:45:43 +0200 Subject: [PATCH] EditTableDialog: Fix handling of default values Fix the handling of default values in the Edit Table dialog by doing two things: First, change the sqlb::Field class so that it doesn't put quotes around any default values no matter what. This breaks for example NULL default values but also causes invalid syntax if the default value is already in quotes. Secondly, change the Edit Table dialog itself to double check if the user should've put quotes around their default value but hasn't. In this case add them automatically. See issues #64 and #126. --- src/EditTableDialog.cpp | 24 +++++++++++++++++++++++- src/sqlitetypes.cpp | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/EditTableDialog.cpp b/src/EditTableDialog.cpp index 98e2c652..5118a2f7 100644 --- a/src/EditTableDialog.cpp +++ b/src/EditTableDialog.cpp @@ -334,10 +334,32 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column) } break; case kDefault: + { + QString new_value = item->text(column); + // If the default value isn't a SQL keyword perform an extra check: If it isn't numeric but doesn't start and end with quotes, + // add the quotes + if(new_value.compare("null", Qt::CaseInsensitive) && + new_value.compare("current_time", Qt::CaseInsensitive) && + new_value.compare("current_date", Qt::CaseInsensitive) && + new_value.compare("current_timestamp", Qt::CaseInsensitive)) + { + if(!((new_value.trimmed().startsWith('\'') || + new_value.trimmed().startsWith('"')) && (new_value.trimmed().endsWith('\'') || new_value.trimmed().endsWith('"')))) + { + bool is_numeric; + new_value.toDouble(&is_numeric); + if(!is_numeric) + { + new_value = QString("'%1'").arg(new_value.replace("'", "''")); + item->setText(column, new_value); + } + } + } field->setDefaultValue(item->text(column)); if(!m_bNewTable) callRenameColumn = true; - break; + } + break; case kCheck: field->setCheck(item->text(column)); if(!m_bNewTable) diff --git a/src/sqlitetypes.cpp b/src/sqlitetypes.cpp index 3bccce9c..67329672 100644 --- a/src/sqlitetypes.cpp +++ b/src/sqlitetypes.cpp @@ -15,7 +15,7 @@ QString Field::toString(const QString& indent, const QString& sep) const if(m_notnull) str += " NOT NULL"; if(!m_defaultvalue.isEmpty()) - str += QString(" DEFAULT '%1'").arg(m_defaultvalue); + str += QString(" DEFAULT %1").arg(m_defaultvalue); if(!m_check.isEmpty()) str += " CHECK(" + m_check + ")"; if(m_autoincrement)