From 1fdc150116cad39aae5c5da407c3312b47a59e3a Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Fri, 17 Dec 2021 07:06:30 -0500 Subject: New upstream version 3.3.3+dfsg1. --- Plugins/SqlExport/package.xml | 10 ------- Plugins/SqlExport/sqlexport.cpp | 62 ++++++++++++++++++++++++++++++----------- Plugins/SqlExport/sqlexport.h | 8 ++++-- 3 files changed, 51 insertions(+), 29 deletions(-) delete mode 100644 Plugins/SqlExport/package.xml (limited to 'Plugins/SqlExport') diff --git a/Plugins/SqlExport/package.xml b/Plugins/SqlExport/package.xml deleted file mode 100644 index f83fe22..0000000 --- a/Plugins/SqlExport/package.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - SQL export plugin - Plugin for exporting schema and data into SQL/DDL format. - %VERSION% - %DATE% - pl.com.salsoft.sqlitestudio.plugins.sqlexport - pl.com.salsoft.sqlitestudio.plugins - true - \ No newline at end of file diff --git a/Plugins/SqlExport/sqlexport.cpp b/Plugins/SqlExport/sqlexport.cpp index 0ae91ff..331bfd6 100644 --- a/Plugins/SqlExport/sqlexport.cpp +++ b/Plugins/SqlExport/sqlexport.cpp @@ -44,10 +44,9 @@ bool SqlExport::beforeExportQueryResults(const QString& query, QListgetDialect(); QStringList colDefs; for (QueryExecutor::ResultColumnPtr resCol : columns) - colDefs << wrapObjIfNeeded(resCol->displayName, dialect); + colDefs << wrapObjIfNeeded(resCol->displayName); this->columns = colDefs.join(", "); @@ -61,10 +60,10 @@ bool SqlExport::beforeExportQueryResults(const QString& query, QListcolumns + ");"; writeln(""); @@ -88,6 +87,14 @@ bool SqlExport::exportTable(const QString& database, const QString& table, const { UNUSED(createTable); UNUSED(providedData); + + tableGeneratedColumns.clear(); + for (SqliteCreateTable::Column* col : createTable->columns) + { + if (col->hasConstraint(SqliteCreateTable::Column::Constraint::GENERATED)) + tableGeneratedColumns << col->name; + } + return exportTable(database, table, columnNames, ddl); } @@ -102,11 +109,20 @@ bool SqlExport::exportTable(const QString& database, const QString& table, const { static_qstring(dropDdl, "DROP TABLE IF EXISTS %1;"); - Dialect dialect = db->getDialect(); - + generatedColumnIndexes.clear(); QStringList colList; + int colIdx = 0; for (const QString& colName : columnNames) - colList << wrapObjIfNeeded(colName, dialect); + { + if (tableGeneratedColumns.contains(colName, Qt::CaseInsensitive)) + { + generatedColumnIndexes << colIdx++; + continue; + } + + colList << wrapObjIfNeeded(colName); + colIdx++; + } columns = colList.join(", "); @@ -121,7 +137,7 @@ bool SqlExport::exportTable(const QString& database, const QString& table, const writeln(""); writeln(tr("-- Table: %1").arg(fullName)); - theTable = getNameForObject(database, table, true, dialect); + theTable = getNameForObject(database, table, true); if (cfg.SqlExport.GenerateDrop.get()) writeln(formatQuery(dropDdl.arg(theTable))); @@ -132,7 +148,7 @@ bool SqlExport::exportTable(const QString& database, const QString& table, const bool SqlExport::exportTableRow(SqlResultsRowPtr data) { - QStringList argList = rowToArgList(data); + QStringList argList = rowToArgList(data, true); QString argStr = argList.join(", "); QString sql = "INSERT INTO " + theTable + " (" + columns + ") VALUES (" + argStr + ");"; if (!cfg.SqlExport.FormatDdlsOnly.get()) @@ -167,7 +183,7 @@ bool SqlExport::exportIndex(const QString& database, const QString& name, const writeln(""); writeln(tr("-- Index: %1").arg(index)); - QString fullName = getNameForObject(database, name, true, db->getDialect()); + QString fullName = getNameForObject(database, name, true); if (cfg.SqlExport.GenerateDrop.get()) writeln(formatQuery(dropDdl.arg(fullName))); @@ -184,7 +200,7 @@ bool SqlExport::exportTrigger(const QString& database, const QString& name, cons writeln(""); writeln(tr("-- Trigger: %1").arg(trig)); - QString fullName = getNameForObject(database, name, true, db->getDialect()); + QString fullName = getNameForObject(database, name, true); if (cfg.SqlExport.GenerateDrop.get()) writeln(dropDdl.arg(fullName)); @@ -201,7 +217,7 @@ bool SqlExport::exportView(const QString& database, const QString& name, const Q writeln(""); writeln(tr("-- View: %1").arg(view)); - QString fullName = getNameForObject(database, name, true, db->getDialect()); + QString fullName = getNameForObject(database, name, true); if (cfg.SqlExport.GenerateDrop.get()) writeln(dropDdl.arg(fullName)); @@ -254,18 +270,32 @@ QString SqlExport::formatQuery(const QString& sql) return sql.trimmed() + ";"; } -QString SqlExport::getNameForObject(const QString& database, const QString& name, bool wrapped, Dialect dialect) +QString SqlExport::getNameForObject(const QString& database, const QString& name, bool wrapped) { - QString obj = wrapped ? wrapObjIfNeeded(name, dialect) : name; + QString obj = wrapped ? wrapObjIfNeeded(name) : name; if (!database.isNull() && database.toLower() != "main") - obj = (wrapped ? wrapObjIfNeeded(database, dialect) : database) + "." + obj; + obj = (wrapped ? wrapObjIfNeeded(database) : database) + "." + obj; return obj; } -QStringList SqlExport::rowToArgList(SqlResultsRowPtr row) +QStringList SqlExport::rowToArgList(SqlResultsRowPtr row, bool honorGeneratedColumns) { - return valueListToSqlList(row->valueList(), db->getDialect()); + if (honorGeneratedColumns) + { + QList filteredValues; + int i = 0; + for (const QVariant& value : row->valueList()) + { + if (generatedColumnIndexes.contains(i++)) + continue; + + filteredValues << value; + } + return valueListToSqlList(filteredValues); + } + + return valueListToSqlList(row->valueList()); } void SqlExport::validateOptions() diff --git a/Plugins/SqlExport/sqlexport.h b/Plugins/SqlExport/sqlexport.h index 65a4c82..49e2669 100644 --- a/Plugins/SqlExport/sqlexport.h +++ b/Plugins/SqlExport/sqlexport.h @@ -7,7 +7,7 @@ CFG_CATEGORIES(SqlExportConfig, CFG_CATEGORY(SqlExport, - CFG_ENTRY(QString, QueryTable, QString::null) + CFG_ENTRY(QString, QueryTable, QString()) CFG_ENTRY(bool, GenerateCreateTable, false) CFG_ENTRY(bool, IncludeQueryInComments, true) CFG_ENTRY(bool, UseFormatter, false) @@ -55,11 +55,13 @@ class SQLEXPORTSHARED_EXPORT SqlExport : public GenericExportPlugin void writeFkDisable(); void writeFkEnable(); QString formatQuery(const QString& sql); - QString getNameForObject(const QString& database, const QString& name, bool wrapped, Dialect dialect = Dialect::Sqlite3); - QStringList rowToArgList(SqlResultsRowPtr row); + QString getNameForObject(const QString& database, const QString& name, bool wrapped); + QStringList rowToArgList(SqlResultsRowPtr row, bool honorGeneratedColumns = false); QString theTable; QString columns; + QStringList tableGeneratedColumns; + QList generatedColumnIndexes; CFG_LOCAL_PERSISTABLE(SqlExportConfig, cfg) }; -- cgit v1.2.3