From: <dg...@us...> - 2009-11-19 21:10:42
|
Revision: 392 http://pytrainer.svn.sourceforge.net/pytrainer/?rev=392&view=rev Author: dgranda Date: 2009-11-19 21:10:35 +0000 (Thu, 19 Nov 2009) Log Message: ----------- Moving logic to dictionaries. Adding logic for checkTable. Warning to mysql users Modified Paths: -------------- pytrainer/trunk/pytrainer/lib/ddbb.py pytrainer/trunk/pytrainer/lib/sqliteUtils.py Modified: pytrainer/trunk/pytrainer/lib/ddbb.py =================================================================== --- pytrainer/trunk/pytrainer/lib/ddbb.py 2009-11-19 06:04:01 UTC (rev 391) +++ pytrainer/trunk/pytrainer/lib/ddbb.py 2009-11-19 21:10:35 UTC (rev 392) @@ -24,10 +24,10 @@ class DDBB: def __init__(self, configuration): - ddbb_type = configuration.getValue("pytraining","prf_ddbb") - if ddbb_type == "mysql": + self.ddbb_type = configuration.getValue("pytraining","prf_ddbb") + if self.ddbb_type == "mysql": from mysqlUtils import Sql - if ddbb_type == "sqlite": + else: from sqliteUtils import Sql ddbb_host = configuration.getValue("pytraining","prf_ddbbhost") @@ -39,7 +39,7 @@ def connect(self): #si devolvemos 1 ha ido todo con exito #con 0 es que no estaba la bbdd creada - #con 1 imposible conectar a la maquina. + #con -1 imposible conectar a la maquina. var = self.ddbbObject.connect() if var == 0: self.ddbbObject.createDDBB() @@ -206,6 +206,9 @@ args: none returns: none""" logging.debug('>>') + if self.ddbb_type != "sqlite": + logging.error('Support for MySQL database is decommissioned, please migrate to SQLite. Exiting check') + exit(-2) columnsSports = {"id_sports":"integer primary key autoincrement", "name":"varchar(100)", "weight":"float", @@ -236,8 +239,8 @@ "time":"date", "name":"varchar(200)", "sym":"varchar(200)"} - tablesList = ["records","sports","waypoints"] columns = [columnsRecords,columnsSports,columnsWaypoints] + tablesList = {"records":columnsRecords,"sports":columnsSports,"waypoints":columnsWaypoints} try: tablesDB = self.ddbbObject.select("sqlite_master","name", "type IN ('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name") logging.debug('Found '+ str(len(tablesDB))+' tables in db: '+ str(tablesDB)) @@ -248,15 +251,15 @@ if len(tablesDB) > len(tablesList): logging.info('Database has more tables than expected, please check duplicity!') for entry in tablesDB: - if tablesList.count(entry[0]) > 0: + if entry[0] in tablesList: logging.debug('Inspecting '+str(entry[0])+' table') - #self.checkTable(table,columns[tablesList.pos(table)]) # ToDo - tablesList.remove(entry[0]) + self.ddbbObject.checkTable(entry[0],tablesList[entry[0]]) # ToDo + del tablesList[entry[0]] if len(tablesList) > 0: logging.info('Missing '+str(len(tablesList))+' tables in database, adding them') for table in tablesList: logging.info('Adding table '+str(table)) - #self.createTableDefault(table,columns[tablesList.pos(table)]) # ToDo -> review sqliteUtils.createTables + #self.ddbbObject.createTableDefault(table,columns[tablesList.pos(table)]) # ToDo -> review sqliteUtils.createTables else: logging.info('Database has all needed tables') logging.debug('<<') Modified: pytrainer/trunk/pytrainer/lib/sqliteUtils.py =================================================================== --- pytrainer/trunk/pytrainer/lib/sqliteUtils.py 2009-11-19 06:04:01 UTC (rev 391) +++ pytrainer/trunk/pytrainer/lib/sqliteUtils.py 2009-11-19 21:10:35 UTC (rev 392) @@ -179,3 +179,29 @@ retorno.append(row) return retorno + def checkTable(self,tableName,columns): + """19.11.2009 - dgranda + Checks column names and values from table and adds something if missed. New in version 1.7.0 + args: + tableName - string with name of the table + columns - dictionary containing column names and data types coming from definition + returns: none""" + logging.debug('>>') + cur = self.db.cursor() + sql = "PRAGMA table_info(%s);" %tableName + cur.execute(sql) + tableInfo = [] + for row in cur: + tableInfo.append(row) + logging.debug('Raw data retrieved from table '+str(tableName)+': '+str(tableInfo)) + logging.debug('Table '+str(tableName)+' definition: '+str(columns)) + # Comparing data retrieved from DB and what comes from definition + # Extracting data needed (column names and types) + tableInfoComp = {} + for field in tableInfo: + newField = {field[1]:field[2]} + tableInfoComp.update(newField) + logging.debug('Useful data retrieved from table '+str(tableName)+': '+str(tableInfoComp)) + # Finding out if they differ and what exactly if yes - ToDo + logging.debug('<<') + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |