cleanup imports section and spacing in fpdb_db

This commit is contained in:
Eric Blade 2009-09-15 22:10:37 -05:00
parent ba663c231c
commit bcecf643f2

View File

@ -21,19 +21,18 @@ import re
import sys import sys
import logging import logging
from time import time, strftime from time import time, strftime
from Exceptions import *
use_pool = False
try: try:
import sqlalchemy.pool as pool import sqlalchemy.pool as pool
use_pool = True use_pool = True
except: except ImportError:
logging.info("Not using sqlalchemy connection pool.") logging.info("Not using sqlalchemy connection pool.")
use_pool = False
import fpdb_simple import fpdb_simple
import FpdbSQLQueries import FpdbSQLQueries
from Exceptions import *
class fpdb_db: class fpdb_db:
MYSQL_INNODB = 2 MYSQL_INNODB = 2
@ -67,12 +66,12 @@ class fpdb_db:
"""Connects a database with the given parameters""" """Connects a database with the given parameters"""
if backend is None: if backend is None:
raise FpdbError('Database backend not defined') raise FpdbError('Database backend not defined')
self.backend=backend self.backend = backend
self.host=host self.host = host
self.user=user self.user = user
self.password=password self.password = password
self.database=database self.database = database
if backend==fpdb_db.MYSQL_INNODB: if backend == fpdb_db.MYSQL_INNODB:
import MySQLdb import MySQLdb
if use_pool: if use_pool:
MySQLdb = pool.manage(MySQLdb, pool_size=5) MySQLdb = pool.manage(MySQLdb, pool_size=5)
@ -113,7 +112,7 @@ class fpdb_db:
msg = "PostgreSQL connection to database (%s) user (%s) failed." % (database, user) msg = "PostgreSQL connection to database (%s) user (%s) failed." % (database, user)
print msg print msg
raise FpdbError(msg) raise FpdbError(msg)
elif backend==fpdb_db.SQLITE: elif backend == fpdb_db.SQLITE:
logging.info("Connecting to SQLite:%(database)s" % {'database':database}) logging.info("Connecting to SQLite:%(database)s" % {'database':database})
import sqlite3 import sqlite3
if use_pool: if use_pool:
@ -125,20 +124,20 @@ class fpdb_db:
sqlite3.register_adapter(bool, lambda x: "1" if x else "0") sqlite3.register_adapter(bool, lambda x: "1" if x else "0")
else: else:
raise FpdbError("unrecognised database backend:"+backend) raise FpdbError("unrecognised database backend:"+backend)
self.cursor=self.db.cursor() self.cursor = self.db.cursor()
# Set up query dictionary as early in the connection process as we can. # Set up query dictionary as early in the connection process as we can.
self.sql = FpdbSQLQueries.FpdbSQLQueries(self.get_backend_name()) self.sql = FpdbSQLQueries.FpdbSQLQueries(self.get_backend_name())
self.cursor.execute(self.sql.query['set tx level']) self.cursor.execute(self.sql.query['set tx level'])
self.wrongDbVersion=False self.wrongDbVersion = False
try: try:
self.cursor.execute("SELECT * FROM Settings") self.cursor.execute("SELECT * FROM Settings")
settings=self.cursor.fetchone() settings = self.cursor.fetchone()
if settings[0]!=118: if settings[0] != 118:
print "outdated or too new database version - please recreate tables" print "outdated or too new database version - please recreate tables"
self.wrongDbVersion=True self.wrongDbVersion = True
except:# _mysql_exceptions.ProgrammingError: except:# _mysql_exceptions.ProgrammingError:
if database != ":memory:": print "failed to read settings table - please recreate tables" if database != ":memory:": print "failed to read settings table - please recreate tables"
self.wrongDbVersion=True self.wrongDbVersion = True
#end def connect #end def connect
def disconnect(self, due_to_error=False): def disconnect(self, due_to_error=False):