mirror of
https://github.com/gryf/pygtktalog.git
synced 2025-12-17 19:40:21 +01:00
Moved pygtktalog to pycatalog.
Also, clean up setup things and imports.
This commit is contained in:
43
pycatalog/dbcommon.py
Normal file
43
pycatalog/dbcommon.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
Project: pyGTKtalog
|
||||
Description: Common database operations.
|
||||
Type: core
|
||||
Author: Roman 'gryf' Dobosz, gryf73@gmail.com
|
||||
Created: 2009-08-07
|
||||
"""
|
||||
from sqlalchemy import MetaData, create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
from pycatalog.logger import get_logger
|
||||
|
||||
|
||||
# Prepare SQLAlchemy objects
|
||||
Meta = MetaData()
|
||||
Base = declarative_base(metadata=Meta)
|
||||
Session = sessionmaker()
|
||||
DbFilename = None
|
||||
|
||||
LOG = get_logger("dbcommon")
|
||||
|
||||
|
||||
def connect(filename=None):
|
||||
"""
|
||||
create engine and bind to Meta object.
|
||||
Arguments:
|
||||
@filename - string with absolute or relative path to sqlite database
|
||||
file. If None, db in-memory will be created
|
||||
"""
|
||||
global DbFilename
|
||||
|
||||
if not filename:
|
||||
filename = ':memory:'
|
||||
|
||||
LOG.info("db filename: %s" % filename)
|
||||
DbFilename = filename
|
||||
|
||||
connect_string = "sqlite:///%s" % filename
|
||||
engine = create_engine(connect_string)
|
||||
Meta.bind = engine
|
||||
Meta.create_all(checkfirst=True)
|
||||
return engine
|
||||
Reference in New Issue
Block a user