Exemplo n.º 1
0
def setup_db():
    global engine
    global session
    global cc_classes

    if config.config_calibre_dir is None or config.config_calibre_dir == u'':
        content = ub.session.query(ub.Settings).first()
        content.config_calibre_dir = None
        content.db_configured = False
        ub.session.commit()
        config.loadSettings()
        return False

    dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
    try:
        if not os.path.exists(dbpath):
            raise
        engine = create_engine('sqlite:///' + dbpath,
                               echo=False,
                               isolation_level="SERIALIZABLE",
                               connect_args={'check_same_thread': False})
        conn = engine.connect()
    except Exception:
        content = ub.session.query(ub.Settings).first()
        content.config_calibre_dir = None
        content.db_configured = False
        ub.session.commit()
        config.loadSettings()
        return False
    content = ub.session.query(ub.Settings).first()
    content.db_configured = True
    ub.session.commit()
    config.loadSettings()
    conn.connection.create_function('title_sort', 1, title_sort)
    conn.connection.create_function('lower', 1, lcase)
    conn.connection.create_function('upper', 1, ucase)

    if not cc_classes:
        cc = conn.execute("SELECT id, datatype FROM custom_columns")

        cc_ids = []
        books_custom_column_links = {}
        cc_classes = {}
        for row in cc:
            if row.datatype not in cc_exceptions:
                books_custom_column_links[row.id] = Table(
                    'books_custom_column_' + str(row.id) + '_link',
                    Base.metadata,
                    Column('book',
                           Integer,
                           ForeignKey('books.id'),
                           primary_key=True),
                    Column('value',
                           Integer,
                           ForeignKey('custom_column_' + str(row.id) + '.id'),
                           primary_key=True))
                cc_ids.append([row.id, row.datatype])
                if row.datatype == 'bool':
                    ccdict = {
                        '__tablename__': 'custom_column_' + str(row.id),
                        'id': Column(Integer, primary_key=True),
                        'book': Column(Integer, ForeignKey('books.id')),
                        'value': Column(Boolean)
                    }
                elif row.datatype == 'int':
                    ccdict = {
                        '__tablename__': 'custom_column_' + str(row.id),
                        'id': Column(Integer, primary_key=True),
                        'book': Column(Integer, ForeignKey('books.id')),
                        'value': Column(Integer)
                    }
                else:
                    ccdict = {
                        '__tablename__': 'custom_column_' + str(row.id),
                        'id': Column(Integer, primary_key=True),
                        'value': Column(String)
                    }
                cc_classes[row.id] = type('Custom_Column_' + str(row.id),
                                          (Base, ), ccdict)

        for cc_id in cc_ids:
            if (cc_id[1] == 'bool') or (cc_id[1] == 'int'):
                setattr(
                    Books, 'custom_column_' + str(cc_id[0]),
                    relationship(
                        cc_classes[cc_id[0]],
                        primaryjoin=(Books.id == cc_classes[cc_id[0]].book),
                        backref='books'))
            else:
                setattr(
                    Books, 'custom_column_' + str(cc_id[0]),
                    relationship(cc_classes[cc_id[0]],
                                 secondary=books_custom_column_links[cc_id[0]],
                                 backref='books'))

    Session = scoped_session(
        sessionmaker(autocommit=False, autoflush=False, bind=engine))
    session = Session()
    return True
Exemplo n.º 2
0
def setup_db():
    global session
    global cc_exceptions
    global cc_classes
    global cc_ids
    global books_custom_column_links
    global engine

    if config.config_calibre_dir is None or config.config_calibre_dir ==  u'':
        return False

    dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
    engine = create_engine('sqlite:///{0}'.format(dbpath.encode('utf-8')), echo=False)
    try:
        conn = engine.connect()

    except:
        content = ub.session.query(ub.Settings).first()
        content.config_calibre_dir = None
        content.db_configured = False
        ub.session.commit()
        config.loadSettings()
        return False
    content = ub.session.query(ub.Settings).first()
    content.db_configured = True
    ub.session.commit()
    config.loadSettings()
    conn.connection.create_function('title_sort', 1, title_sort)

    cc = conn.execute("SELECT id, datatype FROM custom_columns")

    cc_ids = []
    cc_exceptions = ['datetime', 'int', 'comments', 'float', 'composite', 'series']
    books_custom_column_links = {}
    cc_classes = {}
    for row in cc:
        if row.datatype not in cc_exceptions:
            books_custom_column_links[row.id] = Table('books_custom_column_' + str(row.id) + '_link', Base.metadata,
                                                      Column('book', Integer, ForeignKey('books.id'),
                                                             primary_key=True),
                                                      Column('value', Integer,
                                                             ForeignKey('custom_column_' + str(row.id) + '.id'),
                                                             primary_key=True)
                                                      )
            cc_ids.append([row.id, row.datatype])
            if row.datatype == 'bool':
                ccdict = {'__tablename__': 'custom_column_' + str(row.id),
                          'id': Column(Integer, primary_key=True),
                          'book': Column(Integer, ForeignKey('books.id')),
                          'value': Column(Boolean)}
            else:
                ccdict = {'__tablename__': 'custom_column_' + str(row.id),
                          'id': Column(Integer, primary_key=True),
                          'value': Column(String)}
            cc_classes[row.id] = type('Custom_Column_' + str(row.id), (Base,), ccdict)

    for id in cc_ids:
        if id[1] == 'bool':
            setattr(Books, 'custom_column_' + str(id[0]), relationship(cc_classes[id[0]],
                                                                       primaryjoin=(
                                                                       Books.id == cc_classes[id[0]].book),
                                                                       backref='books'))
        else:
            setattr(Books, 'custom_column_' + str(id[0]), relationship(cc_classes[id[0]],
                                                                       secondary=books_custom_column_links[id[0]],
                                                                       backref='books'))

    # Base.metadata.create_all(engine)
    Session = sessionmaker()
    Session.configure(bind=engine)
    session = Session()
    return True
Exemplo n.º 3
0
def setup_db():
    global engine
    global session
    global cc_classes

    if config.config_calibre_dir is None or config.config_calibre_dir == u'':
        content = ub.session.query(ub.Settings).first()
        content.config_calibre_dir = None
        content.db_configured = False
        ub.session.commit()
        config.loadSettings()
        return False

    dbpath = os.path.join(config.config_calibre_dir, "metadata.db")
    try:
        if not os.path.exists(dbpath):
            raise
        engine = create_engine('sqlite:///' + dbpath, echo=False, isolation_level="SERIALIZABLE", connect_args={'check_same_thread': False})
        conn = engine.connect()
    except Exception:
        content = ub.session.query(ub.Settings).first()
        content.config_calibre_dir = None
        content.db_configured = False
        ub.session.commit()
        config.loadSettings()
        return False
    content = ub.session.query(ub.Settings).first()
    content.db_configured = True
    ub.session.commit()
    config.loadSettings()
    conn.connection.create_function('title_sort', 1, title_sort)
    # conn.connection.create_function('lower', 1, lcase)
    # conn.connection.create_function('upper', 1, ucase)

    if not cc_classes:
        cc = conn.execute("SELECT id, datatype FROM custom_columns")

        cc_ids = []
        books_custom_column_links = {}
        cc_classes = {}
        for row in cc:
            if row.datatype not in cc_exceptions:
                books_custom_column_links[row.id] = Table('books_custom_column_' + str(row.id) + '_link', Base.metadata,
                                                          Column('book', Integer, ForeignKey('books.id'),
                                                                 primary_key=True),
                                                          Column('value', Integer,
                                                                 ForeignKey('custom_column_' + str(row.id) + '.id'),
                                                                 primary_key=True)
                                                          )
                cc_ids.append([row.id, row.datatype])
                if row.datatype == 'bool':
                    ccdict = {'__tablename__': 'custom_column_' + str(row.id),
                              'id': Column(Integer, primary_key=True),
                              'book': Column(Integer, ForeignKey('books.id')),
                              'value': Column(Boolean)}
                elif row.datatype == 'int':
                    ccdict = {'__tablename__': 'custom_column_' + str(row.id),
                              'id': Column(Integer, primary_key=True),
                              'book': Column(Integer, ForeignKey('books.id')),
                              'value': Column(Integer)}
                else:
                    ccdict = {'__tablename__': 'custom_column_' + str(row.id),
                              'id': Column(Integer, primary_key=True),
                              'value': Column(String)}
                cc_classes[row.id] = type('Custom_Column_' + str(row.id), (Base,), ccdict)

        for cc_id in cc_ids:
            if (cc_id[1] == 'bool') or (cc_id[1] == 'int'):
                setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(cc_classes[cc_id[0]],
                                                                           primaryjoin=(
                                                                           Books.id == cc_classes[cc_id[0]].book),
                                                                           backref='books'))
            else:
                setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(cc_classes[cc_id[0]],
                                                                           secondary=books_custom_column_links[cc_id[0]],
                                                                           backref='books'))


    Session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    session = Session()
    return True