示例#1
0
文件: _sqlite3.py 项目: Mu-L/pypy
    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.")
示例#2
0
    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.")
示例#3
0
    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])
示例#4
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)
     finally:
         _lib.sqlite3_finalize(statement_star[0])
示例#5
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])
示例#6
0
    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.")
示例#7
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)
        finally:
            _lib.sqlite3_finalize(statement_star[0])
示例#8
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])
示例#9
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])