15 changed files with 502 additions and 431 deletions
			
			
		@ -1,38 +0,0 @@ | 
				
			|||
<Project Sdk="Microsoft.NET.Sdk"> | 
				
			|||
 | 
				
			|||
	<PropertyGroup> | 
				
			|||
		<OutputType>Exe</OutputType> | 
				
			|||
		<!--<TargetFrameworks>net20</TargetFrameworks>--> | 
				
			|||
		<!--<TargetFrameworks>net40</TargetFrameworks>--> | 
				
			|||
		<!--<TargetFrameworks>net461</TargetFrameworks>--> | 
				
			|||
		<TargetFrameworks>net461</TargetFrameworks> | 
				
			|||
	</PropertyGroup> | 
				
			|||
 | 
				
			|||
	<!-- .NET Framework 4.6.1 --> | 
				
			|||
	<PropertyGroup Condition="'$(TargetFramework)'=='net461'"> | 
				
			|||
		<AssemblyTitle>$(AssemblyName) - .NET Framework 4.6.1</AssemblyTitle> | 
				
			|||
		<DefineConstants>NETFX;NET461;NET45;HAVE_ASYNC;HAVE_BIG_INTEGER;$(DefineConstants);$(AdditionalConstants)</DefineConstants> | 
				
			|||
	</PropertyGroup> | 
				
			|||
	<ItemGroup Condition="'$(TargetFramework)'=='net461'"> | 
				
			|||
		<Reference Include="CustomMarshalers" /> | 
				
			|||
		<Reference Include="Microsoft.CSharp" /> | 
				
			|||
		<Reference Include="PresentationCore" /> | 
				
			|||
		<Reference Include="System.Core" /> | 
				
			|||
		<Reference Include="System.Configuration" /> | 
				
			|||
		<Reference Include="System.Design" /> | 
				
			|||
		<Reference Include="System.Drawing" /> | 
				
			|||
		<Reference Include="System.Management" /> | 
				
			|||
		<Reference Include="System.Runtime.Caching" /> | 
				
			|||
		<Reference Include="System.Security" /> | 
				
			|||
		<Reference Include="System.Speech" /> | 
				
			|||
		<Reference Include="System.Transactions" /> | 
				
			|||
		<Reference Include="System.Web" /> | 
				
			|||
		<Reference Include="System.Windows.Forms" /> | 
				
			|||
		<Reference Include="WindowsBase" /> | 
				
			|||
	</ItemGroup> | 
				
			|||
 | 
				
			|||
	<ItemGroup> | 
				
			|||
		<ProjectReference Include="..\Apewer\Apewer.csproj" /> | 
				
			|||
	</ItemGroup> | 
				
			|||
 | 
				
			|||
</Project> | 
				
			|||
@ -1,11 +0,0 @@ | 
				
			|||
namespace Apewer.Run | 
				
			|||
{ | 
				
			|||
 | 
				
			|||
    class Program | 
				
			|||
    { | 
				
			|||
 | 
				
			|||
        static void Main(string[] args) => RuntimeUtility.InvokePublicClass(); | 
				
			|||
 | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,223 @@ | 
				
			|||
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); | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue