예제 #1
0
파일: conftest.py 프로젝트: geigi/cozy
def prepare_db():
    from playhouse.pool import PooledSqliteDatabase
    from cozy.db.artwork_cache import ArtworkCache
    from cozy.db.book import Book
    from cozy.db.offline_cache import OfflineCache
    from cozy.db.settings import Settings
    from cozy.db.storage import Storage
    from cozy.db.storage_blacklist import StorageBlackList
    from cozy.db.track import Track
    from cozy.db.file import File
    from cozy.db.track_to_file import TrackToFile

    models = [
        Track, Book, File, TrackToFile, Settings, ArtworkCache, Storage,
        StorageBlackList, OfflineCache
    ]

    print("Setup database...")

    db_path = ':memory:'
    test_db = PooledSqliteDatabase(db_path, pragmas=[('journal_mode', 'wal')])
    test_db.bind(models, bind_refs=False, bind_backrefs=False)
    test_db.connect()
    test_db.create_tables(models)

    return db_path, models, test_db
예제 #2
0
파일: db.py 프로젝트: leuc/cozy
def init_db():
    tmp_db = None

    _connect_db(_db)

    sqlite_version = ".".join([str(num) for num in _db.server_version])
    log.info("SQLite version: {}, APSW version: {}".format(
        sqlite_version, apswversion()))

    if Settings.table_exists():
        update_db()
    else:
        tmp_db = PooledSqliteDatabase(os.path.join(get_data_dir(), "cozy.db"))
        if PeeweeVersion[0] == '2':
            tmp_db.create_tables([
                Track, Book, Settings, ArtworkCache, Storage, StorageBlackList,
                OfflineCache
            ], True)
        else:
            with tmp_db.connection_context():
                tmp_db.create_tables([
                    Track, Book, Settings, ArtworkCache, Storage,
                    StorageBlackList, OfflineCache
                ])

    # this is necessary to ensure that the tables have indeed been created
    if tmp_db:
        if PeeweeVersion[0] == '2':
            while not Settings.table_exists():
                time.sleep(0.01)
        else:
            while not tmp_db.table_exists("settings"):
                time.sleep(0.01)

    _connect_db(_db)

    if PeeweeVersion[0] == '3':
        _db.bind([
            Book, Track, Settings, ArtworkCache, StorageBlackList,
            OfflineCache, Storage
        ],
                 bind_refs=False,
                 bind_backrefs=False)

    if (Settings.select().count() == 0):
        Settings.create(path="", last_played_book=None)

    # TODO: Properly handle errors within the database
    # Remove this later. It prevents empty book objects in the database
    clean_books()
예제 #3
0
def database_init(app, models):
    logging.info("Init database connection: %s %s" % (app.config["DATABASE"], app.config["DATABASE_NAME"]))
    if app.config["DATABASE"] == "postgresql":
        from playhouse.pool import PooledPostgresqlExtDatabase
        database = PooledPostgresqlExtDatabase(app.config["DATABASE_NAME"], max_connections=16, stale_timeout=300, **app.config["DATABASE_AUTH"])

    elif app.config["DATABASE"] == "sqlite":
        from playhouse.pool import PooledSqliteDatabase
        database = PooledSqliteDatabase(app.config["DATABASE_PATH"], pragmas={
            "journal_mode": "wal",
            "cache_size": -1024 * 64,
            "foreign_keys": 1
        })
    else:
        raise RuntimeError("No database set or invalid")
    try:
        DB.initialize(database)
    except:
        logging.exception("Could not initialize database")
예제 #4
0
def database_init(app, models):
    logging.debug("ConnectDB: " + app.config["DATABASE"])
    if app.config["DATABASE"] == "postgresql":
        from playhouse.pool import PooledPostgresqlExtDatabase
        database = PooledPostgresqlExtDatabase(app.config["DATABASE_NAME"],
                                               max_connections=16,
                                               stale_timeout=300,
                                               **app.config["DATABASE_AUTH"])

    elif app.config["DATABASE"] == "sqlite":
        from playhouse.pool import PooledSqliteDatabase
        database = PooledSqliteDatabase(app.config["DATABASE_PATH"],
                                        pragmas=(("journal_mode", "wal"),
                                                 ("cache_size", -1024 * 64)))

    else:
        raise RuntimeError("No database set or invalid")

    DB.initialize(database)
'''
    tornado 使用peewee

'''

import tornado.ioloop
import tornado.web
from peewee import *
from playhouse.pool import PooledSqliteDatabase

# 使用进程池
db = PooledSqliteDatabase('Test8.db')


class User(Model):
    name = CharField()

    @classmethod
    def all_user(cls):
        return [x.name for x in cls.select()]

    class Meta:
        database = db


# 顶一个一个basehandler 里面执行数据库操作
class BaseHandler(tornado.web.RequestHandler):
    def prepare(self):
        db.create_tables(User)
        u = User(name='111')
        u.save()
예제 #6
0
def __open_database():
    global _db
    if not os.path.exists(get_data_dir()):
        os.makedirs(get_data_dir())
    _db = PooledSqliteDatabase(os.path.join(get_data_dir(), "cozy.db"),
                               pragmas=[('journal_mode', 'wal')])
예제 #7
0
파일: db.py 프로젝트: slice/pawfile
__all__ = ['db', 'Model']

from peewee import Model
from playhouse.pool import PooledSqliteDatabase

db = PooledSqliteDatabase(None)


class BaseModel(Model):
    class Meta:
        database = db
예제 #8
0
def peewee_database():
    from playhouse.pool import PooledSqliteDatabase

    from cozy.db.artwork_cache import ArtworkCache
    from cozy.db.book import Book
    from cozy.db.offline_cache import OfflineCache
    from cozy.db.settings import Settings
    from cozy.db.storage import Storage
    from cozy.db.storage_blacklist import StorageBlackList
    from cozy.db.track import Track

    models = [
        Track, Book, Settings, ArtworkCache, Storage, StorageBlackList,
        OfflineCache
    ]

    print("Setup database...")
    db_path = '/tmp/cozy_test.db'
    test_db = PooledSqliteDatabase(db_path, max_connections=32)
    test_db.bind(models, bind_refs=False, bind_backrefs=False)
    test_db.connect()
    test_db.create_tables(models)

    path_of_test_folder = os.path.dirname(os.path.realpath(__file__)) + '/'

    with open(path_of_test_folder + 'books.json') as json_file:
        book_data = json.load(json_file)

    with open(path_of_test_folder + 'tracks.json') as json_file:
        track_data = json.load(json_file)

    Book.insert_many(book_data).execute()
    Track.insert_many(track_data).execute()

    print("Provide database...")
    yield test_db

    print("Teardown database...")
    test_db.drop_tables(models)
    test_db.close()
    os.remove(db_path)
예제 #9
0
# import logging
import concurrent.futures
import settings

# logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)


def get_name():
    return '/'.join([settings.DATA_DIR, settings.DATABASENAME])


db = PooledSqliteDatabase(
    get_name(),
    max_connections=32,
    pragmas={
        'journal_mode': 'wal',
        'cache_size': -1 * 512000,  # 64MB
        'foreign_keys': 1,
        'ignore_check_constraints': 0,
        'synchronous': 0
    })


class dbBuild:
    @classmethod
    def populate_store(cls, values, *arg, **kwargs):
        index = Carrier.select().count()
        task_runner(cls.create_objects_atomic,
                    values,
                    index,
                    chunk_size=1000,
                    *arg,
예제 #10
0
from peewee import Model
from playhouse.pool import PooledSqliteDatabase

from ai.landmark_detection.environment import DB_PATH

sqlite_db = PooledSqliteDatabase(DB_PATH,
                                 max_connections=32,
                                 stale_timeout=300)


class Entity(Model):
    class Meta:
        database = sqlite_db
예제 #11
0
from peewee import *
from playhouse.pool import PooledSqliteDatabase
from playhouse.shortcuts import model_to_dict, dict_to_model
import datetime

"""peewee提供了一个数据库的封装模型,playhouse.db_url为其连接的一种方式通过数据库的指定格式url连接
连接后创建完以后需要模型生成表使用db.connect(),db.create_tables([Person, Pet])"""

blog = PooledSqliteDatabase('static/cookies.db')


class BaseModel(Model):
    """基类"""
    class Meta:
        database = blog


class Cookies(BaseModel):

    domain = CharField(verbose_name='域名', max_length=20, null=False, unique=True)
    count = IntegerField(verbose_name='记录条数', null=False)
    use_number = IntegerField(verbose_name='使用次数', default=0)
    test_type = CharField(verbose_name='检测方式', max_length=10, null=True, unique=False)
    test_url = CharField(verbose_name='检测地址', max_length=100, null=True, unique=True)
    test_sign = CharField(verbose_name='有效标示', max_length=100, null=True, unique=False)
    testing_date = DateTimeField(verbose_name='写入时间', default=datetime.datetime.now)
    insert_date = DateTimeField(verbose_name='写入时间', default=datetime.datetime.now)
    update_date = DateTimeField(verbose_name='更新时间', default=datetime.datetime.now)
    is_delete = BooleanField(verbose_name='删除', default=False, null=False)

    def to_dict(self):