11package org .broken .arrow .database .library ;
22
33import org .broken .arrow .database .library .builders .DataWrapper ;
4+ import org .broken .arrow .database .library .builders .LoadDataWrapper ;
45import org .broken .arrow .database .library .builders .TableWrapper ;
56import org .broken .arrow .database .library .builders .tables .TableRow ;
67import org .broken .arrow .database .library .log .LogMsg ;
78import org .broken .arrow .database .library .log .Validate ;
89import org .broken .arrow .database .library .utility .serialize .ConfigurationSerializeUtility ;
10+ import org .broken .arrow .database .library .utility .serialize .DeSerialize ;
911
1012import javax .annotation .Nonnull ;
1113import javax .annotation .Nullable ;
3032public abstract class Database {
3133
3234 protected Connection connection ;
35+ private DeSerialize deSerialize = new DeSerialize ();
3336 protected boolean batchUpdateGoingOn = false ;
3437 private final Map <String , TableWrapper > tables = new HashMap <>();
3538 protected boolean hasStartWriteToDb = false ;
@@ -42,13 +45,17 @@ public Database() {
4245
4346 public abstract Connection connect ();
4447
48+ protected abstract void batchUpdate (@ Nonnull final List <String > batchList , @ Nonnull final TableWrapper ... tableWrappers );
49+
50+ protected abstract void remove (@ Nonnull final List <String > batchList , @ Nonnull final TableWrapper tableWrappers );
51+
52+ protected abstract void dropTable (@ Nonnull final List <String > batchList , @ Nonnull final TableWrapper tableWrappers );
53+
4554 public void createTables () {
4655 Validate .checkBoolean (tables .isEmpty (), "The table is empty, add tables to the map before call this method" );
4756 try {
4857 openConnection ();
49- for (final Entry <String , TableWrapper > entitytables : tables .entrySet ()) {
50- load (entitytables );
51- }
58+ createAllTables ();
5259 try {
5360 for (final Entry <String , TableWrapper > entityTables : tables .entrySet ()) {
5461 final List <String > columns = updateTableColumnsInDb (entityTables .getKey ());
@@ -109,7 +116,7 @@ public void saveAll(@Nonnull final Map<String, DataWrapper> utilityMap) {
109116 }
110117 this .getSqlsCommands (sqls , tableWrapper );
111118 }
112- this .batchUpdate (sqls );
119+ this .batchUpdate (sqls , this . getTables (). values (). toArray ( new TableWrapper [ 0 ]) );
113120 }
114121
115122 /**
@@ -142,10 +149,83 @@ public void save(@Nonnull final String tableName, @Nonnull final String primaryK
142149 tableWrapper .addCustom (entry .getKey (), column .getBuilder ().setColumnValue (entry .getValue ()));
143150 }
144151 this .getSqlsCommands (sqls , tableWrapper );
145- this .batchUpdate (sqls );
152+ this .batchUpdate (sqls , tableWrapper );
153+ }
154+
155+ /**
156+ * Load one row from specified database table.
157+ *
158+ * @param tableName name of the table you want to get data from.
159+ * @param clazz the class you have your static deserialize method.
160+ * @return one row you have in the table.
161+ */
162+ @ Nullable
163+ public <T extends ConfigurationSerializeUtility > LoadDataWrapper <T > load (@ Nonnull final String tableName , @ Nonnull final Class <T > clazz ) {
164+ TableWrapper tableWrapper = this .getTable (tableName );
165+ if (tableWrapper == null ) {
166+ LogMsg .warn ("Could not find table " + tableName );
167+ return null ;
168+ }
169+ Validate .checkNotNull (tableWrapper .getPrimaryRow (), "Colud not find primary column for table " + tableName );
170+ String primaryColumn = tableWrapper .getPrimaryRow ().getColumnName ();
171+ Map <String , Object > dataFromDB = new HashMap <>();
172+ TableRow column = tableWrapper .getColumn (primaryColumn );
173+ Validate .checkNotNull (column , "Colud not find column for " + primaryColumn );
174+
175+ final String sql = "SELECT * FROM `" + tableWrapper .getTableName () + "` WHERE `" + primaryColumn + "` = '" + column .getColumnValue () + "'" ;
176+ PreparedStatement preparedStatement = null ;
177+ ResultSet resultSet = null ;
178+ try {
179+ preparedStatement = this .connection .prepareStatement (sql );
180+ resultSet = preparedStatement .executeQuery ();
181+ dataFromDB .putAll (this .getDataFromDB (resultSet ));
182+
183+ } catch (SQLException e ) {
184+ e .printStackTrace ();
185+ } finally {
186+ this .close (preparedStatement , resultSet );
187+ }
188+ T deserialize = this .deSerialize .invokeDeSerializeMethod (clazz , "deserialize" , dataFromDB );
189+ return new LoadDataWrapper <>(tableWrapper .getPrimaryRow ().getColumnName (), dataFromDB , deserialize );
190+ }
191+
192+ /**
193+ * Load all rows from specified database table.
194+ *
195+ * @param tableName name of the table you want to get data from.
196+ * @param clazz the class you have your static deserialize method.
197+ * @return list of all data you have in the table.
198+ */
199+ public <T extends ConfigurationSerializeUtility > List <LoadDataWrapper <T >> loadAll (@ Nonnull final String tableName , @ Nonnull final Class <T > clazz ) {
200+ final List <LoadDataWrapper <T >> loadDataWrappers = new ArrayList <>();
201+ PreparedStatement preparedStatement = null ;
202+ ResultSet resultSet = null ;
203+
204+ TableWrapper tableWrapper = this .getTable (tableName );
205+ if (tableWrapper == null ) {
206+ LogMsg .warn ("Could not find table " + tableName );
207+ return null ;
208+ }
209+ try {
210+ final String sql = "SELECT * FROM `" + tableWrapper .getTableName () + "`" ;
211+
212+ preparedStatement = connection .prepareStatement (sql );
213+ resultSet = preparedStatement .executeQuery ();
214+ while (resultSet .next ()) {
215+ Map <String , Object > dataFromDB = this .getDataFromDB (resultSet );
216+ T deserialize = this .deSerialize .invokeDeSerializeMethod (clazz , "deserialize" , dataFromDB );
217+ loadDataWrappers .add (new LoadDataWrapper <>(tableWrapper .getPrimaryRow ().getColumnName (), dataFromDB , deserialize ));
218+ }
219+
220+ } catch (SQLException e ) {
221+ e .printStackTrace ();
222+ } finally {
223+ this .close (preparedStatement , resultSet );
224+ }
225+ return loadDataWrappers ;
146226 }
147227
148- public boolean load (final String tableName ) {
228+ public boolean createTable (final String tableName ) {
149229 if (!openConnection ()) return false ;
150230
151231 try {
@@ -157,42 +237,53 @@ public boolean load(final String tableName) {
157237 final PreparedStatement statement = this .connection .prepareStatement (wrapperEntry .createTable ());
158238 statement .executeUpdate ();
159239 close (statement );
240+ TableRow wraper = wrapperEntry .getColumns ().values ().stream ().findFirst ().orElse (null );
241+ Validate .checkNotNull (wraper , "Could not find a column for this table " + tableName );
242+ checkIfTableExist (tableName , wraper .getColumnName ());
160243 return true ;
161244 } catch (final SQLException e ) {
162245 e .printStackTrace ();
163246 }
164247 return true ;
165248 }
166249
167- private void load (final Entry <String , TableWrapper > wrapperEntry ) {
168- if (!openConnection ()) return ;
169-
170- try {
171- final PreparedStatement statement = this .connection .prepareStatement (wrapperEntry .getValue ().createTable ());
172- statement .executeUpdate ();
173- close (statement );
174- } catch (final SQLException e ) {
175- e .printStackTrace ();
250+ public void createAllTables () {
251+ for (final Entry <String , TableWrapper > wrapperEntry : tables .entrySet ()) {
252+ try {
253+ final PreparedStatement statement = this .connection .prepareStatement (wrapperEntry .getValue ().createTable ());
254+ statement .executeUpdate ();
255+ close (statement );
256+ } catch (final SQLException e ) {
257+ e .printStackTrace ();
258+ }
259+ TableRow wraper = wrapperEntry .getValue ().getColumns ().values ().stream ().findFirst ().orElse (null );
260+ Validate .checkNotNull (wraper , "Could not find a column for this table " + wrapperEntry .getKey ());
261+ checkIfTableExist (wrapperEntry .getKey (), wraper .getColumnName ());
176262 }
177- TableRow wraper = wrapperEntry .getValue ().getColumns ().values ().stream ().findFirst ().orElse (null );
178- Validate .checkNotNull (wraper , "Could not find a column for this table " + wrapperEntry .getKey ());
179- checkIfTableExist (wrapperEntry .getKey (), wraper .getColumnName ());
180263 }
181264
182265 public void remove (final String tableName , final String columnName , final String value ) {
266+ TableWrapper tableWrapper = this .getTable (tableName );
267+ if (tableWrapper == null ) {
268+ LogMsg .warn ("Could not find table " + tableName );
269+ return ;
270+ }
183271 final String sql = "DELETE FROM `" + tableName + "` WHERE `" + columnName + "` = `" + value + "`" ;
184- this .batchUpdate (Collections .singletonList (sql ));
272+ this .remove (Collections .singletonList (sql ), tableWrapper );
185273 }
186274
187275 public void dropTable (final String tableName ) {
276+ TableWrapper tableWrapper = this .getTable (tableName );
277+ if (tableWrapper == null ) {
278+ LogMsg .warn ("Could not find table " + tableName );
279+ return ;
280+ }
188281 final String sql = "DROP TABLE `" + tableName + "`" ;
189- this .batchUpdate (Collections .singletonList (sql ));
282+ this .dropTable (Collections .singletonList (sql ), tableWrapper );
190283 }
191284
192- protected abstract void batchUpdate (@ Nonnull final List <String > batchupdate );
193-
194- protected void batchUpdate (@ Nonnull final List <String > batchupdate , int resultSetType , int resultSetConcurrency ) {
195- final ArrayList <String > sqls = new ArrayList <>(batchupdate );
285+ protected void batchUpdate (@ Nonnull final List <String > batchList , int resultSetType , int resultSetConcurrency ) {
286+ final ArrayList <String > sqls = new ArrayList <>(batchList );
196287 if (!openConnection ()) return ;
197288
198289 if (sqls .size () == 0 )
@@ -415,6 +506,16 @@ protected void closeConnection() {
415506 }
416507 }
417508
509+ public Map <String , Object > getDataFromDB (final ResultSet resultSet ) throws SQLException {
510+ final ResultSetMetaData rsmd = resultSet .getMetaData ();
511+ final int columnCount = rsmd .getColumnCount ();
512+ final Map <String , Object > objectMap = new HashMap <>();
513+ for (int i = 1 ; i <= columnCount ; i ++) {
514+ objectMap .put (rsmd .getColumnName (i ), resultSet .getObject (i ));
515+ }
516+ return objectMap ;
517+ }
518+
418519 public abstract boolean isHasCastExeption ();
419520
420521}
0 commit comments