Esempio n. 1
0
class Cursor(BaseCursor):
    driver = "sqlite"

    # sqlite is smart - this is only a noop
    def binary(self, s):
        return s

    # capture and translate exceptions
    def _tryExecute(self, func, *params, **kw):
        try:
            ret = func(*params, **kw)
        except sqlite3.ProgrammingError, e:
            if ((e.args[0].startswith("column")
                 and e.args[0].endswith("not unique"))
                    or e.args[0].startswith("UNIQUE constraint failed")):
                raise sqlerrors.ColumnNotUnique(e)
            elif e.args[0] == 'attempt to write a readonly database':
                raise sqlerrors.ReadOnlyDatabase(str(e))
            raise sqlerrors.CursorError(e.args[0], e)
        except sqlite3.DatabaseError, e:
            if e.args[0].startswith('duplicate column name:'):
                raise sqlerrors.DuplicateColumnName(str(e))
            if e.args[0].startswith('database is locked'):
                raise sqlerrors.DatabaseLocked(str(e))
            if e.args[0].startswith("no such table"):
                raise sqlerrors.InvalidTable(str(e))
            raise sqlerrors.CursorError(e.args[0], e)
Esempio n. 2
0
    def execute(self, sql, *args, **kw):
        self._executeCheck(sql)
        keys = []

        kw.pop("start_transaction", True)
        args, kw  = self._executeArgs(args, kw)

        # don't do unnecessary work
        if len(args) or len(kw):
            sql, keys = _mungeSQL(sql)

        # if we have args, we can not have keywords
        if len(args):
            if len(kw) or len(keys):
                raise sqlerrors.CursorError(
                    "Do not pass both positional and named bind arguments",
                    *args, **kw)
            ret = self._tryExecute(self._cursor.execute, sql, args)
        elif len(keys): # check that all keys used in the query appear in the kw
            if False in [kw.has_key(x) for x in keys]:
                raise CursorError(
                    "Query keys not defined in named argument dict",
                    sorted(keys), sorted(kw.keys()))
            # need to transform kw into pozitional args
            ret = self._tryExecute(self._cursor.execute, sql,
                                   [kw[x] for x in keys])
        else:
            ret = self._tryExecute(self._cursor.execute, sql)
        if ret == self._cursor:
            return self
        return ret
Esempio n. 3
0
 def __rowDict(self, row):
     assert (self._cursor)
     if row is None:
         return None
     if len(row) != len(self._cursor.description):
         raise sqlerrors.CursorError(
             "Cursor description doew not match row data",
             row=row,
             desc=self._cursor.description)
     return dict(zip(self.fields(), row))
Esempio n. 4
0
 def _tryExecute(self, func, *params, **kw):
     try:
         ret = func(*params, **kw)
     except sqlite3.ProgrammingError, e:
         if e.args[0].startswith("column") and e.args[0].endswith(
                 "not unique"):
             raise sqlerrors.ColumnNotUnique(e)
         elif e.args[0] == 'attempt to write a readonly database':
             raise sqlerrors.ReadOnlyDatabase(str(e))
         raise sqlerrors.CursorError(e.args[0], e)
Esempio n. 5
0
 def execute(self, sql, *args, **kw):
     self._executeCheck(sql)
     # process the query args
     args, kw = self._executeArgs(args, kw)
     # force dbi compliance here. we prefer args over the kw
     if len(args) == 0:
         return self._tryExecute(self._cursor.execute, sql, **kw)
     if len(kw):
         raise sqlerrors.CursorError(
             "Do not pass both positional and named bind arguments", *args,
             **kw)
     return self._tryExecute(self._cursor.execute, sql, *args)
Esempio n. 6
0
 def _tryExecute(self, func, *params, **kw):
     try:
         ret = func(*params, **kw)
     except pgsql.DatabaseError, e:
         msg = e.args[0]
         if msg.find("violates foreign key constraint") > 0:
             raise sqlerrors.ConstraintViolation(msg)
         if re.search('relation \S+ does not exist', msg, re.I):
             raise sqlerrors.InvalidTable(msg)
         if re.search("duplicate key (value )?violates unique constraint", msg):
             raise sqlerrors.ColumnNotUnique(msg)
         raise sqlerrors.CursorError(msg, e)
Esempio n. 7
0
 def execute(self, sql, *args, **kw):
     self._executeCheck(sql)
     args, kw = self._executeArgs(args, kw)
     if len(args) == 0:
         ret = self._tryExecute(self._cursor.execute, sql, **kw)
     else:
         st = kw.pop("start_transaction", True)
         if len(kw):
             raise sqlerrors.CursorError(
                 "Do not pass both positional and named bind arguments",
                 *args, **kw)
         kw["start_transaction"] = st
         ret = self._tryExecute(self._cursor.execute, sql, *args, **kw)
     if ret == self._cursor:
         return self
     return ret
Esempio n. 8
0
 def currval(self):
     if self.__currval is None:
         raise sqlerrors.CursorError(
             "Sequence has currval undefined until nextval() is called",
             self.name)
     return self.__currval