diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/Logger.java b/src/main/java/io/github/deathgod7/SE7ENLib/Logger.java new file mode 100644 index 0000000..81f572c --- /dev/null +++ b/src/main/java/io/github/deathgod7/SE7ENLib/Logger.java @@ -0,0 +1,25 @@ +// This file is part of SE7ENLib, created on 17/04/2024 (18:01 PM) +// Name : Logger +// Author : Death GOD 7 + +package io.github.deathgod7.SE7ENLib; + +import io.github.deathgod7.SE7ENLib.database.DatabaseManager; + +/** + * Represents the Logger + * @version 1.0 + * @since 1.0 + */ +public class Logger { + /** + * Util function to log the message + * @param message The message to log + */ + public static void log(String message) { + boolean x = DatabaseManager.getInstance().getDebugMode(); + if (x) { + System.out.println(message); + } + } +} diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/DatabaseManager.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/DatabaseManager.java index f7c28ed..0c7cba5 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/DatabaseManager.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/DatabaseManager.java @@ -15,82 +15,204 @@ */ public class DatabaseManager { private static DatabaseManager _dbmInstance; + /** + * Get the instance of DatabaseManager + * @return {@link DatabaseManager} + */ public static DatabaseManager getInstance() { return _dbmInstance; } private SQLite _sqlite; + /** + * Get the SQLite + * @return {@link SQLite} + */ public SQLite getSQLite() { return this._sqlite; } private MySQL _mysql; + /** + * Get the MySQL + * @return {@link MySQL} + */ public MySQL getMySQL() { return this._mysql; } private MongoDB _mongodb; + /** + * Get the MongoDB + * @return {@link MongoDB} + */ public MongoDB getMongoDB() { return this._mongodb; } private DatabaseInfo _dbInfo; + + /** + * Get the DatabaseInfo + * @return {@link DatabaseInfo} + */ public DatabaseInfo getDbInfo() { return this._dbInfo; } + /** + * Check if the database is connected + * @return {@link Boolean} + */ public boolean isConnected() { return (this.getConnection() != null); } + private boolean debugMode = false; + + /** + * Set the debug mode + * @param value The value to set + */ + public void setDebugMode(boolean value) { + this.debugMode = value; + } + + /** + * Check the debug mode + * @return {@link Boolean} + */ + public boolean getDebugMode() { + return this.debugMode; + } + + /** + * Get the connection of database + * @return {@link Object} + */ public Object getConnection() { - if (_dbInfo.getDbType() == DatabaseType.SQLite) { - return _sqlite.getConnection(); - } - else if (_dbInfo.getDbType() == DatabaseType.MySQL){ - // to do mysql support - return _mysql.getConnection(); - } else if (_dbInfo.getDbType() == DatabaseType.MongoDB) { - return _mongodb.getConnection(); - } - else { - return null; - } + if (_dbInfo.getDbType() == DatabaseType.SQLite) { return this.getSQLite().getConnection(); } + else if (_dbInfo.getDbType() == DatabaseType.MySQL){ return this.getMySQL().getConnection(); } + else if (_dbInfo.getDbType() == DatabaseType.MongoDB) { return this.getMongoDB().getConnection(); } + else { return null; } + } + + /** + * Get the database (all) + * @return {@link Object} + */ + public Object getDatabase() { + if (_dbInfo.getDbType() == DatabaseType.SQLite) { return this.getSQLite(); } + else if (_dbInfo.getDbType() == DatabaseType.MySQL){ return this.getMySQL(); } + else if (_dbInfo.getDbType() == DatabaseType.MongoDB) { return this.getMongoDB(); } + else { return null; } } + /** + * Get the database name + * @return {@link String} + */ public String getDBName() { return this._dbInfo.getDbName(); } + /** + * Represents the Data Type + */ public enum DataType { + /** + * Varchar Data Type + */ VARCHAR, + /** + * Text Data Type + */ TEXT, + /** + * Integer Data Type + */ INTEGER, + /** + * Boolean Data Type + */ BOOLEAN, + /** + * Float Data Type + */ FLOAT, + /** + * Double Data Type + */ DOUBLE, + /** + * Date Data Type + */ DATE, + /** + * Time Data Type + */ TIME, + /** + * DateTime Data Type + */ DATETIME, // MongoDB Special + /** + * Array Data Type + */ ARRAY, + /** + * Document Data Type + */ DOCUMENT, + /** + * ObjectID Data Type + */ OBJECTID } + /** + * Represents the Order Type + */ public enum OrderType { + /** + * Ascending Order + */ ASCENDING, + /** + * Descending Order + */ DESCENDING } + /** + * Represents the Database Type + */ public enum DatabaseType { + /** + * SQLite Database + */ MySQL, + /** + * MySQL Database + */ SQLite, + /** + * MongoDB Database + */ MongoDB } + /** + * DatabaseManager constructor that takes DatabaseInfo + * @param dbinfo The DatabaseInfo object + */ public DatabaseManager(DatabaseInfo dbinfo){ _dbmInstance = this; loadDB(dbinfo); } - + /** + * Close the SQL connection + * @param ps The PreparedStatement + * @param rs The ResultSet + */ public void closeConnection(PreparedStatement ps, ResultSet rs) { try { if (ps != null) @@ -102,6 +224,10 @@ public void closeConnection(PreparedStatement ps, ResultSet rs) { } } + /** + * Load the database based on the DatabaseInfo + * @param dbinfo The DatabaseInfo object provided while creating instance of this class + */ private void loadDB(DatabaseInfo dbinfo) { _dbInfo = dbinfo; @@ -123,6 +249,10 @@ else if (dbinfo.getDbType() == DatabaseType.MongoDB){ } } + /** + * Get the tables from the database + * @return {@link LinkedHashMap} + */ public LinkedHashMap getTables() { if (_dbInfo.getDbType() == DatabaseType.SQLite) { return _sqlite.getTables(); diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Column.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Column.java index 1142b2c..1f63fa2 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Column.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Column.java @@ -124,59 +124,114 @@ private int getBoolInt(boolean bool) { return bool ? 1 : 0; } + /** + * Get the name of the column + * @return {@link String} + */ public String getName() { return this.name; } + /** + * Set the name of the column + * @param name The name of the column + */ public void setName(String name) { this.name = name; } + /** + * Get the data type of the column + * @return {@linkplain DataType} + */ public DataType getDataType() { return this.dataType; } + /** + * Set the data type of the column + * @param dataType The data type of the column + */ public void setDataType(DataType dataType) { this.dataType = dataType; } + /** + * Get the limit of the column + * @return {@link int} + */ public int getLimit() { return this.limit; } + /** + * Set the limit of the column + * @param limit The limit of the column + */ public void setLimit(int limit) { this.limit = limit; } + /** + * Get the value of the column + * @return {@link Object} + */ public Object getValue() { return this.value; } + /** + * Set the value of the column + * @param value The value of the column + */ public void setValue(Object value) { this.value = value; } - + /** + * Get if the column is nullable + * @return {@link boolean} + */ public boolean isNullable() { return this.isNullable; } + /** + * Set if the column is nullable + * @param value The boolean to enable or disable nullable + */ public void setNullable(boolean value) { this.isNullable = value; } + /** + * Get if the column is auto increment + * @return {@link boolean} + */ public boolean isAutoIncrement() { return this.autoIncrement; } + /** + * Set if the column is auto increment + * @param value The boolean value to enable or disable auto increment + */ public void setAutoIncrement(boolean value) { this.autoIncrement = value; } + /** + * Get the default value of the column + * @return {@link Object} + */ public Object getDefaultValue() { return this.defaultValue; } + /** + * Set the default value of the column + * @param value The default value of the column + */ public void setDefaultValue(Object value) { this.defaultValue = value; } diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Table.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Table.java index 1ea5746..f71d89b 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Table.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/component/Table.java @@ -78,26 +78,50 @@ public Table(String name, Collection columns) { this.primaryKey.setNullable(false); } + /** + * Get the name of the table + * @return {@link String} + */ public String getName() { return name; } + /** + * Set the name of the table + * @param name The name to set + */ public void setName(String name) { this.name = name; } + /** + * Get the columns of the table + * @return {@link List} + */ public List getColumns() { return columns; } + /** + * Set the columns of the table + * @param columns The columns to set + */ public void setColumns(List columns) { this.columns = columns; } + /** + * Get the primary key of the table + * @return {@link Column} + */ public Column getPrimaryKey() { return primaryKey; } + /** + * Set the primary key of the table + * @param primaryKey The primary key to set + */ public void setPrimaryKey(Column primaryKey) { this.primaryKey = primaryKey; } diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mongodb/MongoDB.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mongodb/MongoDB.java index 059adc7..a4d891e 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mongodb/MongoDB.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mongodb/MongoDB.java @@ -27,43 +27,66 @@ public class MongoDB extends MongoOperations { private final String password; private final String dbName; + /** + * Get the database name + * @return {@link String} + */ public String getDBName(){ return dbName; } - private MongoDatabase connection; + private final MongoDatabase connection; + /** + * Get the connection + * @return {@link MongoDatabase} + */ public MongoDatabase getConnection(){ - if (connection == null){ - connection = connectMongoDB(); - return connection; - } return connection; } - + /** + * Check if the database is connected + * @return {@link Boolean} + */ + public boolean isConnected(){ + return (connection != null); + } private final LinkedHashMap tables = new LinkedHashMap<>(); + /** + * Get the tables + * @return {@link LinkedHashMap} + */ public LinkedHashMap getTables() { return tables; } + /** + * Add the table in table list + * @param table The Table object to add + */ public void addTable(Table table) { tables.put(table.getName(), table); } + /** + * Remove the table from table list + * @param tablename The name of the table to remove + */ public void removeTable(String tablename) { tables.remove(tablename); } + + /** + * Creates a new MongoDB + * @param dbinfo The DatabaseInfo object + */ public MongoDB(DatabaseInfo dbinfo){ this.host = dbinfo.getHostAddress(); this.username = dbinfo.getUsername(); this.password = dbinfo.getPassword(); this.dbName = dbinfo.getDbName(); - this.connection = connectMongoDB(); } - public boolean isConnected(){ - return (connection != null); - } // mongodb uri creator using srv private String URICreator(String username, String password, String hostname) { @@ -87,9 +110,13 @@ private MongoDatabase connectMongoDB() { } } + /** + * Load the tables from the database + */ public void loadMongoTables() { for (String tablename : this.getConnection().listCollectionNames()) { tables.put(tablename, this.loadTable(tablename)); } } + } diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mysql/MySQL.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mysql/MySQL.java index 50fac23..88aec5b 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mysql/MySQL.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/mysql/MySQL.java @@ -27,32 +27,59 @@ public class MySQL extends SQLOperations { private final String password; private final String dbName; + /** + * Get the database name + * @return {@link String} + */ public String getDBName(){ return dbName; } private final Connection connection; + /** + * Get the connection + * @return {@link Connection} + */ public Connection getConnection(){ - if (connection == null){ - //connection = connectSQLite(); - return connection; - } - return connection; + return connection; } + /** + * Check if the database is connected + * @return {@link Boolean} + */ public boolean isConnected(){ return (connection != null); } private final LinkedHashMap tables = new LinkedHashMap<>(); + /** + * Get the tables + * @return {@link LinkedHashMap} + */ public LinkedHashMap getTables() { return tables; } + /** + * Add the table in table list + * @param table {@link Table} + */ public void addTable(Table table) { tables.put(table.getName(), table); } + /** + * Remove the table from table list + * @param tablename {@link String} + */ public void removeTable(String tablename) { tables.remove(tablename); } + /** + * Create a new MySQL object + * @param dbname {@link String} + * @param host {@link String} + * @param username {@link String} + * @param password {@link String} + */ public MySQL(String dbname, String host, String username, String password){ this.host = host; this.username = username; @@ -61,6 +88,10 @@ public MySQL(String dbname, String host, String username, String password){ this.connection = connectMySQL(); } + /** + * Create a new MySQL object + * @param dbInfo {@link DatabaseInfo} + */ public MySQL(DatabaseInfo dbInfo){ this.host = dbInfo.getHostAddress(); this.username = dbInfo.getUsername(); @@ -96,7 +127,9 @@ private Connection connectMySQL() { } } - + /** + * Load the MySQL tables + */ public void loadMysqlTables() { String query = "SELECT table_name FROM information_schema.tables " + "WHERE table_schema = '"+ this.getDBName() +"' AND table_type = 'base table' " + @@ -113,6 +146,12 @@ public void loadMysqlTables() { ex.printStackTrace(); } } + + /** + * Load the table from the database + * @param tablename The name of the table + * @return {@link Table} + */ public Table loadTable(String tablename) { // query all the column name and its type String query = "SHOW COLUMNS FROM `" + tablename + "`;"; diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/sqlite/SQLite.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/sqlite/SQLite.java index cd69ffe..b49d0ab 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/sqlite/SQLite.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/dbtype/sqlite/SQLite.java @@ -26,36 +26,71 @@ public class SQLite extends SQLOperations { // old name = sqlite_master ( < 3.33.0 ) // works in all newer version // new name = sqlite_schema ( >= 3.33.0 ) // won't work in older version + /** + * Get the database name + * @return {@link String} + */ public String getDBName(){ return dbName; } + /** + * Check if the database is connected + * @return {@link Boolean} + */ public boolean isConnected(){ return (connection != null); } + /** + * Get the connection + * @return {@link Connection} + */ public Connection getConnection() { - return connection; + return connection; } private final LinkedHashMap tables = new LinkedHashMap<>(); + + /** + * Get the tables + * @return {@link LinkedHashMap} + */ public LinkedHashMap getTables() { return tables; } + /** + * Add the table in table list + * @param table {@link Table} The Table object to add + */ public void addTable(Table table) { tables.put(table.getName(), table); } + /** + * Remove the table from table list + * @param tablename {@link String} The name of the table to remove + */ public void removeTable(String tablename) { tables.remove(tablename); } + + /** + * Create a new SQLite object + * @param dbName {@link String} The name of the database file + * @param directory {@link String} The directory of the database file + */ public SQLite(String dbName, String directory){ this.dbName = dbName; this.dirDB = directory; this.connection = connectSQLite(); } + /** + * Create a new SQLite object + * @param dbInfo {@link DatabaseInfo} The DatabaseInfo object + */ public SQLite(DatabaseInfo dbInfo){ this.dbName = dbInfo.getDbName(); this.dirDB = dbInfo.getDirDb(); @@ -87,6 +122,9 @@ private Connection connectSQLite() { } } + /** + * Close the SQL connection + */ public void closeConnection() { if (isConnected()) { try { @@ -102,6 +140,9 @@ public void closeConnection() { } } + /** + * Load the SQLite tables + */ public void loadSqliteTables() { String sqlitever; int[] sqliteverformatted = new int[3]; @@ -145,7 +186,11 @@ public void loadSqliteTables() { } } - // sqlite based querys + /** + * Load the table from the database + * @param tablename The name of the table + * @return {@link Table} + */ public Table loadTable(String tablename) { // query all the column name and its type // String query = "SELECT * FROM pragma_table_info('" + tablename +"');"; diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/MongoOperations.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/MongoOperations.java index 709fece..b472142 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/MongoOperations.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/MongoOperations.java @@ -5,12 +5,14 @@ package io.github.deathgod7.SE7ENLib.database.handler; import io.github.deathgod7.SE7ENLib.database.DatabaseManager; +import io.github.deathgod7.SE7ENLib.database.DatabaseManager.DataType; import io.github.deathgod7.SE7ENLib.database.component.Column; import io.github.deathgod7.SE7ENLib.database.component.Table; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.CreateCollectionOptions; import com.mongodb.client.model.ValidationOptions; +import io.github.deathgod7.SE7ENLib.Logger; import org.bson.Document; import org.bson.types.ObjectId; @@ -26,6 +28,12 @@ */ public class MongoOperations implements DatabaseOperations { + /** + * Check if the collection exists + * @param collectionName The name of the collection + * @param database The database + * @return {@link Boolean} + */ public boolean collectionExists(String collectionName, MongoDatabase database) { for (String existingCollection : database.listCollectionNames()) { if (existingCollection.equals(collectionName)) { @@ -127,6 +135,12 @@ private List extractColumnNames(List columns) { return columnNames; } + /** + * Convert the object to document + * @param value The object to convert (usually obtained from Column#getValue() + * @param type The data type of the object + * @return {@link Document} + */ @SuppressWarnings("unchecked") public Document getObjAsDocument(Object value, DatabaseManager.DataType type) { if (type != DatabaseManager.DataType.DOCUMENT) { @@ -143,7 +157,7 @@ public Document getObjAsDocument(Object value, DatabaseManager.DataType type) { alltempcols = (List) value; } catch (Exception ex) { - System.out.println(ex.getMessage()); + Logger.log("[ERROR] " + ex.getMessage()); return null; } @@ -169,6 +183,11 @@ public Document getObjAsDocument(Object value, DatabaseManager.DataType type) { return doc; } } + /** + * Get the data type for MongoDB + * @param type The data type + * @return {@link String} + */ public String getDataTypeForMongo(DatabaseManager.DataType type) { switch (type) { case DATE: @@ -191,6 +210,11 @@ public String getDataTypeForMongo(DatabaseManager.DataType type) { } } + /** + * Parse the data type class + * @param clazz The class to parse + * @return {@link DatabaseManager.DataType} + */ public DatabaseManager.DataType parseDataTypeClass(Class clazz) { if (clazz == String.class) { return DatabaseManager.DataType.TEXT; @@ -224,7 +248,12 @@ else if (clazz == ObjectId.class) { } } - public DatabaseManager.DataType getDataTypeFromString(String type) { + /** + * Get the data type from string + * @param type The type to parse + * @return {@link DataType} + */ + public DataType getDataTypeFromString(String type) { switch (type) { case "date": return DatabaseManager.DataType.DATE; @@ -240,7 +269,7 @@ public DatabaseManager.DataType getDataTypeFromString(String type) { return DatabaseManager.DataType.DOCUMENT; case "string": default: - return DatabaseManager.DataType.VARCHAR; + return DatabaseManager.DataType.TEXT; } } @@ -310,11 +339,17 @@ public boolean dropTable(String tablename) { DatabaseManager.getInstance().getMongoDB().removeTable(tablename); return true; } catch (Exception e) { - System.out.println("Error : " + e.getMessage()); + Logger.log("[ERROR] " + e.getMessage()); return false; } } + + /** + * Convert the list of columns to document + * @param value The list of columns + * @return {@link Document} + */ @SuppressWarnings("unchecked") public Document getColsAsDocument(List value) { Document doc = new Document(); @@ -361,7 +396,7 @@ public boolean insertData(String tablename, List columns) { Table table = DatabaseManager.getInstance().getTables().get(tablename); if (table == null) { - System.out.println("Table not found"); + Logger.log("[ERROR] Table not found."); return false; } @@ -375,7 +410,7 @@ public boolean insertData(String tablename, List columns) { return true; } catch (Exception e) { - System.out.println("Error : " + e.getMessage()); + Logger.log("[ERROR] " + e.getMessage()); return false; } } @@ -416,8 +451,8 @@ public boolean updateData(String tablename, Column primaryKey, List colu collection.updateOne(docToFind, new Document("$set", doc)); return true; - } catch (Exception ex) { - System.out.println("Error : " + ex.getMessage()); + } catch (Exception e) { + Logger.log("[ERROR] " + e.getMessage()); return false; } } @@ -458,13 +493,17 @@ public boolean deleteData(String tablename, Column primaryKey) { return true; } catch (Exception e) { - System.out.println("Error : " + e.getMessage()); + Logger.log("[ERROR] " + e.getMessage()); return false; } } - // parse the document to list of columns + /** + * Parse the document to columns + * @param doc The document to parse + * @return {@link List}<{@link Column}> + */ @SuppressWarnings("unchecked") public List parseDocToColumns(Document doc) { List columns = new ArrayList<>(); @@ -535,7 +574,7 @@ public List getExactData(String tablename, Column primaryKey) { } } catch (Exception e) { - System.out.println("Error : " + e.getMessage()); + Logger.log("[ERROR] " + e.getMessage()); } return columns; @@ -576,7 +615,7 @@ public List> findData(String tablename, Column column) { } } catch (Exception e) { - System.out.println("Error : " + e.getMessage()); + Logger.log("[ERROR] " + e.getMessage()); } return allData; @@ -616,12 +655,18 @@ public List> getAllDatas(String tablename) { } } catch (Exception e) { - System.out.println("Error : " + e.getMessage()); + Logger.log("[ERROR] " + e.getMessage()); } return allData; } + /** + * Get the count of documents in the table + * + * @param tablename The name of the table in the database + * @return {@link int} + */ public int getDocumentCount(String tablename) { return this.getAllDatas(tablename).size(); } diff --git a/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/SQLOperations.java b/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/SQLOperations.java index 73cd83c..97f6562 100644 --- a/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/SQLOperations.java +++ b/src/main/java/io/github/deathgod7/SE7ENLib/database/handler/SQLOperations.java @@ -4,6 +4,7 @@ package io.github.deathgod7.SE7ENLib.database.handler; +import io.github.deathgod7.SE7ENLib.Logger; import io.github.deathgod7.SE7ENLib.database.DatabaseManager; import io.github.deathgod7.SE7ENLib.database.DatabaseManager.DataType; import io.github.deathgod7.SE7ENLib.database.DatabaseManager.DatabaseType; @@ -133,7 +134,7 @@ else if (dbtype == DatabaseType.MySQL && table.getPrimaryKey().isAutoIncrement() // end closing query.replace(query.length() - 2, query.length(), ");"); - System.out.println(query); + Logger.log("[QUERY] " + query); try { Connection con = (Connection) DatabaseManager.getInstance().getConnection(); @@ -169,6 +170,8 @@ else if (dbtype == DatabaseType.MySQL && table.getPrimaryKey().isAutoIncrement() public boolean dropTable(String tablename) { StringBuilder query = new StringBuilder("DROP TABLE IF EXISTS `" + tablename + "`;"); + Logger.log("[QUERY] " + query); + try { Connection con = (Connection) DatabaseManager.getInstance().getConnection(); PreparedStatement ps = con.prepareStatement(query.toString()); @@ -221,6 +224,7 @@ public boolean insertData(String tablename, List columns) { } query.append(";"); + Logger.log("[QUERY] " + query); try { Connection con = (Connection) DatabaseManager.getInstance().getConnection(); @@ -286,13 +290,12 @@ public boolean insertData(String tablename, List columns) { break; } } - System.out.println(s); s.executeUpdate(); s.close(); return true; } catch (SQLException e) { - e.printStackTrace(); + Logger.log("[ERROR] " + e.getMessage()); return false; } } @@ -333,7 +336,8 @@ public boolean updateData(String tablename, Column primaryKey, List colu } query.append(";"); - System.out.println(query); + Logger.log("[QUERY] " + query); + try { Connection con = (Connection) DatabaseManager.getInstance().getConnection(); PreparedStatement s = con.prepareStatement(query.toString()); @@ -341,7 +345,7 @@ public boolean updateData(String tablename, Column primaryKey, List colu s.close(); return true; } catch (SQLException e) { - e.printStackTrace(); + Logger.log("[ERROR] " + e.getMessage()); return false; } @@ -357,6 +361,9 @@ public boolean updateData(String tablename, Column primaryKey, List colu @Override public boolean deleteData(String tablename, Column primaryKey) { String query = "DELETE FROM `" + tablename + "` WHERE `" + primaryKey.getName() + "` = ?"; + + Logger.log("[QUERY] " + query); + try { Connection con = (Connection) DatabaseManager.getInstance().getConnection(); PreparedStatement s = con.prepareStatement(query); @@ -423,7 +430,7 @@ public boolean deleteData(String tablename, Column primaryKey) { s.close(); return true; } catch (SQLException e) { - e.printStackTrace(); + Logger.log("[ERROR] " + e.getMessage()); return false; } } @@ -444,6 +451,8 @@ public List getExactData(String tablename, Column primaryKey) { query = query.replace("= ?", "LIKE ?"); } + Logger.log("[QUERY] " + query); + try { Connection con = (Connection) DatabaseManager.getInstance().getConnection(); PreparedStatement s = con.prepareStatement(query); @@ -509,11 +518,10 @@ public List getExactData(String tablename, Column primaryKey) { } ResultSet rs = s.executeQuery(); - // System.out.println(s.toString()); // primary key and so on - List allTableCols = table.getColumns(); - allTableCols.add(0, table.getPrimaryKey()); + List allTableCols = new ArrayList<>(table.getColumns()); + allTableCols.add(0, primaryKey); // if there is result found then proceed if (rs.next()) { @@ -525,8 +533,6 @@ public List getExactData(String tablename, Column primaryKey) { allTableCols.get(i).getLimit() ); - // System.out.println(rCol.getName() + " = " + rs.getString(i+1)); - switch (rCol.getDataType()) { case VARCHAR: case TEXT: @@ -561,11 +567,11 @@ public List getExactData(String tablename, Column primaryKey) { DatabaseManager.getInstance().closeConnection(s, rs); } catch (SQLException e) { s.close(); - e.printStackTrace(); + Logger.log("[ERROR] " + e.getMessage()); } } } catch (SQLException e) { - e.printStackTrace(); + Logger.log("[ERROR] " + e.getMessage()); } return result; @@ -587,6 +593,8 @@ public List> findData(String tablename, Column column) { query = query.replace("= ?", "LIKE ?"); } + Logger.log("[QUERY] " + query); + try { Connection con = (Connection) DatabaseManager.getInstance().getConnection(); PreparedStatement s = con.prepareStatement(query); @@ -652,7 +660,6 @@ public List> findData(String tablename, Column column) { } ResultSet rs = s.executeQuery(); - // System.out.println(s.toString()); // primary key and so on List allTableCols = table.getColumns(); @@ -667,8 +674,6 @@ public List> findData(String tablename, Column column) { allTableCols.get(i).getLimit() ); - // System.out.println(rCol.getName() + " = " + rs.getString(i+1)); - // set value switch (rCol.getDataType()) { case VARCHAR: @@ -703,7 +708,7 @@ public List> findData(String tablename, Column column) { } DatabaseManager.getInstance().closeConnection(s, rs); } catch (SQLException e) { - e.printStackTrace(); + Logger.log("[ERROR] " + e.getMessage()); } return results; @@ -721,6 +726,8 @@ public List> getAllDatas(String tablename) { Table table = DatabaseManager.getInstance().getTables().get(tablename); String query = "SELECT * FROM `" + tablename + "`;"; try { + Logger.log("[QUERY] " + query); + Connection con = (Connection) DatabaseManager.getInstance().getConnection(); PreparedStatement s = con.prepareStatement(query); ResultSet rs = s.executeQuery(); @@ -774,7 +781,7 @@ public List> getAllDatas(String tablename) { // close both PS and RS DatabaseManager.getInstance().closeConnection(s, rs); } catch (SQLException e) { - e.printStackTrace(); + Logger.log("[ERROR] " + e.getMessage()); } return allDatas; }