1 #ifndef EASYQTSQL_SQLFACTORY_H 2 #define EASYQTSQL_SQLFACTORY_H 29 #ifndef EASY_QT_SQL_MAIN 58 DBSetting(
const QString &type,
const QString &connectionString)
59 : type(type), dbName(connectionString)
63 :
DBSetting(type, createConnectionString(params))
66 DBSetting(
const QString &type,
const QString &host,
int port,
const QString &username,
const QString &password,
const QString &dbname)
67 : type(type), port(port), host(host), username(username), password(password), dbName(dbname)
88 {
"Driver",
"{SQL Server}"}
90 , {
"Database", database}
101 {
"Driver",
"{SQL Server}"}
103 , {
"Database", database}
104 , {
"Trusted_Connection",
"Yes"}
113 {
"Driver",
"{SQL Native Client}"}
115 , {
"Database", database}
126 {
"Driver",
"{SQL Native Client}"}
128 , {
"Database", database}
129 , {
"Trusted_Connection",
"Yes"}
137 return type.isEmpty() ?
"QODBC" : type;
148 static QString createConnectionString(
const ConnParameters ¶ms)
155 const QString &key = pair.first;
156 const QString &value = pair.second;
158 res.append(key).append(
'=');
160 auto containsControlChar = [](
const QString &value) ->
bool 162 for (
int i = 0; i < value.size(); ++i)
164 if (value[i].category() == QChar::Other_Control)
171 if ( value.startsWith(QChar::Space) || value.endsWith(QChar::Space) || value.contains(
';') || containsControlChar(value) )
173 const bool containsSingleQuotes = value.contains(QChar(
'\''));
174 const bool containsDoubleQuotes = value.contains(QChar(
'\"'));
176 if (containsSingleQuotes && containsDoubleQuotes)
181 else if (containsSingleQuotes)
183 res.append(
'\"').append(value).append(
'\"');
185 else if (containsDoubleQuotes)
187 res.append(
'\'').append(value).append(
'\'');
208 for (
const QString &uniqueConnName : m_connNameMap.values())
211 QSqlDatabase db = QSqlDatabase::database(uniqueConnName);
218 QSqlDatabase::removeDatabase(uniqueConnName);
224 const QString uniqueConnectionName = connectionName + QUuid::createUuid().toString();
226 m_connNameMap.insert(connectionName, uniqueConnectionName);
228 QSqlDatabase db = QSqlDatabase::addDatabase(settings.
getType(), uniqueConnectionName);
230 db.setDatabaseName(settings.dbName);
231 db.setHostName(settings.host);
232 db.setUserName(settings.username);
233 db.setPassword(settings.password);
235 if (settings.port.isValid())
237 db.setPort(settings.port.toInt());
243 return m_connNameMap.contains(connectionName);
248 const QString &uniqueConnectionName = m_connNameMap.value(connectionName);
250 QSqlDatabase db = QSqlDatabase::database(uniqueConnectionName);
256 qCritical() << db.lastError().text();
264 QMap<QString, QString> m_connNameMap;
269 QMutexLocker locker(&mutex);
271 m_settings.insert(connectionName, settings);
278 QMutexLocker locker(&mutex);
280 if (newConnectionName == connectionName)
283 if (m_settings.contains(connectionName))
285 const DBSetting &setting = m_settings[connectionName];
286 m_settings.insert(newConnectionName, setting);
292 QSqlDatabase
getDatabase(
const QString &connectionName = QSqlDatabase::defaultConnection)
294 QMutexLocker locker(&mutex);
296 if (m_settings.contains(connectionName))
300 if (!m_dbPool.hasLocalData())
304 m_dbPool.setLocalData(threadDbPool);
308 threadDbPool = m_dbPool.localData();
315 const DBSetting &settings = m_settings[connectionName];
324 return QSqlDatabase();
330 QMap<QString, DBSetting> m_settings;
331 QThreadStorage<ThreadDBPool*> m_dbPool;
333 QString m_defaultConnName;
341 #endif // EASYQTSQL_SQLFACTORY_H static DBSetting msSqlServerODBC(const QString &server, const QString &database, const QString &userID, const QString &password, const ConnParameters &extraParams=ConnParameters())
Definition: EasyQtSql_SqlFactory.h:84
Definition: EasyQtSql_SqlFactory.h:202
SqlFactory * config(const DBSetting &settings, const QString &connectionName=QSqlDatabase::defaultConnection)
Definition: EasyQtSql_SqlFactory.h:267
static DBSetting msSqlServerNativeClient90ODBC(const QString &server, const QString &database, const QString &userID, const QString &password, const ConnParameters &extraParams=ConnParameters())
Definition: EasyQtSql_SqlFactory.h:109
void addConnection(const DBSetting &settings, const QString &connectionName)
Definition: EasyQtSql_SqlFactory.h:222
DBSetting(const QString &type, const QString &connectionString)
Definition: EasyQtSql_SqlFactory.h:58
static DBSetting msSqlServerODBC(const QString &server, const QString &database, const ConnParameters &extraParams=ConnParameters())
Definition: EasyQtSql_SqlFactory.h:97
static DBSetting msSqlServerNativeClient90ODBC(const QString &server, const QString &database, const ConnParameters &extraParams=ConnParameters())
Definition: EasyQtSql_SqlFactory.h:122
~ThreadDBPool()
Definition: EasyQtSql_SqlFactory.h:206
static DBSetting sqlite(const QString &fileName)
Definition: EasyQtSql_SqlFactory.h:75
static SqlFactory * getInstance()
Definition: EasyQtSql_SqlFactory.h:40
DBSetting()
Definition: EasyQtSql_SqlFactory.h:55
QSqlDatabase getDatabase(const QString &connectionName) const
Definition: EasyQtSql_SqlFactory.h:246
QSqlDatabase getDatabase(const QString &connectionName=QSqlDatabase::defaultConnection)
Definition: EasyQtSql_SqlFactory.h:292
DBSetting(const QString &type, const ConnParameters ¶ms)
Definition: EasyQtSql_SqlFactory.h:62
bool connectionExists(const QString &connectionName) const
Definition: EasyQtSql_SqlFactory.h:241
static DBSetting sqliteMemory()
Definition: EasyQtSql_SqlFactory.h:70
SqlFactory * clone(const QString &newConnectionName, const QString &connectionName)
Definition: EasyQtSql_SqlFactory.h:276
Definition: EasyQtSql_SqlFactory.h:36
QVector< KeywordValuePair > ConnParameters
Definition: EasyQtSql_SqlFactory.h:53
DBSetting(const QString &type, const QString &host, int port, const QString &username, const QString &password, const QString &dbname)
Definition: EasyQtSql_SqlFactory.h:66
QString getType() const
Definition: EasyQtSql_SqlFactory.h:135
QPair< QString, QString > KeywordValuePair
Definition: EasyQtSql_SqlFactory.h:52
Definition: EasyQtSql_SqlFactory.h:48