Skip to content

Commit

Permalink
add function for database.
Browse files Browse the repository at this point in the history
  • Loading branch information
known committed Jan 16, 2018
1 parent 349dba2 commit e66162a
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 10 deletions.
11 changes: 11 additions & 0 deletions DataLite/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public Command(string text)
Parameters = new Dictionary<string, object>();
}

/// <summary>
/// 构造函数。
/// </summary>
/// <param name="text">SQL语句。</param>
/// <param name="parameters">SQL语句参数字典。</param>
public Command(string text, Dictionary<string, object> parameters)
{
Text = text;
Parameters = parameters;
}

/// <summary>
/// 取得SQL语句。
/// </summary>
Expand Down
63 changes: 59 additions & 4 deletions DataLite/CommandCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class CommandCache
/// </summary>
/// <param name="sql">SQL语句。</param>
/// <param name="param">语句参数。</param>
/// <returns></returns>
/// <returns>数据库命令。</returns>
public static Command GetCommand(string sql, object param = null)
{
var command = new Command(sql);
Expand All @@ -82,13 +82,13 @@ public static Command GetCommand(string sql, object param = null)
/// </summary>
/// <typeparam name="T">实体对象类型。</typeparam>
/// <param name="entity">实体对象。</param>
/// <returns></returns>
/// <returns>数据库命令。</returns>
public static Command GetSaveCommand<T>(T entity) where T : EntityBase
{
var type = typeof(T);
var tableName = GetCachedTableAttribute(type).TableName;
var columnInfos = GetCachedColumnInfos(type);

if (entity.IsNew)
{
var columnNames = columnInfos.Select(c => c.ColumnName);
Expand Down Expand Up @@ -127,7 +127,7 @@ public static Command GetSaveCommand<T>(T entity) where T : EntityBase
/// </summary>
/// <typeparam name="T">实体对象类型。</typeparam>
/// <param name="entity">实体对象。</param>
/// <returns></returns>
/// <returns>数据库命令。</returns>
public static Command GetDeleteCommand<T>(T entity) where T : EntityBase
{
var type = typeof(T);
Expand All @@ -142,6 +142,61 @@ public static Command GetDeleteCommand<T>(T entity) where T : EntityBase
return command;
}

/// <summary>
/// 根据表名及参数获取查询数据命令。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="parameters">命令参数字典。</param>
/// <returns>数据库命令。</returns>
public static Command GetSelectCommand(string tableName, Dictionary<string, object> parameters)
{
var whereSql = string.Join(" and ", parameters.Keys.Select(k => string.Format("{0}=@{0}", k)));
var text = string.Format("select * from {0} where {1}", tableName, whereSql);
return new Command(text, parameters);
}

/// <summary>
/// 根据表名及参数获取插入数据命令。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="parameters">命令参数字典。</param>
/// <returns>数据库命令。</returns>
public static Command GetInsertCommand(string tableName, Dictionary<string, object> parameters)
{
var columnSql = string.Join(",", parameters.Keys.Select(k => k));
var valueSql = string.Join(",", parameters.Keys.Select(k => string.Format("@{0}", k)));
var text = string.Format("insert into {0}({1}) values({2})", tableName, columnSql, valueSql);
return new Command(text, parameters);
}

/// <summary>
/// 根据表名及参数获取修改数据命令。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="keyFields">主键字段名,多个用“,”分割。</param>
/// <param name="parameters">命令参数字典。</param>
/// <returns>数据库命令。</returns>
public static Command GetUpdateCommand(string tableName, string keyFields, Dictionary<string, object> parameters)
{
var columnSql = string.Join(",", parameters.Keys.Where(k => !keyFields.Contains(k)).Select(k => string.Format("{0}=@{0}", k)));
var whereSql = string.Join(" and ", keyFields.Split(',').Select(k => string.Format("{0}=@{0}", k)));
var text = string.Format("update {0} set {1} where {2}", tableName, columnSql, whereSql);
return new Command(text, parameters);
}

/// <summary>
/// 根据表名及参数获取删除数据命令。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="parameters">命令参数字典。</param>
/// <returns>数据库命令。</returns>
public static Command GetDeleteCommand(string tableName, Dictionary<string, object> parameters)
{
var whereSql = string.Format(" and ", parameters.Keys.Select(k => string.Format("{0}=@{0}", k)));
var text = string.Format("delete from {0} where {1}", tableName, whereSql);
return new Command(text, parameters);
}

private static IEnumerable<ColumnInfo> GetCachedColumnInfos(Type type)
{
if (TypeColumnNames.TryGetValue(type.TypeHandle, out IEnumerable<ColumnInfo> columns))
Expand Down
108 changes: 102 additions & 6 deletions DataLite/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ public T Scalar<T>(string sql, object param = null)
/// <returns>指定类型的单个对象。</returns>
public T Query<T>(string sql, object param = null) where T : EntityBase
{
var command = CommandCache.GetCommand(sql, param);
var data = database.Query(command);
if (data == null || data.Rows.Count == 0)
var row = QueryRow(sql, param);
if (row == null)
return default(T);

return GetEntity<T>(data.Rows[0]);
return GetEntity<T>(row);
}

/// <summary>
Expand All @@ -85,8 +84,7 @@ public T Query<T>(string sql, object param = null) where T : EntityBase
/// <returns>指定类型的对象列表。</returns>
public List<T> QueryList<T>(string sql, object param = null) where T : EntityBase
{
var command = CommandCache.GetCommand(sql, param);
var data = database.Query(command);
var data = QueryTable(sql, param);
if (data == null || data.Rows.Count == 0)
return null;

Expand Down Expand Up @@ -156,6 +154,104 @@ public void SubmitChanges()
}
}

/// <summary>
/// 将整表数据写入数据库,表名及栏位名需与数据库一致。
/// </summary>
/// <param name="table">数据表。</param>
public void WriteTable(DataTable table)
{
database.WriteTable(table);
}

/// <summary>
/// 执行查询SQL语句,返回数据表。
/// </summary>
/// <param name="sql">查询SQL语句。</param>
/// <param name="param">SQL语句参数。</param>
/// <returns>数据表。</returns>
public DataTable QueryTable(string sql, object param = null)
{
var command = CommandCache.GetCommand(sql, param);
return database.Query(command);
}

/// <summary>
/// 执行查询SQL语句,返回数据行。
/// </summary>
/// <param name="sql">查询SQL语句。</param>
/// <param name="param">SQL语句参数。</param>
/// <returns>数据行。</returns>
public DataRow QueryRow(string sql, object param = null)
{
var command = CommandCache.GetCommand(sql, param);
var data = database.Query(command);
if (data == null || data.Rows.Count == 0)
return null;

return data.Rows[0];
}

/// <summary>
/// 根据表名及参数查询数据表。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="parameters">命令参数字典。</param>
/// <returns>数据表。</returns>
public DataTable SelectTable(string tableName, Dictionary<string, object> parameters)
{
var command = CommandCache.GetSelectCommand(tableName, parameters);
return database.Query(command);
}

/// <summary>
/// 根据表名及参数查询数据行。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="parameters">命令参数字典。</param>
/// <returns>数据行。</returns>
public DataRow SelectRow(string tableName, Dictionary<string, object> parameters)
{
var data = SelectTable(tableName, parameters);
if (data == null || data.Rows.Count == 0)
return null;

return data.Rows[0];
}

/// <summary>
/// 根据表名及参数插入数据。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="parameters">命令参数字典。</param>
public void Insert(string tableName, Dictionary<string, object> parameters)
{
var command = CommandCache.GetInsertCommand(tableName, parameters);
commands.Add(command);
}

/// <summary>
/// 根据表名及参数修改数据。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="keyFields">主键字段名,多个用“,”分割。</param>
/// <param name="parameters">命令参数字典。</param>
public void Update(string tableName, string keyFields, Dictionary<string, object> parameters)
{
var command = CommandCache.GetUpdateCommand(tableName, keyFields, parameters);
commands.Add(command);
}

/// <summary>
/// 根据表名及参数删除数据。
/// </summary>
/// <param name="tableName">表名。</param>
/// <param name="parameters">命令参数字典。</param>
public void Delete(string tableName, Dictionary<string, object> parameters)
{
var command = CommandCache.GetDeleteCommand(tableName, parameters);
commands.Add(command);
}

/// <summary>
/// 释放资源。
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions DataLite/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ public interface IDatabase
/// <param name="command">查询数据库命令。</param>
/// <returns>查询结果集。</returns>
DataTable Query(Command command);

/// <summary>
/// 将整表数据写入数据库,表名及栏位名需与数据库一致。
/// </summary>
/// <param name="table">数据表。</param>
void WriteTable(DataTable table);
}
}
9 changes: 9 additions & 0 deletions DataLite/MySqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ public DataTable Query(Command command)
}
}

/// <summary>
/// 将整表数据写入数据库,表名及栏位名需与数据库一致。
/// </summary>
/// <param name="table">数据表。</param>
public void WriteTable(DataTable table)
{
throw new NotSupportedException();
}

private void PrepareCommand(MySqlCommand cmd, MySqlTransaction trans, Command command, CommandType cmdType = CommandType.Text)
{
if (trans != null)
Expand Down
18 changes: 18 additions & 0 deletions DataLite/OracleDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ public DataTable Query(Command command)
}
}

/// <summary>
/// 将整表数据写入数据库,表名及栏位名需与数据库一致。
/// </summary>
/// <param name="table">数据表。</param>
public void WriteTable(DataTable table)
{
var bulkCopy = new OracleBulkCopy(ConnectionString, OracleBulkCopyOptions.UseInternalTransaction)
{
BulkCopyTimeout = 300,
DestinationTableName = table.TableName
};
foreach (DataColumn item in table.Columns)
{
bulkCopy.ColumnMappings.Add(item.ColumnName, item.ColumnName);
}
bulkCopy.WriteToServer(table);
}

private void PrepareCommand(OracleCommand cmd, OracleTransaction trans, Command command, CommandType cmdType = CommandType.Text)
{
if (trans != null)
Expand Down
9 changes: 9 additions & 0 deletions DataLite/SQLiteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ public DataTable Query(Command command)
}
}

/// <summary>
/// 将整表数据写入数据库,表名及栏位名需与数据库一致。
/// </summary>
/// <param name="table">数据表。</param>
public void WriteTable(DataTable table)
{
throw new NotSupportedException();
}

private void PrepareCommand(SQLiteCommand cmd, SQLiteTransaction trans, Command command, CommandType cmdType = CommandType.Text)
{
if (trans != null)
Expand Down
25 changes: 25 additions & 0 deletions DataLite/SqlServerDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ public DataTable Query(Command command)
}
}

/// <summary>
/// 将整表数据写入数据库,表名及栏位名需与数据库一致。
/// </summary>
/// <param name="table">数据表。</param>
public void WriteTable(DataTable table)
{
var bulkCopy = new SqlBulkCopy(ConnectionString, SqlBulkCopyOptions.UseInternalTransaction)
{
BulkCopyTimeout = 300,
DestinationTableName = table.TableName
};
var dt = new DataTable(table.TableName);
using (var adapter = new SqlDataAdapter($"select top 0 * from {table.TableName}", ConnectionString))
{
adapter.Fill(dt);
}
foreach (DataColumn item in table.Columns)
{
var sourceColumn = item.ColumnName;
var destinationColumn = dt.Columns[sourceColumn].ColumnName;
bulkCopy.ColumnMappings.Add(sourceColumn, destinationColumn);
}
bulkCopy.WriteToServer(table);
}

private void PrepareCommand(SqlCommand cmd, SqlTransaction trans, Command command, CommandType cmdType = CommandType.Text)
{
if (trans != null)
Expand Down

0 comments on commit e66162a

Please sign in to comment.