def __init__(self, connection, sql): self.__con = connection self._in_use = False if not isinstance(sql, basestring): raise Warning("SQL is of wrong type. Must be string or unicode.") if '\0' in sql: raise ValueError("the query contains a null character") if sql.strip(): first_word = sql.lstrip().split()[0].upper() if first_word == '': self._type = _STMT_TYPE_INVALID if first_word == "SELECT": self._type = _STMT_TYPE_SELECT elif first_word == "INSERT": self._type = _STMT_TYPE_INSERT elif first_word == "UPDATE": self._type = _STMT_TYPE_UPDATE elif first_word == "DELETE": self._type = _STMT_TYPE_DELETE elif first_word == "REPLACE": self._type = _STMT_TYPE_REPLACE else: self._type = _STMT_TYPE_OTHER else: self._type = _STMT_TYPE_INVALID if isinstance(sql, unicode): sql = sql.encode('utf-8') self._valid = True statement_star = _ffi.new('sqlite3_stmt **') next_char = _ffi.new('char **') c_sql = _ffi.new("char[]", sql) ret = _lib.sqlite3_prepare_v2(self.__con._db, c_sql, -1, statement_star, next_char) self._statement = statement_star[0] if ret == _lib.SQLITE_OK and not self._statement: # an empty statement, work around that, as it's the least trouble self._type = _STMT_TYPE_SELECT c_sql = _ffi.new("char[]", b"select 42 where 42 = 23") ret = _lib.sqlite3_prepare_v2(self.__con._db, c_sql, -1, statement_star, next_char) self._statement = statement_star[0] self._valid = False if ret != _lib.SQLITE_OK: raise self.__con._get_exception(ret) self.__con._remember_statement(self) tail = _ffi.string(next_char[0]).decode('utf-8') if _check_remaining_sql(tail): raise Warning("You can only execute one statement at a time.")
def __init__(self, connection, sql): self.__con = connection self._in_use = False if not isinstance(sql, basestring): raise Warning("SQL is of wrong type. Must be string or unicode.") if '\0' in sql: raise ValueError("the query contains a null character") first_word = sql.lstrip().split(" ")[0].upper() if first_word == "": self._type = _STMT_TYPE_INVALID elif first_word == "SELECT": self._type = _STMT_TYPE_SELECT elif first_word == "INSERT": self._type = _STMT_TYPE_INSERT elif first_word == "UPDATE": self._type = _STMT_TYPE_UPDATE elif first_word == "DELETE": self._type = _STMT_TYPE_DELETE elif first_word == "REPLACE": self._type = _STMT_TYPE_REPLACE else: self._type = _STMT_TYPE_OTHER if isinstance(sql, unicode): sql = sql.encode('utf-8') statement_star = _ffi.new('sqlite3_stmt **') next_char = _ffi.new('char **') c_sql = _ffi.new("char[]", sql) ret = _lib.sqlite3_prepare_v2(self.__con._db, c_sql, -1, statement_star, next_char) self._statement = statement_star[0] if ret == _lib.SQLITE_OK and not self._statement: # an empty statement, work around that, as it's the least trouble self._type = _STMT_TYPE_SELECT c_sql = _ffi.new("char[]", b"select 42") ret = _lib.sqlite3_prepare_v2(self.__con._db, c_sql, -1, statement_star, next_char) self._statement = statement_star[0] if ret != _lib.SQLITE_OK: raise self.__con._get_exception(ret) self.__con._remember_statement(self) tail = _ffi.string(next_char[0]).decode('utf-8') if _check_remaining_sql(tail): raise Warning("You can only execute one statement at a time.")
def commit(self): self._check_thread() self._check_closed() if not self._in_transaction: return # PyPy fix for non-refcounting semantics: since 2.7.13 (and in # <= 2.6.x), the statements are not automatically reset upon # commit. However, if this is followed by some specific SQL # operations like "drop table", these open statements come in # the way and cause the "drop table" to fail. On CPython the # problem is much less important because typically all the old # statements are freed already by reference counting. So here, # we copy all the still-alive statements to another list which # is usually ignored, except if we get SQLITE_LOCKED # afterwards---at which point we reset all statements in this # list. self.__statements_already_committed = self.__statements[:] statement_star = _ffi.new('sqlite3_stmt **') ret = _lib.sqlite3_prepare_v2(self._db, b"COMMIT", -1, statement_star, _ffi.NULL) try: if ret != _lib.SQLITE_OK: raise self._get_exception(ret) ret = _lib.sqlite3_step(statement_star[0]) if ret != _lib.SQLITE_DONE: raise self._get_exception(ret) self._in_transaction = False finally: _lib.sqlite3_finalize(statement_star[0])
def __init__(self, database, timeout=5.0, detect_types=0, isolation_level="", 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.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 _lib.sqlite3_busy_timeout(self._db, timeout) self.row_factory = None self.text_factory = _unicode_text_factory self._detect_types = detect_types self.isolation_level = isolation_level self.__cursors = [] self.__cursors_counter = 0 self.__statements = [] self.__statements_counter = 0 self.__rawstatements = set() self._statement_cache = _StatementCache(self, cached_statements) self.__statements_already_committed = [] self.__func_cache = {} self.__aggregates = {} self.__aggregate_instances = {} self.__collations = {} if check_same_thread: self.__thread_ident = threading.get_ident() if not check_same_thread and _lib.sqlite3_libversion_number() < 3003001: raise NotSupportedError("shared connections not available") self.Error = Error self.Warning = Warning self.InterfaceError = InterfaceError self.DatabaseError = DatabaseError self.InternalError = InternalError self.OperationalError = OperationalError self.ProgrammingError = ProgrammingError self.IntegrityError = IntegrityError self.DataError = DataError self.NotSupportedError = NotSupportedError
def _begin(self): statement_star = _ffi.new('sqlite3_stmt **') ret = _lib.sqlite3_prepare_v2(self._db, self._begin_statement, -1, statement_star, _ffi.NULL) try: if ret != _lib.SQLITE_OK: raise self._get_exception(ret) ret = _lib.sqlite3_step(statement_star[0]) if ret != _lib.SQLITE_DONE: raise self._get_exception(ret) finally: _lib.sqlite3_finalize(statement_star[0])
def _begin(self): statement_star = _ffi.new("sqlite3_stmt **") ret = _lib.sqlite3_prepare_v2(self._db, self.__begin_statement, -1, statement_star, _ffi.NULL) try: if ret != _lib.SQLITE_OK: raise self._get_exception(ret) ret = _lib.sqlite3_step(statement_star[0]) if ret != _lib.SQLITE_DONE: raise self._get_exception(ret) self._in_transaction = True finally: _lib.sqlite3_finalize(statement_star[0])
def __init__( self, database, timeout=5.0, detect_types=0, isolation_level="", check_same_thread=True, factory=None, cached_statements=100, ): 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") self._db = db_star[0] if timeout is not None: timeout = int(timeout * 1000) # pysqlite2 uses timeout in seconds _lib.sqlite3_busy_timeout(self._db, timeout) self.row_factory = None self.text_factory = _unicode_text_factory self._detect_types = detect_types self._in_transaction = False self.isolation_level = isolation_level self.__cursors = [] self.__cursors_counter = 0 self.__statements = [] self.__statements_counter = 0 self.__rawstatements = set() self._statement_cache = _StatementCache(self, cached_statements) self.__func_cache = {} self.__aggregates = {} self.__aggregate_instances = {} self.__collations = {} if check_same_thread: self.__thread_ident = _thread_get_ident() self.Error = Error self.Warning = Warning self.InterfaceError = InterfaceError self.DatabaseError = DatabaseError self.InternalError = InternalError self.OperationalError = OperationalError self.ProgrammingError = ProgrammingError self.IntegrityError = IntegrityError self.DataError = DataError self.NotSupportedError = NotSupportedError
def executescript(self, sql): self.__check_cursor() self._reset = False if isinstance(sql, unicode): sql = sql.encode('utf-8') elif not isinstance(sql, str): raise ValueError("script argument must be unicode or string.") statement_star = _ffi.new('sqlite3_stmt **') next_char = _ffi.new('char **') self.__connection.commit() while True: c_sql = _ffi.new("char[]", sql) rc = _lib.sqlite3_prepare(self.__connection._db, c_sql, -1, statement_star, next_char) if rc != _lib.SQLITE_OK: raise self.__connection._get_exception(rc) rc = _lib.SQLITE_ROW while rc == _lib.SQLITE_ROW: if not statement_star[0]: rc = _lib.SQLITE_OK else: rc = _lib.sqlite3_step(statement_star[0]) if rc != _lib.SQLITE_DONE: _lib.sqlite3_finalize(statement_star[0]) if rc == _lib.SQLITE_OK: break else: raise self.__connection._get_exception(rc) rc = _lib.sqlite3_finalize(statement_star[0]) if rc != _lib.SQLITE_OK: raise self.__connection._get_exception(rc) sql = _ffi.string(next_char[0]) if not sql: break return self
def __init__(self, database, timeout=5.0, detect_types=0, isolation_level="", check_same_thread=True, factory=None, cached_statements=100): 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") self._db = db_star[0] if timeout is not None: timeout = int(timeout * 1000) # pysqlite2 uses timeout in seconds _lib.sqlite3_busy_timeout(self._db, timeout) self.row_factory = None self.text_factory = _unicode_text_factory self._detect_types = detect_types self._in_transaction = False self.isolation_level = isolation_level self.__cursors = [] self.__cursors_counter = 0 self.__statements = [] self.__statements_counter = 0 self.__rawstatements = set() self._statement_cache = _StatementCache(self, cached_statements) self.__statements_already_committed = [] self.__func_cache = {} self.__aggregates = {} self.__aggregate_instances = {} self.__collations = {} if check_same_thread: self.__thread_ident = _thread_get_ident() self.Error = Error self.Warning = Warning self.InterfaceError = InterfaceError self.DatabaseError = DatabaseError self.InternalError = InternalError self.OperationalError = OperationalError self.ProgrammingError = ProgrammingError self.IntegrityError = IntegrityError self.DataError = DataError self.NotSupportedError = NotSupportedError
def __init__(self, connection, sql): self.__con = connection self._in_use = False if not isinstance(sql, basestring): raise Warning("SQL is of wrong type. Must be string or unicode.") if '\0' in sql: raise ValueError("the query contains a null character") to_check = sql.lstrip().upper() self._valid = bool(to_check) self._is_dml = to_check.startswith(('INSERT', 'UPDATE', 'DELETE', 'REPLACE')) statement_star = _ffi.new('sqlite3_stmt **') next_char = _ffi.new('char **') c_sql = _ffi.new("char[]", sql.encode('utf-8')) ret = _lib.sqlite3_prepare_v2(self.__con._db, c_sql, -1, statement_star, next_char) self._statement = statement_star[0] if ret == _lib.SQLITE_OK and not self._statement: # an empty statement, work around that, as it's the least trouble c_sql = _ffi.new("char[]", b"select 42") ret = _lib.sqlite3_prepare_v2(self.__con._db, c_sql, -1, statement_star, next_char) self._statement = statement_star[0] if ret != _lib.SQLITE_OK: raise self.__con._get_exception(ret) self.__con._remember_statement(self) tail = _ffi.string(next_char[0]).decode('utf-8') if _check_remaining_sql(tail): raise Warning("You can only execute one statement at a time.")
def rollback(self): self._check_thread() self._check_closed() if not self.in_transaction: return self.__do_all_statements(Statement._reset, True) statement_star = _ffi.new('sqlite3_stmt **') ret = _lib.sqlite3_prepare_v2(self._db, b"ROLLBACK", -1, statement_star, _ffi.NULL) try: if ret != _lib.SQLITE_OK: raise self._get_exception(ret) ret = _lib.sqlite3_step(statement_star[0]) if ret != _lib.SQLITE_DONE: raise self._get_exception(ret) finally: _lib.sqlite3_finalize(statement_star[0])
def rollback(self): self._check_thread() self._check_closed() if not self._in_transaction: return self.__do_all_statements(Statement._reset, True) statement_star = _ffi.new("sqlite3_stmt **") ret = _lib.sqlite3_prepare_v2(self._db, b"ROLLBACK", -1, statement_star, _ffi.NULL) try: if ret != _lib.SQLITE_OK: raise self._get_exception(ret) ret = _lib.sqlite3_step(statement_star[0]) if ret != _lib.SQLITE_DONE: raise self._get_exception(ret) self._in_transaction = False finally: _lib.sqlite3_finalize(statement_star[0])
def commit(self): self._check_thread() self._check_closed() if not self._in_transaction: return self.__do_all_statements(Statement._reset, False) statement_star = _ffi.new('sqlite3_stmt **') ret = _lib.sqlite3_prepare_v2(self._db, b"COMMIT", -1, statement_star, _ffi.NULL) try: if ret != _lib.SQLITE_OK: raise self._get_exception(ret) ret = _lib.sqlite3_step(statement_star[0]) if ret != _lib.SQLITE_DONE: raise self._get_exception(ret) self._in_transaction = False finally: _lib.sqlite3_finalize(statement_star[0])