Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1609)

Unified Diff: plaso/parsers/sqlite.py

Issue 316460043: [plaso] Added database schema matching to sqlite plugins #1093 (Closed)
Patch Set: Changes after review Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: plaso/parsers/sqlite.py
diff --git a/plaso/parsers/sqlite.py b/plaso/parsers/sqlite.py
index f1cd8bb9bfb663624d42ecca9c021627b9381b3a..acfe38a69a1a7bd618593ab5ec20aa67aac4d721 100644
--- a/plaso/parsers/sqlite.py
+++ b/plaso/parsers/sqlite.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-"""This file contains a SQLite parser."""
+"""SQLite parser."""
import logging
import os
@@ -20,7 +20,7 @@ from plaso.parsers import plugins
class SQLiteCache(plugins.BasePluginCache):
- """A cache storing query results for SQLite plugins."""
+ """Cache for storing results of SQL queries."""
def CacheQueryResults(
self, sql_results, attribute_name, key_name, column_names):
@@ -82,10 +82,21 @@ class SQLiteCache(plugins.BasePluginCache):
class SQLiteDatabase(object):
- """A simple wrapper for opening up a SQLite database."""
+ """SQLite database.
+
+ Attributes:
+ schema (dict[str, str]): schema as an SQL query per table name, for
+ example {'Users': 'CREATE TABLE Users ("id" INTEGER PRIMARY KEY, ...)'}.
+ """
_READ_BUFFER_SIZE = 65536
+ SCHEMA_QUERY = (
+ u'SELECT tbl_name, sql '
+ u'FROM sqlite_master '
+ u'WHERE type = "table" AND tbl_name != "xp_proc" '
+ u'AND tbl_name != "sqlite_sequence"')
+
def __init__(self, filename, temporary_directory=None):
"""Initializes the database object.
@@ -97,15 +108,17 @@ class SQLiteDatabase(object):
self._database = None
self._filename = filename
self._is_open = False
- self._table_names = []
self._temp_db_file_path = u''
self._temporary_directory = temporary_directory
self._temp_wal_file_path = u''
+ self.schema = {}
+
@property
def tables(self):
"""list[str]: names of all the tables."""
- return self._table_names
+ if self._is_open:
+ return self.schema.keys()
def _CopyFileObjectToTemporaryFile(self, file_object, temporary_file):
"""Copies the contents of the file-like object to a temporary file.
@@ -121,8 +134,8 @@ class SQLiteDatabase(object):
data = file_object.read(self._READ_BUFFER_SIZE)
def Close(self):
- """Closes the database connection and clean up the temporary file."""
- self._table_names = []
+ """Closes the database connection and cleans up the temporary file."""
+ self.schema = {}
if self._is_open:
self._database.close()
@@ -221,10 +234,11 @@ class SQLiteDatabase(object):
self._database.row_factory = sqlite3.Row
cursor = self._database.cursor()
- sql_results = cursor.execute(
- u'SELECT name FROM sqlite_master WHERE type="table"')
+ sql_results = cursor.execute(self.SCHEMA_QUERY)
- self._table_names = [row[0] for row in sql_results]
+ self.schema = {
+ table_name: u' '.join(query.split())
+ for table_name, query in sql_results}
except sqlite3.DatabaseError as exception:
self._database.close()
@@ -251,6 +265,9 @@ class SQLiteDatabase(object):
Returns:
sqlite3.Cursor: results.
+
+ Raises:
+ sqlite3.DatabaseError: if querying the database fails.
"""
cursor = self._database.cursor()
cursor.execute(query)

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b