Skip to content

Commit

Permalink
Merge pull request #155 from keremet/mysql_fix_1
Browse files Browse the repository at this point in the history
Add ability to delete and update records in MySQL
  • Loading branch information
ibre5041 committed Apr 5, 2021
2 parents 8849ed7 + 00ce4c5 commit f31e755
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/connection/toqmysqltraits.cpp
Expand Up @@ -33,6 +33,8 @@
* END_COMMON_COPYRIGHT_HEADER */

#include "connection/toqmysqltraits.h"
#include "core/toconnectionsubloan.h"
#include "core/toquery.h"

QString toQMySqlTraits::quote(const QString &name) const
{
Expand All @@ -52,3 +54,36 @@ QString toQMySqlTraits::schemaSwitchSQL(const QString &schema) const
static const QString USE_DATABASE("USE `%1`");
return USE_DATABASE.arg(schema);
}

QList<QString> toQMySqlTraits::primaryKeys(toConnection &conn, toCache::ObjectRef const&obj) const
{
toCache::CacheEntry const* e = conn.getCache().findEntry(obj);
if (!e || e->type != toCache::TABLE )
return QList<QString>();

// Cache is used because the function is called twice for the same table
static QString cached_table_schema;
static QString cached_table_name;
static QList<QString> cached_pk;

if (obj.owner() == cached_table_schema && obj.name() == cached_table_name)
return cached_pk;

cached_table_schema = obj.owner();
cached_table_name = obj.name();
cached_pk.clear();

toConnectionSubLoan loan(conn);
toQuery QueryR(loan,
"SELECT column_name"
" FROM information_schema.key_column_usage"
" WHERE table_schema = :f1<char[101]>"
" AND table_name = :f2<char[101]>"
" AND constraint_name = 'PRIMARY'"
" ORDER BY ordinal_position",
toQueryParams() << obj.owner() << obj.name());
while (!QueryR.eof())
cached_pk << QueryR.readValue();

return cached_pk;
}
3 changes: 3 additions & 0 deletions src/connection/toqmysqltraits.h
Expand Up @@ -34,6 +34,7 @@
#pragma once

#include "core/toconnectiontraits.h"
#include "core/toconnection.h"

class toQMySqlTraits: public toConnectionTraits
{
Expand Down Expand Up @@ -113,4 +114,6 @@ class toQMySqlTraits: public toConnectionTraits
retval.append('\'');
return retval;
}

QList<QString> primaryKeys(toConnection &, toCache::ObjectRef const&) const override;
};

0 comments on commit f31e755

Please sign in to comment.