最近事情比较多很久没有写学习笔记了,数据库线程池,+ 数据库封装,虽说数据库操作有很多不需要写sql 的,ORM 封装的方式去操作数据库。但是从业这些年一直是自己动手写sql ,还是改不了这个习惯。不说了直接上代码。
数据库线程池
ConnectionPool.h 文件
#ifndef CONNECTIONPOOL_H
#define CONNECTIONPOOL_H
#include
#include
#include
#include
#include
#include "ConnectionPool.h"
class ConnectionPool {
public:
static void release(); // 关闭所有的数据库连接
static QSqlDatabase createConnection(); // 获取数据库连接
static void closeConnection(QSqlDatabase connection); // 释放数据库连接回连接池
~ConnectionPool();
private:
static ConnectionPool& getInstance();
ConnectionPool();
ConnectionPool(const ConnectionPool &other);
ConnectionPool& operator=(const ConnectionPool &other);
QSqlDatabase createConnection(const QString &connectionName); // 创建数据库连接
void initialize();
void loadConfigFile();
public:
QQueue usedConnectionNames; // 已使用的数据库连接名
QQueue unusedConnectionNames; // 未使用的数据库连接名
QJsonObject mJsonObject;
// 数据库信息
QString hostName;
QString databaseName;
QString username;
QString password;
QString databaseType;
int port;
bool testOnBorrow; // 取得连接的时候验证连接是否有效
QString testOnBorrowSql; // 测试访问数据库的 SQL
int maxWaitTime; // 获取连接最大等待时间
int waitInterval; // 尝试获取连接时等待间隔时间
int maxConnectionCount; // 最大连接数
static QMutex mutex;
static QWaitCondition waitConnection;
static ConnectionPool *instance;
};
#endif // CONNECTIONPOOL_H
ConnectionPool.cpp
#include "ConnectionPool.h"
#include
QMutex ConnectionPool::mutex;
QWaitCondition ConnectionPool::waitConnection;
ConnectionPool* ConnectionPool::instance = NULL;
ConnectionPool::ConnectionPool()
{
initialize();
}
ConnectionPool::~ConnectionPool()
{
// 销毁连接池的时候删除所有的连接
foreach(QString connectionName, usedConnectionNames)
{
QSqlDatabase::removeDatabase(connectionName);
}
foreach(QString connectionName, unusedConnectionNames)
{
QSqlDatabase::removeDatabase(connectionName);
}
}
void ConnectionPool::loadConfigFile()
{
QString path = qApp->applicationDirPath();
QString strFile;
strFile = path + "/config/DBConfig.json";
QFile file(strFile);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "could't open projects json";
return;
}
QString value = file.readAll();
file.close();
QJsonParseError parseJsonErr;
QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr);
if(!(parseJsonErr.error == QJsonParseError::NoError))
{
qDebug() << parseJsonErr.errorString();
return;
}
mJsonObject=document.object();
}
void ConnectionPool::initialize()
{
//loadConfigFile();
hostName = "127.0.0.1";
databaseName = "test";
username = "root";
password = "admin";
databaseType = "QMYSQL";
port = 3306;
testOnBorrow = true;
testOnBorrowSql = "SELECT 1";
waitInterval = 200;
maxWaitTime = 5000;
maxConnectionCount = 10;
}
ConnectionPool& ConnectionPool::getInstance()
{
if (NULL == instance)
{
QMutexLocker locker(&mutex);
if (NULL == instance)
{
instance = new ConnectionPool();
}
}
return *instance;
}
void ConnectionPool::release()
{
QMutexLocker locker(&mutex);
delete instance;
instance = NULL;
}
QSqlDatabase ConnectionPool::createConnection()
{
ConnectionPool& pool = ConnectionPool::getInstance();
QString connectionName;
QMutexLocker locker(&mutex);
// 已创建连接数
int connectionCount = pool.unusedConnectionNames.size() + pool.usedConnectionNames.size();
// 如果连接已经用完,等待 waitInterval 毫秒看看是否有可用连接,最长等待 maxWaitTime 毫秒
for (int i = 0;
i < pool.maxWaitTime
&& pool.unusedConnectionNames.size() == 0 && connectionCount == pool.maxConnectionCount;
i += pool.waitInterval)
{
waitConnection.wait(&mutex, pool.waitInterval);
// 重新计算已创建连接数
connectionCount = pool.unusedConnectionNames.size() + pool.usedConnectionNames.size();
}
qDebug() << "connectionCount:"< 0)
{
// 有已经回收的连接,复用它们
connectionName = pool.unusedConnectionNames.dequeue();
}
else if (connectionCount < pool.maxConnectionCount)
{
// 没有已经回收的连接,但是没有达到最大连接数,则创建新的连接
connectionName = QString("Connection-%1").arg(connectionCount + 1);
}
else
{
// 已经达到最大连接数
qDebug() << "Cannot create more connections.";
return QSqlDatabase();
}
// 创建连接
QSqlDatabase db = pool.createConnection(connectionName);
// 有效的连接才放入 usedConnectionNames
if (db.isOpen())
{
pool.usedConnectionNames.enqueue(connectionName);
}
return db;
}
void ConnectionPool::closeConnection(QSqlDatabase connection)
{
ConnectionPool& pool = ConnectionPool::getInstance();
QString connectionName = connection.connectionName();
// 如果是我们创建的连接,从 used 里删除,放入 unused 里
if (pool.usedConnectionNames.contains(connectionName))
{
QMutexLocker locker(&mutex);
pool.usedConnectionNames.removeOne(connectionName);
pool.unusedConnectionNames.enqueue(connectionName);
waitConnection.wakeOne();
}
}
QSqlDatabase ConnectionPool::createConnection(const QString &connectionName)
{
// 连接已经创建过了,复用它,而不是重新创建
if (QSqlDatabase::contains(connectionName))
{
QSqlDatabase db1 = QSqlDatabase::database(connectionName);
if (testOnBorrow)
{
// 返回连接前访问数据库,如果连接断开,重新建立连接
qDebug() << "Test connection on borrow, execute:" << testOnBorrowSql << ", for" << connectionName;
QSqlQuery query(testOnBorrowSql, db1);
if (query.lastError().type() != QSqlError::NoError && !db1.open())
{
qDebug() << "Open datatabase error:" << db1.lastError().text();
return QSqlDatabase();
}
}
return db1;
}
// 创建一个新的连接
QSqlDatabase db = QSqlDatabase::addDatabase(databaseType, connectionName);
db.setHostName(hostName);
db.setDatabaseName(databaseName);
db.setUserName(username);
db.setPassword(password);
db.setPort(port);
if (!db.open())
{
qDebug() << "Open datatabase error:" << db.lastError().text();
return QSqlDatabase();
}
return db;
}
数据库封装:
SqlDatabase.h
#ifndef SQLDATABASE_H
#define SQLDATABASE_H
#include
#include
#include
class SqlDatabase
{
public:
SqlDatabase();
~SqlDatabase();
public:
static void InsertDB(QString strTableName,QJsonArray jsonValue);
static void UpdateDB(QString strTableName,QJsonArray jsonValue,QString strColumnCondition);
static int InsertDB(QString strTableName,QJsonObject jsonValue);
static void UpdateDB(QString strTableName,QJsonObject jsonValue,QString strCondition);
static void QueryRecord(QString strSql,QJsonArray &nJsonValue);
static int QueryCount(QString strSql);
static bool Delete(QString strSql);
static bool Delete(QString strTableName,QString strCondition);
};
#endif // SQLDATABASE_H
SqlDatabase.cpp
#include "SqlDatabase.h"
#include "ConnectionPool.h"
#include
#pragma execution_character_set("utf-8")
SqlDatabase::SqlDatabase()
{
}
SqlDatabase::~SqlDatabase()
{
ConnectionPool::Destroy();
}
void SqlDatabase::InsertDB(QString strTableName,QJsonArray jsonValues)
{
QSqlDatabase db =ConnectionPool::CreateConnection();
QString strValues="";
QString strNames="";
bool iskeyName=false;
for(int j=0;j
此处说一下批量更新
源数据是这样需要更新
id:1 shorName :aaaaa score1:56.9 ranking :22
id:2 shorName :bbbbb score1:89.9 score3:59.9 ranking :27
id:3 shorName :ccccc score1:76.9 score2:81.9 ranking :29
sql 语句 批量更新
UPDATE test m,(
SELECT
'1.000' AS `id`,
'a' AS `name`,
'22.000' AS `ranking`,
'56.900' AS `score1`,
'39.500' AS `score2`,
'56.700' AS `score3`,
'aaaa' AS `shorName` UNION
SELECT
'2.000' AS `id`,
'b' AS `name`,
'27.000' AS `ranking`,
'89.900' AS `score1`,
'39.500' AS `score2`,
'59.900' AS `score3`,
'bbbbb' AS `shorName` UNION
SELECT
'3.000' AS `id`,
'c' AS `name`,
'29.000' AS `ranking`,
'76.900' AS `score1`,
'72.900' AS `score2`,
'81.900' AS `score3`,
'ccccc' AS `shorName`
) n
SET m.id = n.id,
m.NAME = n.NAME,
m.ranking = n.ranking,
m.score1 = n.score1,
m.score2 = n.score2,
m.score3 = n.score3,
m.shorName = n.shorName
WHERE
m.id = n.id