From 94da9bfd2a46268387de6667d3f055e4399683b6 Mon Sep 17 00:00:00 2001 From: ygl Date: Fri, 7 Dec 2018 14:50:20 +0800 Subject: [PATCH] updated --- src/_sqlite3.py | 25 +++++++++++++++++++------ src/_sqlite3_build.py | 12 ++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/_sqlite3.py b/src/_sqlite3.py index 9ef0952..321de2f 100644 --- a/src/_sqlite3.py +++ b/src/_sqlite3.py @@ -31,10 +31,11 @@ import sys import weakref import threading try: - from __pypy__ import newlist_hint + from __pypy__ import newlist_hint, add_memory_pressure except ImportError: assert '__pypy__' not in sys.builtin_module_names newlist_hint = lambda sizehint: [] + add_memory_pressure = lambda size: None if sys.version_info[0] >= 3: StandardError = Exception @@ -150,10 +151,13 @@ class NotSupportedError(DatabaseError): def connect(database, timeout=5.0, detect_types=0, isolation_level="", - check_same_thread=True, factory=None, cached_statements=100): + check_same_thread=True, factory=None, cached_statements=100, + uri=0): factory = Connection if not factory else factory return factory(database, timeout, detect_types, isolation_level, - check_same_thread, factory, cached_statements) + check_same_thread, factory, cached_statements, uri) + add_memory_pressure(100 * 1024) + return res def _unicode_text_factory(x): @@ -197,14 +201,23 @@ class Connection(object): _db = None def __init__(self, database, timeout=5.0, detect_types=0, isolation_level="", - check_same_thread=True, factory=None, cached_statements=100): + check_same_thread=True, factory=None, cached_statements=100, uri=0): self.__initialized = True db_star = _ffi.new('sqlite3 **') if isinstance(database, unicode): database = database.encode('utf-8') - if _lib.sqlite3_open(database, db_star) != _lib.SQLITE_OK: - raise OperationalError("Could not open database") + if _lib.SQLITE_OPEN_URI != 0: + if uri and _lib.SQLITE_OPEN_URI == 0: + raise NotSupportedError("URIs not supported") + flags = _lib.SQLITE_OPEN_READWRITE | _lib.SQLITE_OPEN_CREATE + if uri: + flags |= _lib.SQLITE_OPEN_URI + if _lib.sqlite3_open_v2(database, db_star, flags, _ffi.NULL) != _lib.SQLITE_OK: + raise OperationalError("Could not open database") + else: + if _lib.sqlite3_open(database, db_star) != _lib.SQLITE_OK: + raise OperationalError("Could not open database") self._db = db_star[0] if timeout is not None: timeout = int(timeout * 1000) # pysqlite2 uses timeout in seconds diff --git a/src/_sqlite3_build.py b/src/_sqlite3_build.py index d920028..dae699f 100644 --- a/src/_sqlite3_build.py +++ b/src/_sqlite3_build.py @@ -105,7 +105,12 @@ static void *const SQLITE_TRANSIENT; #define SQLITE_DETERMINISTIC ... #define SQLITE_PREPARE_PERSISTENT ... +static const long SQLITE_OPEN_URI; +static const long SQLITE_OPEN_READWRITE; +static const long SQLITE_OPEN_CREATE; + const char *sqlite3_libversion(void); +int sqlite3_libversion_number(void); typedef ... sqlite3; typedef ... sqlite3_stmt; @@ -120,6 +125,13 @@ int sqlite3_open( sqlite3 **ppDb /* OUT: SQLite db handle */ ); +int sqlite3_open_v2( + const char *filename, /* Database filename (UTF-8) */ + sqlite3 **ppDb, /* OUT: SQLite db handle */ + int flags, /* Flags */ + const char *zVfs /* Name of VFS module to use */ +); + int sqlite3_close(sqlite3 *); int sqlite3_busy_timeout(sqlite3*, int ms);