|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.sdcworld.controller; |
| 5 | + |
| 6 | +import java.lang.reflect.Field; |
| 7 | +import java.lang.reflect.Modifier; |
| 8 | +import java.sql.Connection; |
| 9 | +import java.sql.DriverManager; |
| 10 | +import java.sql.PreparedStatement; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.sql.Statement; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.LinkedHashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import com.sdcworld.controller.helper.EnumDatabaseCase; |
| 22 | +import com.sdcworld.utility.ConvertUtility; |
| 23 | +import com.sdcworld.utility.StringUtility; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author souro |
| 27 | + * |
| 28 | + */ |
| 29 | +public class JdbcConnection<T> { |
| 30 | + |
| 31 | + private String url = "jdbc:mysql://localhost/sdc"; |
| 32 | + private String username = "devuser"; |
| 33 | + private String password = "Demo@123"; |
| 34 | + private String driver = "com.mysql.jdbc.Driver"; |
| 35 | + private String tableName; |
| 36 | + private Map<String, String> columnsMap = new HashMap<String, String>(); |
| 37 | + |
| 38 | + private EnumDatabaseCase databaseCase = EnumDatabaseCase.SENTENCECASE_UNDERSCORE; |
| 39 | + |
| 40 | + private static final String SELECT_ALL = "SELECT * FROM ${table}"; |
| 41 | + private static final String INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})"; |
| 42 | + private static final String TABLE_NAME = "\\$\\{table\\}"; |
| 43 | + private static final String KEYS = "\\$\\{keys\\}"; |
| 44 | + private static final String VALUES = "\\$\\{values\\}"; |
| 45 | + |
| 46 | + private Class<T> genericType; |
| 47 | + |
| 48 | + private boolean isInitializationCompleted; |
| 49 | + private boolean hasAutomaticField; |
| 50 | + private boolean hasPrimaryKey; |
| 51 | + private boolean hasForeignKey; |
| 52 | + private boolean isUnique; |
| 53 | + |
| 54 | + private Map<String, Field> privateFields = new LinkedHashMap<>(); |
| 55 | + |
| 56 | + private Connection dbConnection = null; |
| 57 | + private Statement statement = null; |
| 58 | + private PreparedStatement preparedStatement = null; |
| 59 | + private ResultSet resultSet = null; |
| 60 | + |
| 61 | + // Constructors |
| 62 | + public JdbcConnection(final Class<T> type) |
| 63 | + { |
| 64 | + this.genericType = type; |
| 65 | + connectToDb(); |
| 66 | + initialize(); |
| 67 | + } |
| 68 | + |
| 69 | + public JdbcConnection(final Class<T> type, EnumDatabaseCase databaseCase) |
| 70 | + { |
| 71 | + this.genericType = type; |
| 72 | + connectToDb(); |
| 73 | + this.databaseCase = databaseCase; |
| 74 | + initialize(); |
| 75 | + } |
| 76 | + |
| 77 | + public JdbcConnection(final Class<T> type, String url, String username, String password) |
| 78 | + { |
| 79 | + this.genericType = type; |
| 80 | + this.url = url; |
| 81 | + this.username = username; |
| 82 | + this.password = password; |
| 83 | + |
| 84 | + connectToDb(); |
| 85 | + initialize(); |
| 86 | + } |
| 87 | + |
| 88 | + public JdbcConnection(final Class<T> type, String url, String username, String password, EnumDatabaseCase databaseCase) |
| 89 | + { |
| 90 | + this.genericType = type; |
| 91 | + this.url = url; |
| 92 | + this.username = username; |
| 93 | + this.password = password; |
| 94 | + this.databaseCase = databaseCase; |
| 95 | + |
| 96 | + connectToDb(); |
| 97 | + initialize(); |
| 98 | + } |
| 99 | + |
| 100 | + public JdbcConnection(final Class<T> type, String url, String username, String password, String driver) |
| 101 | + { |
| 102 | + this.genericType = type; |
| 103 | + this.url = url; |
| 104 | + this.username = username; |
| 105 | + this.password = password; |
| 106 | + this.driver = driver; |
| 107 | + |
| 108 | + connectToDb(); |
| 109 | + initialize(); |
| 110 | + } |
| 111 | + |
| 112 | + public JdbcConnection(final Class<T> type, String url, String username, String password, String driver, EnumDatabaseCase databaseCase) |
| 113 | + { |
| 114 | + this.genericType = type; |
| 115 | + this.url = url; |
| 116 | + this.username = username; |
| 117 | + this.password = password; |
| 118 | + this.driver = driver; |
| 119 | + this.databaseCase = databaseCase; |
| 120 | + |
| 121 | + connectToDb(); |
| 122 | + initialize(); |
| 123 | + } |
| 124 | + |
| 125 | + private void initialize() |
| 126 | + { |
| 127 | + if (!this.isInitializationCompleted) { |
| 128 | + // this section is only for finding private fields |
| 129 | + // in a class to add them in field class |
| 130 | + Field[] fields = genericType.getDeclaredFields(); |
| 131 | + for (Field field : fields) { |
| 132 | + if (Modifier.isPrivate(field.getModifiers())) { |
| 133 | + privateFields.put(field.getName(), field); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + try |
| 138 | + { |
| 139 | + // getSimpleName() will not work for static methods |
| 140 | + this.tableName = genericType.newInstance().getClass().getSimpleName().toLowerCase(); |
| 141 | + |
| 142 | + List<String> listOfFieldNames = new ArrayList<>(privateFields.keySet()); |
| 143 | + |
| 144 | + for (String string : listOfFieldNames) { |
| 145 | + this.columnsMap.put(string, StringUtility.toSentenceUnderScore(string)); |
| 146 | + } |
| 147 | + } |
| 148 | + catch(Exception e) |
| 149 | + { |
| 150 | + e.printStackTrace(); |
| 151 | + } |
| 152 | + |
| 153 | + this.isInitializationCompleted = true; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * A method for connecting to the database |
| 159 | + */ |
| 160 | + private void connectToDb() { |
| 161 | + try |
| 162 | + { |
| 163 | + Class.forName(driver); |
| 164 | + dbConnection = DriverManager.getConnection(url, username, password); |
| 165 | + } |
| 166 | + catch(Exception e) |
| 167 | + { |
| 168 | + e.printStackTrace(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public Connection getConnection() |
| 173 | + { |
| 174 | + return dbConnection; |
| 175 | + } |
| 176 | + |
| 177 | + public List<T> getAll() |
| 178 | + { |
| 179 | + List<T> list = new ArrayList<>(); |
| 180 | + |
| 181 | + try |
| 182 | + { |
| 183 | + // List<String> columns = new ArrayList<String>(columnsMap.values()); |
| 184 | + String query = SELECT_ALL.replaceFirst(TABLE_NAME, tableName); |
| 185 | + statement = dbConnection.createStatement(); |
| 186 | + resultSet = statement.executeQuery(query); |
| 187 | + |
| 188 | + while (resultSet.next()) { |
| 189 | + T refObject = genericType.newInstance(); |
| 190 | + this.loadResultSetIntoObject(resultSet, refObject); |
| 191 | + list.add(refObject); |
| 192 | + } |
| 193 | + |
| 194 | + return list; |
| 195 | + } |
| 196 | + catch(Exception e) |
| 197 | + { |
| 198 | + e.printStackTrace(); |
| 199 | + } |
| 200 | + |
| 201 | + return null; |
| 202 | + } |
| 203 | + |
| 204 | + private void loadResultSetIntoObject(ResultSet resultSet2, T refObject) throws IllegalArgumentException, IllegalAccessException, SQLException { |
| 205 | + Class<?> classDenoter = refObject.getClass(); |
| 206 | + for (Field field : classDenoter.getDeclaredFields()) { |
| 207 | + String name = field.getName(); |
| 208 | + field.setAccessible(true); |
| 209 | + Object value = resultSet2.getObject(columnsMap.get(name)); |
| 210 | + Class<?> type = field.getType(); |
| 211 | + if (ConvertUtility.isPrimitive(type)) { |
| 212 | + Class<?> boxed = ConvertUtility.boxPrimitiveClass(type); |
| 213 | + value = boxed.cast(value); |
| 214 | + } |
| 215 | + field.set(refObject, value); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + public T getAll(Object condition) |
| 220 | + { |
| 221 | + return null; |
| 222 | + } |
| 223 | +} |
0 commit comments