2013-11-28 12:41:01 +01:00
|
|
|
'''
|
|
|
|
|
Created on 26.11.2013
|
|
|
|
|
|
2013-12-05 16:45:57 +01:00
|
|
|
@author: Philipp Rauch
|
2013-11-28 12:41:01 +01:00
|
|
|
'''
|
|
|
|
|
from MySQLdb import connect
|
2014-01-22 18:02:24 +01:00
|
|
|
from datetime import date, timedelta
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
|
|
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
|
2013-11-28 12:41:01 +01:00
|
|
|
|
|
|
|
|
def setup(conf):
|
2014-01-15 18:02:59 +01:00
|
|
|
'''
|
|
|
|
|
establishes a connection to the database and returns a cursor
|
|
|
|
|
|
|
|
|
|
@return: cursor to the database
|
|
|
|
|
'''
|
2013-11-28 12:41:01 +01:00
|
|
|
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()
|
2014-01-22 18:02:24 +01:00
|
|
|
|
|
|
|
|
tab = get_tables(cursor, conf['mySQL_database'])
|
|
|
|
|
for i in tab:
|
|
|
|
|
primes = get_prime(cursor, i)
|
|
|
|
|
tables[i] = primes
|
|
|
|
|
print tables
|
|
|
|
|
|
2013-11-28 12:41:01 +01:00
|
|
|
return cursor
|
|
|
|
|
|
|
|
|
|
def loop(cursor, item):
|
2014-01-15 18:02:59 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
2014-01-22 18:02:24 +01:00
|
|
|
sql_values = 'SELECT * FROM %s ORDER BY %s DESC LIMIT 1'
|
2013-11-28 12:41:01 +01:00
|
|
|
|
|
|
|
|
path = item.split('/')
|
|
|
|
|
if path[0] == '':
|
|
|
|
|
path.remove('')
|
|
|
|
|
|
2014-01-22 18:02:24 +01:00
|
|
|
table = path[len(path) -1]
|
|
|
|
|
if table == 'device':
|
|
|
|
|
tmp = { table : {} }
|
|
|
|
|
for i in tables.keys():
|
|
|
|
|
tmp[table][i] = {}
|
|
|
|
|
return tmp
|
|
|
|
|
|
2013-11-28 12:41:01 +01:00
|
|
|
|
2014-01-22 18:02:24 +01:00
|
|
|
cursor.execute(sql_values % (table, tables[table][0]))
|
|
|
|
|
values = []
|
2013-11-28 12:41:01 +01:00
|
|
|
for row in cursor:
|
|
|
|
|
values = row
|
|
|
|
|
|
|
|
|
|
result = {}
|
2014-01-22 18:02:24 +01:00
|
|
|
|
|
|
|
|
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]
|
2013-11-28 12:41:01 +01:00
|
|
|
|
|
|
|
|
# for p in range(len(path)):
|
|
|
|
|
# result = { path.pop() : result }
|
|
|
|
|
result = {item : result}
|
|
|
|
|
|
|
|
|
|
return result
|
2013-12-13 12:34:24 +01:00
|
|
|
|
|
|
|
|
def close(cursor):
|
|
|
|
|
pass
|