@ -1,4 +1,4 @@
/* 2021.10.14 */
/* 2021.11.07 */
using Apewer ;
using Apewer ;
using Apewer.Source ;
using Apewer.Source ;
@ -6,15 +6,14 @@ using System;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Data ;
using System.Data ;
using System.Data.Common ;
using System.Data.Common ;
using System.Data.SqlClient ;
using System.Net ;
using System.Net ;
using System.Text ;
using System.Text ;
using static Apewer . Source . OrmHelper ;
using static Apewer . Source . OrmHelper ;
using System.Data.SqlClient ;
#if NETFRAMEWORK
#if NETFRAMEWORK
using System.Data.Sql ;
using System.Data.Sql ;
#else
#endif
#endif
namespace Apewer.Source
namespace Apewer.Source
@ -48,10 +47,10 @@ namespace Apewer.Source
{
{
if ( timeout = = null ) timeout = Timeout . Default ;
if ( timeout = = null ) timeout = Timeout . Default ;
var a = TextUtility . AntiInject ( address ) ;
var a = address ? ? "" ;
var s = TextUtility . AntiInject ( store ) ;
var s = store ? ? "" ;
var u = TextUtility . AntiInject ( user ) ;
var u = user ? ? "" ;
var p = TextUtility . AntiInject ( pass ) ;
var p = pass ? ? "" ;
var cs = $"data source = {a}; initial catalog = {s}; " ;
var cs = $"data source = {a}; initial catalog = {s}; " ;
if ( string . IsNullOrEmpty ( u ) ) cs + = "integrated security = sspi; " ;
if ( string . IsNullOrEmpty ( u ) ) cs + = "integrated security = sspi; " ;
else
else
@ -88,7 +87,7 @@ namespace Apewer.Source
}
}
/// <summary>连接数据库,若未连接则尝试连接,获取连接成功的状态。</summary>
/// <summary>连接数据库,若未连接则尝试连接,获取连接成功的状态。</summary>
public bool Connect ( )
public string Connect ( )
{
{
if ( _d b = = null )
if ( _d b = = null )
{
{
@ -97,22 +96,22 @@ namespace Apewer.Source
}
}
else
else
{
{
if ( _d b . State = = ConnectionState . Open ) return true ;
if ( _d b . State = = ConnectionState . Open ) return null ;
}
}
try
try
{
{
_d b . Open ( ) ;
_d b . Open ( ) ;
switch ( _d b . State )
switch ( _d b . State )
{
{
case ConnectionState . Open : return true ;
case ConnectionState . Open : return null ;
default : return false ;
default : return $"连接失败,当前处于 {_db.State} 状态。" ;
}
}
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
Logger . Error ( nameof ( SqlClient ) , "Connection" , ex , _d b . ConnectionString ) ;
Logger . Error ( nameof ( SqlClient ) , "Connection" , ex , _d b . ConnectionString ) ;
Close ( ) ;
Close ( ) ;
return fals e;
return ex . Messag e;
}
}
}
}
@ -148,7 +147,8 @@ namespace Apewer.Source
/// <summary>启动事务。</summary>
/// <summary>启动事务。</summary>
public string Begin ( bool commit , Class < IsolationLevel > isolation )
public string Begin ( bool commit , Class < IsolationLevel > isolation )
{
{
if ( ! Connect ( ) ) return "未连接。" ;
var connect = Connect ( ) ;
if ( connect . NotEmpty ( ) ) return connect ;
if ( _ transaction ! = null ) return "存在已启动的事务,无法再次启动。" ;
if ( _ transaction ! = null ) return "存在已启动的事务,无法再次启动。" ;
try
try
{
{
@ -215,7 +215,7 @@ namespace Apewer.Source
{
{
if ( TextUtility . IsBlank ( sql ) ) return Example . InvalidQueryStatement ;
if ( TextUtility . IsBlank ( sql ) ) return Example . InvalidQueryStatement ;
var connected = Connect ( ) ;
var connected = Connect ( ) ;
if ( ! connected ) return Example . InvalidQueryConnection ;
if ( connected . NotEmpty ( ) ) return new Query ( false , connected ) ;
try
try
{
{
@ -259,7 +259,7 @@ namespace Apewer.Source
if ( TextUtility . IsBlank ( sql ) ) return Example . InvalidExecuteStatement ;
if ( TextUtility . IsBlank ( sql ) ) return Example . InvalidExecuteStatement ;
var connected = Connect ( ) ;
var connected = Connect ( ) ;
if ( ! connected ) return Example . InvalidExecuteConnection ;
if ( connected . NotEmpty ( ) ) return new Execute ( false , connected ) ;
var inTransaction = _ transaction ! = null ;
var inTransaction = _ transaction ! = null ;
if ( ! inTransaction ) Begin ( ) ;
if ( ! inTransaction ) Begin ( ) ;
@ -299,7 +299,7 @@ namespace Apewer.Source
public string [ ] TableNames ( )
public string [ ] TableNames ( )
{
{
var list = new List < string > ( ) ;
var list = new List < string > ( ) ;
if ( Connect ( ) )
if ( Connect ( ) . IsEmpty ( ) )
{
{
var sql = "select [name] from [sysobjects] where [type] = 'u' order by [name]; " ;
var sql = "select [name] from [sysobjects] where [type] = 'u' order by [name]; " ;
var query = ( Query ) Query ( sql ) ;
var query = ( Query ) Query ( sql ) ;
@ -318,7 +318,7 @@ namespace Apewer.Source
public string [ ] StoreNames ( )
public string [ ] StoreNames ( )
{
{
var list = new List < string > ( ) ;
var list = new List < string > ( ) ;
if ( Connect ( ) )
if ( Connect ( ) . IsEmpty ( ) )
{
{
var sql = "select [name] from [master]..[sysdatabases] order by [name]; " ;
var sql = "select [name] from [master]..[sysdatabases] order by [name]; " ;
var query = ( Query ) Query ( sql ) ;
var query = ( Query ) Query ( sql ) ;
@ -341,7 +341,7 @@ namespace Apewer.Source
public string [ ] ColumnNames ( string tableName )
public string [ ] ColumnNames ( string tableName )
{
{
var list = new List < string > ( ) ;
var list = new List < string > ( ) ;
if ( Connect ( ) )
if ( Connect ( ) . IsEmpty ( ) )
{
{
var table = TextUtility . AntiInject ( tableName ) ;
var table = TextUtility . AntiInject ( tableName ) ;
var sql = TextUtility . Merge ( "select [name] from [syscolumns] where [id] = object_id('" , table , "'); " ) ;
var sql = TextUtility . Merge ( "select [name] from [syscolumns] where [id] = object_id('" , table , "'); " ) ;
@ -357,6 +357,60 @@ namespace Apewer.Source
return list . ToArray ( ) ;
return list . ToArray ( ) ;
}
}
static string XType ( int xtype )
{
switch ( xtype )
{
case 3 4 : return "image" ;
case 3 5 : return "text" ;
case 3 6 : return "uniqueidentifier" ;
case 4 8 : return "tinyint" ;
case 5 2 : return "smallint" ;
case 5 6 : return "int" ;
case 5 8 : return "smalldatetime" ;
case 5 9 : return "real" ;
case 6 0 : return "money" ;
case 6 1 : return "datetime" ;
case 6 2 : return "float" ;
case 9 8 : return "sql_variant" ;
case 9 9 : return "ntext" ;
case 1 0 4 : return "bit" ;
case 1 0 6 : return "decimal" ;
case 1 0 8 : return "numeric" ;
case 1 2 2 : return "smallmoney" ;
case 1 2 7 : return "bigint" ;
case 1 6 5 : return "varbinary" ;
case 1 6 7 : return "varchar" ;
case 1 7 3 : return "binary" ;
case 1 7 5 : return "char" ;
case 1 8 9 : return "timestamp" ;
case 2 3 1 : return "nvarchar" ;
case 2 3 9 : return "nchar" ;
case 2 4 1 : return "xml" ;
}
return null ;
}
/// <summary>获取列信息。</summary>
public ColumnInfo [ ] ColumnsInfo ( string tableName )
{
if ( tableName . IsEmpty ( ) ) throw new ArgumentNullException ( nameof ( tableName ) ) ;
var sql = $"select name, xtype, length from syscolumns where id = object_id('{tableName}') " ;
using ( var query = Query ( sql ) )
{
var ab = new ArrayBuilder < ColumnInfo > ( ) ;
for ( var i = 0 ; i < query . Rows ; i + + )
{
var info = new ColumnInfo ( ) ;
info . Name = query . Text ( i , "name" ) ;
info . Type = XType ( query . Int32 ( i , "xtype" ) ) ;
info . Length = query . Int32 ( i , "length" ) ;
ab . Add ( info ) ;
}
return ab . Export ( ) ;
}
}
/// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
/// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
public string Initialize < T > ( ) where T : class , new ( ) = > Initialize ( typeof ( T ) ) ;
public string Initialize < T > ( ) where T : class , new ( ) = > Initialize ( typeof ( T ) ) ;
@ -367,7 +421,8 @@ namespace Apewer.Source
if ( structure = = null ) return "无法解析记录模型。" ;
if ( structure = = null ) return "无法解析记录模型。" ;
// 连接数据库。
// 连接数据库。
if ( ! Connect ( ) ) return "连接数据库失败。" ;
var connect = Connect ( ) ;
if ( connect . NotEmpty ( ) ) return connect ;
// 检查现存表。
// 检查现存表。
var exists = false ;
var exists = false ;
@ -411,8 +466,8 @@ namespace Apewer.Source
var lower = column . Field . ToLower ( ) ;
var lower = column . Field . ToLower ( ) ;
if ( columns . Contains ( lower ) ) continue ;
if ( columns . Contains ( lower ) ) continue ;
var type = GetColumn Declaration( column ) ;
var type = Declaration ( column ) ;
if ( type = = TextUtility . Empty ) return TextUtility . Merge ( "类型 " , column . Type . ToString ( ) , " 不受支持。" ) ;
if ( string . IsNullOrEmpty ( type ) ) return TextUtility . Merge ( "类型 " , column . Type . ToString ( ) , " 不受支持。" ) ;
var sql = TextUtility . Merge ( "alter table [" , structure . Name , "] add " , type , "; " ) ;
var sql = TextUtility . Merge ( "alter table [" , structure . Name , "] add " , type , "; " ) ;
var execute = Execute ( sql ) ;
var execute = Execute ( sql ) ;
@ -428,10 +483,10 @@ namespace Apewer.Source
// 检查 Independent 特性。
// 检查 Independent 特性。
if ( structure . Independent & & column . Independent ) continue ;
if ( structure . Independent & & column . Independent ) continue ;
var type = GetColumn Declaration( column ) ;
var type = Declaration ( column ) ;
if ( ! column . Independent & & column . Property . Name = = "Key" ) type = type + " primary key" ;
if ( ! column . Independent & & column . Property . Name = = "Key" ) type = type + " primary key" ;
if ( type = = TextUtility . Empty ) return TextUtility . Merge ( "类型 " , column . Type . ToString ( ) , " 不受支持。" ) ;
if ( string . IsNullOrEmpty ( type ) ) return TextUtility . Merge ( "类型 " , column . Type . ToString ( ) , " 不受支持。" ) ;
sqlcolumns . Add ( type ) ;
sqlcolumns . Add ( type ) ;
}
}
var sql = TextUtility . Merge ( "create table [" , structure . Name , "](" , string . Join ( ", " , sqlcolumns . ToArray ( ) ) , "); " ) ;
var sql = TextUtility . Merge ( "create table [" , structure . Name , "](" , string . Join ( ", " , sqlcolumns . ToArray ( ) ) , "); " ) ;
@ -442,24 +497,41 @@ namespace Apewer.Source
}
}
/// <summary>插入记录。返回错误信息。</summary>
/// <summary>插入记录。返回错误信息。</summary>
public string Insert ( object record )
public string Insert ( object record , string table = null )
{
{
if ( record = = null ) return "参数无效。" ;
if ( record = = null ) return "参数无效。" ;
FixProperties ( record ) ;
FixProperties ( record ) ;
var structure = TableStructure . Parse ( record . GetType ( ) ) ;
var structure = TableStructure . Parse ( record . GetType ( ) ) ;
if ( structure = = null ) return "无法解析记录模型。" ;
if ( structure = = null ) return "无法解析记录模型。" ;
if ( string . IsNullOrEmpty ( table ) ) table = structure . Name ;
if ( string . IsNullOrEmpty ( table ) ) return "表名称无效。" ;
var ps = structure . CreateParameters ( record , Parameter ) ;
var psc = ps . Length ;
if ( psc < 1 ) return "数据模型不包含字段。" ;
var names = new List < string > ( psc ) ;
var values = new List < string > ( psc ) ;
foreach ( var column in ps )
{
//names.Add(TextGenerator.Merge("[", column, "]"));
names . Add ( TextUtility . Merge ( column ) ) ;
values . Add ( "@" + column ) ;
}
var sb = new StringBuilder ( ) ;
sb . Append ( "insert into [" , table , "](" , string . Join ( ", " , names . ToArray ( ) ) , ") " ) ;
sb . Append ( "values(" , string . Join ( ", " , values . ToArray ( ) ) , "); " ) ;
var sql = sb . ToString ( ) ;
var parameters = structure . CreateParameters ( record , CreateDataParameter ) ;
var execute = Execute ( sql , ps ) ;
var sql = GenerateInsertStatement ( structure . Name , parameters ) ;
var execute = Execute ( sql , parameters ) ;
if ( execute . Success ) return TextUtility . Empty ;
if ( execute . Success ) return TextUtility . Empty ;
return execute . Message ;
return execute . Message ;
}
}
/// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
/// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
/// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
/// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
public string Update ( IRecord record )
public string Update ( IRecord record , string table = null )
{
{
if ( record = = null ) return "参数无效。" ;
if ( record = = null ) return "参数无效。" ;
FixProperties ( record ) ;
FixProperties ( record ) ;
@ -468,10 +540,23 @@ namespace Apewer.Source
var structure = TableStructure . Parse ( record . GetType ( ) ) ;
var structure = TableStructure . Parse ( record . GetType ( ) ) ;
if ( structure = = null ) return "无法解析记录模型。" ;
if ( structure = = null ) return "无法解析记录模型。" ;
if ( structure . Independent ) return "无法更新带有 Independent 特性的模型。" ;
if ( structure . Independent ) return "无法更新带有 Independent 特性的模型。" ;
if ( string . IsNullOrEmpty ( table ) ) table = structure . Name ;
if ( string . IsNullOrEmpty ( table ) ) return "表名称无效。" ;
var ps = structure . CreateParameters ( record , Parameter , "_key" ) ;
var psc = ps . Length ;
if ( psc < 1 ) return "数据模型不包含字段。" ;
var items = new List < string > ( ) ;
foreach ( var p in ps )
{
var pn = p . ParameterName ;
items . Add ( TextUtility . Merge ( "[" , pn , "] = @" , pn ) ) ;
}
var key = record . Key . SafeKey ( ) ;
var sql = TextUtility . Merge ( "update [" , table , "] set " , string . Join ( ", " , items . ToArray ( ) ) , " where [_key]='" , key , "'; " ) ;
var parameters = structure . CreateParameters ( record , CreateDataParameter , "_key" ) ;
var execute = Execute ( sql , ps ) ;
var sql = GenerateUpdateStatement ( structure . Name , record . Key , parameters ) ;
var execute = Execute ( sql , parameters ) ;
if ( execute . Success ) return TextUtility . Empty ;
if ( execute . Success ) return TextUtility . Empty ;
return execute . Message ;
return execute . Message ;
}
}
@ -480,7 +565,16 @@ namespace Apewer.Source
public Result < object [ ] > Query ( Type model , string sql ) = > OrmHelper . Query ( this , model , sql ) ;
public Result < object [ ] > Query ( Type model , string sql ) = > OrmHelper . Query ( this , model , sql ) ;
/// <summary>获取按指定语句查询到的所有记录。</summary>
/// <summary>获取按指定语句查询到的所有记录。</summary>
public Result < T [ ] > Query < T > ( string sql ) where T : class , new ( ) = > OrmHelper . Query < T > ( this , sql ) ;
public Result < T [ ] > Query < T > ( string sql ) where T : class , new ( )
{
var query = Query ( sql ) ;
if ( ! query . Success ) return new Result < T [ ] > ( query . Message ) ;
var records = query . Fill < T > ( ) ;
query . Dispose ( ) ;
var result = new Result < T [ ] > ( records ) ;
return result ;
}
/// <summary>获取记录。</summary>
/// <summary>获取记录。</summary>
public Result < object [ ] > Query ( Type model , long flag = 0 ) = > OrmHelper . Query ( this , model , ( tn ) = >
public Result < object [ ] > Query ( Type model , long flag = 0 ) = > OrmHelper . Query ( this , model , ( tn ) = >
@ -497,7 +591,7 @@ namespace Apewer.Source
} ) ;
} ) ;
/// <summary>获取具有指定 Key 的记录。</summary>
/// <summary>获取具有指定 Key 的记录。</summary>
public Result < IRecord > Get ( Type model , string key , long flag = 0 ) = > OrmHelper . Get ( this , model , key , ( tn , sk ) = >
public Result < object > Get ( Type model , string key , long flag = 0 ) = > OrmHelper . Get ( this , model , key , ( tn , sk ) = >
{
{
if ( flag = = 0 ) return $"select top 1 * from [{tn}] _key='{sk}'; " ;
if ( flag = = 0 ) return $"select top 1 * from [{tn}] _key='{sk}'; " ;
return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; " ;
return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; " ;
@ -524,7 +618,7 @@ namespace Apewer.Source
#region public static
#region public static
#if NETFRAMEWORK
#if NET20 || NET40
/// <summary>枚举本地网络中服务器的名称。</summary>
/// <summary>枚举本地网络中服务器的名称。</summary>
public static SqlServerSource [ ] EnumerateServer ( )
public static SqlServerSource [ ] EnumerateServer ( )
@ -565,14 +659,14 @@ namespace Apewer.Source
/// <summary>创建参数。</summary>
/// <summary>创建参数。</summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="InvalidOperationException"></exception>
internal static SqlParameter CreateData Parameter( Parameter parameter )
static SqlParameter Parameter ( Parameter parameter )
{
{
if ( parameter = = null ) throw new InvalidOperationException ( "参数无效。" ) ;
if ( parameter = = null ) throw new InvalidOperationException ( "参数无效。" ) ;
return CreateData Parameter( parameter . Name , parameter . Type , parameter . Size , parameter . Value ) ;
return Parameter ( parameter . Name , parameter . Type , parameter . Size , parameter . Value ) ;
}
}
/// <summary>创建参数。</summary>
/// <summary>创建参数。</summary>
public static SqlParameter CreateData Parameter( string name , ColumnType type , Int32 size , object value )
public static SqlParameter Parameter ( string name , ColumnType type , int size , object value )
{
{
var vname = TextUtility . Trim ( name ) ;
var vname = TextUtility . Trim ( name ) ;
if ( TextUtility . IsBlank ( vname ) ) return null ;
if ( TextUtility . IsBlank ( vname ) ) return null ;
@ -600,7 +694,7 @@ namespace Apewer.Source
case ColumnType . NVarChar :
case ColumnType . NVarChar :
case ColumnType . NVarChar191 :
case ColumnType . NVarChar191 :
case ColumnType . NVarCharMax :
case ColumnType . NVarCharMax :
vtype = SqlDbType . VarChar ;
vtype = SqlDbType . N VarChar;
break ;
break ;
case ColumnType . Text :
case ColumnType . Text :
vtype = SqlDbType . Text ;
vtype = SqlDbType . Text ;
@ -645,7 +739,7 @@ namespace Apewer.Source
}
}
/// <summary>创建参数。</summary>
/// <summary>创建参数。</summary>
public static SqlParameter CreateData Parameter( S tring name , SqlDbType type , Int32 size , O bject value )
public static SqlParameter Parameter ( s tring name , SqlDbType type , int size , o bject value )
{
{
if ( value is string & & value ! = null & & size > 0 )
if ( value is string & & value ! = null & & size > 0 )
{
{
@ -661,7 +755,7 @@ namespace Apewer.Source
}
}
/// <summary>创建参数。</summary>
/// <summary>创建参数。</summary>
public static SqlParameter CreateData Parameter( S tring name , SqlDbType type , O bject value )
public static SqlParameter Parameter ( s tring name , SqlDbType type , o bject value )
{
{
var p = new SqlParameter ( ) ;
var p = new SqlParameter ( ) ;
p . ParameterName = name ? ? "" ;
p . ParameterName = name ? ? "" ;
@ -670,11 +764,7 @@ namespace Apewer.Source
return p ;
return p ;
}
}
#endregion
static string Declaration ( ColumnAttribute column )
#region private
static string GetColumnDeclaration ( ColumnAttribute column )
{
{
var type = TextUtility . Empty ;
var type = TextUtility . Empty ;
var vcolumn = column ;
var vcolumn = column ;
@ -709,7 +799,7 @@ namespace Apewer.Source
type = TextUtility . Merge ( "nvarchar(" , Math . Min ( 4 0 0 0 , length ) . ToString ( ) , ")" ) ;
type = TextUtility . Merge ( "nvarchar(" , Math . Min ( 4 0 0 0 , length ) . ToString ( ) , ")" ) ;
break ;
break ;
case ColumnType . NVarChar191 :
case ColumnType . NVarChar191 :
type = TextUtility . Merge ( "nvarchar(255 )" ) ;
type = TextUtility . Merge ( "nvarchar(191 )" ) ;
break ;
break ;
case ColumnType . NVarCharMax :
case ColumnType . NVarCharMax :
type = TextUtility . Merge ( "nvarchar(max)" ) ;
type = TextUtility . Merge ( "nvarchar(max)" ) ;
@ -723,123 +813,6 @@ namespace Apewer.Source
return TextUtility . Merge ( "[" , vcolumn . Field , "] " , type ) ;
return TextUtility . Merge ( "[" , vcolumn . Field , "] " , type ) ;
}
}
static string GetParameterName ( string parameter )
{
var name = TextUtility . AntiInject ( parameter , 2 5 5 ) ;
if ( name . StartsWith ( "@" ) & & name . Length > 1 )
{
name = name . Substring ( 1 , name . Length - 1 ) ;
}
return name ;
}
static string GetParameterName ( IDataParameter parameter )
{
var name = TextUtility . Empty ;
if ( parameter ! = null )
{
name = GetParameterName ( parameter . ParameterName ) ;
}
return name ;
}
static string [ ] GetParametersNames ( IEnumerable < IDataParameter > parameters )
{
var columns = new List < string > ( ) ;
if ( parameters ! = null )
{
foreach ( var parameter in parameters )
{
var name = GetParameterName ( parameter ) ;
var isblank = TextUtility . IsBlank ( name ) ;
if ( isblank ) continue ;
columns . Add ( name ) ;
}
}
return columns . ToArray ( ) ;
}
static string GenerateInsertStatement ( string table , string [ ] columns )
{
var result = TextUtility . Empty ;
var vtable = TextUtility . AntiInject ( table , 2 5 5 ) ;
if ( columns ! = null & & ! TextUtility . IsBlank ( vtable ) )
{
var count = 0 ;
var names = new List < string > ( ) ;
var values = new List < string > ( ) ;
foreach ( var column in columns )
{
//names.Add(TextGenerator.Merge("[", column, "]"));
names . Add ( TextUtility . Merge ( column ) ) ;
values . Add ( "@" + column ) ;
count + = 1 ;
}
var text = new StringBuilder ( ) ;
if ( count > 0 )
{
text . Append ( "insert into [" , vtable , "](" , string . Join ( ", " , names . ToArray ( ) ) , ") " ) ;
text . Append ( "values(" , string . Join ( ", " , values . ToArray ( ) ) , "); " ) ;
}
result = text . ToString ( ) ;
}
return result ;
}
static string GenerateUpdateStatement ( string table , string key , string [ ] columns )
{
var result = TextUtility . Empty ;
var vtable = TextUtility . AntiInject ( table , 2 5 5 ) ;
var vkey = TextUtility . AntiInject ( key , 2 5 5 ) ;
if ( columns ! = null & & ! TextUtility . IsBlank ( vtable ) & & ! TextUtility . IsBlank ( vkey ) )
{
var items = new List < string > ( ) ;
foreach ( var column in columns )
{
items . Add ( TextUtility . Merge ( "[" , column , "]=@" , column ) ) ;
}
if ( items . Count > 0 )
{
result = TextUtility . Merge ( "update [" , vtable , "] set " , string . Join ( ", " , items . ToArray ( ) ) , " where [_key]='" , vkey , "'; " ) ;
}
}
return result ;
}
/// <summary>生成 INSERT INTO 语句。表名必须有效,无有效参数时将获取空结果。</summary>
/// <exception cref="System.ArgumentException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
static string GenerateInsertStatement ( string table , IEnumerable < IDataParameter > parameters )
{
if ( table = = null ) throw new ArgumentNullException ( nameof ( table ) ) ;
var tableName = TextUtility . AntiInject ( table , 2 5 5 ) ;
if ( TextUtility . IsBlank ( tableName ) ) throw new ArgumentException ( "表名无效。" , nameof ( table ) ) ;
var vcolumns = GetParametersNames ( parameters ) ;
if ( vcolumns . Length < 1 ) return TextUtility . Empty ;
return GenerateInsertStatement ( tableName , vcolumns ) ;
}
/// <summary>生成 UPDATE 语句,键字段名为“_key”。表名必须有效,键值必须有效,无有效参数时将获取空结果。</summary>
/// <exception cref="System.ArgumentException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
static string GenerateUpdateStatement ( string table , string key , IEnumerable < IDataParameter > parameters )
{
if ( table = = null ) throw new ArgumentNullException ( nameof ( table ) ) ;
var t = TextUtility . AntiInject ( table , 2 5 5 ) ;
if ( TextUtility . IsBlank ( t ) ) throw new ArgumentException ( "表名无效。" , nameof ( table ) ) ;
if ( key = = null ) throw new ArgumentNullException ( "argKey" ) ;
var k = TextUtility . AntiInject ( key , 2 5 5 ) ;
if ( TextUtility . IsBlank ( k ) ) throw new ArgumentException ( "键值无效。" , nameof ( table ) ) ;
var columes = GetParametersNames ( parameters ) ;
if ( columes . Length < 1 ) return TextUtility . Empty ;
return GenerateUpdateStatement ( t , k , columes ) ;
}
#endregion
#endregion
}
}