Draft of support for the unique constraint (#95)

This commit is contained in:
Jack Lilhammers 2020-07-07 16:50:17 +02:00 committed by GitHub
parent 0d638a6fae
commit 6ce50e2250
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 3 deletions

View File

@ -147,7 +147,9 @@ QString SqlGeneratorBase::fieldDeclare(FieldModel *field)
QString type = fieldType(field); QString type = fieldType(field);
if (type.isEmpty()) if (type.isEmpty())
return type; return type;
return field->name + " " + type + (field->notNull ? " NOT NULL" : ""); return field->name + " " + type
+ (field->notNull ? " NOT NULL" : "")
+ (field->isUnique ? " UNIQUE" : "");
} }
QStringList SqlGeneratorBase::constraints(TableModel *table) QStringList SqlGeneratorBase::constraints(TableModel *table)

View File

@ -104,6 +104,9 @@ QString SqliteGenerator::fieldDeclare(FieldModel *field)
if (field->notNull) if (field->notNull)
type.append(" NOT NULL"); type.append(" NOT NULL");
if (field->isUnique)
type.append(" UNIQUE");
return field->name + " " + type; return field->name + " " + type;
} }

View File

@ -73,7 +73,7 @@ FieldModel *TableModel::field(const QString &name) const
foreach (FieldModel *f, _fields) foreach (FieldModel *f, _fields)
if(f->name == name) if(f->name == name)
return f; return f;
return nullptr; return nullptr;
} }
@ -251,6 +251,9 @@ TableModel::TableModel(const QJsonObject &json, const QString &tableName) : _typ
if(fieldObject.contains(__nut_NOT_NULL)) if(fieldObject.contains(__nut_NOT_NULL))
f->notNull = fieldObject.value(__nut_NOT_NULL).toBool(); f->notNull = fieldObject.value(__nut_NOT_NULL).toBool();
if(fieldObject.contains(__nut_UNIQUE))
f->isUnique = fieldObject.value(__nut_UNIQUE).toBool();
if(fieldObject.contains(__nut_LEN)) if(fieldObject.contains(__nut_LEN))
f->length = fieldObject.value(__nut_LEN).toInt(); f->length = fieldObject.value(__nut_LEN).toInt();
@ -288,6 +291,9 @@ QJsonObject TableModel::toJson() const
if(f->notNull) if(f->notNull)
fieldObj.insert(__nut_NOT_NULL, f->notNull); fieldObj.insert(__nut_NOT_NULL, f->notNull);
if(f->isUnique)
fieldObj.insert(__nut_UNIQUE, f->isUnique);
if(!f->defaultValue.isNull()) if(!f->defaultValue.isNull())
fieldObj.insert(__nut_DEFAULT_VALUE, f->defaultValue); fieldObj.insert(__nut_DEFAULT_VALUE, f->defaultValue);
@ -359,6 +365,7 @@ FieldModel::FieldModel(const QJsonObject &json)
type = static_cast<QMetaType::Type>(json.value(__TYPE).toInt()); type = static_cast<QMetaType::Type>(json.value(__TYPE).toInt());
length = json.value(__nut_LEN).toInt(); length = json.value(__nut_LEN).toInt();
notNull = json.value(__nut_NOT_NULL).toBool(); notNull = json.value(__nut_NOT_NULL).toBool();
isUnique = json.value(__nut_UNIQUE).toBool();
isAutoIncrement = json.value(__nut_AUTO_INCREMENT).toBool(); isAutoIncrement = json.value(__nut_AUTO_INCREMENT).toBool();
isPrimaryKey = json.value(__nut_PRIMARY_KEY).toBool(); isPrimaryKey = json.value(__nut_PRIMARY_KEY).toBool();
defaultValue = json.value(__nut_DEFAULT_VALUE).toString(); defaultValue = json.value(__nut_DEFAULT_VALUE).toString();
@ -372,6 +379,7 @@ QJsonObject FieldModel::toJson() const
fieldObj.insert(__TYPE, QString(QVariant::typeToName(type))); fieldObj.insert(__TYPE, QString(QVariant::typeToName(type)));
fieldObj.insert(__nut_LEN, length); fieldObj.insert(__nut_LEN, length);
fieldObj.insert(__nut_NOT_NULL, notNull); fieldObj.insert(__nut_NOT_NULL, notNull);
fieldObj.insert(__nut_UNIQUE, isUnique);
fieldObj.insert(__nut_AUTO_INCREMENT, isAutoIncrement); fieldObj.insert(__nut_AUTO_INCREMENT, isAutoIncrement);
fieldObj.insert(__nut_PRIMARY_KEY, isPrimaryKey); fieldObj.insert(__nut_PRIMARY_KEY, isPrimaryKey);
fieldObj.insert(__nut_DEFAULT_VALUE, defaultValue); fieldObj.insert(__nut_DEFAULT_VALUE, defaultValue);

View File

@ -55,7 +55,8 @@ struct FieldModel{
&& type == f.type && type == f.type
&& length == f.length && length == f.length
&& defaultValue == f.defaultValue && defaultValue == f.defaultValue
&& notNull == f.notNull; && notNull == f.notNull
&& isUnique == f.isUnique;
return b; return b;
} }