Example #1
0
def init_db():
    """Initializes the database."""
    sqlite3.register_converter('GUID', lambda b: uuid.UUID(bytes_le=b))
    sqlite3.register_adapter(uuid.UUID, lambda u: buffer(u.bytes_le))
    for i in range(0, 3):
        db = get_db(i)
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()
Example #2
0
    def __init__(self, db_path, disable_sqlite_crypt=False):
        self.db_path = db_path
        self.con = None
        self.disable_sqlite_crypt = disable_sqlite_crypt

        self._log = logging.getLogger('DB')
        self._lock = threading.Lock()

        dbapi2.register_adapter(bool, int)
        dbapi2.register_converter("bool", lambda v: bool(int(v)))
def init_db():
    """
    register_converter(): convert SQLite types to Python types
    register_adapter(): convert Python types to SQLite types
    """
    sqlite3.register_converter('GUID', lambda b: uuid.UUID(bytes_le=b))
    sqlite3.register_adapter(uuid.UUID, lambda u: buffer(u.bytes_le))
    for i in range(0, 3):
        #with app.app_context():
        db = get_db(i)
        #db = queries._engine.raw_connection()
        with app.open_resource('tracks.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()
Example #4
0
def init_db():
    """Initializes the database."""

    schema = open('schema.sql', 'r')
    schema_commands = schema.read()
    schema.close();

    sqlite3.register_converter('GUID', lambda b: uuid.UUID(bytes_le=b))
    sqlite3.register_adapter(uuid.UUID, lambda u: buffer(u.bytes_le))

    for database in DATABASES:
        try:
            db = get_db(database)
            db.cursor().executescript(schema_commands)
            db.commit()
        except sqlite3.Error, err:
            print " [INFO] %s " % err
Example #5
0
    def _connectdb(self, filename):
        # Register the adapter
        sqlite.register_adapter(Compressed, adapt_compressed)
        sqlite.register_converter("compressed", convert_compressed)

        if os.path.exists(filename):
            need_create = False
        else:
            need_create = True

        conn = sqlite.connect(filename, detect_types=sqlite.PARSE_DECLTYPES, check_same_thread = False)

        if need_create:
            self._createdb(conn)
        else:
            if not self._validatedb(conn):
                # TODO: should clobber existing tables?
                self._createdb(conn)

        return conn
Example #6
0
from .client import DatabaseClient  # isort:skip
from .creation import DatabaseCreation  # isort:skip
from .features import DatabaseFeatures  # isort:skip
from .introspection import DatabaseIntrospection  # isort:skip
from .operations import DatabaseOperations  # isort:skip
from .schema import DatabaseSchemaEditor  # isort:skip


def decoder(conv_func):
    """
    Convert bytestrings from Python's sqlite3 interface to a regular string.
    """
    return lambda s: conv_func(s.decode())


Database.register_converter("bool", b'1'.__eq__)
Database.register_converter("time", decoder(parse_time))
Database.register_converter("datetime", decoder(parse_datetime))
Database.register_converter("timestamp", decoder(parse_datetime))
Database.register_converter("TIMESTAMP", decoder(parse_datetime))

Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal)


class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'sqlite'
    display_name = 'SQLite'
    # SQLite doesn't actually support most of these types, but it "does the right
    # thing" given more verbose field definitions, so leave them as is so that
    # schema inspection is more useful.
    data_types = {
Example #7
0
from .client import DatabaseClient                          # isort:skip
from .creation import DatabaseCreation                      # isort:skip
from .features import DatabaseFeatures                      # isort:skip
from .introspection import DatabaseIntrospection            # isort:skip
from .operations import DatabaseOperations                  # isort:skip
from .schema import DatabaseSchemaEditor                    # isort:skip


def decoder(conv_func):
    """
    Convert bytestrings from Python's sqlite3 interface to a regular string.
    """
    return lambda s: conv_func(s.decode())


Database.register_converter("bool", decoder(lambda s: s == '1'))
Database.register_converter("time", decoder(parse_time))
Database.register_converter("date", decoder(parse_date))
Database.register_converter("datetime", decoder(parse_datetime))
Database.register_converter("timestamp", decoder(parse_datetime))
Database.register_converter("TIMESTAMP", decoder(parse_datetime))
Database.register_converter("decimal", decoder(backend_utils.typecast_decimal))

Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal)


class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'sqlite'
    display_name = 'SQLite'
    # SQLite doesn't actually support most of these types, but it "does the right
    # thing" given more verbose field definitions, so leave them as is so that
Example #8
0
    """
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        return None if None in args else func(*args, **kwargs)
    return wrapper


def list_aggregate(function):
    """
    Return an aggregate class that accumulates values in a list and applies
    the provided function to the data.
    """
    return type('ListAggregate', (list,), {'finalize': function, 'step': list.append})


Database.register_converter("bool", b'1'.__eq__)
Database.register_converter("time", decoder(parse_time))
Database.register_converter("datetime", decoder(parse_datetime))
Database.register_converter("timestamp", decoder(parse_datetime))
Database.register_converter("TIMESTAMP", decoder(parse_datetime))

Database.register_adapter(decimal.Decimal, str)


class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'sqlite'
    display_name = 'SQLite'
    # SQLite doesn't actually support most of these types, but it "does the right
    # thing" given more verbose field definitions, so leave them as is so that
    # schema inspection is more useful.
    data_types = {
Example #9
0
    return type("ListAggregate", (list, ), {
        "finalize": function,
        "step": list.append
    })


def check_sqlite_version():
    if Database.sqlite_version_info < (3, 9, 0):
        raise ImproperlyConfigured(
            "SQLite 3.9.0 or later is required (found %s)." %
            Database.sqlite_version)


check_sqlite_version()

Database.register_converter("bool", b"1".__eq__)
Database.register_converter("time", decoder(parse_time))
Database.register_converter("datetime", decoder(parse_datetime))
Database.register_converter("timestamp", decoder(parse_datetime))

Database.register_adapter(decimal.Decimal, str)


class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = "sqlite"
    display_name = "SQLite"
    # SQLite doesn't actually support most of these types, but it "does the right
    # thing" given more verbose field definitions, so leave them as is so that
    # schema inspection is more useful.
    data_types = {
        "AutoField": "integer",
Example #10
0
#
# sqlite registers converters for date and timestamp.
# But the matching is case-sensitive, and since I
# always use uppercase datatypes, reregister the
# variants here (only has effect for some versions of
# pysqlite2 (e.g., 2.2.2)
#
for t, ulist in (('date', ('DATE', )), ('timestamp', ('TIMESTAMP', 'DATE'))):
    try:
        c = sqlite.converters[t]
    except KeyError:
        continue
    else:
        for u in ulist:
            if u not in sqlite.converters:
                sqlite.register_converter(u, c)
        del c, u
del t, ulist


#
# pysqlite has its own mechanism for handling conversions between
# database columns and Python datatypes: Dates and Datetimes are
# added by the module itself, and aliased above. Binary columns
# are returned as buffer objects. Other adapters can be added by
# user code, so there's no need to use PyDO's converter protocols.
#
# I'm not sure therefore if the SqliteConverter class really needs
# to exist, but no harm in its being here explicitly, and maybe some
# other conversion will be needed which sqlite can't cope with.
#
Example #11
0
    from django.core.exceptions import ImproperlyConfigured
    if sys.version_info < (2, 5, 0):
        module = 'pysqlite2'
    else:
        module = 'sqlite3'
    raise ImproperlyConfigured, "Error loading %s module: %s" % (module, e)

try:
    import decimal
except ImportError:
    from django.utils import _decimal as decimal # for Python 2.3

DatabaseError = Database.DatabaseError
IntegrityError = Database.IntegrityError

Database.register_converter("bool", lambda s: str(s) == '1')
Database.register_converter("time", util.typecast_time)
Database.register_converter("date", util.typecast_date)
Database.register_converter("datetime", util.typecast_timestamp)
Database.register_converter("timestamp", util.typecast_timestamp)
Database.register_converter("TIMESTAMP", util.typecast_timestamp)
Database.register_converter("decimal", util.typecast_decimal)
Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)

class DatabaseFeatures(BaseDatabaseFeatures):
    supports_constraints = False

class DatabaseOperations(BaseDatabaseOperations):
    def date_extract_sql(self, lookup_type, field_name):
        # sqlite doesn't support extract, so we fake it with the user-defined
        # function django_extract that's registered in connect().
Example #12
0
from .client import DatabaseClient                          # isort:skip
from .creation import DatabaseCreation                      # isort:skip
from .features import DatabaseFeatures                      # isort:skip
from .introspection import DatabaseIntrospection            # isort:skip
from .operations import DatabaseOperations                  # isort:skip
from .schema import DatabaseSchemaEditor                    # isort:skip


def decoder(conv_func):
    """
    Convert bytestrings from Python's sqlite3 interface to a regular string.
    """
    return lambda s: conv_func(s.decode())


Database.register_converter("bool", lambda s: s == b'1')
Database.register_converter("time", decoder(parse_time))
Database.register_converter("date", decoder(parse_date))
Database.register_converter("datetime", decoder(parse_datetime))
Database.register_converter("timestamp", decoder(parse_datetime))
Database.register_converter("TIMESTAMP", decoder(parse_datetime))
Database.register_converter("decimal", decoder(decimal.Decimal))

Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal)


class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'sqlite'
    display_name = 'SQLite'
    # SQLite doesn't actually support most of these types, but it "does the right
    # thing" given more verbose field definitions, so leave them as is so that
Example #13
0
    if sys.version_info < (2, 5, 0):
        module = 'pysqlite2'
    else:
        module = 'sqlite3'
        exc = e1
    raise ImproperlyConfigured, "Error loading %s module: %s" % (module, exc)

try:
    import decimal
except ImportError:
    from django.utils import _decimal as decimal  # for Python 2.3

DatabaseError = Database.DatabaseError
IntegrityError = Database.IntegrityError

Database.register_converter("bool", lambda s: str(s) == '1')
Database.register_converter("time", util.typecast_time)
Database.register_converter("date", util.typecast_date)
Database.register_converter("datetime", util.typecast_timestamp)
Database.register_converter("timestamp", util.typecast_timestamp)
Database.register_converter("TIMESTAMP", util.typecast_timestamp)
Database.register_converter("decimal", util.typecast_decimal)
Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
if Database.version_info >= (2, 4, 1):
    # Starting in 2.4.1, the str type is not accepted anymore, therefore,
    # we convert all str objects to Unicode
    # As registering a adapter for a primitive type causes a small
    # slow-down, this adapter is only registered for sqlite3 versions
    # needing it.
    Database.register_adapter(str, lambda s: s.decode('utf-8'))
Example #14
0
from .client import DatabaseClient  # isort:skip
from .creation import DatabaseCreation  # isort:skip
from .features import DatabaseFeatures  # isort:skip
from .introspection import DatabaseIntrospection  # isort:skip
from .operations import DatabaseOperations  # isort:skip
from .schema import DatabaseSchemaEditor  # isort:skip


def decoder(conv_func):
    """
    Convert bytestrings from Python's sqlite3 interface to a regular string.
    """
    return lambda s: conv_func(s.decode())


Database.register_converter("bool", lambda s: s == b"1")
Database.register_converter("time", decoder(parse_time))
Database.register_converter("date", decoder(parse_date))
Database.register_converter("datetime", decoder(parse_datetime))
Database.register_converter("timestamp", decoder(parse_datetime))
Database.register_converter("TIMESTAMP", decoder(parse_datetime))
Database.register_converter("decimal", decoder(decimal.Decimal))

Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal)


class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = "sqlite"
    display_name = "SQLite"
    # SQLite doesn't actually support most of these types, but it "does the right
    # thing" given more verbose field definitions, so leave them as is so that
Example #15
0
# configuration
#DATABASE = '/tmp/mini_api.db'
DATABASES = 'DATABASE0','DATABASE1','DATABASE2'
DATABASE0 = '/tmp/mini_api00.db'
DATABASE1 = '/tmp/mini_api01.db'
DATABASE2 = '/tmp/mini_api02.db'
PER_PAGE = 30
DEBUG = True
SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/'

# create our little application :)
app = Flask('mini_api')
app.config.from_object(__name__)
app.config.from_envvar('MINITWIT_SETTINGS', silent=True)

sqlite3.register_converter('GUID', lambda b: uuid.UUID(bytes_le=b))
sqlite3.register_adapter(uuid.UUID, lambda u: buffer(u.bytes_le))



class FileParser(ConfigParser.ConfigParser):
    def as_dict(self):
        usermappings = {}
        with open('mini_api/mappings.ini', 'r') as f:
            for line in f:
                line = line.rstrip() #removes trailing whitespace and '\n' chars
                if "=" not in line: 
                    continue #skips blanks and comments w/o =
                if line.startswith("#"): 
                    continue #skips comments which contain =
                k, v = line.split("=", 1)
Example #16
0
from .creation import DatabaseCreation  # isort:skip
from .features import DatabaseFeatures  # isort:skip
from .introspection import DatabaseIntrospection  # isort:skip
from .operations import DatabaseOperations  # isort:skip
from .schema import DatabaseSchemaEditor  # isort:skip


def decoder(conv_func):
    """ The Python sqlite3 interface returns always byte strings.
        This function converts the received value to a regular string before
        passing it to the receiver function.
    """
    return lambda s: conv_func(s.decode())


Database.register_converter("bool", decoder(lambda s: s == '1'))
Database.register_converter("time", decoder(parse_time))
Database.register_converter("date", decoder(parse_date))
Database.register_converter("datetime", decoder(parse_datetime))
Database.register_converter("timestamp", decoder(parse_datetime))
Database.register_converter("TIMESTAMP", decoder(parse_datetime))
Database.register_converter("decimal", decoder(backend_utils.typecast_decimal))

Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal)


class DatabaseWrapper(BaseDatabaseWrapper):
    vendor = 'sqlite'
    # SQLite doesn't actually support most of these types, but it "does the right
    # thing" given more verbose field definitions, so leave them as is so that
    # schema inspection is more useful.
# sqlite registers converters for date and timestamp.
# But the matching is case-sensitive, and since I
# always use uppercase datatypes, reregister the
# variants here (only has effect for some versions of
# pysqlite2 (e.g., 2.2.2)
#
for t, ulist in (('date', ('DATE',)),
                 ('timestamp', ('TIMESTAMP', 'DATE'))):
  try:
    c=sqlite.converters[t]
  except KeyError:
    continue
  else:
    for u in ulist:
      if u not in sqlite.converters:
        sqlite.register_converter(u, c)
    del c, u
del t, ulist

      
#
# pysqlite has its own mechanism for handling conversions between
# database columns and Python datatypes: Dates and Datetimes are
# added by the module itself, and aliased above. Binary columns
# are returned as buffer objects. Other adapters can be added by
# user code, so there's no need to use PyDO's converter protocols.
#
# I'm not sure therefore if the SqliteConverter class really needs 
# to exist, but no harm in its being here explicitly, and maybe some 
# other conversion will be needed which sqlite can't cope with.
#