Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SqliteCentralRepo.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2020 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 SqliteCentralRepo extends RdbmsCentralRepo {
43 
44  private final static Logger LOGGER = Logger.getLogger(SqliteCentralRepo.class.getName());
45 
46  private static SqliteCentralRepo instance;
47 
48  private BasicDataSource connectionPool = null;
49 
50  private final SqliteCentralRepoSettings 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 SqliteCentralRepo getInstance() throws CentralRepoException {
65  if (instance == null) {
66  instance = new SqliteCentralRepo();
67  }
68 
69  return instance;
70  }
71 
78  private SqliteCentralRepo() throws CentralRepoException {
79  dbSettings = new SqliteCentralRepoSettings();
80  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
81  }
82 
83  @Override
84  public void shutdownConnections() throws CentralRepoException {
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 CentralRepoException("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 CentralRepoException {
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  CentralRepoDbUtil.closeConnection(conn);
145  }
146 
147  RdbmsCentralRepoFactory centralRepoSchemaFactory = new RdbmsCentralRepoFactory(CentralRepoPlatforms.SQLITE, dbSettings);
148  centralRepoSchemaFactory.insertDefaultDatabaseContent();
149  } finally {
150  releaseExclusiveLock();
151  }
152  }
153 
158  @Messages({"SqliteEamDb.databaseMissing.message=Central repository database missing"})
159  private void setupConnectionPool(boolean foreignKeysEnabled) throws CentralRepoException {
160 
161  if (dbSettings.dbFileExists() == false) {
162  throw new CentralRepoException("Central repository database missing", Bundle.SqliteEamDb_databaseMissing_message());
163  }
164 
165  connectionPool = new BasicDataSource();
166  connectionPool.setDriverClassName(dbSettings.getDriver());
167  connectionPool.setUrl(dbSettings.getConnectionURL());
168 
169  // tweak pool configuration
170  connectionPool.setInitialSize(50);
171  connectionPool.setMaxTotal(-1);
172  connectionPool.setMaxIdle(-1);
173  connectionPool.setMaxWaitMillis(1000);
174  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
175  if (foreignKeysEnabled) {
176  connectionPool.setConnectionInitSqls(Arrays.asList("PRAGMA foreign_keys = ON"));
177  } else {
178  connectionPool.setConnectionInitSqls(Arrays.asList("PRAGMA foreign_keys = OFF"));
179  }
180  }
181 
192  @Messages({"SqliteEamDb.connectionFailedMessage.message=Error getting connection to database.",
193  "SqliteEamDb.centralRepositoryDisabled.message=Central Repository module is not enabled."})
194  @Override
195  protected Connection connect(boolean foreignKeys) throws CentralRepoException {
196  synchronized (this) {
197  if (!CentralRepository.isEnabled()) {
198  throw new CentralRepoException("Central repository database missing", Bundle.SqliteEamDb_centralRepositoryDisabled_message()); // NON-NLS
199  }
200  if (connectionPool == null) {
201  setupConnectionPool(foreignKeys);
202  }
203  try {
204  return connectionPool.getConnection();
205  } catch (SQLException ex) {
206  throw new CentralRepoException("Error getting connection from connection pool.", Bundle.SqliteEamDb_connectionFailedMessage_message(), ex); // NON-NLS
207  }
208  }
209  }
210 
219  @Override
220  protected Connection connect() throws CentralRepoException {
221  return connect(true);
222  }
223 
224  @Override
225  protected String getConflictClause() {
226  // For sqlite, our conflict clause is part of the table schema
227  return "";
228  }
229 
230  @Override
231  protected Connection getEphemeralConnection() {
232  return this.dbSettings.getEphemeralConnection();
233  }
242  @Override
243  public void newDbInfo(String name, String value) throws CentralRepoException {
244  try {
245  acquireExclusiveLock();
246  super.newDbInfo(name, value);
247  } finally {
248  releaseExclusiveLock();
249  }
250  }
251 
261  @Override
262  public String getDbInfo(String name) throws CentralRepoException {
263  try {
264  acquireSharedLock();
265  return super.getDbInfo(name);
266  } finally {
267  releaseSharedLock();
268  }
269  }
270 
279  @Override
280  public void updateDbInfo(String name, String value) throws CentralRepoException {
281  try {
282  acquireExclusiveLock();
283  super.updateDbInfo(name, value);
284  } finally {
285  releaseExclusiveLock();
286  }
287  }
288 
294  @Override
295  public CorrelationCase newCase(Case autopsyCase) throws CentralRepoException {
296  try {
297  acquireExclusiveLock();
298  return super.newCase(autopsyCase);
299  } finally {
300  releaseExclusiveLock();
301  }
302  }
303 
304  @Override
305  public void addDataSourceObjectId(int rowId, long dataSourceObjectId) throws CentralRepoException {
306  try {
307  acquireExclusiveLock();
308  super.addDataSourceObjectId(rowId, dataSourceObjectId);
309  } finally {
310  releaseExclusiveLock();
311  }
312  }
313 
321  @Override
322  public CorrelationCase newCase(CorrelationCase eamCase) throws CentralRepoException {
323  try {
324  acquireExclusiveLock();
325  return super.newCase(eamCase);
326  } finally {
327  releaseExclusiveLock();
328  }
329  }
330 
336  @Override
337  public void updateCase(CorrelationCase eamCase) throws CentralRepoException {
338  try {
339  acquireExclusiveLock();
340  super.updateCase(eamCase);
341  } finally {
342  releaseExclusiveLock();
343  }
344  }
345 
353  @Override
354  public CorrelationCase getCaseByUUID(String caseUUID) throws CentralRepoException {
355  try {
356  acquireSharedLock();
357  return super.getCaseByUUID(caseUUID);
358  } finally {
359  releaseSharedLock();
360  }
361  }
362 
370  @Override
371  public CorrelationCase getCaseById(int caseId) throws CentralRepoException {
372  try {
373  acquireSharedLock();
374  return super.getCaseById(caseId);
375  } finally {
376  releaseSharedLock();
377  }
378 
379  }
380 
386  @Override
387  public List<CorrelationCase> getCases() throws CentralRepoException {
388  try {
389  acquireSharedLock();
390  return super.getCases();
391  } finally {
392  releaseSharedLock();
393  }
394  }
395 
401  @Override
402  public CorrelationDataSource newDataSource(CorrelationDataSource eamDataSource) throws CentralRepoException {
403  try {
404  acquireExclusiveLock();
405  return super.newDataSource(eamDataSource);
406  } finally {
407  releaseExclusiveLock();
408  }
409  }
410 
420  @Override
421  public CorrelationDataSource getDataSource(CorrelationCase correlationCase, Long caseDbDataSourceId) throws CentralRepoException {
422  try {
423  acquireSharedLock();
424  return super.getDataSource(correlationCase, caseDbDataSourceId);
425  } finally {
426  releaseSharedLock();
427  }
428  }
429 
439  @Override
440  public CorrelationDataSource getDataSourceById(CorrelationCase correlationCase, int dataSourceId) throws CentralRepoException {
441  try {
442  acquireSharedLock();
443  return super.getDataSourceById(correlationCase, dataSourceId);
444  } finally {
445  releaseSharedLock();
446  }
447  }
448 
454  @Override
455  public List<CorrelationDataSource> getDataSources() throws CentralRepoException {
456  try {
457  acquireSharedLock();
458  return super.getDataSources();
459  } finally {
460  releaseSharedLock();
461  }
462  }
463 
472  @Override
473  public void updateDataSourceName(CorrelationDataSource eamDataSource, String newName) throws CentralRepoException {
474  try {
475  acquireExclusiveLock();
476  super.updateDataSourceName(eamDataSource, newName);
477  } finally {
478  releaseExclusiveLock();
479  }
480  }
481 
487  @Override
488  public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
489  try {
490  acquireExclusiveLock();
491  super.updateDataSourceMd5Hash(eamDataSource);
492  } finally {
493  releaseExclusiveLock();
494  }
495  }
496 
502  @Override
503  public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
504  try {
505  acquireExclusiveLock();
506  super.updateDataSourceSha1Hash(eamDataSource);
507  } finally {
508  releaseExclusiveLock();
509  }
510  }
511 
518  @Override
519  public void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
520  try {
521  acquireExclusiveLock();
522  super.updateDataSourceSha256Hash(eamDataSource);
523  } finally {
524  releaseExclusiveLock();
525  }
526  }
527 
534  @Override
535  public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws CentralRepoException {
536  try {
537  acquireExclusiveLock();
538  super.addArtifactInstance(eamArtifact);
539  } finally {
540  releaseExclusiveLock();
541  }
542  }
543 
544  @Override
545  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
546  try {
547  acquireSharedLock();
548  return super.getArtifactInstancesByTypeValue(aType, value);
549  } finally {
550  releaseSharedLock();
551  }
552  }
553 
554  @Override
555  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValues(CorrelationAttributeInstance.Type aType, List<String> values) throws CentralRepoException, CorrelationAttributeNormalizationException {
556  try {
557  acquireSharedLock();
558  return super.getArtifactInstancesByTypeValues(aType, values);
559  } finally {
560  releaseSharedLock();
561  }
562  }
563 
564  @Override
565  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValuesAndCases(CorrelationAttributeInstance.Type aType, List<String> values, List<Integer> caseIds) throws CentralRepoException, CorrelationAttributeNormalizationException {
566  try {
567  acquireSharedLock();
568  return super.getArtifactInstancesByTypeValuesAndCases(aType, values, caseIds);
569  } finally {
570  releaseSharedLock();
571  }
572  }
573 
586  @Override
587  public Long getCountArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
588  try {
589  acquireSharedLock();
590  return super.getCountArtifactInstancesByTypeValue(aType, value);
591  } finally {
592  releaseSharedLock();
593  }
594  }
595 
596  @Override
597  public int getFrequencyPercentage(CorrelationAttributeInstance corAttr) throws CentralRepoException, CorrelationAttributeNormalizationException {
598  try {
599  acquireSharedLock();
600  return super.getFrequencyPercentage(corAttr);
601  } finally {
602  releaseSharedLock();
603  }
604  }
605 
618  @Override
619  public Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
620  try {
621  acquireSharedLock();
622  return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
623  } finally {
624  releaseSharedLock();
625  }
626  }
627 
628  @Override
629  public Long getCountUniqueDataSources() throws CentralRepoException {
630  try {
631  acquireSharedLock();
632  return super.getCountUniqueDataSources();
633  } finally {
634  releaseSharedLock();
635  }
636  }
637 
649  @Override
650  public Long getCountArtifactInstancesByCaseDataSource(CorrelationDataSource correlationDataSource) throws CentralRepoException {
651  try {
652  acquireSharedLock();
653  return super.getCountArtifactInstancesByCaseDataSource(correlationDataSource);
654  } finally {
655  releaseSharedLock();
656  }
657  }
658 
663  @Override
664  public void commitAttributeInstancesBulk() throws CentralRepoException {
665  try {
666  acquireExclusiveLock();
667  super.commitAttributeInstancesBulk();
668  } finally {
669  releaseExclusiveLock();
670  }
671  }
672 
676  @Override
677  public void bulkInsertCases(List<CorrelationCase> cases) throws CentralRepoException {
678  try {
679  acquireExclusiveLock();
680  super.bulkInsertCases(cases);
681  } finally {
682  releaseExclusiveLock();
683  }
684  }
685 
696  @Override
697  public void setAttributeInstanceKnownStatus(CorrelationAttributeInstance eamArtifact, TskData.FileKnown knownStatus) throws CentralRepoException {
698  try {
699  acquireExclusiveLock();
700  super.setAttributeInstanceKnownStatus(eamArtifact, knownStatus);
701  } finally {
702  releaseExclusiveLock();
703  }
704  }
705 
714  @Override
715  public Long getCountArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
716  try {
717  acquireSharedLock();
718  return super.getCountArtifactInstancesKnownBad(aType, value);
719  } finally {
720  releaseSharedLock();
721  }
722  }
723 
736  @Override
737  public List<String> getListCasesHavingArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
738  try {
739  acquireSharedLock();
740  return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
741  } finally {
742  releaseSharedLock();
743  }
744  }
745 
753  @Override
754  public void deleteReferenceSet(int referenceSetID) throws CentralRepoException {
755  try {
756  acquireExclusiveLock();
757  super.deleteReferenceSet(referenceSetID);
758  } finally {
759  releaseExclusiveLock();
760  }
761  }
762 
772  @Override
773  public boolean isValueInReferenceSet(String value, int referenceSetID, int correlationTypeID) throws CentralRepoException, CorrelationAttributeNormalizationException {
774  try {
775  acquireSharedLock();
776  return super.isValueInReferenceSet(value, referenceSetID, correlationTypeID);
777  } finally {
778  releaseSharedLock();
779  }
780  }
781 
790  @Override
791  public void processInstanceTable(CorrelationAttributeInstance.Type type, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
792  try {
793  acquireSharedLock();
794  super.processInstanceTable(type, instanceTableCallback);
795  } finally {
796  releaseSharedLock();
797  }
798  }
799 
808  @Override
809  public void processInstanceTableWhere(CorrelationAttributeInstance.Type type, String whereClause, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
810  try {
811  acquireSharedLock();
812  super.processInstanceTableWhere(type, whereClause, instanceTableCallback);
813  } finally {
814  releaseSharedLock();
815  }
816  }
817 
826  @Override
827  public void processSelectClause(String selectClause, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
828  try {
829  acquireSharedLock();
830  super.processSelectClause(selectClause, instanceTableCallback);
831  } finally {
832  releaseSharedLock();
833  }
834  }
835 
848  @Override
849  public boolean referenceSetExists(String referenceSetName, String version) throws CentralRepoException {
850  try {
851  acquireSharedLock();
852  return super.referenceSetExists(referenceSetName, version);
853  } finally {
854  releaseSharedLock();
855  }
856  }
857 
866  @Override
867  public boolean isArtifactKnownBadByReference(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
868  try {
869  acquireSharedLock();
870  return super.isArtifactKnownBadByReference(aType, value);
871  } finally {
872  releaseSharedLock();
873  }
874  }
875 
885  @Override
886  public CentralRepoOrganization newOrganization(CentralRepoOrganization eamOrg) throws CentralRepoException {
887  try {
888  acquireExclusiveLock();
889  return super.newOrganization(eamOrg);
890  } finally {
891  releaseExclusiveLock();
892  }
893  }
894 
902  @Override
903  public List<CentralRepoOrganization> getOrganizations() throws CentralRepoException {
904  try {
905  acquireSharedLock();
906  return super.getOrganizations();
907  } finally {
908  releaseSharedLock();
909  }
910  }
911 
921  @Override
922  public CentralRepoOrganization getOrganizationByID(int orgID) throws CentralRepoException {
923  try {
924  acquireSharedLock();
925  return super.getOrganizationByID(orgID);
926  } finally {
927  releaseSharedLock();
928  }
929  }
930 
931  @Override
932  public void updateOrganization(CentralRepoOrganization updatedOrganization) throws CentralRepoException {
933  try {
934  acquireExclusiveLock();
935  super.updateOrganization(updatedOrganization);
936  } finally {
937  releaseExclusiveLock();
938  }
939  }
940 
941  @Override
942  public void deleteOrganization(CentralRepoOrganization organizationToDelete) throws CentralRepoException {
943  try {
944  acquireExclusiveLock();
945  super.deleteOrganization(organizationToDelete);
946  } finally {
947  releaseExclusiveLock();
948  }
949  }
950 
960  @Override
961  public int newReferenceSet(CentralRepoFileSet eamGlobalSet) throws CentralRepoException {
962  try {
963  acquireExclusiveLock();
964  return super.newReferenceSet(eamGlobalSet);
965  } finally {
966  releaseExclusiveLock();
967  }
968  }
969 
979  @Override
980  public CentralRepoFileSet getReferenceSetByID(int referenceSetID) throws CentralRepoException {
981  try {
982  acquireSharedLock();
983  return super.getReferenceSetByID(referenceSetID);
984  } finally {
985  releaseSharedLock();
986  }
987  }
988 
998  @Override
999  public List<CentralRepoFileSet> getAllReferenceSets(CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1000  try {
1001  acquireSharedLock();
1002  return super.getAllReferenceSets(correlationType);
1003  } finally {
1004  releaseSharedLock();
1005  }
1006  }
1007 
1017  @Override
1018  public void addReferenceInstance(CentralRepoFileInstance eamGlobalFileInstance, CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1019  try {
1020  acquireExclusiveLock();
1021  super.addReferenceInstance(eamGlobalFileInstance, correlationType);
1022  } finally {
1023  releaseExclusiveLock();
1024  }
1025  }
1026 
1032  @Override
1033  public void bulkInsertReferenceTypeEntries(Set<CentralRepoFileInstance> globalInstances, CorrelationAttributeInstance.Type contentType) throws CentralRepoException {
1034  try {
1035  acquireExclusiveLock();
1036  super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
1037  } finally {
1038  releaseExclusiveLock();
1039  }
1040  }
1041 
1052  @Override
1053  public List<CentralRepoFileInstance> getReferenceInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String aValue) throws CentralRepoException, CorrelationAttributeNormalizationException {
1054  try {
1055  acquireSharedLock();
1056  return super.getReferenceInstancesByTypeValue(aType, aValue);
1057  } finally {
1058  releaseSharedLock();
1059  }
1060  }
1061 
1071  @Override
1072  public int newCorrelationType(CorrelationAttributeInstance.Type newType) throws CentralRepoException {
1073  try {
1074  acquireExclusiveLock();
1075  return super.newCorrelationType(newType);
1076  } finally {
1077  releaseExclusiveLock();
1078  }
1079  }
1080 
1090  @Override
1091  public List<CorrelationAttributeInstance.Type> getDefinedCorrelationTypes() throws CentralRepoException {
1092  try {
1093  acquireSharedLock();
1094  return super.getDefinedCorrelationTypes();
1095  } finally {
1096  releaseSharedLock();
1097  }
1098  }
1099 
1109  @Override
1110  public List<CorrelationAttributeInstance.Type> getEnabledCorrelationTypes() throws CentralRepoException {
1111  try {
1112  acquireSharedLock();
1113  return super.getEnabledCorrelationTypes();
1114  } finally {
1115  releaseSharedLock();
1116  }
1117  }
1118 
1128  @Override
1129  public List<CorrelationAttributeInstance.Type> getSupportedCorrelationTypes() throws CentralRepoException {
1130  try {
1131  acquireSharedLock();
1132  return super.getSupportedCorrelationTypes();
1133  } finally {
1134  releaseSharedLock();
1135  }
1136  }
1137 
1145  @Override
1146  public void updateCorrelationType(CorrelationAttributeInstance.Type aType) throws CentralRepoException {
1147  try {
1148  acquireExclusiveLock();
1149  super.updateCorrelationType(aType);
1150  } finally {
1151  releaseExclusiveLock();
1152  }
1153  }
1154 
1164  @Override
1165  public CorrelationAttributeInstance.Type getCorrelationTypeById(int typeId) throws CentralRepoException {
1166  try {
1167  acquireSharedLock();
1168  return super.getCorrelationTypeById(typeId);
1169  } finally {
1170  releaseSharedLock();
1171  }
1172  }
1173 
1179  @Override
1180  public void upgradeSchema() throws CentralRepoException, SQLException, IncompatibleCentralRepoException {
1181  try {
1182  acquireExclusiveLock();
1183  super.upgradeSchema();
1184  } finally {
1185  releaseExclusiveLock();
1186  }
1187  }
1188 
1200  @Override
1201  public CoordinationService.Lock getExclusiveMultiUserDbLock() throws CentralRepoException {
1202  // Multiple users are not supported for SQLite
1203  return null;
1204  }
1205 
1211  private void acquireExclusiveLock() {
1212  rwLock.writeLock().lock();
1213  }
1214 
1220  private void releaseExclusiveLock() {
1221  rwLock.writeLock().unlock();
1222  }
1223 
1229  private void acquireSharedLock() {
1230  rwLock.readLock().lock();
1231  }
1232 
1238  private void releaseSharedLock() {
1239  rwLock.readLock().unlock();
1240  }
1241 
1242  @Override
1243  boolean doesColumnExist(Connection conn, String tableName, String columnName) throws SQLException {
1244  final String tableInfoQueryTemplate = "PRAGMA table_info(%s)"; //NON-NLS
1245  ResultSet resultSet = null;
1246  Statement statement = null;
1247  boolean columnExists = false;
1248  try {
1249  statement = conn.createStatement();
1250  resultSet = statement.executeQuery(String.format(tableInfoQueryTemplate, tableName));
1251  while (resultSet.next()) {
1252  // the second value ( 2 ) is the column name
1253  if (resultSet.getString(2).equals(columnName)) {
1254  columnExists = true;
1255  break;
1256  }
1257  }
1258  } finally {
1259  CentralRepoDbUtil.closeResultSet(resultSet);
1260  CentralRepoDbUtil.closeStatement(statement);
1261  }
1262  return columnExists;
1263  }
1264 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.