Commit 71564067 authored by Julien Muchembled's avatar Julien Muchembled

sqlite: accept -d URI, where query string can configure the DB connection

parent a0280bec
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
# for more information about the syntax. # for more information about the syntax.
# ~ and ~user constructions are expanded for all paths, even for those that # ~ and ~user constructions are expanded for all paths, even for those that
# may appear in 'database' option. # may appear in 'database' option (except SQLite URI).
# Common parameters. # Common parameters.
[DEFAULT] [DEFAULT]
...@@ -58,7 +58,12 @@ partitions: 12 ...@@ -58,7 +58,12 @@ partitions: 12
# database: Storage nodes only. Syntax for: # database: Storage nodes only. Syntax for:
# - MySQL: [user[:password]@]database[unix_socket] # - MySQL: [user[:password]@]database[unix_socket]
# Database must be created manually. # Database must be created manually.
# - SQLite: path # - SQLite: path or URI
# URI extends the https://www.sqlite.org/c3ref/open.html syntax
# by accepting some PRAGMA settings via the query string
# (currently supported: cache_size, journal_mode, synchronous),
# but it can't be used yet to pass open options
# because Python 2.7's sqlite3.connect does not support URIs.
# engine: Optional parameter for MySQL. # engine: Optional parameter for MySQL.
# Can be InnoDB (default) or RocksDB. # Can be InnoDB (default) or RocksDB.
......
...@@ -20,8 +20,9 @@ import sqlite3 ...@@ -20,8 +20,9 @@ import sqlite3
from hashlib import sha1 from hashlib import sha1
import string import string
import traceback import traceback
from urlparse import urlsplit, parse_qsl
from . import LOG_QUERIES from . import LOG_QUERIES, DatabaseFailure
from .manager import DatabaseManager, splitOIDField from .manager import DatabaseManager, splitOIDField
from neo.lib import logging, util from neo.lib import logging, util
from neo.lib.exception import NonReadableCell, UndoPackError from neo.lib.exception import NonReadableCell, UndoPackError
...@@ -72,7 +73,22 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -72,7 +73,22 @@ class SQLiteDatabaseManager(DatabaseManager):
VERSION = 4 VERSION = 4
def _parse(self, database): def _parse(self, database):
self.db = os.path.expanduser(database) pragmas = self.pragmas = {}
if database.startswith('file:'):
database = urlsplit(database)
for k, v in parse_qsl(database.query, keep_blank_values=True,
strict_parsing=True):
if k in pragmas:
raise DatabaseFailure("duplicate pragma: " + k)
if k not in ('cache_size', 'journal_mode', 'synchronous'):
raise DatabaseFailure("unsupported pragma: " + k)
pragmas[k] = v
self.db = database.path
else:
self.db = os.path.expanduser(database)
if self.UNSAFE:
pragmas.setdefault('synchronous', 'OFF')
pragmas.setdefault('journal_mode', 'MEMORY')
def _close(self): def _close(self):
self.conn.close() self.conn.close()
...@@ -82,10 +98,14 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -82,10 +98,14 @@ class SQLiteDatabaseManager(DatabaseManager):
self.conn = sqlite3.connect(self.db, check_same_thread=False) self.conn = sqlite3.connect(self.db, check_same_thread=False)
self.conn.text_factory = str self.conn.text_factory = str
self.lockFile(self.db) self.lockFile(self.db)
if self.UNSAFE: try:
q = self.query for pragma in map("PRAGMA %s = %s".__mod__,
q("PRAGMA synchronous = OFF").fetchall() self.pragmas.iteritems()):
q("PRAGMA journal_mode = MEMORY").fetchall() logging.info(pragma)
self.query(pragma).fetchall()
except Exception:
self.close()
raise
self._config = {} self._config = {}
def _getDevPath(self): def _getDevPath(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment