using System;
using System.Collections.Generic;
using System.Data;
namespace Apewer.Source
{
/// 数据库客户端基类。
public abstract class DbClient : IDbClient
{
///
public virtual Logger Logger { get; set; }
///
internal DbClient(Timeout timeout)
{
_timeout = timeout ?? Timeout.Default;
}
#region Connection
string _key = TextUtility.Key();
Timeout _timeout = null;
IDbConnection _conn = null;
bool _disposed = false;
///
internal protected object Locker = new object();
/// 此连接的唯一标识。
public virtual string Key { get => _key; }
///
public virtual Timeout Timeout { get => _timeout; set => _timeout = value ?? Timeout.Default; }
/// 获取当前的 SqlConnection 对象。
public IDbConnection Connection { get => _conn; }
/// 此连接已释放。
public virtual bool Disposed { get => _disposed; }
/// 连接处于打开状态。
public virtual bool Online { get => _conn == null ? false : (_conn.State == ConnectionState.Open); }
/// 连接字符串。
public abstract string ConnectionString { get; }
/// 关闭连接后,执行的方法。
public virtual Action OnClosed { get; set; }
/// 连接数据库,若未连接则尝试连接,获取连接成功的状态。
public string Connect()
{
if (_disposed)
{
if (ThrowAdoException) throw new ObjectDisposedException(GetType().Name);
return "当前连接已释放,无法再次连接。";
}
if (_conn == null) _conn = GetConnection();
if (_conn.State == ConnectionState.Open) return null;
if (ThrowAdoException)
{
_conn.Open();
if (_conn.State != ConnectionState.Open)
{
var message = $"连接后状态为 {_conn.State},无法验证打开状态。";
Logger.Error(this, "Connect", message);
throw new SqlException(message);
}
return null;
}
try
{
_conn.Open();
if (_conn.State != ConnectionState.Open)
{
var message = $"连接后状态为 {_conn.State},无法验证打开状态。";
Logger.Error(this, "Connect", message);
return "连接失败," + message;
}
return null;
}
catch (Exception ex)
{
Logger.Error(this, "Connect", ex.Message);
if (ThrowAdoException) throw;
return ex.Message;
}
}
/// 更改已打开的数据库。
public virtual string Change(string store)
{
if (store.IsEmpty())
{
if (ThrowAdoException) throw new ArgumentNullException(nameof(store));
return "未指定数据名称。";
}
var connect = Connect();
if (connect.NotEmpty())
{
if (ThrowAdoException) throw new Exception(connect);
return connect;
}
try
{
Connection.ChangeDatabase(store);
return null;
}
catch (Exception ex)
{
Logger.Error(this, "Change", ex.Message);
if (ThrowAdoException) throw;
return ex.Message();
}
}
/// 关闭连接,并释放对象所占用的系统资源。
public virtual void Close()
{
if (_disposed) return;
_disposed = true;
if (_conn != null)
{
if (_transaction != null)
{
if (_autocommit) Commit();
else Rollback();
_transaction = null;
}
try { _conn.Close(); } catch { }
var onClosed = OnClosed;
if (onClosed != null) onClosed.Invoke(this);
}
}
/// 关闭连接,释放对象所占用的系统资源,并清除连接信息。
public virtual void Dispose()
{
Close();
}
#endregion
#region Transaction
const string TransactionNotFound = "事务不存在。";
private IDbTransaction _transaction = null;
private bool _autocommit = false;
/// 获取已启动的事务。
public virtual IDbTransaction Transaction { get => _transaction; }
string Begin(bool commit, Class isolation)
{
if (_transaction != null)
{
const string msg = "已存在未完成的事务,无法再次启动。";
if (ThrowAdoException) throw new SqlException(msg);
return msg;
}
var connect = Connect();
if (connect.NotEmpty()) return $"无法启动事务:连接失败。(${connect})";
try
{
_transaction = isolation ? _conn.BeginTransaction(isolation.Value) : _conn.BeginTransaction();
_autocommit = commit;
return null;
}
catch (Exception ex)
{
Logger.Error(this, "Begin", ex.Message());
if (ThrowAdoException) throw;
return ex.Message();
}
}
///
/// 使用默认的事务锁定行为启动事务。
/// Chaos
无法覆盖隔离级别更高的事务中的挂起的更改。
/// ReadCommitted
在正在读取数据时保持共享锁,以避免脏读,但是在事务结束之前可以更改数据,从而导致不可重复的读取或幻像数据。
/// ReadUncommitted
可以进行脏读,意思是说,不发布共享锁,也不接受独占锁。
/// RepeatableRead
在查询中使用的所有数据上放置锁,以防止其他用户更新这些数据。 防止不可重复的读取,但是仍可以有幻像行。
/// Serializable
在 System.Data.DataSet 上放置范围锁,以防止在事务完成之前由其他用户更新行或向数据集中插入行。
/// Snapshot
通过在一个应用程序正在修改数据时存储另一个应用程序可以读取的相同数据版本来减少阻止。 表示您无法从一个事务中看到在其他事务中进行的更改,即便重新查询也是如此。
/// Unspecified = -1
正在使用与指定隔离级别不同的隔离级别,但是无法确定该级别。
///
/// 在连接的生命周期结束时未结束事务,指定 TRUE 将自动提交,指定 FALSE 将自动回滚。
public virtual string Begin(bool commit = false) => Begin(commit, null);
///
/// 使用指定的事务锁定行为启动事务。
/// Chaos
无法覆盖隔离级别更高的事务中的挂起的更改。
/// ReadCommitted
在正在读取数据时保持共享锁,以避免脏读,但是在事务结束之前可以更改数据,从而导致不可重复的读取或幻像数据。
/// ReadUncommitted
可以进行脏读,意思是说,不发布共享锁,也不接受独占锁。
/// RepeatableRead
在查询中使用的所有数据上放置锁,以防止其他用户更新这些数据。 防止不可重复的读取,但是仍可以有幻像行。
/// Serializable
在 System.Data.DataSet 上放置范围锁,以防止在事务完成之前由其他用户更新行或向数据集中插入行。
/// Snapshot
通过在一个应用程序正在修改数据时存储另一个应用程序可以读取的相同数据版本来减少阻止。 表示您无法从一个事务中看到在其他事务中进行的更改,即便重新查询也是如此。
/// Unspecified = -1
正在使用与指定隔离级别不同的隔离级别,但是无法确定该级别。
///
/// 在连接的生命周期结束时未结束事务,指定 TRUE 将自动提交,指定 FALSE 将自动回滚。
/// 指定事务锁定行为,不指定时将使用默认值。
public virtual string Begin(bool commit, IsolationLevel isolation) => Begin(commit, new Class(isolation));
/// 提交事务。
public virtual string Commit()
{
if (_transaction == null)
{
if (ThrowAdoException) throw new SqlException(TransactionNotFound);
return TransactionNotFound;
}
try
{
_transaction.Commit();
RuntimeUtility.Dispose(_transaction);
_transaction = null;
return null;
}
catch (Exception ex)
{
RuntimeUtility.Dispose(_transaction);
_transaction = null;
Logger.Error(this, "Commit", ex.Message());
if (ThrowAdoException) throw;
return ex.Message();
}
}
/// 从挂起状态回滚事务。
public virtual string Rollback()
{
if (_transaction == null)
{
if (ThrowAdoException) throw new SqlException(TransactionNotFound);
return TransactionNotFound;
}
try
{
_transaction.Rollback();
RuntimeUtility.Dispose(_transaction);
_transaction = null;
return null;
}
catch (Exception ex)
{
RuntimeUtility.Dispose(_transaction);
_transaction = null;
Logger.Error(this, "Rollback", ex.Message());
if (ThrowAdoException) throw;
return ex.Message();
}
}
#endregion
#region ADO
/// 允许 ADO 抛出异常,取代返回错误信息。
/// FALSE(默认值)
public virtual bool ThrowAdoException { get; set; }
/// 查询。
/// SQL 语句。
/// 为 SQL 语句提供的参数。
public IQuery Query(string sql, IEnumerable parameters = null)
{
if (TextUtility.IsEmpty(sql)) return new Query(false, "语句无效。");
var connected = Connect();
if (connected.NotEmpty()) return new Query(false, connected);
lock (Locker)
{
try
{
using (var command = _conn.CreateCommand())
{
command.Connection = _conn;
if (_timeout != null) command.CommandTimeout = Timeout.Query;
command.CommandText = sql;
if (_transaction != null) command.Transaction = _transaction;
if (parameters != null)
{
foreach (var parameter in parameters)
{
if (parameter != null) command.Parameters.Add(parameter);
}
}
var ex = null as Exception;
var da = null as IDataAdapter;
try { da = CreateDataAdapter(command); }
catch (Exception adapterEx) { ex = adapterEx; }
if (ex != null)
{
Logger.Error(this, "Query", ex.Message, sql);
return new Query(ex);
}
using (var ds = new DataSet())
{
da.Fill(ds);
if (ds.Tables.Count > 0)
{
var tables = new DataTable[ds.Tables.Count];
ds.Tables.CopyTo(tables, 0);
Logger.Info(this, "Query", sql);
return new Query(tables, true);
}
else
{
Logger.Error(this, "Query", "查询结果不包含任何数据表。", sql);
return new Query(false, "查询结果不包含任何数据表。");
}
}
}
}
catch (Exception exception)
{
Logger.Error(this, "Query", exception.Message, sql);
if (ThrowAdoException) throw exception;
return new Query(exception);
}
}
}
/// 输出查询结果的首列数据。
///
protected string[] QueryStrings(string sql, string[] excluded = null)
{
if (Connect().NotEmpty()) return new string[0];
using (var query = Query(sql))
{
if (!query.Success) throw new SqlException(query, sql);
var rows = query.Rows;
var list = new List(rows);
for (int r = 0; r < query.Rows; r++)
{
var cell = query.Text(r, 0);
if (TextUtility.IsEmpty(cell)) continue;
if (excluded != null && excluded.Contains(cell)) continue;
list.Add(cell);
}
return list.ToArray();
}
}
/// 执行。
/// SQL 语句。
/// 为 SQL 语句提供的参数。
/// 自动启动事务。
public IExecute Execute(string sql, IEnumerable parameters = null, bool autoTransaction = false)
{
if (TextUtility.IsEmpty(sql)) return new Execute(false, "语句无效。");
lock (Locker)
{
var connected = Connect();
if (connected.NotEmpty()) return new Execute(false, connected);
var tempTran = _transaction == null && autoTransaction;
if (tempTran) Begin();
try
{
using (var command = _conn.CreateCommand())
{
command.Connection = _conn;
if (_transaction != null) command.Transaction = _transaction;
if (Timeout != null) command.CommandTimeout = Timeout.Execute;
command.CommandText = sql;
if (parameters != null)
{
foreach (var parameter in parameters)
{
if (parameter != null) command.Parameters.Add(parameter);
}
}
var rows = command.ExecuteNonQuery();
if (tempTran) Commit(); // todo 此处应该检查事务提交产生的错误。
Logger.Info(this, "Execute", sql);
return new Execute(true, rows);
}
}
catch (Exception exception)
{
Logger.Error(this, "Execute", exception.Message, sql);
if (tempTran) Rollback();
if (ThrowAdoException) throw exception;
return new Execute(exception);
}
}
}
/// 查询数据库中的所有表名。
public abstract string[] TableNames();
/// 查询数据库实例中的所有数据库名。
public abstract string[] StoreNames();
/// 查询表中的所有列名。
public abstract string[] ColumnNames(string tableName);
#endregion
#region Parameter
/// 创建参数。
///
public IDataParameter Parameter(string name, object value)
{
if (name.IsEmpty()) throw new ArgumentNullException(nameof(name));
var p = CreateParameter();
p.ParameterName = name;
p.Value = value ?? DBNull.Value;
return p;
}
/// 创建参数。
///
public IDataParameter Parameter(string name, object value, DbType type)
{
if (name.IsEmpty()) throw new ArgumentNullException(nameof(name));
var p = CreateParameter();
p.ParameterName = name;
p.Value = value ?? DBNull.Value;
p.DbType = type;
return p;
}
#endregion
#region ORM
/// 检查数据模型结构,存在异常时抛出异常。
///
///
protected static TableStructure Parse(Type model)
{
if (model == null) throw new ArgumentNullException(nameof(model), "数据模型类型无效。");
var ts = TableStructure.Parse(model);
if (ts == null) throw ModelException.InvalidStructure(model);
if (ts.TableName.IsEmpty()) throw ModelException.InvalidTableName(ts.Model);
if (ts.Key == null || ts.Key.Field.IsEmpty()) throw ModelException.MissingKey(ts.Model);
if (ts.Flag == null || ts.Flag.Field.IsEmpty()) throw ModelException.MissingFlag(ts.Model);
return ts;
}
/// 初始化指定类型,以创建表或增加字段。
/// 指定新的表名。
/// 错误信息。当成功时候返回空字符串。
public string Initialize(string table = null) where T : class, new() => Initialize(typeof(T), table);
/// 初始化指定类型,以创建表或增加字段。
/// 要初始化的类型。
/// 指定新的表名。
/// 错误信息。当成功时候返回空字符串。
public string Initialize(Type model, string table = null)
{
if (model == null) return "参数 model 无效。";
if (!model.IsClass) return $"类型 <{model.FullName}> 不是类。";
if (model.IsAbstract) return $"类型 <{model.FullName}> 是抽象类。";
if (!RuntimeUtility.CanNew(model)) return $"类型 <{model.FullName}> 无法创建新实例。";
var structure = TableStructure.Parse(model);
if (structure == null) return "无法解析记录模型。";
// 连接数据库。
var connect = Connect();
if (connect.NotEmpty()) return connect;
return Initialize(structure, table);
}
///
protected abstract string Initialize(TableStructure structure, string table = null);
/// 插入记录。
/// 要插入的记录实体。
/// 插入到指定表。当不指定时,由 record 类型决定。
/// 调整数据模型,补充缺少的属性。
/// 错误信息。当成功时候返回空字符串。
public abstract string Insert(object record, string table = null, bool adjust = true);
/// 使用指定语句查询,获取查询结果。
/// 目标记录的类型。
/// 要执行的 SQL 语句。
/// 为 SQL 语句提供的参数。
///
///
///
///
public object[] Query(Type model, string sql, IEnumerable parameters = null)
{
if (string.IsNullOrEmpty(sql)) throw new ArgumentNullException(nameof(sql), "SQL 语句无效。");
using (var query = Query(sql, parameters))
{
if (!query.Success) throw new SqlException(query, sql);
return SourceUtility.Fill(query, model);
}
}
/// 使用指定语句查询,获取查询结果。
/// 要执行的 SQL 语句。
/// 为 SQL 语句提供的参数。
///
///
///
///
public T[] Query(string sql, IEnumerable parameters = null) where T : class, new() => Query(typeof(T), sql, parameters).As