Esempio n. 1
0
def guard(fn):
    """Decorator function that converts sqlite exceptions into weedb exceptions."""
    def guarded_fn(*args, **kwargs):
        try:
            return fn(*args, **kwargs)
        except sqlite3.IntegrityError, e:
            raise weedb.IntegrityError(e)
        except sqlite3.OperationalError, e:
            msg = str(e).lower()
            if msg.startswith("unable to open"):
                raise weedb.PermissionError(e)
            elif msg.startswith("no such table"):
                raise weedb.NoTableError(e)
            elif msg.endswith("already exists"):
                raise weedb.TableExistsError(e)
            elif msg.startswith("no such column"):
                raise weedb.NoColumnError(e)
            else:
                raise weedb.OperationalError(e)
Esempio n. 2
0
 def guarded_fn(*args, **kwargs):
     try:
         return fn(*args, **kwargs)
     except sqlite3.IntegrityError as e:
         raise weedb.IntegrityError(e)
     except sqlite3.OperationalError as e:
         msg = str(e).lower()
         if msg.startswith("unable to open"):
             raise weedb.PermissionError(e)
         elif msg.startswith("no such table"):
             raise weedb.NoTableError(e)
         elif msg.endswith("already exists"):
             raise weedb.TableExistsError(e)
         elif msg.startswith("no such column"):
             raise weedb.NoColumnError(e)
         else:
             raise weedb.OperationalError(e)
     except sqlite3.ProgrammingError as e:
         raise weedb.ProgrammingError(e)
Esempio n. 3
0
    def drop_columns(self, table, column_names):
        """Drop the set of 'column_names' from table 'table'.

        table: The name of the table from which the column(s) are to be dropped.

        column_names: A set (or list) of column names to be dropped. It is not an error to try to
        drop a non-existent column.
        """

        existing_column_set = set()
        create_list = []
        insert_list = []

        self.execute("""PRAGMA table_info(%s);""" % table)

        for row in self.fetchall():
            # Unpack the row
            row_no, obs_name, obs_type, no_null, default, pk = row
            existing_column_set.add(obs_name)
            # Search through the target columns.
            if obs_name in column_names:
                continue
            no_null_str = " NOT NULL" if no_null else ""
            pk_str = " UNIQUE PRIMARY KEY" if pk else ""
            default_str = " DEFAULT %s" % default if default is not None else ""
            create_list.append("`%s` %s%s%s%s" % (obs_name, obs_type, no_null_str,
                                                  pk_str, default_str))
            insert_list.append(obs_name)

        for column in column_names:
            if column not in existing_column_set:
                raise weedb.NoColumnError("Cannot DROP '%s'; column does not exist." % column)

        create_str = ", ".join(create_list)
        insert_str = ", ".join(insert_list)

        self.execute("CREATE TEMPORARY TABLE %s_temp (%s);" % (table, create_str))
        self.execute("INSERT INTO %s_temp SELECT %s FROM %s;" % (table, insert_str, table))
        self.execute("DROP TABLE %s;" % table)
        self.execute("CREATE TABLE %s (%s);" % (table, create_str))
        self.execute("INSERT INTO %s SELECT %s FROM %s_temp;" % (table, insert_str, table))
        self.execute("DROP TABLE %s_temp;" % table)