aboutsummaryrefslogtreecommitdiffstats
path: root/Plugins/SqlExport
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2021-12-17 07:06:30 -0500
committerLibravatarUnit 193 <unit193@unit193.net>2021-12-17 07:06:30 -0500
commit1fdc150116cad39aae5c5da407c3312b47a59e3a (patch)
tree123c79a4d7ad2d45781ba03ce939f7539fb428d8 /Plugins/SqlExport
parentfeda8a7db8d1d7c5439aa8f8feef7cc0dd2b59a0 (diff)
New upstream version 3.3.3+dfsg1.upstream/3.3.3+dfsg1
Diffstat (limited to 'Plugins/SqlExport')
-rw-r--r--Plugins/SqlExport/package.xml10
-rw-r--r--Plugins/SqlExport/sqlexport.cpp62
-rw-r--r--Plugins/SqlExport/sqlexport.h8
3 files changed, 51 insertions, 29 deletions
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 @@
-<?xml version="1.0"?>
-<Package>
- <DisplayName>SQL export plugin</DisplayName>
- <Description>Plugin for exporting schema and data into SQL/DDL format.</Description>
- <Version>%VERSION%</Version>
- <ReleaseDate>%DATE%</ReleaseDate>
- <Name>pl.com.salsoft.sqlitestudio.plugins.sqlexport</Name>
- <Dependencies>pl.com.salsoft.sqlitestudio.plugins</Dependencies>
- <Default>true</Default>
-</Package> \ 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, QList<QueryExecut
UNUSED(providedData);
static_qstring(dropDdl, "DROP TABLE IF EXISTS %1;");
- Dialect dialect = db->getDialect();
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, QList<QueryExecut
writeBegin();
+ theTable = wrapObjIfNeeded(cfg.SqlExport.QueryTable.get());
if (!cfg.SqlExport.GenerateCreateTable.get())
return true;
- theTable = wrapObjIfNeeded(cfg.SqlExport.QueryTable.get(), dialect);
QString ddl = "CREATE TABLE " + theTable + " (" + this->columns + ");";
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<QVariant> 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<int> generatedColumnIndexes;
CFG_LOCAL_PERSISTABLE(SqlExportConfig, cfg)
};