Code refactoring

This commit refactors vast parts of the sqlitetypes.h interface. Its
main goals are: less code, easier code, a more modern interface, reduced
likelihood for strange errors and more flexibility for future
extensions.

The main reason why the sqlitetypes.h functions were working so well in
DB4S was not because they were that stable but because they were
extremely interlinked with the rest of the code. This is fine because we
do not plan to ship them as a separate library. But it makes it hard to
find the obvious spot to fix an issue or to put a new function. It can
always be done in the sqlitetypes function or in the rest of the DB4S
code because it is just not clear what the interface between the two
should look like. This is supposed to be improved by this commit. One
main thing here is to make ownership of objects a bit clearer.

In theory the new code should be faster too but that difference will be
neglectable from a user POV.

This commit also fixes a hidden bug which caused all table constraints
to be removed in the Edit Table dialog when a single field was removed
from the table.

This is all still WIP and more work is needed to be done here.
This commit is contained in:
Martin Kleusberg
2018-09-04 19:46:39 +02:00
parent f3e6aec57d
commit bf505edf66
16 changed files with 685 additions and 792 deletions

View File

@@ -118,7 +118,7 @@ private:
* @param field Field to get the max value
* @return the max value of the field or 0 on error
*/
QString max(const sqlb::ObjectIdentifier& tableName, sqlb::FieldPtr field) const;
QString max(const sqlb::ObjectIdentifier& tableName, const sqlb::Field& field) const;
public:
void updateSchema();
@@ -137,9 +137,9 @@ public:
bool deleteRecords(const sqlb::ObjectIdentifier& table, const QStringList& rowids, const QString& pseudo_pk = QString());
bool updateRecord(const sqlb::ObjectIdentifier& table, const QString& column, const QString& rowid, const QByteArray& value, bool itsBlob, const QString& pseudo_pk = QString());
bool createTable(const sqlb::ObjectIdentifier& name, const sqlb::FieldVector& structure);
bool createTable(const sqlb::ObjectIdentifier& name, const sqlb::FieldPtrVector& structure);
bool renameTable(const QString& schema, const QString& from_table, const QString& to_table);
bool addColumn(const sqlb::ObjectIdentifier& tablename, const sqlb::FieldPtr& field);
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
@@ -155,7 +155,18 @@ public:
bool alterTable(const sqlb::ObjectIdentifier& tablename, const sqlb::Table& table, const QString& name, sqlb::FieldPtr to, int move = 0, QString newSchemaName = QString());
objectMap getBrowsableObjects(const QString& schema) const;
const sqlb::ObjectPtr getObjectByName(const sqlb::ObjectIdentifier& name) const;
template<typename T = sqlb::Object>
const std::shared_ptr<T> getObjectByName(const sqlb::ObjectIdentifier& name) const
{
for(auto& it : schemata[name.schema()])
{
if(it->name() == name.name())
return std::dynamic_pointer_cast<T>(it);
}
return std::shared_ptr<T>();
}
bool isOpen() const;
bool encrypted() const { return isEncrypted; }
bool readOnly() const { return isReadOnly; }
@@ -173,6 +184,8 @@ public:
bool loadExtension(const QString& filename);
void loadExtensionsFromSettings();
static QStringList Datatypes;
private:
QVector<QPair<QString, QString>> queryColumnInformation(const QString& schema_name, const QString& object_name);