mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-02-10 21:58:33 -06:00
Allow any number of changes to the table schema in one alterTable call
In the Edit Table dialog we used to call our alterTable function (which works around SQLite's missing full ALTER TABLE support by - besided other things - copying all the data of the table) for pretty much every change immediately. This was taking a lot of time for larger tables. Our alterTable function allowed any number of changes if they affect only one field of the table at once. So we could have reduced the number of calls a lot by just using that capability. Instead however, this commit improves the alterTable function to make possible transforming a table completely in just one call. It does so by taking the new table schema and using that without further modification. It also takes a new parameter to keep track of what column in the old table becomes what column in the new table, so the data can be preserved. This commit obviously also changes the Edit Table dialog to make proper use of the new features. This means that whatever changes you make to a table, you will only have to wait once until for the alterTable call, and that's when clicking the OK button. See issue #1444.
This commit is contained in:
@@ -3,13 +3,13 @@
|
||||
|
||||
#include "sql/sqlitetypes.h"
|
||||
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
#include <QStringList>
|
||||
#include <QMultiMap>
|
||||
#include <QByteArray>
|
||||
#include <QMultiMap>
|
||||
#include <QStringList>
|
||||
|
||||
struct sqlite3;
|
||||
class CipherSettings;
|
||||
@@ -155,17 +155,25 @@ public:
|
||||
bool addColumn(const sqlb::ObjectIdentifier& tablename, const sqlb::Field& field);
|
||||
|
||||
/**
|
||||
* @brief alterTable Can be used to rename, modify or drop an existing column of a given table
|
||||
* @param schema Specifies the name of the schema, i.e. the database name, of the table
|
||||
* @param tablename Specifies the name of the table to edit
|
||||
* @param table Specifies the table to edit. The table constraints are used from this but not the columns
|
||||
* @param name Name of the column to edit
|
||||
* @param to The new field definition with changed name, type or the like. If Null-Pointer is given the column is dropped.
|
||||
* @param move Set this to a value != 0 to move the new column to a different position
|
||||
* @brief This type maps from old column names to new column names. Given the old and the new table definition, this suffices to
|
||||
* track fields between the two.
|
||||
* USE CASES:
|
||||
* 1) Don't specify a column at all or specify equal column names: Keep its name as-is.
|
||||
* 2) Specify different column names: Rename the field.
|
||||
* 3) Map from an existing column name to a Null string: Delete the column.
|
||||
* 4) Map from a Null column name to a new column name: Add the column.
|
||||
*/
|
||||
using AlterTableTrackColumns = QMap<QString, QString>;
|
||||
|
||||
/**
|
||||
* @brief alterTable Can be used to rename, modify or drop existing columns of a given table
|
||||
* @param tablename Specifies the schema and name of the table to edit
|
||||
* @param new_table Specifies the new table schema. This is exactly how the new table is going to look like.
|
||||
* @param track_columns Maps old column names to new column names. This is used to copy the data from the old table to the new one.
|
||||
* @param newSchema Set this to a non-empty string to move the table to a new schema
|
||||
* @return true if renaming was successful, false if not. In the latter case also lastErrorMessage is set
|
||||
*/
|
||||
bool alterTable(const sqlb::ObjectIdentifier& tablename, const sqlb::Table& table, QString name, const sqlb::Field* to, int move = 0, QString newSchemaName = QString());
|
||||
bool alterTable(const sqlb::ObjectIdentifier& tablename, const sqlb::Table& new_table, AlterTableTrackColumns track_columns, QString newSchemaName = QString());
|
||||
|
||||
objectMap getBrowsableObjects(const QString& schema) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user