def create(database_name='', SQLITE_ROOT='', driver='', **argv): # @UnusedVariable """Create the database specified by the db_dict. If it already exists, an exception of type DatabaseExistsError will be thrown.""" file_path = _get_filepath(SQLITE_ROOT, database_name, **argv) # Check whether the database file exists: if os.path.exists(file_path): raise weedb.DatabaseExistsError("Database %s already exists" % (file_path, )) else: # If it doesn't exist, create the parent directories fileDirectory = os.path.dirname(file_path) if not os.path.exists(fileDirectory): try: os.makedirs(fileDirectory) except OSError: raise weedb.PermissionError("No permission to create %s" % fileDirectory) timeout = to_int(argv.get('timeout', 5)) isolation_level = argv.get('isolation_level') # Open, then immediately close the database. connection = sqlite3.connect(file_path, timeout=timeout, isolation_level=isolation_level) connection.close()
def drop(database_name='', SQLITE_ROOT='', driver='', **argv): # @UnusedVariable file_path = _get_filepath(SQLITE_ROOT, database_name, **argv) try: os.remove(file_path) except OSError as e: errno = getattr(e, 'errno', 2) if errno == 13: raise weedb.PermissionError("No permission to drop database %s" % file_path) else: raise weedb.NoDatabaseError("Attempt to drop non-existent database %s" % file_path)
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)
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)