2008-08-04 05:44:28 +02:00
|
|
|
#!/usr/bin/python
|
2009-08-29 21:16:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-08-04 05:44:28 +02:00
|
|
|
|
|
|
|
#Copyright 2008 Steffen Jobbagy-Felso
|
|
|
|
#This program is free software: you can redistribute it and/or modify
|
|
|
|
#it under the terms of the GNU Affero General Public License as published by
|
|
|
|
#the Free Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
#This program is distributed in the hope that it will be useful,
|
|
|
|
#but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
#GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
#You should have received a copy of the GNU Affero General Public License
|
|
|
|
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#In the "official" distribution you can find the license in
|
|
|
|
#agpl-3.0.txt in the docs folder of the package.
|
|
|
|
|
|
|
|
import os
|
2008-10-07 19:41:06 +02:00
|
|
|
import re
|
2009-06-07 21:07:18 +02:00
|
|
|
import sys
|
2009-07-14 01:04:10 +02:00
|
|
|
import logging
|
2009-11-17 17:12:24 +01:00
|
|
|
import math
|
2009-06-07 23:21:58 +02:00
|
|
|
from time import time, strftime
|
2009-09-16 05:10:37 +02:00
|
|
|
from Exceptions import *
|
2009-08-29 21:16:27 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import sqlalchemy.pool as pool
|
|
|
|
use_pool = True
|
2009-09-16 05:10:37 +02:00
|
|
|
except ImportError:
|
2009-08-29 21:16:27 +02:00
|
|
|
logging.info("Not using sqlalchemy connection pool.")
|
2009-09-16 05:10:37 +02:00
|
|
|
use_pool = False
|
2009-08-29 21:16:27 +02:00
|
|
|
|
2009-11-16 08:19:24 +01:00
|
|
|
try:
|
|
|
|
from numpy import var
|
|
|
|
use_numpy = True
|
|
|
|
except ImportError:
|
|
|
|
logging.info("Not using numpy to define variance in sqlite.")
|
|
|
|
use_numpy = False
|
2009-06-07 21:07:18 +02:00
|
|
|
|
2008-08-04 05:44:28 +02:00
|
|
|
import fpdb_simple
|
2008-10-07 11:33:37 +02:00
|
|
|
import FpdbSQLQueries
|
2009-11-03 10:50:13 +01:00
|
|
|
import Configuration
|
2008-08-04 05:44:28 +02:00
|
|
|
|
2009-11-16 08:19:24 +01:00
|
|
|
# Variance created as sqlite has a bunch of undefined aggregate functions.
|
|
|
|
|
|
|
|
class VARIANCE:
|
|
|
|
def __init__(self):
|
|
|
|
self.store = []
|
|
|
|
|
|
|
|
def step(self, value):
|
|
|
|
self.store.append(value)
|
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
return float(var(self.store))
|
|
|
|
|
2009-11-17 17:12:24 +01:00
|
|
|
class sqlitemath:
|
|
|
|
def mod(self, a, b):
|
|
|
|
return a%b
|
|
|
|
|
2008-08-04 05:44:28 +02:00
|
|
|
class fpdb_db:
|
2009-07-14 01:04:10 +02:00
|
|
|
MYSQL_INNODB = 2
|
|
|
|
PGSQL = 3
|
|
|
|
SQLITE = 4
|
2009-09-25 21:18:13 +02:00
|
|
|
|
2008-12-06 13:40:04 +01:00
|
|
|
def __init__(self):
|
|
|
|
"""Simple constructor, doesnt really do anything"""
|
2009-03-17 01:52:50 +01:00
|
|
|
self.db = None
|
|
|
|
self.cursor = None
|
|
|
|
self.sql = {}
|
2008-12-06 13:40:04 +01:00
|
|
|
#end def __init__
|
2008-12-18 00:03:17 +01:00
|
|
|
|
|
|
|
def do_connect(self, config=None):
|
|
|
|
"""Connects a database using information in config"""
|
|
|
|
if config is None:
|
|
|
|
raise FpdbError('Configuration not defined')
|
|
|
|
|
|
|
|
self.settings = {}
|
2009-03-17 01:52:50 +01:00
|
|
|
self.settings['os'] = "linuxmac" if os.name != "nt" else "windows"
|
2008-12-18 00:03:17 +01:00
|
|
|
|
2009-07-14 01:04:10 +02:00
|
|
|
db = config.get_db_parameters()
|
|
|
|
self.connect(backend=db['db-backend'],
|
|
|
|
host=db['db-host'],
|
|
|
|
database=db['db-databaseName'],
|
2009-11-22 06:00:23 +01:00
|
|
|
user=db['db-user'],
|
2009-07-14 01:04:10 +02:00
|
|
|
password=db['db-password'])
|
2008-12-18 00:03:17 +01:00
|
|
|
#end def do_connect
|
2009-11-22 06:00:23 +01:00
|
|
|
|
2008-12-06 14:07:37 +01:00
|
|
|
def connect(self, backend=None, host=None, database=None,
|
|
|
|
user=None, password=None):
|
2008-12-06 13:40:04 +01:00
|
|
|
"""Connects a database with the given parameters"""
|
2008-12-06 14:07:37 +01:00
|
|
|
if backend is None:
|
|
|
|
raise FpdbError('Database backend not defined')
|
2009-09-16 05:10:37 +02:00
|
|
|
self.backend = backend
|
|
|
|
self.host = host
|
|
|
|
self.user = user
|
|
|
|
self.password = password
|
|
|
|
self.database = database
|
|
|
|
if backend == fpdb_db.MYSQL_INNODB:
|
2008-12-06 13:40:04 +01:00
|
|
|
import MySQLdb
|
2009-08-29 21:16:27 +02:00
|
|
|
if use_pool:
|
|
|
|
MySQLdb = pool.manage(MySQLdb, pool_size=5)
|
2009-11-22 06:00:23 +01:00
|
|
|
try:
|
|
|
|
self.db = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True)
|
2009-10-27 16:01:45 +01:00
|
|
|
#TODO: Add port option
|
2009-11-22 06:00:23 +01:00
|
|
|
except MySQLdb.Error, ex:
|
|
|
|
if ex.args[0] == 1045:
|
|
|
|
raise FpdbMySQLAccessDenied(ex.args[0], ex.args[1])
|
2009-12-01 12:58:33 +01:00
|
|
|
elif ex.args[0] == 2002 or ex.args[0] == 2003: # 2002 is no unix socket, 2003 is no tcp socket
|
2009-11-27 13:19:43 +01:00
|
|
|
raise FpdbMySQLNoDatabase(ex.args[0], ex.args[1])
|
2009-11-22 06:00:23 +01:00
|
|
|
else:
|
|
|
|
print "*** WARNING UNKNOWN MYSQL ERROR", ex
|
|
|
|
elif backend == fpdb_db.PGSQL:
|
2008-12-06 13:40:04 +01:00
|
|
|
import psycopg2
|
2009-08-29 21:16:27 +02:00
|
|
|
import psycopg2.extensions
|
|
|
|
if use_pool:
|
|
|
|
psycopg2 = pool.manage(psycopg2, pool_size=5)
|
2009-05-07 22:24:06 +02:00
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
2008-12-06 14:07:37 +01:00
|
|
|
# If DB connection is made over TCP, then the variables
|
|
|
|
# host, user and password are required
|
|
|
|
# For local domain-socket connections, only DB name is
|
|
|
|
# needed, and everything else is in fact undefined and/or
|
|
|
|
# flat out wrong
|
2009-06-09 22:38:30 +02:00
|
|
|
# sqlcoder: This database only connect failed in my windows setup??
|
|
|
|
# Modifed it to try the 4 parameter style if the first connect fails - does this work everywhere?
|
|
|
|
connected = False
|
2009-06-02 16:59:54 +02:00
|
|
|
if self.host == "localhost" or self.host == "127.0.0.1":
|
2009-06-09 22:38:30 +02:00
|
|
|
try:
|
|
|
|
self.db = psycopg2.connect(database = database)
|
|
|
|
connected = True
|
|
|
|
except:
|
2009-12-12 10:51:07 +01:00
|
|
|
# direct connection failed so try user/pass/... version
|
2009-06-09 22:38:30 +02:00
|
|
|
pass
|
|
|
|
if not connected:
|
|
|
|
try:
|
|
|
|
self.db = psycopg2.connect(host = host,
|
2009-11-22 06:00:23 +01:00
|
|
|
user = user,
|
|
|
|
password = password,
|
2009-06-09 22:38:30 +02:00
|
|
|
database = database)
|
2009-12-12 10:51:07 +01:00
|
|
|
except Exception, ex:
|
|
|
|
if 'Connection refused' in ex.args[0]:
|
|
|
|
# meaning eg. db not running
|
|
|
|
raise FpdbPostgresqlNoDatabase(errmsg = ex.args[0])
|
|
|
|
elif 'password authentication' in ex.args[0]:
|
|
|
|
raise FpdbPostgresqlAccessDenied(errmsg = ex.args[0])
|
|
|
|
else:
|
|
|
|
msg = ex.args[0]
|
2009-06-09 22:38:30 +02:00
|
|
|
print msg
|
2009-08-12 02:46:39 +02:00
|
|
|
raise FpdbError(msg)
|
2009-09-16 05:10:37 +02:00
|
|
|
elif backend == fpdb_db.SQLITE:
|
2009-07-15 01:14:25 +02:00
|
|
|
logging.info("Connecting to SQLite:%(database)s" % {'database':database})
|
2009-07-14 01:04:10 +02:00
|
|
|
import sqlite3
|
2009-08-29 21:16:27 +02:00
|
|
|
if use_pool:
|
|
|
|
sqlite3 = pool.manage(sqlite3, pool_size=1)
|
|
|
|
else:
|
|
|
|
logging.warning("SQLite won't work well without 'sqlalchemy' installed.")
|
2009-09-25 21:18:13 +02:00
|
|
|
|
2009-11-26 15:02:48 +01:00
|
|
|
if not os.path.isdir(Configuration.DIR_DATABASES) and not database == ":memory:":
|
2009-11-03 10:50:13 +01:00
|
|
|
print "Creating directory: '%s'" % (Configuration.DIR_DATABASES)
|
|
|
|
os.mkdir(Configuration.DIR_DATABASES)
|
2009-12-15 15:56:18 +01:00
|
|
|
database = os.path.join(Configuration.DIR_DATABASES, database)
|
2009-11-26 15:02:48 +01:00
|
|
|
self.db = sqlite3.connect(database, detect_types=sqlite3.PARSE_DECLTYPES )
|
2009-07-14 01:04:10 +02:00
|
|
|
sqlite3.register_converter("bool", lambda x: bool(int(x)))
|
|
|
|
sqlite3.register_adapter(bool, lambda x: "1" if x else "0")
|
2009-11-17 17:12:24 +01:00
|
|
|
self.db.create_function("floor", 1, math.floor)
|
|
|
|
tmp = sqlitemath()
|
|
|
|
self.db.create_function("mod", 2, tmp.mod)
|
2009-11-16 08:19:24 +01:00
|
|
|
if use_numpy:
|
|
|
|
self.db.create_aggregate("variance", 1, VARIANCE)
|
|
|
|
else:
|
|
|
|
logging.warning("Some database functions will not work without NumPy support")
|
2008-12-06 13:40:04 +01:00
|
|
|
else:
|
2009-08-12 02:46:39 +02:00
|
|
|
raise FpdbError("unrecognised database backend:"+backend)
|
2009-12-12 10:51:07 +01:00
|
|
|
|
2009-09-16 05:10:37 +02:00
|
|
|
self.cursor = self.db.cursor()
|
2008-12-06 13:40:04 +01:00
|
|
|
# Set up query dictionary as early in the connection process as we can.
|
|
|
|
self.sql = FpdbSQLQueries.FpdbSQLQueries(self.get_backend_name())
|
2009-07-14 01:04:10 +02:00
|
|
|
self.cursor.execute(self.sql.query['set tx level'])
|
2009-09-16 05:10:37 +02:00
|
|
|
self.wrongDbVersion = False
|
2008-12-06 13:40:04 +01:00
|
|
|
try:
|
|
|
|
self.cursor.execute("SELECT * FROM Settings")
|
2009-09-16 05:10:37 +02:00
|
|
|
settings = self.cursor.fetchone()
|
|
|
|
if settings[0] != 118:
|
2008-12-06 13:40:04 +01:00
|
|
|
print "outdated or too new database version - please recreate tables"
|
2009-09-16 05:10:37 +02:00
|
|
|
self.wrongDbVersion = True
|
2008-12-06 13:40:04 +01:00
|
|
|
except:# _mysql_exceptions.ProgrammingError:
|
2009-09-04 23:12:35 +02:00
|
|
|
if database != ":memory:": print "failed to read settings table - please recreate tables"
|
2009-09-16 05:10:37 +02:00
|
|
|
self.wrongDbVersion = True
|
2008-12-06 13:40:04 +01:00
|
|
|
#end def connect
|
2008-08-04 05:44:28 +02:00
|
|
|
|
2008-12-06 13:40:04 +01:00
|
|
|
def disconnect(self, due_to_error=False):
|
|
|
|
"""Disconnects the DB"""
|
|
|
|
if due_to_error:
|
|
|
|
self.db.rollback()
|
|
|
|
else:
|
|
|
|
self.db.commit()
|
|
|
|
self.cursor.close()
|
|
|
|
self.db.close()
|
|
|
|
#end def disconnect
|
2009-11-22 06:00:23 +01:00
|
|
|
|
2008-12-06 13:40:04 +01:00
|
|
|
def reconnect(self, due_to_error=False):
|
|
|
|
"""Reconnects the DB"""
|
|
|
|
#print "started fpdb_db.reconnect"
|
|
|
|
self.disconnect(due_to_error)
|
|
|
|
self.connect(self.backend, self.host, self.database, self.user, self.password)
|
2009-11-22 06:00:23 +01:00
|
|
|
|
2008-12-06 13:40:04 +01:00
|
|
|
def get_backend_name(self):
|
|
|
|
"""Returns the name of the currently used backend"""
|
|
|
|
if self.backend==2:
|
|
|
|
return "MySQL InnoDB"
|
|
|
|
elif self.backend==3:
|
|
|
|
return "PostgreSQL"
|
2009-07-14 01:04:10 +02:00
|
|
|
elif self.backend==4:
|
|
|
|
return "SQLite"
|
2008-12-06 13:40:04 +01:00
|
|
|
else:
|
2009-08-12 02:46:39 +02:00
|
|
|
raise FpdbError("invalid backend")
|
2008-12-06 13:40:04 +01:00
|
|
|
#end def get_backend_name
|
2009-11-22 06:00:23 +01:00
|
|
|
|
2008-12-06 13:40:04 +01:00
|
|
|
def get_db_info(self):
|
|
|
|
return (self.host, self.database, self.user, self.password)
|
|
|
|
#end def get_db_info
|
2009-06-07 21:45:09 +02:00
|
|
|
|
2009-06-07 21:07:18 +02:00
|
|
|
#end class fpdb_db
|