mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-21 19:49:36 +08:00
实现 SQLHelper 的自增 ID、异步版本功能
This commit is contained in:
parent
4f6d70abc3
commit
e6d740f780
@ -47,6 +47,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\FunGame.Core\FunGame.Core.csproj" />
|
<ProjectReference Include="..\..\FunGame.Core\FunGame.Core.csproj" />
|
||||||
|
<ProjectReference Include="..\..\FunGame.Extension\FunGame.SQLQueryExtension\FunGame.SQLQueryExtension.csproj" />
|
||||||
<ProjectReference Include="..\FunGame.Implement\FunGame.Implement.csproj" />
|
<ProjectReference Include="..\FunGame.Implement\FunGame.Implement.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
using Milimoe.FunGame.Core.Model;
|
using Milimoe.FunGame.Core.Model;
|
||||||
@ -9,14 +10,59 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
{
|
{
|
||||||
public class MySQLHelper : SQLHelper
|
public class MySQLHelper : SQLHelper
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// FunGame 类型
|
||||||
|
/// </summary>
|
||||||
public override FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server;
|
public override FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用的数据库类型
|
||||||
|
/// </summary>
|
||||||
public override SQLMode Mode { get; } = SQLMode.MySQL;
|
public override SQLMode Mode { get; } = SQLMode.MySQL;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SQL 脚本
|
||||||
|
/// </summary>
|
||||||
public override string Script { get; set; } = "";
|
public override string Script { get; set; } = "";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 命令类型
|
||||||
|
/// </summary>
|
||||||
public override CommandType CommandType { get; set; } = CommandType.Text;
|
public override CommandType CommandType { get; set; } = CommandType.Text;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 数据库事务
|
||||||
|
/// </summary>
|
||||||
|
public override DbTransaction? Transaction => _transaction;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行结果
|
||||||
|
/// </summary>
|
||||||
public override SQLResult Result => _result;
|
public override SQLResult Result => _result;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SQL 服务器信息
|
||||||
|
/// </summary>
|
||||||
public override SQLServerInfo ServerInfo => _serverInfo ?? SQLServerInfo.Create();
|
public override SQLServerInfo ServerInfo => _serverInfo ?? SQLServerInfo.Create();
|
||||||
public override int UpdateRows => _updateRows;
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上一次执行命令影响的行数
|
||||||
|
/// </summary>
|
||||||
|
public override int AffectedRows => _affectedRows;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上一次执行的命令是 Insert 时,返回的自增 ID,大于 0 有效
|
||||||
|
/// </summary>
|
||||||
|
public override long LastInsertId => _lastInsertId;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上一次执行命令的查询结果集
|
||||||
|
/// </summary>
|
||||||
public override DataSet DataSet => _dataSet;
|
public override DataSet DataSet => _dataSet;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SQL 语句参数
|
||||||
|
/// </summary>
|
||||||
public override Dictionary<string, object> Parameters { get; } = [];
|
public override Dictionary<string, object> Parameters { get; } = [];
|
||||||
|
|
||||||
private readonly string _connectionString = "";
|
private readonly string _connectionString = "";
|
||||||
@ -25,7 +71,8 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
private DataSet _dataSet = new();
|
private DataSet _dataSet = new();
|
||||||
private SQLResult _result = SQLResult.NotFound;
|
private SQLResult _result = SQLResult.NotFound;
|
||||||
private readonly SQLServerInfo? _serverInfo;
|
private readonly SQLServerInfo? _serverInfo;
|
||||||
private int _updateRows = 0;
|
private int _affectedRows = 0;
|
||||||
|
private long _lastInsertId = 0;
|
||||||
|
|
||||||
public MySQLHelper(string script = "", CommandType type = CommandType.Text)
|
public MySQLHelper(string script = "", CommandType type = CommandType.Text)
|
||||||
{
|
{
|
||||||
@ -69,7 +116,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行一个命令
|
/// 执行现有命令(<see cref="Script"/>)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override int Execute()
|
public override int Execute()
|
||||||
@ -104,14 +151,24 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
if (_transaction != null) command.Transaction = _transaction;
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
_updateRows = command.ExecuteNonQuery();
|
ReSet();
|
||||||
_result = SQLResult.Success;
|
_affectedRows = command.ExecuteNonQuery();
|
||||||
|
if (_affectedRows > 0)
|
||||||
|
{
|
||||||
|
_result = SQLResult.Success;
|
||||||
|
if (script.Contains(Core.Library.SQLScript.Constant.Command_Insert, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
_lastInsertId = command.LastInsertedId;
|
||||||
|
if (_lastInsertId < 0) _lastInsertId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else _result = SQLResult.Fail;
|
||||||
if (localTransaction) Commit();
|
if (localTransaction) Commit();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
if (localTransaction) Rollback();
|
if (localTransaction) Rollback();
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -119,11 +176,75 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
if (localTransaction) Close();
|
if (localTransaction) Close();
|
||||||
if (ClearParametersAfterExecute) Parameters.Clear();
|
if (ClearParametersAfterExecute) Parameters.Clear();
|
||||||
}
|
}
|
||||||
return UpdateRows;
|
return AffectedRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询DataSet
|
/// 异步执行现有命令(<see cref="Script"/>)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task<int> ExecuteAsync()
|
||||||
|
{
|
||||||
|
return await ExecuteAsync(Script);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步执行一个指定的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="script"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task<int> ExecuteAsync(string script)
|
||||||
|
{
|
||||||
|
bool localTransaction = _transaction == null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (localTransaction)
|
||||||
|
{
|
||||||
|
NewTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenConnection();
|
||||||
|
Script = script;
|
||||||
|
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
|
||||||
|
using MySqlCommand command = new(script, _connection);
|
||||||
|
command.CommandType = CommandType;
|
||||||
|
foreach (KeyValuePair<string, object> param in Parameters)
|
||||||
|
{
|
||||||
|
command.Parameters.AddWithValue(param.Key, param.Value);
|
||||||
|
}
|
||||||
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
|
ReSet();
|
||||||
|
_affectedRows = await command.ExecuteNonQueryAsync();
|
||||||
|
if (_affectedRows > 0)
|
||||||
|
{
|
||||||
|
_result = SQLResult.Success;
|
||||||
|
if (script.Contains(Core.Library.SQLScript.Constant.Command_Insert, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
_lastInsertId = command.LastInsertedId;
|
||||||
|
if (_lastInsertId < 0) _lastInsertId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else _result = SQLResult.Fail;
|
||||||
|
if (localTransaction) Commit();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
if (localTransaction) Rollback();
|
||||||
|
_result = SQLResult.SQLError;
|
||||||
|
ServerHelper.Error(e);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (localTransaction) Close();
|
||||||
|
if (ClearParametersAfterExecute) Parameters.Clear();
|
||||||
|
}
|
||||||
|
return AffectedRows;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行现有命令(<see cref="Script"/>)查询 DataSet
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override DataSet ExecuteDataSet()
|
public override DataSet ExecuteDataSet()
|
||||||
@ -132,7 +253,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行指定的命令查询DataSet
|
/// 执行指定的命令查询 DataSet
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="script"></param>
|
/// <param name="script"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
@ -161,12 +282,13 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
if (_transaction != null) command.Transaction = _transaction;
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
|
ReSet();
|
||||||
MySqlDataAdapter adapter = new()
|
MySqlDataAdapter adapter = new()
|
||||||
{
|
{
|
||||||
SelectCommand = command
|
SelectCommand = command
|
||||||
};
|
};
|
||||||
_dataSet = new();
|
_dataSet = new();
|
||||||
adapter.Fill(_dataSet);
|
_affectedRows = adapter.Fill(_dataSet);
|
||||||
|
|
||||||
if (localTransaction) Commit();
|
if (localTransaction) Commit();
|
||||||
|
|
||||||
@ -175,7 +297,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
if (localTransaction) Rollback();
|
if (localTransaction) Rollback();
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -187,26 +309,68 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检查数据库是否存在
|
/// 异步执行现有命令(<see cref="Script"/>)查询 DataSet
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool DatabaseExists()
|
public override async Task<DataSet> ExecuteDataSetAsync()
|
||||||
{
|
{
|
||||||
|
return await ExecuteDataSetAsync(Script);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步执行指定的命令查询 DataSet
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="script"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task<DataSet> ExecuteDataSetAsync(string script)
|
||||||
|
{
|
||||||
|
bool localTransaction = _transaction == null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ExecuteDataSet(Core.Library.SQLScript.Common.Configs.Select_GetConfig(this, "Initialization"));
|
if (localTransaction)
|
||||||
return Success;
|
{
|
||||||
|
NewTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenConnection();
|
||||||
|
Script = script;
|
||||||
|
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
|
||||||
|
|
||||||
|
using MySqlCommand command = new(script, _connection)
|
||||||
|
{
|
||||||
|
CommandType = CommandType
|
||||||
|
};
|
||||||
|
foreach (KeyValuePair<string, object> param in Parameters)
|
||||||
|
{
|
||||||
|
command.Parameters.AddWithValue(param.Key, param.Value);
|
||||||
|
}
|
||||||
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
|
ReSet();
|
||||||
|
MySqlDataAdapter adapter = new()
|
||||||
|
{
|
||||||
|
SelectCommand = command
|
||||||
|
};
|
||||||
|
_dataSet = new();
|
||||||
|
_affectedRows = await adapter.FillAsync(_dataSet);
|
||||||
|
|
||||||
|
if (localTransaction) Commit();
|
||||||
|
|
||||||
|
_result = _dataSet.Tables.Cast<DataTable>().Any(table => table.Rows.Count > 0) ? SQLResult.Success : SQLResult.NotFound;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
if (localTransaction) Rollback();
|
||||||
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
_result = SQLResult.Fail;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
Close();
|
if (localTransaction) Close();
|
||||||
|
if (ClearParametersAfterExecute) Parameters.Clear();
|
||||||
}
|
}
|
||||||
|
return _dataSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -233,7 +397,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -254,7 +418,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -263,12 +427,35 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查数据库是否存在
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override bool DatabaseExists()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ExecuteDataSet(Core.Library.SQLScript.Common.Configs.Select_GetConfig(this, "Initialization"));
|
||||||
|
return Success;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ServerHelper.Error(e);
|
||||||
|
_result = SQLResult.SQLError;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private bool _isDisposed = false;
|
private bool _isDisposed = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 资源清理
|
/// 资源清理
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose(bool disposing)
|
private void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (!_isDisposed)
|
if (!_isDisposed)
|
||||||
{
|
{
|
||||||
@ -289,5 +476,13 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
Dispose(true);
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ReSet()
|
||||||
|
{
|
||||||
|
_result = SQLResult.NotFound;
|
||||||
|
_affectedRows = 0;
|
||||||
|
_lastInsertId = 0;
|
||||||
|
DataSet.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
@ -9,14 +10,59 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
{
|
{
|
||||||
public class SQLiteHelper : SQLHelper
|
public class SQLiteHelper : SQLHelper
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// FunGame 类型
|
||||||
|
/// </summary>
|
||||||
public override FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server;
|
public override FunGameInfo.FunGame FunGameType { get; } = FunGameInfo.FunGame.FunGame_Server;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用的数据库类型
|
||||||
|
/// </summary>
|
||||||
public override SQLMode Mode { get; } = SQLMode.SQLite;
|
public override SQLMode Mode { get; } = SQLMode.SQLite;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SQL 脚本
|
||||||
|
/// </summary>
|
||||||
public override string Script { get; set; } = "";
|
public override string Script { get; set; } = "";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 命令类型
|
||||||
|
/// </summary>
|
||||||
public override CommandType CommandType { get; set; } = CommandType.Text;
|
public override CommandType CommandType { get; set; } = CommandType.Text;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 数据库事务
|
||||||
|
/// </summary>
|
||||||
|
public override DbTransaction? Transaction => _transaction;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行结果
|
||||||
|
/// </summary>
|
||||||
public override SQLResult Result => _result;
|
public override SQLResult Result => _result;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SQL 服务器信息
|
||||||
|
/// </summary>
|
||||||
public override SQLServerInfo ServerInfo => _serverInfo ?? SQLServerInfo.Create();
|
public override SQLServerInfo ServerInfo => _serverInfo ?? SQLServerInfo.Create();
|
||||||
public override int UpdateRows => _updateRows;
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上一次执行命令影响的行数
|
||||||
|
/// </summary>
|
||||||
|
public override int AffectedRows => _affectedRows;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上一次执行的命令是 Insert 时,返回的自增 ID,大于 0 有效
|
||||||
|
/// </summary>
|
||||||
|
public override long LastInsertId => _lastInsertId;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上一次执行命令的查询结果集
|
||||||
|
/// </summary>
|
||||||
public override DataSet DataSet => _dataSet;
|
public override DataSet DataSet => _dataSet;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SQL 语句参数
|
||||||
|
/// </summary>
|
||||||
public override Dictionary<string, object> Parameters { get; } = [];
|
public override Dictionary<string, object> Parameters { get; } = [];
|
||||||
|
|
||||||
private readonly string _connectionString = "";
|
private readonly string _connectionString = "";
|
||||||
@ -25,7 +71,8 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
private DataSet _dataSet = new();
|
private DataSet _dataSet = new();
|
||||||
private SQLResult _result = SQLResult.NotFound;
|
private SQLResult _result = SQLResult.NotFound;
|
||||||
private readonly SQLServerInfo? _serverInfo;
|
private readonly SQLServerInfo? _serverInfo;
|
||||||
private int _updateRows = 0;
|
private int _affectedRows = 0;
|
||||||
|
private long _lastInsertId = 0;
|
||||||
|
|
||||||
public SQLiteHelper(string script = "", CommandType type = CommandType.Text)
|
public SQLiteHelper(string script = "", CommandType type = CommandType.Text)
|
||||||
{
|
{
|
||||||
@ -67,7 +114,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行一个命令
|
/// 执行现有命令(<see cref="Script"/>)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override int Execute()
|
public override int Execute()
|
||||||
@ -102,14 +149,26 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
if (_transaction != null) command.Transaction = _transaction;
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
_updateRows = command.ExecuteNonQuery();
|
ReSet();
|
||||||
_result = SQLResult.Success;
|
_affectedRows = command.ExecuteNonQuery();
|
||||||
|
if (_affectedRows > 0)
|
||||||
|
{
|
||||||
|
_result = SQLResult.Success;
|
||||||
|
if (script.Contains(Core.Library.SQLScript.Constant.Command_Insert, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
using SqliteCommand idCommand = new("SELECT last_insert_rowid()", _connection);
|
||||||
|
if (_transaction != null) idCommand.Transaction = _transaction;
|
||||||
|
_lastInsertId = (long?)idCommand.ExecuteScalar() ?? 0;
|
||||||
|
if (_lastInsertId < 0) _lastInsertId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else _result = SQLResult.Fail;
|
||||||
if (localTransaction) Commit();
|
if (localTransaction) Commit();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
if (localTransaction) Rollback();
|
if (localTransaction) Rollback();
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -117,11 +176,77 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
if (localTransaction) Close();
|
if (localTransaction) Close();
|
||||||
if (ClearParametersAfterExecute) Parameters.Clear();
|
if (ClearParametersAfterExecute) Parameters.Clear();
|
||||||
}
|
}
|
||||||
return UpdateRows;
|
return AffectedRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询DataSet
|
/// 异步执行现有命令(<see cref="Script"/>)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task<int> ExecuteAsync()
|
||||||
|
{
|
||||||
|
return await ExecuteAsync(Script);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步执行一个指定的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="script"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task<int> ExecuteAsync(string script)
|
||||||
|
{
|
||||||
|
bool localTransaction = _transaction == null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (localTransaction)
|
||||||
|
{
|
||||||
|
NewTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenConnection();
|
||||||
|
Script = script;
|
||||||
|
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
|
||||||
|
using SqliteCommand command = new(script, _connection);
|
||||||
|
command.CommandType = CommandType;
|
||||||
|
foreach (KeyValuePair<string, object> param in Parameters)
|
||||||
|
{
|
||||||
|
command.Parameters.AddWithValue(param.Key, param.Value);
|
||||||
|
}
|
||||||
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
|
ReSet();
|
||||||
|
_affectedRows = await command.ExecuteNonQueryAsync();
|
||||||
|
if (_affectedRows > 0)
|
||||||
|
{
|
||||||
|
_result = SQLResult.Success;
|
||||||
|
if (script.Contains(Core.Library.SQLScript.Constant.Command_Insert, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
using SqliteCommand idCommand = new("SELECT last_insert_rowid()", _connection);
|
||||||
|
if (_transaction != null) idCommand.Transaction = _transaction;
|
||||||
|
_lastInsertId = (long?)await idCommand.ExecuteScalarAsync() ?? 0;
|
||||||
|
if (_lastInsertId < 0) _lastInsertId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else _result = SQLResult.Fail;
|
||||||
|
if (localTransaction) Commit();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
if (localTransaction) Rollback();
|
||||||
|
_result = SQLResult.SQLError;
|
||||||
|
ServerHelper.Error(e);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (localTransaction) Close();
|
||||||
|
if (ClearParametersAfterExecute) Parameters.Clear();
|
||||||
|
}
|
||||||
|
return AffectedRows;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行现有命令(<see cref="Script"/>)查询 DataSet
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override DataSet ExecuteDataSet()
|
public override DataSet ExecuteDataSet()
|
||||||
@ -130,7 +255,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行指定的命令查询DataSet
|
/// 执行指定的命令查询 DataSet
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="script"></param>
|
/// <param name="script"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
@ -158,6 +283,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
if (_transaction != null) command.Transaction = _transaction;
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
|
ReSet();
|
||||||
using SqliteDataReader reader = command.ExecuteReader();
|
using SqliteDataReader reader = command.ExecuteReader();
|
||||||
_dataSet = new();
|
_dataSet = new();
|
||||||
do
|
do
|
||||||
@ -174,7 +300,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
if (localTransaction) Rollback();
|
if (localTransaction) Rollback();
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -186,7 +312,73 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建一个SQL事务
|
/// 异步执行现有命令(<see cref="Script"/>)查询 DataSet
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task<DataSet> ExecuteDataSetAsync()
|
||||||
|
{
|
||||||
|
return await ExecuteDataSetAsync(Script);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步执行指定的命令查询 DataSet
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="script"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override async Task<DataSet> ExecuteDataSetAsync(string script)
|
||||||
|
{
|
||||||
|
bool localTransaction = _transaction == null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (localTransaction)
|
||||||
|
{
|
||||||
|
NewTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenConnection();
|
||||||
|
Script = script;
|
||||||
|
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
|
||||||
|
using SqliteCommand command = new(script, _connection)
|
||||||
|
{
|
||||||
|
CommandType = CommandType
|
||||||
|
};
|
||||||
|
foreach (KeyValuePair<string, object> param in Parameters)
|
||||||
|
{
|
||||||
|
command.Parameters.AddWithValue(param.Key, param.Value);
|
||||||
|
}
|
||||||
|
if (_transaction != null) command.Transaction = _transaction;
|
||||||
|
|
||||||
|
ReSet();
|
||||||
|
using SqliteDataReader reader = await command.ExecuteReaderAsync();
|
||||||
|
_dataSet = new();
|
||||||
|
do
|
||||||
|
{
|
||||||
|
DataTable table = new();
|
||||||
|
table.Load(reader);
|
||||||
|
_dataSet.Tables.Add(table);
|
||||||
|
} while (!reader.IsClosed && reader.NextResult());
|
||||||
|
|
||||||
|
if (localTransaction) Commit();
|
||||||
|
|
||||||
|
_result = _dataSet.Tables.Cast<DataTable>().Any(table => table.Rows.Count > 0) ? SQLResult.Success : SQLResult.NotFound;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
if (localTransaction) Rollback();
|
||||||
|
_result = SQLResult.SQLError;
|
||||||
|
ServerHelper.Error(e);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (localTransaction) Close();
|
||||||
|
if (ClearParametersAfterExecute) Parameters.Clear();
|
||||||
|
}
|
||||||
|
return _dataSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建一个 SQL 事务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void NewTransaction()
|
public override void NewTransaction()
|
||||||
{
|
{
|
||||||
@ -209,7 +401,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -230,7 +422,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -253,7 +445,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
_result = SQLResult.Fail;
|
_result = SQLResult.SQLError;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -267,7 +459,7 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 资源清理
|
/// 资源清理
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose(bool disposing)
|
private void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (!_isDisposed)
|
if (!_isDisposed)
|
||||||
{
|
{
|
||||||
@ -288,5 +480,13 @@ namespace Milimoe.FunGame.Server.Services.DataUtility
|
|||||||
Dispose(true);
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ReSet()
|
||||||
|
{
|
||||||
|
_result = SQLResult.NotFound;
|
||||||
|
_affectedRows = 0;
|
||||||
|
_lastInsertId = 0;
|
||||||
|
DataSet.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\FunGame.Core\FunGame.Core.csproj" />
|
<ProjectReference Include="..\..\FunGame.Core\FunGame.Core.csproj" />
|
||||||
|
<ProjectReference Include="..\..\FunGame.Extension\FunGame.SQLQueryExtension\FunGame.SQLQueryExtension.csproj" />
|
||||||
<ProjectReference Include="..\FunGame.Implement\FunGame.Implement.csproj" />
|
<ProjectReference Include="..\FunGame.Implement\FunGame.Implement.csproj" />
|
||||||
<ProjectReference Include="..\FunGame.Server\FunGame.Server.csproj" />
|
<ProjectReference Include="..\FunGame.Server\FunGame.Server.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunGame.WebAPI", "FunGame.W
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunGame.Core", "..\FunGame.Core\FunGame.Core.csproj", "{33CAC80F-8394-41DB-B5AF-5E91123E6C84}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunGame.Core", "..\FunGame.Core\FunGame.Core.csproj", "{33CAC80F-8394-41DB-B5AF-5E91123E6C84}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunGame.SQLQueryExtension", "..\FunGame.Extension\FunGame.SQLQueryExtension\FunGame.SQLQueryExtension.csproj", "{9EEB3474-B9A1-4E5E-BEF0-14F30D81873C}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -33,6 +35,10 @@ Global
|
|||||||
{33CAC80F-8394-41DB-B5AF-5E91123E6C84}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{33CAC80F-8394-41DB-B5AF-5E91123E6C84}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{33CAC80F-8394-41DB-B5AF-5E91123E6C84}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{33CAC80F-8394-41DB-B5AF-5E91123E6C84}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{33CAC80F-8394-41DB-B5AF-5E91123E6C84}.Release|Any CPU.Build.0 = Release|Any CPU
|
{33CAC80F-8394-41DB-B5AF-5E91123E6C84}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{9EEB3474-B9A1-4E5E-BEF0-14F30D81873C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{9EEB3474-B9A1-4E5E-BEF0-14F30D81873C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{9EEB3474-B9A1-4E5E-BEF0-14F30D81873C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{9EEB3474-B9A1-4E5E-BEF0-14F30D81873C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user