19 package org.sleuthkit.autopsy.coreutils;
22 import java.io.IOException;
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.ResultSetMetaData;
28 import java.sql.SQLException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.function.BooleanSupplier;
33 import java.util.function.Consumer;
34 import java.util.logging.Level;
61 private final AbstractFile
file;
71 static <T> Consumer<T> doNothing() {
83 this.forAllColumnNamesConsumer =
Builder.doNothing();
84 this.forAllStringValuesConsumer =
Builder.doNothing();
85 this.forAllLongValuesConsumer =
Builder.doNothing();
86 this.forAllIntegerValuesConsumer =
Builder.doNothing();
87 this.forAllFloatValuesConsumer =
Builder.doNothing();
88 this.forAllBlobValuesConsumer =
Builder.doNothing();
89 this.forAllTableValuesConsumer =
Builder.doNothing();
101 this.forAllColumnNamesConsumer = action;
114 this.forAllStringValuesConsumer = action;
127 this.forAllIntegerValuesConsumer = action;
140 this.forAllFloatValuesConsumer = action;
153 this.forAllLongValuesConsumer = action;
166 this.forAllBlobValuesConsumer = action;
180 this.forAllTableValuesConsumer = action;
195 private final AbstractFile
file;
220 this.file = builder.
file;
233 try (ResultSet tableNameResult = conn.createStatement()
234 .executeQuery(
"SELECT name FROM sqlite_master "
235 +
" WHERE type= 'table' ")) {
236 List<String> tableNames =
new ArrayList<>();
237 while (tableNameResult.next()) {
238 tableNames.add(tableNameResult.getString(
"name"));
241 }
catch (SQLException ex) {
242 throw new SQLiteTableReaderException(ex);
257 try (ResultSet countResult = conn.createStatement()
258 .executeQuery(
"SELECT count (*) as count FROM "
259 +
"\"" + tableName +
"\"")) {
260 return countResult.getInt(
"count");
261 }
catch (SQLException ex) {
277 try (ResultSet columnCount = conn.createStatement()
278 .executeQuery(String.format(SELECT_ALL_QUERY, tableName))) {
279 return columnCount.getMetaData().getColumnCount();
280 }
catch (SQLException ex) {
296 readHelper(String.format(SELECT_ALL_QUERY, tableName), () ->
false);
313 readHelper(String.format(SELECT_ALL_QUERY, tableName) +
" LIMIT " + limit
314 +
" OFFSET " + offset, () ->
false);
328 if (Objects.isNull(prevTableName) || !prevTableName.equals(tableName)) {
329 prevTableName = tableName;
332 readHelper(String.format(SELECT_ALL_QUERY, tableName), condition);
343 if (!liveResultSet) {
349 while (columnNameIndex < totalColumnCount) {
350 if (condition.getAsBoolean()) {
354 .getColumnName(++columnNameIndex));
357 while (unfinishedRow || queryResults.next()) {
358 while (currRowColumnIndex < totalColumnCount) {
359 if (condition.getAsBoolean()) {
360 unfinishedRow =
true;
364 Object item = queryResults.getObject(++currRowColumnIndex);
365 if (item instanceof String) {
367 }
else if (item instanceof Integer) {
369 }
else if (item instanceof Double) {
371 }
else if (item instanceof Long) {
373 }
else if (item instanceof byte[]) {
379 unfinishedRow =
false;
384 }
catch (SQLException ex) {
398 if (Objects.isNull(conn)) {
400 Class.forName(
"org.sqlite.JDBC");
406 conn = DriverManager.getConnection(
"jdbc:sqlite:" + localDiskPath);
408 | ClassNotFoundException | SQLException ex) {
409 throw new SQLiteTableReaderException(ex);
434 List<AbstractFile> metaFiles = fileManager.
findFiles(
435 sqliteFile.getDataSource(), metaFileName,
436 sqliteFile.getParent().getName());
438 if (metaFiles != null) {
439 for (AbstractFile metaFile : metaFiles) {
458 throws IOException, NoCurrentCaseException {
461 + File.separator + fileId + file.getName();
462 File localDatabaseFile =
new File(localDiskPath);
463 if (!localDatabaseFile.exists()) {
466 return localDiskPath;
479 statement = conn.prepareStatement(query);
480 queryResults = statement.executeQuery();
481 currentMetadata = queryResults.getMetaData();
482 totalColumnCount = currentMetadata.getColumnCount();
483 liveResultSet =
true;
484 }
catch (SQLException ex) {
494 if (Objects.nonNull(statement)) {
497 if (Objects.nonNull(queryResults)) {
498 queryResults.close();
500 liveResultSet =
false;
501 }
catch (SQLException ex) {
502 logger.log(Level.SEVERE,
"Failed to close table resources", ex);
514 if (Objects.nonNull(conn)) {
517 }
catch (SQLException ex) {
518 throw new SQLiteTableReaderException(ex);
541 logger.log(Level.SEVERE,
"Failed to close reader in finalizer", ex);
SQLiteTableReader(Builder builder)
Builder forAllIntegerValues(Consumer< Integer > action)
FileManager getFileManager()
static final String SELECT_ALL_QUERY
List< String > getTableNames()
String getTempDirectory()
int getRowCount(String tableName)
Builder forAllStringValues(Consumer< String > action)
int getColumnCount(String tableName)
ResultSetMetaData currentMetadata
static final Logger logger
Builder forAllLongValues(Consumer< Long > action)
void closeTableResources()
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
Builder forAllColumnNames(Consumer< String > action)
Builder(AbstractFile file)
Builder forAllTableValues(Consumer< Object > action)
Consumer< String > forAllColumnNamesConsumer
Builder forAllFloatValues(Consumer< Double > action)
void read(String tableName, BooleanSupplier condition)
Consumer< Long > forAllLongValuesConsumer
String copyFileToTempDirectory(AbstractFile file, long fileId)
void read(String tableName, int limit, int offset)
Consumer< String > forAllStringValuesConsumer
void readHelper(String query, BooleanSupplier condition)
Consumer< Object > forAllTableValuesConsumer
Consumer< Integer > forAllIntegerValuesConsumer
SleuthkitCase getSleuthkitCase()
PreparedStatement statement
Consumer< byte[]> forAllBlobValuesConsumer
Builder forAllBlobValues(Consumer< byte[]> action)
Consumer< Double > forAllFloatValuesConsumer
void findAndCopySQLiteMetaFile(AbstractFile sqliteFile, String metaFileName)
synchronized List< AbstractFile > findFiles(String fileName)
synchronized static Logger getLogger(String name)
static Case getCurrentCaseThrows()
SQLiteTableReader build()
void openTableResources(String query)
void read(String tableName)