@@ -14,15 +14,23 @@ public class ColumnInfo
1414 public bool IsKey = false ;
1515 public int Size = 256 ;
1616 }
17- public class DbObject < T > where T : DbObject < T > , new ( )
17+
18+ public abstract class DbObject
1819 {
19- public DbObject ( )
20- {
20+ public static string SchemaName { get ; set ; }
21+ }
2122
23+ public class DbObject < T > : DbObject where T : DbObject < T > , new ( )
24+ {
25+ // ReSharper disable once StaticMemberInGenericType
26+ public static string DbTableName ;
27+
28+ public static string ObjectName
29+ {
30+ get { return string . Format ( "\" {0}\" .\" {1}\" " , SchemaName , DbTableName ) ; }
2231 }
2332
24- public string db_TableName ;
25- public List < ColumnInfo > db_Columns = new List < ColumnInfo > ( ) ;
33+ public List < ColumnInfo > DBColumns = new List < ColumnInfo > ( ) ;
2634
2735 public virtual object GetValue ( string key )
2836 {
@@ -39,12 +47,12 @@ public virtual int Insert(NpgsqlConnection connection)
3947 {
4048 using ( var command = connection . CreateCommand ( ) )
4149 {
42- command . CommandText = string . Format ( "INSERT INTO \" {0}\" ({1}) VALUES ({2})" ,
43- db_TableName ,
44- String . Join ( "," , db_Columns . Select ( c=> string . Format ( "\" {0}\" " , c . Name ) ) ) ,
45- String . Join ( "," , db_Columns . Select ( c=> "@" + c . Name ) ) ) ;
50+ command . CommandText = string . Format ( "INSERT INTO {0} ({1}) VALUES ({2})" ,
51+ ObjectName ,
52+ String . Join ( "," , DBColumns . Select ( c=> string . Format ( "\" {0}\" " , c . Name ) ) ) ,
53+ String . Join ( "," , DBColumns . Select ( c=> "@" + c . Name ) ) ) ;
4654
47- command . Parameters . AddRange ( db_Columns . Select ( c => CreateParameter ( c ) ) . ToArray ( ) ) ;
55+ command . Parameters . AddRange ( DBColumns . Select ( CreateParameter ) . ToArray ( ) ) ;
4856 command . CommandType = CommandType . Text ;
4957 int cnt = command . ExecuteNonQuery ( ) ;
5058 return cnt ;
@@ -53,13 +61,12 @@ public virtual int Insert(NpgsqlConnection connection)
5361
5462 public int Update ( NpgsqlConnection connection )
5563 {
56- string command = string . Format ( "UPDATE \" {0}\" SET {1} WHERE {2}" ,
57- db_TableName ,
58- String . Join ( "," , db_Columns . Where ( c => ! c . IsKey ) . Select ( c => string . Format ( "\" {0}\" = @{0}" , c . Name ) ) ) ,
59- String . Join ( " AND " , db_Columns . Where ( c => c . IsKey ) . Select ( c => string . Format ( "\" {0}\" = @{0}" , c . Name ) ) ) ) ;
64+ string command = string . Format ( "UPDATE {0} SET {1} WHERE {2}" ,
65+ ObjectName ,
66+ String . Join ( "," , DBColumns . Where ( c => ! c . IsKey ) . Select ( c => string . Format ( "\" {0}\" = @{0}" , c . Name ) ) ) ,
67+ String . Join ( " AND " , DBColumns . Where ( c => c . IsKey ) . Select ( c => string . Format ( "\" {0}\" = @{0}" , c . Name ) ) ) ) ;
6068
61- var parameters = db_Columns . Select ( c =>
62- CreateParameter ( c ) ) . ToArray ( ) ;
69+ var parameters = DBColumns . Select ( CreateParameter ) . ToArray ( ) ;
6370
6471 return ExecuteCommand ( connection , command , parameters ) ;
6572
@@ -69,30 +76,29 @@ public static T SelectByKey(NpgsqlConnection connection, object id)
6976 {
7077 var t = new T ( ) ;
7178
72- var key = t . db_Columns . FirstOrDefault ( c => c . IsKey ) ;
79+ var key = t . DBColumns . FirstOrDefault ( c => c . IsKey ) ;
7380 if ( key == null )
7481 {
75- throw new Exception ( string . Format ( "Key for table {0} isn't defined." , t . db_TableName ) ) ;
82+ throw new Exception ( string . Format ( "Key for table {0} isn't defined." , DbTableName ) ) ;
7683 }
7784
78- string selectText = string . Format ( "SELECT * FROM \" {0}\" WHERE \" {1}\" = @p_id" , t . db_TableName , key . Name ) ;
79- var p_id = new NpgsqlParameter ( "p_id" , key . Type ) ;
80- p_id . Value = id ;
85+ string selectText = string . Format ( "SELECT * FROM {0} WHERE \" {1}\" = @p_id" , ObjectName , key . Name ) ;
86+ var pId = new NpgsqlParameter ( "p_id" , key . Type ) { Value = id } ;
8187
82- return Select ( connection , selectText , p_id ) . FirstOrDefault ( ) ;
88+ return Select ( connection , selectText , pId ) . FirstOrDefault ( ) ;
8389 }
8490
8591 public static int Delete ( NpgsqlConnection connection , object id , NpgsqlTransaction transaction = null )
8692 {
8793 var t = new T ( ) ;
88- var key = t . db_Columns . FirstOrDefault ( c => c . IsKey ) ;
94+ var key = t . DBColumns . FirstOrDefault ( c => c . IsKey ) ;
8995 if ( key == null )
90- throw new Exception ( string . Format ( "Key for table {0} isn't defined." , t . db_TableName ) ) ;
96+ throw new Exception ( string . Format ( "Key for table {0} isn't defined." , DbTableName ) ) ;
9197
9298 var pId = new NpgsqlParameter ( "p_id" , key . Type ) { Value = id } ;
9399
94100 return ExecuteCommand ( connection ,
95- string . Format ( "DELETE FROM \" {0}\" WHERE \" {1}\" = @p_id" , t . db_TableName , key . Name ) , transaction , pId ) ;
101+ string . Format ( "DELETE FROM {0} WHERE \" {1}\" = @p_id" , ObjectName , key . Name ) , transaction , pId ) ;
96102 }
97103
98104 public static int ExecuteCommand ( NpgsqlConnection connection , string commandText ,
@@ -148,7 +154,7 @@ public static T[] Select(NpgsqlConnection connection, string commandText, params
148154 foreach ( DataRow row in dt . Rows )
149155 {
150156 T item = new T ( ) ;
151- foreach ( var c in item . db_Columns )
157+ foreach ( var c in item . DBColumns )
152158 item . SetValue ( c . Name , row [ c . Name ] ) ;
153159 res . Add ( item ) ;
154160 }
@@ -160,8 +166,7 @@ public static T[] Select(NpgsqlConnection connection, string commandText, params
160166
161167 public virtual NpgsqlParameter CreateParameter ( ColumnInfo c )
162168 {
163- var p = new NpgsqlParameter ( c . Name , c . Type ) ;
164- p . Value = GetValue ( c . Name ) ;
169+ var p = new NpgsqlParameter ( c . Name , c . Type ) { Value = GetValue ( c . Name ) } ;
165170 return p ;
166171 }
167172 }
0 commit comments