@@ -15,8 +15,11 @@ public class ColumnInfo
1515 }
1616 public class DbObject < T > where T : DbObject < T > , new ( )
1717 {
18- public string db_TableName ;
19- public List < ColumnInfo > db_Columns = new List < ColumnInfo > ( ) ;
18+ // ReSharper disable once StaticMemberInGenericType
19+ public static string DbTableName ;
20+
21+
22+ public List < ColumnInfo > DBColumns = new List < ColumnInfo > ( ) ;
2023
2124 public virtual object GetValue ( string key )
2225 {
@@ -34,11 +37,11 @@ public virtual int Insert(MySqlConnection connection)
3437 using ( var command = connection . CreateCommand ( ) )
3538 {
3639 command . CommandText = string . Format ( "INSERT INTO {0} ({1}) VALUES ({2})" ,
37- db_TableName ,
38- String . Join ( "," , db_Columns . Select ( c=> string . Format ( "`{0}`" , c . Name ) ) ) ,
39- String . Join ( "," , db_Columns . Select ( c=> "@" + c . Name ) ) ) ;
40+ DbTableName ,
41+ String . Join ( "," , DBColumns . Select ( c=> string . Format ( "`{0}`" , c . Name ) ) ) ,
42+ String . Join ( "," , DBColumns . Select ( c=> "@" + c . Name ) ) ) ;
4043
41- command . Parameters . AddRange ( db_Columns . Select ( c=> CreateParameter ( c ) ) . ToArray ( ) ) ;
44+ command . Parameters . AddRange ( DBColumns . Select ( c=> CreateParameter ( c ) ) . ToArray ( ) ) ;
4245 command . CommandType = CommandType . Text ;
4346 int cnt = command . ExecuteNonQuery ( ) ;
4447 return cnt ;
@@ -48,11 +51,11 @@ public virtual int Insert(MySqlConnection connection)
4851 public int Update ( MySqlConnection connection )
4952 {
5053 string command = string . Format ( @"UPDATE {0} SET {1} WHERE {2}" ,
51- db_TableName ,
52- String . Join ( "," , db_Columns . Where ( c => ! c . IsKey ) . Select ( c => string . Format ( "`{0}` = @{0}" , c . Name ) ) ) ,
53- String . Join ( " AND " , db_Columns . Where ( c => c . IsKey ) . Select ( c => string . Format ( "`{0}` = @{0}" , c . Name ) ) ) ) ;
54+ DbTableName ,
55+ String . Join ( "," , DBColumns . Where ( c => ! c . IsKey ) . Select ( c => string . Format ( "`{0}` = @{0}" , c . Name ) ) ) ,
56+ String . Join ( " AND " , DBColumns . Where ( c => c . IsKey ) . Select ( c => string . Format ( "`{0}` = @{0}" , c . Name ) ) ) ) ;
5457
55- var parameters = db_Columns . Select ( c =>
58+ var parameters = DBColumns . Select ( c =>
5659 CreateParameter ( c ) ) . ToArray ( ) ;
5760
5861 return ExecuteCommand ( connection , command , parameters ) ;
@@ -63,31 +66,29 @@ public static T SelectByKey(MySqlConnection connection, object id)
6366 {
6467 var t = new T ( ) ;
6568
66- var key = t . db_Columns . Where ( c => c . IsKey ) . FirstOrDefault ( ) ;
69+ var key = t . DBColumns . FirstOrDefault ( c => c . IsKey ) ;
6770 if ( key == null )
6871 {
69- throw new Exception ( string . Format ( "Key for table {0} isn't defined." , t . db_TableName ) ) ;
72+ throw new Exception ( string . Format ( "Key for table {0} isn't defined." , DbTableName ) ) ;
7073 }
7174
72- string selectText = string . Format ( "SELECT * FROM {0} WHERE `{1}` = @p_id" , t . db_TableName , key . Name . ToUpper ( ) ) ;
73- var p_id = new MySqlParameter ( "p_id" , key . Type ) ;
74- p_id . Value = ConvertToDBCompatibilityType ( id ) ;
75+ string selectText = string . Format ( "SELECT * FROM {0} WHERE `{1}` = @p_id" , DbTableName , key . Name . ToUpper ( ) ) ;
76+ var pId = new MySqlParameter ( "p_id" , key . Type ) { Value = ConvertToDBCompatibilityType ( id ) } ;
7577
76- return Select ( connection , selectText , p_id ) . FirstOrDefault ( ) ;
78+ return Select ( connection , selectText , pId ) . FirstOrDefault ( ) ;
7779 }
7880
7981 public static int Delete ( MySqlConnection connection , object id , MySqlTransaction transaction = null )
8082 {
8183 var t = new T ( ) ;
82- var key = t . db_Columns . Where ( c => c . IsKey ) . FirstOrDefault ( ) ;
84+ var key = t . DBColumns . FirstOrDefault ( c => c . IsKey ) ;
8385 if ( key == null )
84- throw new Exception ( string . Format ( "Key for table {0} isn't defined." , t . db_TableName ) ) ;
86+ throw new Exception ( string . Format ( "Key for table {0} isn't defined." , DbTableName ) ) ;
8587
86- var p_id = new MySqlParameter ( "p_id" , key . Type ) ;
87- p_id . Value = ConvertToDBCompatibilityType ( id ) ;
88+ var pId = new MySqlParameter ( "p_id" , key . Type ) { Value = ConvertToDBCompatibilityType ( id ) } ;
8889
8990 return ExecuteCommand ( connection ,
90- string . Format ( "DELETE FROM {0} WHERE `{1}` = @p_id" , t . db_TableName , key . Name . ToUpper ( ) ) , transaction , p_id ) ;
91+ string . Format ( "DELETE FROM {0} WHERE `{1}` = @p_id" , DbTableName , key . Name . ToUpper ( ) ) , transaction , pId ) ;
9192 }
9293 public static int ExecuteCommand ( MySqlConnection connection , string commandText ,
9394 params MySqlParameter [ ] parameters )
@@ -131,7 +132,7 @@ public static T[] Select(MySqlConnection connection, string commandText, params
131132 command . CommandType = CommandType . Text ;
132133 command . Parameters . AddRange ( parameters ) ;
133134
134- DataTable dt = new DataTable ( ) ;
135+ var dt = new DataTable ( ) ;
135136 using ( var oda = new MySqlDataAdapter ( command ) )
136137 {
137138 oda . Fill ( dt ) ;
@@ -142,7 +143,7 @@ public static T[] Select(MySqlConnection connection, string commandText, params
142143 foreach ( DataRow row in dt . Rows )
143144 {
144145 T item = new T ( ) ;
145- foreach ( var c in item . db_Columns )
146+ foreach ( var c in item . DBColumns )
146147 item . SetValue ( c . Name , row [ c . Name . ToUpper ( ) ] ) ;
147148 res . Add ( item ) ;
148149 }
@@ -161,8 +162,7 @@ public static object ConvertToDBCompatibilityType(object obj)
161162
162163 public virtual MySqlParameter CreateParameter ( ColumnInfo c )
163164 {
164- var p = new MySqlParameter ( c . Name , c . Type ) ;
165- p . Value = GetValue ( c . Name ) ;
165+ var p = new MySqlParameter ( c . Name , c . Type ) { Value = GetValue ( c . Name ) } ;
166166 return p ;
167167 }
168168 }
0 commit comments