umbau auf profile/database abgeschlossen, lauffähige version

This commit is contained in:
Philipp Rauch 2014-01-29 18:29:53 +01:00
parent fdd7afd35b
commit 51610ad421
9 changed files with 353 additions and 401 deletions

View file

@ -3,67 +3,30 @@ Created on 26.11.2013
@author: Philipp Rauch
'''
from MySQLdb import connect
from datetime import date, timedelta
from decimal import Decimal
from profile.database import Database
tables = {}
def get_tables(cur, db):
sql_tables = 'SHOW TABLES FROM %s'
tables = []
cur.execute(sql_tables % db)
#TODO try - except
for i in cur:
tables.append(i[0])
return tables
def get_prime(cur, tabele):
sql_get_prim = "SHOW COLUMNS FROM %s WHERE `Key` = 'PRI'"
sql_get_no_prim = "SHOW COLUMNS FROM %s WHERE `Key` != 'PRI'"
collum = []
cur.execute(sql_get_prim % tabele)
#TODO try - except
for i in cur:
collum.append(i[0])
cur.execute(sql_get_no_prim % tabele)
for i in cur:
collum.append(i[0])
return collum
_sort_lookup = { 'EA_Last' : 'DateTime' }
def setup(conf):
'''
establishes a connection to the database and returns a cursor
establishes a connection to the database and returns a Database object
@return: cursor to the database
'''
connection = connect(host = conf['mySQL_server'],
user = conf['mySQL_user'],
passwd = conf['mySQL_pass'],
db = conf['mySQL_database'],
port = int(conf['mySQL_port']))
cursor = connection.cursor()
tab = get_tables(cursor, conf['mySQL_database'])
for i in tab:
primes = get_prime(cursor, i)
tables[i] = primes
print tables
return cursor
def loop(cursor, item):
@return: Database object
'''
db = Database()
db.loadDatabase(strHost = conf['mySQL_server'],
intPort = int(conf['mySQL_port']),
strUser = conf['mySQL_user'],
strPasswd = conf['mySQL_pass'],
strDatabase = conf['mySQL_database'],
strTable = None)
return db
def loop(db, item):
'''
sql_values = 'SELECT * FROM %s ORDER BY %s DESC LIMIT 0, 1'
'''
sql_values = 'SELECT * FROM %s ORDER BY %s DESC LIMIT 1'
path = item.split('/')
if path[0] == '':
path.remove('')
@ -71,34 +34,25 @@ def loop(cursor, item):
table = path[len(path) -1]
if table == 'device':
tmp = { table : {} }
for i in tables.keys():
for i in db.getInformation()['Table'].keys():
tmp[table][i] = {}
return tmp
try:
sort = db.getInformation()['Table_Prime'][table][0]
except:
try:
sort = _sort_lookup[table]
except:
return { 'error/db_error' : { table : 'no Prime-Key / Sort defined' } }
cursor.execute(sql_values % (table, tables[table][0]))
values = []
for row in cursor:
values = row
res = db.readDatabase(intRowOffset=0, intRowNumber=1, strTable=table, strSort=sort, invertSort=True)
result = res.to_dict()
result = {}
for k in result:
result[k] = result[k][0]
for i in range(0, len(tables[table])):
if values == []:
result[tables[table][i]] = None
else:
if isinstance(values[i], date) or isinstance(values[i], timedelta):
result[tables[table][i]] = str(values[i])
elif isinstance(values[i], Decimal):
result[tables[table][i]] = float(values[i])
else:
result[tables[table][i]] = values[i]
# for p in range(len(path)):
# result = { path.pop() : result }
result = {item : result}
return result
return {item : result}
def close(cursor):
pass