You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
9.2 KiB
223 lines
9.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Apewer.Source
|
|
{
|
|
|
|
/// <summary>ORM 帮助程序。</summary>
|
|
public static class OrmHelper
|
|
{
|
|
|
|
private static List<T> Convert<T>(List<IRecord> input) where T : IRecord
|
|
{
|
|
if (input == null) return null;
|
|
var output = new List<T>(input.Count);
|
|
foreach (var record in input)
|
|
{
|
|
if (record == null) continue;
|
|
var t = (T)record;
|
|
output.Add(t);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
private static Result<T> Convert<T>(Result<IRecord> input) where T : IRecord
|
|
{
|
|
if (input == null) return null;
|
|
var result = new Result<T>();
|
|
result.Code = input.Code;
|
|
result.Exception = input.Exception;
|
|
result.Message = input.Message;
|
|
if (input.HasEntity) result.Entity = (T)input.Entity;
|
|
return result;
|
|
}
|
|
|
|
private static Result<List<T>> Convert<T>(Result<List<IRecord>> input) where T : IRecord
|
|
{
|
|
if (input == null) return null;
|
|
var result = new Result<List<T>>();
|
|
result.Code = input.Code;
|
|
result.Exception = input.Exception;
|
|
result.Message = input.Message;
|
|
result.Entity = Convert<T>(input.Entity);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>查询记录。</summary>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="model">记录模型。</param>
|
|
/// <param name="sql">SQL 语句。</param>
|
|
public static Result<List<IRecord>> Query(IDatabase database, Type model, string sql)
|
|
{
|
|
if (database == null) return Result<List<IRecord>>.Error("数据库无效。");
|
|
if (model == null) return Result<List<IRecord>>.Error("模型类型无效。");
|
|
if (string.IsNullOrEmpty(sql)) return Result<List<IRecord>>.Error("SQL 语句无效。");
|
|
using (var query = database.Query(sql) as Query)
|
|
{
|
|
if (query == null) return Result<List<IRecord>>.Error("查询实例无效。");
|
|
if (query.Exception != null) return Result<List<IRecord>>.Error(query.Exception);
|
|
try
|
|
{
|
|
var list = query.Fill(model);
|
|
return new Result<List<IRecord>>(list);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<List<IRecord>>.Error(ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>查询记录。</summary>
|
|
/// <typeparam name="T">记录模型。</typeparam>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="sql">SQL 语句。</param>
|
|
public static Result<List<T>> Query<T>(IDatabase database, string sql) where T : IRecord
|
|
{
|
|
return Convert<T>(Query(database, typeof(T), sql));
|
|
}
|
|
|
|
/// <summary>查询记录。</summary>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="model">记录模型。</param>
|
|
/// <param name="sqlGetter">生成 SQL 语句的函数,传入参数为表名。</param>
|
|
public static Result<List<IRecord>> Query(IDatabase database, Type model, Func<string, string> sqlGetter)
|
|
{
|
|
if (sqlGetter == null) return Result<List<IRecord>>.Error("SQL 语句获取函数无效。");
|
|
try
|
|
{
|
|
var tableName = TableStructure.ParseModel(model).Table;
|
|
if (string.IsNullOrEmpty(tableName)) return Result<List<IRecord>>.Error("表名无效。");
|
|
return Query(database, model, sqlGetter(tableName));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<List<IRecord>>.Error(ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>查询记录。</summary>
|
|
/// <typeparam name="T">记录模型。</typeparam>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="sqlGetter">生成 SQL 语句的函数,传入参数为表名。</param>
|
|
public static Result<List<T>> Query<T>(IDatabase database, Func<string, string> sqlGetter) where T : IRecord
|
|
{
|
|
return Convert<T>(Query(database, typeof(T), sqlGetter));
|
|
}
|
|
|
|
/// <summary>获取具有指定主键的记录。</summary>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="model">记录模型。</param>
|
|
/// <param name="key">主键。</param>
|
|
/// <param name="sqlGetter">生成 SQL 语句的函数,传入参数为表名和主键值。</param>
|
|
public static Result<IRecord> Get(IDatabase database, Type model, string key, Func<string, string, string> sqlGetter)
|
|
{
|
|
if (sqlGetter == null) return Result<IRecord>.Error("SQL 语句获取函数无效。");
|
|
|
|
var safetyKey = TextUtility.SafeKey(key);
|
|
if (string.IsNullOrEmpty(safetyKey)) return Result<IRecord>.Error("主键无效。");
|
|
|
|
string tableName;
|
|
try
|
|
{
|
|
tableName = TableStructure.ParseModel(model).Table;
|
|
if (string.IsNullOrEmpty(tableName)) return Result<IRecord>.Error("表名无效。");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new Result<IRecord>(ex);
|
|
}
|
|
|
|
var sql = sqlGetter(tableName, safetyKey);
|
|
var list = Query(database, model, sql);
|
|
var result = new Result<IRecord>();
|
|
result.Code = list.Code;
|
|
result.Exception = list.Exception;
|
|
result.Message = list.Message;
|
|
result.Entity = list.Entity.SafeFirst();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>获取具有指定主键的记录。</summary>
|
|
/// <typeparam name="T">记录模型。</typeparam>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="key">主键。</param>
|
|
/// <param name="sqlGetter">生成 SQL 语句的函数,传入参数为表名和主键值。</param>
|
|
public static Result<T> Get<T>(IDatabase database, string key, Func<string, string, string> sqlGetter) where T : IRecord
|
|
{
|
|
return Convert<T>(Get(database, typeof(T), key, sqlGetter));
|
|
}
|
|
|
|
/// <summary>获取主键。</summary>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="model">记录模型。</param>
|
|
/// <param name="sqlGetter">生成 SQL 语句的函数,传入参数为表名。</param>
|
|
public static Result<List<string>> Keys(IDatabase database, Type model, Func<string, string> sqlGetter)
|
|
{
|
|
if (database == null) return Result<List<string>>.Error("数据库无效。");
|
|
if (model == null) return Result<List<string>>.Error("模型类型无效。");
|
|
if (sqlGetter == null) return Result<List<string>>.Error("SQL 语句获取函数无效。");
|
|
|
|
var tableStructure = null as TableStructure;
|
|
try
|
|
{
|
|
tableStructure = TableStructure.ParseModel(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<List<string>>.Error(ex);
|
|
}
|
|
|
|
var tableName = tableStructure.Table;
|
|
if (string.IsNullOrEmpty(tableName)) return Result<List<string>>.Error("表名无效。");
|
|
|
|
// var keyName = null as string;
|
|
// foreach (var column in tableStructure.Columns)
|
|
// {
|
|
// if (column.Key == "Key")
|
|
// {
|
|
// keyName = column.Value.Field;
|
|
// break;
|
|
// }
|
|
// }
|
|
// if (string.IsNullOrEmpty(keyName)) return Result<List<string>>.Error("主键字段无效。");
|
|
|
|
// var sql = sqlGetter(tableName, keyName);
|
|
var sql = sqlGetter(tableName);
|
|
var query = null as IQuery;
|
|
try
|
|
{
|
|
query = database.Query(sql);
|
|
if (query == null) return Result<List<string>>.Error("查询实例无效。");
|
|
|
|
var list = new List<string>(query.Rows);
|
|
for (var r = 0; r < query.Rows; r++)
|
|
{
|
|
var key = TextUtility.SafeKey(query.Text(r));
|
|
if (string.IsNullOrEmpty(key)) continue;
|
|
list.Add(key);
|
|
}
|
|
query.Dispose();
|
|
list.Capacity = list.Count;
|
|
return new Result<List<string>>(list);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RuntimeUtility.Dispose(query);
|
|
return Result<List<string>>.Error(ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>获取主键。</summary>
|
|
/// <typeparam name="T">记录模型。</typeparam>
|
|
/// <param name="database">数据库对象。</param>
|
|
/// <param name="sqlGetter">生成 SQL 语句的函数,传入参数为表名。</param>
|
|
public static Result<List<string>> Keys<T>(IDatabase database, Func<string, string> sqlGetter) where T : IRecord
|
|
{
|
|
return Keys(database, typeof(T), sqlGetter);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|