Autopsy  4.13.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SqliteEamDb.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.datamodel;
20 
21 import java.sql.Connection;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.concurrent.locks.ReentrantReadWriteLock;
29 import java.util.logging.Level;
30 import org.apache.commons.dbcp2.BasicDataSource;
31 import org.openide.util.NbBundle.Messages;
33 import org.sleuthkit.datamodel.TskData;
36 
42 final class SqliteEamDb extends AbstractSqlEamDb {
43 
44  private final static Logger LOGGER = Logger.getLogger(SqliteEamDb.class.getName());
45 
46  private static SqliteEamDb instance;
47 
48  private BasicDataSource connectionPool = null;
49 
50  private final SqliteEamDbSettings dbSettings;
51 
52  // While the Sqlite database should only be used for single users, it is still
53  // possible for multiple threads to attempt to write to the database simultaneously.
54  private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(true);
55 
64  public synchronized static SqliteEamDb getInstance() throws EamDbException {
65  if (instance == null) {
66  instance = new SqliteEamDb();
67  }
68 
69  return instance;
70  }
71 
78  private SqliteEamDb() throws EamDbException {
79  dbSettings = new SqliteEamDbSettings();
80  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
81  }
82 
83  @Override
84  public void shutdownConnections() throws EamDbException {
85  try {
86  synchronized (this) {
87  if (null != connectionPool) {
88  connectionPool.close();
89  connectionPool = null; // force it to be re-created on next connect()
90  }
91  clearCaches();
92  }
93  } catch (SQLException ex) {
94  throw new EamDbException("Failed to close existing database connections.", ex); // NON-NLS
95  }
96  }
97 
98  @Override
99  public void updateSettings() {
100  synchronized (this) {
101  dbSettings.loadSettings();
102  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
103  }
104  }
105 
106  @Override
107  public void saveSettings() {
108  synchronized (this) {
109  dbSettings.saveSettings();
110  }
111  }
112 
113  @Override
114  public void reset() throws EamDbException {
115  try {
116  acquireExclusiveLock();
117 
118  Connection conn = connect();
119 
120  try {
121 
122  Statement dropContent = conn.createStatement();
123  dropContent.executeUpdate("DELETE FROM organizations");
124  dropContent.executeUpdate("DELETE FROM cases");
125  dropContent.executeUpdate("DELETE FROM data_sources");
126  dropContent.executeUpdate("DELETE FROM reference_sets");
127  dropContent.executeUpdate("DELETE FROM artifact_types");
128  dropContent.executeUpdate("DELETE FROM db_info");
129 
130  String instancesTemplate = "DELETE FROM %s_instances";
131  String referencesTemplate = "DELETE FROM global_files";
132  for (CorrelationAttributeInstance.Type type : defaultCorrelationTypes) {
133  dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
134  // FUTURE: support other reference types
135  if (type.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
136  dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
137  }
138  }
139 
140  dropContent.executeUpdate("VACUUM");
141  } catch (SQLException ex) {
142  LOGGER.log(Level.WARNING, "Failed to reset database.", ex);
143  } finally {
144  EamDbUtil.closeConnection(conn);
145  }
146 
147  dbSettings.insertDefaultDatabaseContent();
148  } finally {
149  releaseExclusiveLock();
150  }
151  }
152 
157  @Messages({"SqliteEamDb.databaseMissing.message=Central repository database missing"})
158  private void setupConnectionPool(boolean foreignKeysEnabled) throws EamDbException {
159 
160  if (dbSettings.dbFileExists() == false) {
161  throw new EamDbException("Central repository database missing", Bundle.SqliteEamDb_databaseMissing_message());
162  }
163 
164  connectionPool = new BasicDataSource();
165  connectionPool.setDriverClassName(dbSettings.getDriver());
166  connectionPool.setUrl(dbSettings.getConnectionURL());
167 
168  // tweak pool configuration
169  connectionPool.setInitialSize(50);
170  connectionPool.setMaxTotal(-1);
171  connectionPool.setMaxIdle(-1);
172  connectionPool.setMaxWaitMillis(1000);
173  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
174  if (foreignKeysEnabled) {
175  connectionPool.setConnectionInitSqls(Arrays.asList("PRAGMA foreign_keys = ON"));
176  } else {
177  connectionPool.setConnectionInitSqls(Arrays.asList("PRAGMA foreign_keys = OFF"));
178  }
179  }
180 
191  @Messages({"SqliteEamDb.connectionFailedMessage.message=Error getting connection to database.",
192  "SqliteEamDb.centralRepositoryDisabled.message=Central Repository module is not enabled."})
193  @Override
194  protected Connection connect(boolean foreignKeys) throws EamDbException {
195  synchronized (this) {
196  if (!EamDb.isEnabled()) {
197  throw new EamDbException("Central repository database missing", Bundle.SqliteEamDb_centralRepositoryDisabled_message()); // NON-NLS
198  }
199  if (connectionPool == null) {
200  setupConnectionPool(foreignKeys);
201  }
202  try {
203  return connectionPool.getConnection();
204  } catch (SQLException ex) {
205  throw new EamDbException("Error getting connection from connection pool.", Bundle.SqliteEamDb_connectionFailedMessage_message(), ex); // NON-NLS
206  }
207  }
208  }
209 
218  @Override
219  protected Connection connect() throws EamDbException {
220  return connect(true);
221  }
222 
223  @Override
224  protected String getConflictClause() {
225  // For sqlite, our conflict clause is part of the table schema
226  return "";
227  }
228 
237  @Override
238  public void newDbInfo(String name, String value) throws EamDbException {
239  try {
240  acquireExclusiveLock();
241  super.newDbInfo(name, value);
242  } finally {
243  releaseExclusiveLock();
244  }
245  }
246 
256  @Override
257  public String getDbInfo(String name) throws EamDbException {
258  try {
259  acquireSharedLock();
260  return super.getDbInfo(name);
261  } finally {
262  releaseSharedLock();
263  }
264  }
265 
274  @Override
275  public void updateDbInfo(String name, String value) throws EamDbException {
276  try {
277  acquireExclusiveLock();
278  super.updateDbInfo(name, value);
279  } finally {
280  releaseExclusiveLock();
281  }
282  }
283 
289  @Override
290  public CorrelationCase newCase(Case autopsyCase) throws EamDbException {
291  try {
292  acquireExclusiveLock();
293  return super.newCase(autopsyCase);
294  } finally {
295  releaseExclusiveLock();
296  }
297  }
298 
299  @Override
300  public void addDataSourceObjectId(int rowId, long dataSourceObjectId) throws EamDbException {
301  try {
302  acquireExclusiveLock();
303  super.addDataSourceObjectId(rowId, dataSourceObjectId);
304  } finally {
305  releaseExclusiveLock();
306  }
307  }
308 
316  @Override
317  public CorrelationCase newCase(CorrelationCase eamCase) throws EamDbException {
318  try {
319  acquireExclusiveLock();
320  return super.newCase(eamCase);
321  } finally {
322  releaseExclusiveLock();
323  }
324  }
325 
331  @Override
332  public void updateCase(CorrelationCase eamCase) throws EamDbException {
333  try {
334  acquireExclusiveLock();
335  super.updateCase(eamCase);
336  } finally {
337  releaseExclusiveLock();
338  }
339  }
340 
348  @Override
349  public CorrelationCase getCaseByUUID(String caseUUID) throws EamDbException {
350  try {
351  acquireSharedLock();
352  return super.getCaseByUUID(caseUUID);
353  } finally {
354  releaseSharedLock();
355  }
356  }
357 
365  @Override
366  public CorrelationCase getCaseById(int caseId) throws EamDbException {
367  try {
368  acquireSharedLock();
369  return super.getCaseById(caseId);
370  } finally {
371  releaseSharedLock();
372  }
373 
374  }
375 
381  @Override
382  public List<CorrelationCase> getCases() throws EamDbException {
383  try {
384  acquireSharedLock();
385  return super.getCases();
386  } finally {
387  releaseSharedLock();
388  }
389  }
390 
396  @Override
397  public CorrelationDataSource newDataSource(CorrelationDataSource eamDataSource) throws EamDbException {
398  try {
399  acquireExclusiveLock();
400  return super.newDataSource(eamDataSource);
401  } finally {
402  releaseExclusiveLock();
403  }
404  }
405 
415  @Override
416  public CorrelationDataSource getDataSource(CorrelationCase correlationCase, Long caseDbDataSourceId) throws EamDbException {
417  try {
418  acquireSharedLock();
419  return super.getDataSource(correlationCase, caseDbDataSourceId);
420  } finally {
421  releaseSharedLock();
422  }
423  }
424 
434  @Override
435  public CorrelationDataSource getDataSourceById(CorrelationCase correlationCase, int dataSourceId) throws EamDbException {
436  try {
437  acquireSharedLock();
438  return super.getDataSourceById(correlationCase, dataSourceId);
439  } finally {
440  releaseSharedLock();
441  }
442  }
443 
449  @Override
450  public List<CorrelationDataSource> getDataSources() throws EamDbException {
451  try {
452  acquireSharedLock();
453  return super.getDataSources();
454  } finally {
455  releaseSharedLock();
456  }
457  }
458 
467  @Override
468  public void updateDataSourceName(CorrelationDataSource eamDataSource, String newName) throws EamDbException {
469  try {
470  acquireExclusiveLock();
471  super.updateDataSourceName(eamDataSource, newName);
472  } finally {
473  releaseExclusiveLock();
474  }
475  }
476 
482  @Override
483  public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws EamDbException {
484  try {
485  acquireExclusiveLock();
486  super.updateDataSourceMd5Hash(eamDataSource);
487  } finally {
488  releaseExclusiveLock();
489  }
490  }
491 
497  @Override
498  public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws EamDbException {
499  try {
500  acquireExclusiveLock();
501  super.updateDataSourceSha1Hash(eamDataSource);
502  } finally {
503  releaseExclusiveLock();
504  }
505  }
506 
513  @Override
514  public void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws EamDbException {
515  try {
516  acquireExclusiveLock();
517  super.updateDataSourceSha256Hash(eamDataSource);
518  } finally {
519  releaseExclusiveLock();
520  }
521  }
522 
529  @Override
530  public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws EamDbException {
531  try {
532  acquireExclusiveLock();
533  super.addArtifactInstance(eamArtifact);
534  } finally {
535  releaseExclusiveLock();
536  }
537  }
538 
539  @Override
540  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws EamDbException, CorrelationAttributeNormalizationException {
541  try {
542  acquireSharedLock();
543  return super.getArtifactInstancesByTypeValue(aType, value);
544  } finally {
545  releaseSharedLock();
546  }
547  }
548 
549  @Override
550  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValues(CorrelationAttributeInstance.Type aType, List<String> values) throws EamDbException, CorrelationAttributeNormalizationException {
551  try {
552  acquireSharedLock();
553  return super.getArtifactInstancesByTypeValues(aType, values);
554  } finally {
555  releaseSharedLock();
556  }
557  }
558 
559  @Override
560  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValuesAndCases(CorrelationAttributeInstance.Type aType, List<String> values, List<Integer> caseIds) throws EamDbException, CorrelationAttributeNormalizationException {
561  try {
562  acquireSharedLock();
563  return super.getArtifactInstancesByTypeValuesAndCases(aType, values, caseIds);
564  } finally {
565  releaseSharedLock();
566  }
567  }
568 
581  @Override
582  public Long getCountArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws EamDbException, CorrelationAttributeNormalizationException {
583  try {
584  acquireSharedLock();
585  return super.getCountArtifactInstancesByTypeValue(aType, value);
586  } finally {
587  releaseSharedLock();
588  }
589  }
590 
591  @Override
592  public int getFrequencyPercentage(CorrelationAttributeInstance corAttr) throws EamDbException, CorrelationAttributeNormalizationException {
593  try {
594  acquireSharedLock();
595  return super.getFrequencyPercentage(corAttr);
596  } finally {
597  releaseSharedLock();
598  }
599  }
600 
613  @Override
614  public Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttributeInstance.Type aType, String value) throws EamDbException, CorrelationAttributeNormalizationException {
615  try {
616  acquireSharedLock();
617  return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
618  } finally {
619  releaseSharedLock();
620  }
621  }
622 
623  @Override
624  public Long getCountUniqueDataSources() throws EamDbException {
625  try {
626  acquireSharedLock();
627  return super.getCountUniqueDataSources();
628  } finally {
629  releaseSharedLock();
630  }
631  }
632 
644  @Override
645  public Long getCountArtifactInstancesByCaseDataSource(CorrelationDataSource correlationDataSource) throws EamDbException {
646  try {
647  acquireSharedLock();
648  return super.getCountArtifactInstancesByCaseDataSource(correlationDataSource);
649  } finally {
650  releaseSharedLock();
651  }
652  }
653 
658  @Override
659  public void commitAttributeInstancesBulk() throws EamDbException {
660  try {
661  acquireExclusiveLock();
662  super.commitAttributeInstancesBulk();
663  } finally {
664  releaseExclusiveLock();
665  }
666  }
667 
671  @Override
672  public void bulkInsertCases(List<CorrelationCase> cases) throws EamDbException {
673  try {
674  acquireExclusiveLock();
675  super.bulkInsertCases(cases);
676  } finally {
677  releaseExclusiveLock();
678  }
679  }
680 
691  @Override
692  public void setAttributeInstanceKnownStatus(CorrelationAttributeInstance eamArtifact, TskData.FileKnown knownStatus) throws EamDbException {
693  try {
694  acquireExclusiveLock();
695  super.setAttributeInstanceKnownStatus(eamArtifact, knownStatus);
696  } finally {
697  releaseExclusiveLock();
698  }
699  }
700 
709  @Override
710  public Long getCountArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws EamDbException, CorrelationAttributeNormalizationException {
711  try {
712  acquireSharedLock();
713  return super.getCountArtifactInstancesKnownBad(aType, value);
714  } finally {
715  releaseSharedLock();
716  }
717  }
718 
731  @Override
732  public List<String> getListCasesHavingArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws EamDbException, CorrelationAttributeNormalizationException {
733  try {
734  acquireSharedLock();
735  return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
736  } finally {
737  releaseSharedLock();
738  }
739  }
740 
748  @Override
749  public void deleteReferenceSet(int referenceSetID) throws EamDbException {
750  try {
751  acquireExclusiveLock();
752  super.deleteReferenceSet(referenceSetID);
753  } finally {
754  releaseExclusiveLock();
755  }
756  }
757 
767  @Override
768  public boolean isValueInReferenceSet(String value, int referenceSetID, int correlationTypeID) throws EamDbException, CorrelationAttributeNormalizationException {
769  try {
770  acquireSharedLock();
771  return super.isValueInReferenceSet(value, referenceSetID, correlationTypeID);
772  } finally {
773  releaseSharedLock();
774  }
775  }
776 
785  @Override
786  public void processInstanceTable(CorrelationAttributeInstance.Type type, InstanceTableCallback instanceTableCallback) throws EamDbException {
787  try {
788  acquireSharedLock();
789  super.processInstanceTable(type, instanceTableCallback);
790  } finally {
791  releaseSharedLock();
792  }
793  }
794 
803  @Override
804  public void processInstanceTableWhere(CorrelationAttributeInstance.Type type, String whereClause, InstanceTableCallback instanceTableCallback) throws EamDbException {
805  try {
806  acquireSharedLock();
807  super.processInstanceTableWhere(type, whereClause, instanceTableCallback);
808  } finally {
809  releaseSharedLock();
810  }
811  }
812 
821  @Override
822  public void processSelectClause(String selectClause, InstanceTableCallback instanceTableCallback) throws EamDbException {
823  try {
824  acquireSharedLock();
825  super.processSelectClause(selectClause, instanceTableCallback);
826  } finally {
827  releaseSharedLock();
828  }
829  }
830 
843  @Override
844  public boolean referenceSetExists(String referenceSetName, String version) throws EamDbException {
845  try {
846  acquireSharedLock();
847  return super.referenceSetExists(referenceSetName, version);
848  } finally {
849  releaseSharedLock();
850  }
851  }
852 
861  @Override
862  public boolean isArtifactKnownBadByReference(CorrelationAttributeInstance.Type aType, String value) throws EamDbException, CorrelationAttributeNormalizationException {
863  try {
864  acquireSharedLock();
865  return super.isArtifactKnownBadByReference(aType, value);
866  } finally {
867  releaseSharedLock();
868  }
869  }
870 
880  @Override
881  public EamOrganization newOrganization(EamOrganization eamOrg) throws EamDbException {
882  try {
883  acquireExclusiveLock();
884  return super.newOrganization(eamOrg);
885  } finally {
886  releaseExclusiveLock();
887  }
888  }
889 
897  @Override
898  public List<EamOrganization> getOrganizations() throws EamDbException {
899  try {
900  acquireSharedLock();
901  return super.getOrganizations();
902  } finally {
903  releaseSharedLock();
904  }
905  }
906 
916  @Override
917  public EamOrganization getOrganizationByID(int orgID) throws EamDbException {
918  try {
919  acquireSharedLock();
920  return super.getOrganizationByID(orgID);
921  } finally {
922  releaseSharedLock();
923  }
924  }
925 
926  @Override
927  public void updateOrganization(EamOrganization updatedOrganization) throws EamDbException {
928  try {
929  acquireExclusiveLock();
930  super.updateOrganization(updatedOrganization);
931  } finally {
932  releaseExclusiveLock();
933  }
934  }
935 
936  @Override
937  public void deleteOrganization(EamOrganization organizationToDelete) throws EamDbException {
938  try {
939  acquireExclusiveLock();
940  super.deleteOrganization(organizationToDelete);
941  } finally {
942  releaseExclusiveLock();
943  }
944  }
945 
955  @Override
956  public int newReferenceSet(EamGlobalSet eamGlobalSet) throws EamDbException {
957  try {
958  acquireExclusiveLock();
959  return super.newReferenceSet(eamGlobalSet);
960  } finally {
961  releaseExclusiveLock();
962  }
963  }
964 
974  @Override
975  public EamGlobalSet getReferenceSetByID(int referenceSetID) throws EamDbException {
976  try {
977  acquireSharedLock();
978  return super.getReferenceSetByID(referenceSetID);
979  } finally {
980  releaseSharedLock();
981  }
982  }
983 
993  @Override
994  public List<EamGlobalSet> getAllReferenceSets(CorrelationAttributeInstance.Type correlationType) throws EamDbException {
995  try {
996  acquireSharedLock();
997  return super.getAllReferenceSets(correlationType);
998  } finally {
999  releaseSharedLock();
1000  }
1001  }
1002 
1012  @Override
1013  public void addReferenceInstance(EamGlobalFileInstance eamGlobalFileInstance, CorrelationAttributeInstance.Type correlationType) throws EamDbException {
1014  try {
1015  acquireExclusiveLock();
1016  super.addReferenceInstance(eamGlobalFileInstance, correlationType);
1017  } finally {
1018  releaseExclusiveLock();
1019  }
1020  }
1021 
1027  @Override
1028  public void bulkInsertReferenceTypeEntries(Set<EamGlobalFileInstance> globalInstances, CorrelationAttributeInstance.Type contentType) throws EamDbException {
1029  try {
1030  acquireExclusiveLock();
1031  super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
1032  } finally {
1033  releaseExclusiveLock();
1034  }
1035  }
1036 
1047  @Override
1048  public List<EamGlobalFileInstance> getReferenceInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String aValue) throws EamDbException, CorrelationAttributeNormalizationException {
1049  try {
1050  acquireSharedLock();
1051  return super.getReferenceInstancesByTypeValue(aType, aValue);
1052  } finally {
1053  releaseSharedLock();
1054  }
1055  }
1056 
1066  @Override
1067  public int newCorrelationType(CorrelationAttributeInstance.Type newType) throws EamDbException {
1068  try {
1069  acquireExclusiveLock();
1070  return super.newCorrelationType(newType);
1071  } finally {
1072  releaseExclusiveLock();
1073  }
1074  }
1075 
1085  @Override
1086  public List<CorrelationAttributeInstance.Type> getDefinedCorrelationTypes() throws EamDbException {
1087  try {
1088  acquireSharedLock();
1089  return super.getDefinedCorrelationTypes();
1090  } finally {
1091  releaseSharedLock();
1092  }
1093  }
1094 
1104  @Override
1105  public List<CorrelationAttributeInstance.Type> getEnabledCorrelationTypes() throws EamDbException {
1106  try {
1107  acquireSharedLock();
1108  return super.getEnabledCorrelationTypes();
1109  } finally {
1110  releaseSharedLock();
1111  }
1112  }
1113 
1123  @Override
1124  public List<CorrelationAttributeInstance.Type> getSupportedCorrelationTypes() throws EamDbException {
1125  try {
1126  acquireSharedLock();
1127  return super.getSupportedCorrelationTypes();
1128  } finally {
1129  releaseSharedLock();
1130  }
1131  }
1132 
1140  @Override
1141  public void updateCorrelationType(CorrelationAttributeInstance.Type aType) throws EamDbException {
1142  try {
1143  acquireExclusiveLock();
1144  super.updateCorrelationType(aType);
1145  } finally {
1146  releaseExclusiveLock();
1147  }
1148  }
1149 
1159  @Override
1160  public CorrelationAttributeInstance.Type getCorrelationTypeById(int typeId) throws EamDbException {
1161  try {
1162  acquireSharedLock();
1163  return super.getCorrelationTypeById(typeId);
1164  } finally {
1165  releaseSharedLock();
1166  }
1167  }
1168 
1174  @Override
1175  public void upgradeSchema() throws EamDbException, SQLException, IncompatibleCentralRepoException {
1176  try {
1177  acquireExclusiveLock();
1178  super.upgradeSchema();
1179  } finally {
1180  releaseExclusiveLock();
1181  }
1182  }
1183 
1195  @Override
1196  public CoordinationService.Lock getExclusiveMultiUserDbLock() throws EamDbException {
1197  // Multiple users are not supported for SQLite
1198  return null;
1199  }
1200 
1206  private void acquireExclusiveLock() {
1207  rwLock.writeLock().lock();
1208  }
1209 
1215  private void releaseExclusiveLock() {
1216  rwLock.writeLock().unlock();
1217  }
1218 
1224  private void acquireSharedLock() {
1225  rwLock.readLock().lock();
1226  }
1227 
1233  private void releaseSharedLock() {
1234  rwLock.readLock().unlock();
1235  }
1236 
1237  @Override
1238  boolean doesColumnExist(Connection conn, String tableName, String columnName) throws SQLException {
1239  final String tableInfoQueryTemplate = "PRAGMA table_info(%s)"; //NON-NLS
1240  ResultSet resultSet = null;
1241  Statement statement = null;
1242  boolean columnExists = false;
1243  try {
1244  statement = conn.createStatement();
1245  resultSet = statement.executeQuery(String.format(tableInfoQueryTemplate, tableName));
1246  while (resultSet.next()) {
1247  // the second value ( 2 ) is the column name
1248  if (resultSet.getString(2).equals(columnName)) {
1249  columnExists = true;
1250  break;
1251  }
1252  }
1253  } finally {
1254  EamDbUtil.closeResultSet(resultSet);
1255  EamDbUtil.closeStatement(statement);
1256  }
1257  return columnExists;
1258  }
1259 }

Copyright © 2012-2019 Basis Technology. Generated on: Tue Jan 7 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.