Skip to content

Commit

Permalink
column default def
Browse files Browse the repository at this point in the history
  • Loading branch information
mugiseyebrows committed Nov 19, 2023
1 parent fdc5319 commit 3946cd6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/schema2/schema2tablecolumn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Schema2TableColumn::Schema2TableColumn() : notNull(false), autoincrement(false) {}

Schema2TableColumn::Schema2TableColumn(const QString &name, const QString &type, bool notNull, bool autoincrement) :
name(name), type(type), notNull(notNull), autoincrement(autoincrement) {
Schema2TableColumn::Schema2TableColumn(const QString &name, const QString &type, bool notNull, const QString &default_, bool autoincrement) :
name(name), type(type), notNull(notNull), default_(default_), autoincrement(autoincrement) {

}
4 changes: 3 additions & 1 deletion src/schema2/schema2tablecolumn.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
class Schema2TableColumn {
public:
Schema2TableColumn();
Schema2TableColumn(const QString& name, const QString& type, bool notNull, bool autoincrement);
Schema2TableColumn(const QString& name, const QString& type,
bool notNull, const QString& default_, bool autoincrement);
QString name;
QString type;
bool notNull;
QString default_;
bool autoincrement;
};

Expand Down
60 changes: 54 additions & 6 deletions src/schema2/schema2tablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Schema2TableModel::Schema2TableModel(const QString &name, Status status, QObject
}

void Schema2TableModel::insertColumnsIfNotContains(const QString &name, const QString &type, bool notNull,
const QString& default_,
bool autoIncrement, const QString& prev)
{
int insertRow = rowCount();
Expand All @@ -39,8 +40,8 @@ void Schema2TableModel::insertColumnsIfNotContains(const QString &name, const QS

beginInsertRows(QModelIndex(), insertRow, insertRow);
//mColumns.insert(insertRow, {name, name, type, type});
mColumns.insert(insertRow, Schema2TableColumn(name, type, notNull, autoIncrement));
mColumnsPrev.insert(insertRow, Schema2TableColumn(name, type, notNull, autoIncrement));
mColumns.insert(insertRow, Schema2TableColumn(name, type, notNull, default_, autoIncrement));
mColumnsPrev.insert(insertRow, Schema2TableColumn(name, type, notNull, default_, autoIncrement));
endInsertRows();
}

Expand Down Expand Up @@ -98,6 +99,14 @@ bool Schema2TableModel::autoincrementPrev(int row) const {
return mColumnsPrev[row].autoincrement;
}

QString Schema2TableModel::default_(int row) const {
return mColumns[row].default_;
}

QString Schema2TableModel::defaultPrev(int row) const {
return mColumnsPrev[row].default_;
}

bool Schema2TableModel::hasPendingChanges() const
{
return false;
Expand Down Expand Up @@ -227,7 +236,11 @@ Status Schema2TableModel::status() const {
if (name(row).isEmpty()) {
continue;
}
if (namePrev(row) != name(row) || typePrev(row) != type(row) || autoincrement(row) != autoincrementPrev(row)) {
if (namePrev(row) != name(row)
|| typePrev(row) != type(row)
|| notNullPrev(row) != notNull(row)
|| defaultPrev(row) != default_(row)
|| autoincrementPrev(row) != autoincrement(row)) {
return StatusModified;
}
}
Expand Down Expand Up @@ -277,11 +290,14 @@ QStringList Schema2TableModel::alterQueries(const QString &driverName, QSqlDrive
bool notNullPrev = this->notNullPrev(row);
bool autoincrement = this->autoincrement(row);
bool autoincrementPrev = this->autoincrementPrev(row);
QString default_ = this->default_(row);
QString defaultPrev = this->defaultPrev(row);

bool nameChanged = name != namePrev;
bool typeChanged = type != typePrev;
bool notNullChanged = notNull != notNullPrev;
bool autoincrementChanged = autoincrement != autoincrementPrev;
bool defaultChanged = default_ != defaultPrev;

if (name.isEmpty()) {

Expand All @@ -295,7 +311,7 @@ QStringList Schema2TableModel::alterQueries(const QString &driverName, QSqlDrive

res.append(expr);

} else if (nameChanged || typeChanged || notNullChanged || autoincrementChanged) {
} else if (nameChanged || typeChanged || notNullChanged || autoincrementChanged || defaultChanged) {

if (driverName == DRIVER_ODBC) {

Expand Down Expand Up @@ -354,6 +370,10 @@ QStringList Schema2TableModel::autoincrementQueries(const QString &driverName, Q
return res;
}

static bool is_text_type(const QString& driverName, const QString& type) {
return type.toLower().startsWith("varchar") || type.toLower() == "text";
}

QString Schema2TableModel::columnDefinition(const QString &driverName, QSqlDriver *driver, int row, bool skipAutoIncrement) const {

//Q_ASSERT(driverName == DRIVER_MYSQL || driverName == DRIVER_MARIADB);
Expand All @@ -363,10 +383,33 @@ QString Schema2TableModel::columnDefinition(const QString &driverName, QSqlDrive
QString name = this->name(row);
QString type = this->type(row);
bool notNull = this->notNull(row);
QString default_ = this->default_(row);
bool autoincrement = this->autoincrement(row);

QString default_def;
if (!default_.isEmpty()) {
if (driverName == DRIVER_MYSQL || driverName == DRIVER_MARIADB) {
QString value = default_;
if (default_.startsWith("'")) {

} else if (default_.startsWith("(")) {

} else {
if (is_text_type(driverName, type)) {
value = "'" + value + "'";
} else {
value = "(" + value + ")";
}
}
default_def = QString("DEFAULT %1").arg(value);
} else {
default_def = QString("DEFAULT %1").arg(default_);
}
}

QString res = filterEmpty({es.field(name), type,
notNull ? "NOT NULL" : "",
default_def,
(autoincrement && !skipAutoIncrement) ? "AUTO_INCREMENT" : ""}).join(" ");
return res;
}
Expand Down Expand Up @@ -455,9 +498,10 @@ QVariant Schema2TableModel::data(const QModelIndex &index, int role) const
switch(index.column()) {
case col_name: return mColumns[row].name;
case col_type: return mColumns[row].type;

case col_default: return mColumns[row].default_;
case col_name_prev: return mColumnsPrev[row].name;
case col_type_prev: return mColumnsPrev[row].type;
case col_default_prev: return mColumnsPrev[row].default_;
}
}
if (role == Qt::CheckStateRole) {
Expand All @@ -476,7 +520,7 @@ QVariant Schema2TableModel::data(const QModelIndex &index, int role) const
Qt::ItemFlags Schema2TableModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (index.column() == col_name || index.column() == col_type) {
if (index.column() == col_name || index.column() == col_type || index.column() == col_default) {
flags |= Qt::ItemIsEditable;
}
if (index.column() == col_notnull || index.column() == col_autoincrement) {
Expand All @@ -497,8 +541,10 @@ bool Schema2TableModel::setData(const QModelIndex &index, const QVariant &value,
switch(index.column()) {
case col_name: mColumns[row].name = value.toString(); break;
case col_type: mColumns[row].type = value.toString(); break;
case col_default: mColumns[row].default_ = value.toString(); break;
case col_name_prev: mColumnsPrev[row].name = value.toString(); break;
case col_type_prev: mColumnsPrev[row].type = value.toString(); break;
case col_default_prev: mColumnsPrev[row].default_ = value.toString(); break;
}

emit dataChanged(index, index);
Expand All @@ -523,11 +569,13 @@ QVariant Schema2TableModel::headerData(int section, Qt::Orientation orientation,
{col_notnull, "Non null"},
{col_type, "Type"},
{col_autoincrement, "Autoinc"},
{col_default, "Default"},

{col_name_prev, "Name"},
{col_notnull_prev, "Non null"},
{col_type_prev, "Type"},
{col_autoincrement_prev, "Autoinc"},
{col_default_prev, "Default"},
};

if (role == Qt::DisplayRole) {
Expand Down
9 changes: 8 additions & 1 deletion src/schema2/schema2tablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ class Schema2TableModel : public QAbstractTableModel
col_name,
col_type,
col_notnull,
col_default,
col_autoincrement,

col_name_prev,
col_type_prev,
col_notnull_prev,
col_default_prev,
col_autoincrement_prev,
cols_count
};

explicit Schema2TableModel(const QString& name, Status status, QObject *parent = nullptr);

void insertColumnsIfNotContains(const QString& name, const QString& type, bool notNull, bool autoIncrement, const QString &prev);
void insertColumnsIfNotContains(const QString& name, const QString& type, bool notNull, const QString &default_, bool autoIncrement, const QString &prev);

bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());

Expand All @@ -54,6 +56,10 @@ class Schema2TableModel : public QAbstractTableModel

bool autoincrementPrev(int row) const;

QString defaultPrev(int row) const;

QString default_(int row) const;

bool hasPendingChanges() const;

QStringList columnNames() const;
Expand Down Expand Up @@ -166,6 +172,7 @@ class Schema2TableModel : public QAbstractTableModel
void updateParentRelations(Schema2TablesModel *tables);



};

#endif // SCHEMA2TABLEMODEL_H

0 comments on commit 3946cd6

Please sign in to comment.