@@ -6,10 +6,13 @@ namespace SharpEngine.SQLite;
66/// <summary>
77/// Sqlite Data Table
88/// </summary>
9- public class SQLiteDataTable < T > : IDataTable
10- where T : new ( )
9+ public class SQLiteDataTable < T > : IDataTable < T >
10+ where T : class , new ( )
1111{
12- private List < dynamic > Objects { get ; }
12+ private List < T > Objects { get ; }
13+
14+ private string DbFile { get ; set ; }
15+ private string Version { get ; set ; }
1316
1417 /// <summary>
1518 /// Create Data Table from SQLite
@@ -19,6 +22,8 @@ public class SQLiteDataTable<T> : IDataTable
1922 /// <exception cref="NotImplementedException">If use not implement type</exception>
2023 public SQLiteDataTable ( string dbFile , string version = "3" )
2124 {
25+ DbFile = dbFile ;
26+ Version = version ;
2227 Objects = [ ] ;
2328
2429 var connection = new SQLiteConnection (
@@ -59,8 +64,73 @@ public SQLiteDataTable(string dbFile, string version = "3")
5964 }
6065
6166 /// <inheritdoc />
62- public dynamic ? Get ( Predicate < dynamic > predicate )
67+ public void Add ( T obj )
68+ {
69+ var connection = new SQLiteConnection (
70+ $ "Data Source={ DbFile } ;Version={ Version } ;New=True;Compress=True;"
71+ ) ;
72+ connection . Open ( ) ;
73+ var cmd = connection . CreateCommand ( ) ;
74+ cmd . CommandText = $ "INSERT INTO { typeof ( T ) . Name } VALUES ({ string . Join ( ", " , typeof ( T ) . GetProperties ( ) . Select ( x => $ "@{ x . Name } ") ) } );";
75+ foreach ( var property in typeof ( T ) . GetProperties ( ) )
76+ {
77+ var value = property . GetValue ( obj ) ;
78+ if ( value == null )
79+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", DBNull . Value ) ;
80+ else if ( property . PropertyType == typeof ( string ) )
81+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", value . ToString ( ) ) ;
82+ else if ( property . PropertyType == typeof ( int ) )
83+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", Convert . ToInt32 ( value ) ) ;
84+ else if ( property . PropertyType == typeof ( bool ) )
85+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", Convert . ToBoolean ( value ) ) ;
86+ else if ( property . PropertyType == typeof ( float ) )
87+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", Convert . ToSingle ( value ) ) ;
88+ else
89+ throw new NotImplementedException (
90+ $ "Not implemented type : { property . PropertyType . Name } "
91+ ) ;
92+ }
93+ cmd . ExecuteNonQuery ( ) ;
94+ connection . Close ( ) ;
95+
96+ Objects . Add ( obj ) ;
97+ }
98+
99+ /// <inheritdoc />
100+ public void Remove ( T obj )
101+ {
102+ var connection = new SQLiteConnection (
103+ $ "Data Source={ DbFile } ;Version={ Version } ;New=True;Compress=True;"
104+ ) ;
105+ connection . Open ( ) ;
106+ var cmd = connection . CreateCommand ( ) ;
107+ cmd . CommandText = $ "DELETE FROM { typeof ( T ) . Name } WHERE { string . Join ( " AND " , typeof ( T ) . GetProperties ( ) . Select ( x => $ "{ x . Name } = @{ x . Name } ") ) } ;";
108+ foreach ( var property in typeof ( T ) . GetProperties ( ) )
109+ {
110+ var value = property . GetValue ( obj ) ;
111+ if ( value == null )
112+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", DBNull . Value ) ;
113+ else if ( property . PropertyType == typeof ( string ) )
114+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", value . ToString ( ) ) ;
115+ else if ( property . PropertyType == typeof ( int ) )
116+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", Convert . ToInt32 ( value ) ) ;
117+ else if ( property . PropertyType == typeof ( bool ) )
118+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", Convert . ToBoolean ( value ) ) ;
119+ else if ( property . PropertyType == typeof ( float ) )
120+ cmd . Parameters . AddWithValue ( $ "@{ property . Name } ", Convert . ToSingle ( value ) ) ;
121+ else
122+ throw new NotImplementedException (
123+ $ "Not implemented type : { property . PropertyType . Name } "
124+ ) ;
125+ }
126+ cmd . ExecuteNonQuery ( ) ;
127+ connection . Close ( ) ;
128+ Objects . Remove ( obj ) ;
129+ }
130+
131+ /// <inheritdoc />
132+ public IEnumerable < T > Get ( Func < T , bool > predicate )
63133 {
64- return Objects . Find ( predicate ) ;
134+ return Objects . Where ( predicate ) ;
65135 }
66136}
0 commit comments