44using System . Linq ;
55using System . Data . SqlClient ;
66using System . Reflection ;
7+ using System . Threading . Tasks ;
78
89namespace OptimaJet . Workflow . DbPersistence
910{
@@ -13,6 +14,7 @@ public class ColumnInfo
1314 public SqlDbType Type = SqlDbType . NVarChar ;
1415 public bool IsKey = false ;
1516 public int Size = 256 ;
17+ public bool IsVirtual = false ;
1618 }
1719
1820 public abstract class DbObject
@@ -48,36 +50,46 @@ public virtual void SetValue(string key, object value)
4850 }
4951
5052 #region Command Insert/Update/Delete/Commit
51- public virtual int Insert ( SqlConnection connection )
53+
54+ public int Insert ( SqlConnection connection )
5255 {
53- using ( var command = connection . CreateCommand ( ) )
54- {
55- command . CommandText = string . Format ( "INSERT INTO {0} ({1}) VALUES ({2})" ,
56- ObjectName ,
57- String . Join ( "," , DbColumns . Select ( c=> string . Format ( "[{0}]" , c . Name ) ) ) ,
58- String . Join ( "," , DbColumns . Select ( c=> "@" + c . Name ) ) ) ;
59-
60- command . Parameters . AddRange ( DbColumns . Select ( CreateParameter ) . ToArray ( ) ) ;
61- command . CommandType = CommandType . Text ;
62- int cnt = command . ExecuteNonQuery ( ) ;
63- return cnt ;
64- }
56+ return InsertAsync ( connection ) . Result ;
57+ }
58+
59+ public Task < int > InsertAsync ( SqlConnection connection )
60+ {
61+ var commandText = string . Format ( "INSERT INTO {0} ({1}) VALUES ({2})" ,
62+ ObjectName ,
63+ String . Join ( "," , DbColumns . Where ( c=> ! c . IsVirtual ) . Select ( c => string . Format ( "[{0}]" , c . Name ) ) ) ,
64+ String . Join ( "," , DbColumns . Where ( c => ! c . IsVirtual ) . Select ( c => "@" + c . Name ) ) ) ;
65+ var parameters = DbColumns . Select ( CreateParameter ) . ToArray ( ) ;
66+ return ExecuteCommandAsync ( connection , commandText , parameters ) ;
6567 }
6668
6769 public int Update ( SqlConnection connection )
70+ {
71+ return UpdateAsync ( connection ) . Result ;
72+ }
73+
74+ public Task < int > UpdateAsync ( SqlConnection connection )
6875 {
6976 string command = string . Format ( @"UPDATE {0} SET {1} WHERE {2}" ,
70- ObjectName ,
71- String . Join ( "," , DbColumns . Where ( c => ! c . IsKey ) . Select ( c => string . Format ( "[{0}] = @{0}" , c . Name ) ) ) ,
72- String . Join ( " AND " , DbColumns . Where ( c => c . IsKey ) . Select ( c => string . Format ( "[{0}] = @{0}" , c . Name ) ) ) ) ;
77+ ObjectName ,
78+ String . Join ( "," , DbColumns . Where ( c => ! c . IsVirtual ) . Where ( c => ! c . IsKey ) . Select ( c => string . Format ( "[{0}] = @{0}" , c . Name ) ) ) ,
79+ String . Join ( " AND " , DbColumns . Where ( c => c . IsKey ) . Select ( c => string . Format ( "[{0}] = @{0}" , c . Name ) ) ) ) ;
7380
7481 var parameters = DbColumns . Select ( CreateParameter ) . ToArray ( ) ;
7582
76- return ExecuteCommand ( connection , command , parameters ) ;
83+ return ExecuteCommandAsync ( connection , command , parameters ) ;
7784
7885 }
7986
8087 public static T SelectByKey ( SqlConnection connection , object id )
88+ {
89+ return SelectByKeyAsync ( connection , id ) . Result ;
90+ }
91+
92+ public static async Task < T > SelectByKeyAsync ( SqlConnection connection , object id )
8193 {
8294 var t = new T ( ) ;
8395
@@ -90,10 +102,15 @@ public static T SelectByKey(SqlConnection connection, object id)
90102 string selectText = string . Format ( "SELECT * FROM {0} WHERE [{1}] = @p_id" , ObjectName , key . Name ) ;
91103 var pId = new SqlParameter ( "p_id" , key . Type ) { Value = ConvertToDbCompatibilityType ( id ) } ;
92104
93- return Select ( connection , selectText , pId ) . FirstOrDefault ( ) ;
105+ return ( await SelectAsync ( connection , selectText , pId ) . ConfigureAwait ( false ) ) . FirstOrDefault ( ) ;
94106 }
95107
96108 public static int Delete ( SqlConnection connection , object id , SqlTransaction transaction = null )
109+ {
110+ return DeleteAsync ( connection , id , transaction ) . Result ;
111+ }
112+
113+ public static Task < int > DeleteAsync ( SqlConnection connection , object id , SqlTransaction transaction = null )
97114 {
98115 var t = new T ( ) ;
99116 var key = t . DbColumns . FirstOrDefault ( c => c . IsKey ) ;
@@ -102,21 +119,33 @@ public static int Delete(SqlConnection connection, object id, SqlTransaction tra
102119
103120 var pId = new SqlParameter ( "p_id" , key . Type ) { Value = ConvertToDbCompatibilityType ( id ) } ;
104121
105- return ExecuteCommand ( connection ,
122+ return ExecuteCommandAsync ( connection ,
106123 string . Format ( "DELETE FROM {0} WHERE [{1}] = @p_id" , ObjectName , key . Name ) , transaction , pId ) ;
107124 }
108125
126+
109127 public static int ExecuteCommand ( SqlConnection connection , string commandText ,
110128 params SqlParameter [ ] parameters )
111129 {
112130 return ExecuteCommand ( connection , commandText , null , parameters ) ;
113131 }
132+
133+ public static Task < int > ExecuteCommandAsync ( SqlConnection connection , string commandText ,
134+ params SqlParameter [ ] parameters )
135+ {
136+ return ExecuteCommandAsync ( connection , commandText , null , parameters ) ;
137+ }
114138
115139 public static int ExecuteCommand ( SqlConnection connection , string commandText , SqlTransaction transaction = null , params SqlParameter [ ] parameters )
140+ {
141+ return ExecuteCommandAsync ( connection , commandText , transaction , parameters ) . Result ;
142+ }
143+
144+ public static async Task < int > ExecuteCommandAsync ( SqlConnection connection , string commandText , SqlTransaction transaction = null , params SqlParameter [ ] parameters )
116145 {
117146 if ( connection . State != ConnectionState . Open )
118147 {
119- connection . Open ( ) ;
148+ await connection . OpenAsync ( ) . ConfigureAwait ( false ) ;
120149 }
121150
122151 using ( var command = connection . CreateCommand ( ) )
@@ -129,16 +158,22 @@ public static int ExecuteCommand(SqlConnection connection, string commandText, S
129158 command . CommandText = commandText ;
130159 command . CommandType = CommandType . Text ;
131160 command . Parameters . AddRange ( parameters ) ;
132- var cnt = command . ExecuteNonQuery ( ) ;
161+ var cnt = await command . ExecuteNonQueryAsync ( ) . ConfigureAwait ( false ) ;
133162 return cnt ;
134163 }
135164 }
136165
166+
137167 public static T [ ] Select ( SqlConnection connection , string commandText , params SqlParameter [ ] parameters )
168+ {
169+ return SelectAsync ( connection , commandText , parameters ) . Result ;
170+ }
171+
172+ public static async Task < T [ ] > SelectAsync ( SqlConnection connection , string commandText , params SqlParameter [ ] parameters )
138173 {
139174 if ( connection . State != ConnectionState . Open )
140175 {
141- connection . Open ( ) ;
176+ await connection . OpenAsync ( ) . ConfigureAwait ( false ) ;
142177 }
143178
144179 using ( var command = connection . CreateCommand ( ) )
@@ -148,10 +183,10 @@ public static T[] Select(SqlConnection connection, string commandText, params Sq
148183 command . CommandType = CommandType . Text ;
149184 command . Parameters . AddRange ( parameters ) ;
150185 var res = new List < T > ( ) ;
151- #if NETCOREAPP
152- using ( var reader = command . ExecuteReader ( ) )
186+
187+ using ( var reader = await command . ExecuteReaderAsync ( ) . ConfigureAwait ( false ) )
153188 {
154- while ( reader . Read ( ) )
189+ while ( await reader . ReadAsync ( ) . ConfigureAwait ( false ) )
155190 {
156191 T item = new T ( ) ;
157192 for ( int i = 0 ; i < reader . FieldCount ; i ++ )
@@ -163,28 +198,16 @@ public static T[] Select(SqlConnection connection, string commandText, params Sq
163198 item . SetValue ( column . Name , reader . IsDBNull ( i ) ? null : reader . GetValue ( i ) ) ;
164199 }
165200 }
201+
166202 res . Add ( item ) ;
167203 }
168204 }
169- #else
170- DataTable dt = new DataTable ( ) ;
171- using ( var oda = new SqlDataAdapter ( command ) )
172- {
173- oda . Fill ( dt ) ;
174- }
175-
176- foreach ( DataRow row in dt . Rows )
177- {
178- T item = new T ( ) ;
179- foreach ( var c in item . DbColumns )
180- item . SetValue ( c . Name , row [ c . Name ] ) ;
181- res . Add ( item ) ;
182- }
183- #endif
184205 return res . ToArray ( ) ;
185206 }
186207 }
187- #endregion
208+
209+
210+ #endregion
188211
189212 public static object ConvertToDbCompatibilityType ( object obj )
190213 {
0 commit comments