1 18 19 package sync4j.syncclient.sps.jdbc; 20 21 import sync4j.syncclient.sps.common.*; 22 23 24 31 32 public class DataStoreSql { 33 34 36 43 public static String selectQuery(DataStore dataStore, Record record) { 44 45 String queryFields = null; 46 String query = null; 47 48 String function = null; 49 String nameFieldKey = null; 50 51 Object params = null; 52 53 for (int i=0; i < dataStore.getRecordMetadata().getFieldMetadata().length; i++) { 54 55 function = (String ) dataStore.getRecordMetadata().getFieldMetadata()[i].getAttributes().get("function"); 56 57 if (function.equals("key")) { 58 nameFieldKey = dataStore.getRecordMetadata().getFieldMetadata()[i].getName(); 59 } 60 61 } 62 63 FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata(); 64 65 for(int i = 0; i < fmd.length; i++) { 66 67 if( queryFields == null) { 68 queryFields = fmd[i].getName(); 69 } else { 70 queryFields = queryFields + "," + " " + fmd[i].getName(); 71 } 72 73 } 74 75 query = "SELECT" +" " + queryFields + " " + "FROM" + " " + dataStore.getDataStoreName() + " " + 76 "WHERE " + nameFieldKey + " = ?"; 77 78 return query; 79 80 } 81 82 83 84 91 public static String insertQuery(DataStore dataStore, Record record) { 92 93 String fieldValue = null; 94 String fieldsValue = null; 95 String fieldTmp = null; 96 97 String query = null; 98 99 String nameFieldsQuery = null; 100 101 FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata(); 102 103 104 for(int i = 0; i < fmd.length; i++) { 105 106 fieldValue = "?"; 107 108 if(fieldsValue == null) { 109 fieldsValue = fieldValue; 110 } else { 111 fieldsValue = fieldsValue + "," + " " + fieldValue; 112 } 113 114 115 if(nameFieldsQuery == null) { 116 nameFieldsQuery = fmd[i].getName(); 117 } else { 118 nameFieldsQuery = nameFieldsQuery + "," + " " + fmd[i].getName(); 119 } 120 121 } 122 123 124 query = "INSERT INTO" + " " + dataStore.getDataStoreName() + " " + "(" + nameFieldsQuery + ")" + " " + "VALUES("+ fieldsValue +")"; 125 126 return query; 127 128 } 129 130 131 132 139 public static String updateQuery(DataStore dataStore, Record record) { 140 141 String fieldValue = null; 142 String fieldsValue = null; 143 144 String queryFields = null; 145 String query = null; 146 147 String function = null; 148 String nameFieldKey = null; 149 150 for (int i=0; i < dataStore.getRecordMetadata().getFieldMetadata().length; i++) { 151 152 function = (String ) dataStore.getRecordMetadata().getFieldMetadata()[i].getAttributes().get("function"); 153 154 if (function.equals("key")) { 155 nameFieldKey = dataStore.getRecordMetadata().getFieldMetadata()[i].getName(); 156 } 157 158 } 159 160 FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata(); 161 162 for(int i = 1; i < fmd.length; i++) { 163 164 165 fieldValue = fmd[i].getName() + "=?"; 166 167 if(fieldsValue == null) { 168 fieldsValue = fieldValue; 169 } else { 170 fieldsValue = fieldsValue + "," + fieldValue; 171 } 172 173 } 174 175 query = "UPDATE" + " " + dataStore.getDataStoreName() + " SET " + fieldsValue + 176 " WHERE " + nameFieldKey + " = ?"; 177 178 return query; 179 180 } 181 182 183 184 191 public static String deleteQuery(DataStore dataStore, Record record) { 192 193 String fieldsValue = null; 194 195 String queryFields = null; 196 String query = null; 197 198 String function = null; 199 String nameFieldKey = null; 200 201 for (int i=0; i < dataStore.getRecordMetadata().getFieldMetadata().length; i++) { 202 203 function = (String ) dataStore.getRecordMetadata().getFieldMetadata()[i].getAttributes().get("function"); 204 205 if (function.equals("key")) { 206 nameFieldKey = dataStore.getRecordMetadata().getFieldMetadata()[i].getName(); 207 } 208 209 } 210 211 query = "DELETE FROM " + dataStore.getDataStoreName() + 212 " WHERE " + nameFieldKey +" = ?"; 213 214 return query; 215 216 } 217 218 219 220 226 public static String allRecordsQuery(DataStore dataStore) { 227 228 String queryFields = null; 229 String query = null; 230 231 FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata(); 232 233 for(int i = 0; i < fmd.length; i++) { 234 if( queryFields == null) { 235 queryFields = fmd[i].getName(); 236 } else { 237 queryFields = queryFields + "," + " " + fmd[i].getName(); 238 } 239 240 } 241 242 query = "SELECT" + " " + queryFields + " " + "FROM" + " " + dataStore.getDataStoreName(); 243 244 return query; 245 246 } 247 248 249 250 256 public static String RecordsByStateByDateQuery(DataStore dataStore, String nameFieldState, String nameFieldTimestamp) { 257 258 String queryFields = null; 259 String query = null; 260 261 String where = null; 262 263 FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata(); 264 265 for(int i = 0; i < fmd.length; i++) { 266 if( queryFields == null) { 267 queryFields = fmd[i].getName(); 268 } else { 269 queryFields = queryFields + "," + " " + fmd[i].getName(); 270 } 271 272 } 273 274 275 if (nameFieldState != null && nameFieldTimestamp != null) { 276 where = " " + "where " + nameFieldState + " = ? and " + nameFieldTimestamp + " > ?"; 277 } else if (nameFieldState != null && nameFieldTimestamp == null) { 278 where = " " + "where " + nameFieldState + " = ?"; 279 } else if (nameFieldState == null && nameFieldTimestamp != null) { 280 where = " " + "where " + nameFieldTimestamp + " > ?"; 281 } 282 283 query = "SELECT" + " " + queryFields + " " + "FROM" + " " + dataStore.getDataStoreName() + where; 284 285 return query; 286 287 } 288 289 290 291 297 public static String getNextKeyQuery(DataStore dataStore, String nameFieldKey) { 298 299 String query = null; 300 301 query = "SELECT MAX(" + nameFieldKey + ") + 1 FROM" + " " + dataStore.getDataStoreName(); 302 303 return query; 304 305 } 306 307 } | Popular Tags |