Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert Statement #155

Merged
merged 6 commits into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added TSqlDatabase class.
  • Loading branch information
treefrogframework committed Jul 17, 2017
commit 8931a791a9806e5357caeb48bf57094a61120cbb
3 changes: 3 additions & 0 deletions defaults/database.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ UserName=
Password=
ConnectOptions=
PostOpenStatements=
EnableUpsert=false

[test]
DriverType=QMYSQL
Expand All @@ -35,6 +36,7 @@ UserName=
Password=
ConnectOptions=
PostOpenStatements=
EnableUpsert=false

[product]
DriverType=QMYSQL
Expand All @@ -45,3 +47,4 @@ UserName=
Password=
ConnectOptions=
PostOpenStatements=
EnableUpsert=false
2 changes: 2 additions & 0 deletions src/corelib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ HEADERS += tactionview.h
SOURCES += tactionview.cpp
HEADERS += tactionmailer.h
SOURCES += tactionmailer.cpp
HEADERS += tsqldatabase.h
SOURCES += tsqldatabase.cpp
HEADERS += tsqldatabasepool.h
SOURCES += tsqldatabasepool.cpp
HEADERS += tsqlobject.h
Expand Down
129 changes: 129 additions & 0 deletions src/tsqldatabase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* Copyright (c) 2017, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/

#include "tsqldatabase.h"
#include "tsystemglobal.h"
#include <QMap>
#include <QFileInfo>
#include <QReadWriteLock>


class TDatabaseDict : public QMap<QString, TSqlDatabase>
{
public:
mutable QReadWriteLock lock;
};
Q_GLOBAL_STATIC(TDatabaseDict, dbDict)


TSqlDatabase::DbmsType TSqlDatabase::dbmsType() const
{
#if QT_VERSION >= 0x050400
return (_sqlDatabase.driver()) ? (TSqlDatabase::DbmsType)_sqlDatabase.driver()->dbmsType() : UnknownDbms;
#else

DbmsType dbms = UnknownDbms;
const QString type = _sqlDatabase.driverName();
switch (type[1].toLatin1()) {
case 'P':
if (type == QLatin1String("QPSQL") || type == QLatin1String("QPSQL7")) {
dbms = PostgreSQL;
}
break;

case 'M':
if (type == QLatin1String("QMYSQL") || type == QLatin1String("QMYSQL3")) {
dbms = MySqlServer;
}
break;

case 'O':
if (type == QLatin1String("QODBC") || type == QLatin1String("QODBC3")) {
dbms = MSSqlServer;
break;
}
if (type == QLatin1String("QOCI") || type == QLatin1String("QOCI8")) {
dbms = Oracle;
}
break;

case 'T':
if (type == QLatin1String("QTDS") || type == QLatin1String("QTDS7")) {
dbms = Sybase;
}
break;

case 'D':
if (type == QLatin1String("QDB2")) {
dbms = DB2;
}
break;

case 'S':
if (type == QLatin1String("QSQLITE") || type == QLatin1String("QSQLITE2")) {
dbms = SQLite;
}
break;

case 'I':
if (type == QLatin1String("QIBASE")) {
dbms = Interbase;
}
break;

default:
break;
}
return dbms;
#endif
}


const TSqlDatabase &TSqlDatabase::database(const QString &connectionName)
{
static TSqlDatabase defaultDatabase;
auto *dict = dbDict();
QReadLocker locker(&dict->lock);

if (dict->contains(connectionName)) {
return (*dict)[connectionName];
} else {
return defaultDatabase;
}
}


TSqlDatabase &TSqlDatabase::addDatabase(const QString &driver, const QString &connectionName)
{
TSqlDatabase db(QSqlDatabase::addDatabase(driver, connectionName));
auto *dict = dbDict();
QWriteLocker locker(&dict->lock);

if (dict->contains(connectionName)) {
dict->take(connectionName);
}

dict->insert(connectionName, db);
return (*dict)[connectionName];
}


void TSqlDatabase::removeDatabase(const QString &connectionName)
{
auto *dict = dbDict();
QWriteLocker locker(&dict->lock);
dict->take(connectionName);
QSqlDatabase::removeDatabase(connectionName);
}


bool TSqlDatabase::contains(const QString &connectionName)
{
auto *dict = dbDict();
QReadLocker locker(&dict->lock);
return dict->contains(connectionName);
}
83 changes: 83 additions & 0 deletions src/tsqldatabase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef TSQLDATABASE_H
#define TSQLDATABASE_H

#include <QStringList>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <TGlobal>


class T_CORE_EXPORT TSqlDatabase
{
public:
#if QT_VERSION >= 0x050400
enum DbmsType {
UnknownDbms = QSqlDriver::UnknownDbms,
MSSqlServer = QSqlDriver::MSSqlServer,
MySqlServer = QSqlDriver::MySqlServer,
PostgreSQL = QSqlDriver::PostgreSQL,
Oracle = QSqlDriver::Oracle,
Sybase = QSqlDriver::Sybase,
SQLite = QSqlDriver::SQLite,
Interbase = QSqlDriver::Interbase,
DB2 = QSqlDriver::DB2
};
#else
enum DbmsType {
UnknownDbms,
MSSqlServer,
MySqlServer,
PostgreSQL,
Oracle,
Sybase,
SQLite,
Interbase,
DB2
};
#endif

explicit TSqlDatabase(const QSqlDatabase &database = QSqlDatabase());
TSqlDatabase(const TSqlDatabase &other);
~TSqlDatabase() { }
TSqlDatabase &operator=(const TSqlDatabase &other);

DbmsType dbmsType() const;
bool isValid() const { return _sqlDatabase.isValid(); }
QString connectionName() const { return _sqlDatabase.connectionName(); }
const QSqlDatabase &sqlDatabase() const { return _sqlDatabase; }
QSqlDatabase &sqlDatabase() { return _sqlDatabase; }
QStringList postOpenStatements() const { return _postOpenStatements; }
void setPostOpenStatements(const QStringList &statements) { _postOpenStatements = statements; }
bool isUpsertEnabled() const { return _enableUpsert; }
void setUpsertEnabled(bool enable) { _enableUpsert = enable; }

static const char *const defaultConnection;
static const TSqlDatabase &database(const QString &connectionName = QLatin1String(defaultConnection));
static TSqlDatabase &addDatabase(const QString &driver, const QString &connectionName = QLatin1String(defaultConnection));
static void removeDatabase(const QString &connectionName = QLatin1String(defaultConnection));
static bool contains(const QString &connectionName = QLatin1String(defaultConnection));

private:
QSqlDatabase _sqlDatabase;
QStringList _postOpenStatements;
bool _enableUpsert {false};
};


inline TSqlDatabase::TSqlDatabase(const QSqlDatabase &database)
: _sqlDatabase(database)
{}

inline TSqlDatabase::TSqlDatabase(const TSqlDatabase &other)
: _sqlDatabase(other._sqlDatabase), _postOpenStatements(other._postOpenStatements), _enableUpsert(other._enableUpsert)
{}

inline TSqlDatabase &TSqlDatabase::operator=(const TSqlDatabase &other)
{
_sqlDatabase = other._sqlDatabase;
_postOpenStatements = other._postOpenStatements;
_enableUpsert = other._enableUpsert;
return *this;
}

#endif // TSQLDATABASE_H
Loading