- Timestamp:
- Jun 19, 2012, 5:24:19 PM (14 years ago)
- Location:
- branches/meh_branches/ppstack_test
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
ippToPsps/jython/mysql.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppstack_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/ippToPsps/jython/mysql.py
r33415 r34041 19 19 ''' 20 20 Constructor 21 22 21 ''' 23 22 def __init__(self, logger, config, dbType, dbName=None): … … 35 34 self.dbPass = config.getDbPassword(dbType) 36 35 36 # a user friendly connection sring 37 connectionStr = self.dbName + "@" + self.dbHost 38 37 39 # set up JDBC connection 38 self.url = "jdbc:mysql://"+self.dbHost+"/"+self.dbName+"? user="+self.dbUser+"&password="+self.dbPass40 self.url = "jdbc:mysql://"+self.dbHost+"/"+self.dbName+"?autoReconnect=true&user="+self.dbUser+"&password="+self.dbPass 39 41 try: 40 42 self.con = DriverManager.getConnection(self.url) 41 43 self.connectionID = self.getLastConnectionID() 42 self.logger. debug("MySQL connection to %s with ID %d" % (dbType, self.connectionID))44 self.logger.infoPair("Connected to MySQL Db", connectionStr) 43 45 except: 44 self.logger.error("Unable to connect to " + self.url) 45 self.everythingOK = False # TODO need this? 46 self.logger.errorPair("Unable to connect to", connectionStr) 46 47 self.connected = False 47 return 48 49 self.everythingOK = True 48 raise 49 50 50 self.connected = True 51 51 … … 89 89 sql = "KILL %d" % connectionID 90 90 self.execute(sql) 91 92 ''' 93 Checks whether this table exists 94 ''' 95 def tableExists(self, tableName): 96 97 sql = "SELECT COUNT(*) \ 98 FROM information_schema.tables \ 99 WHERE table_schema = '" + self.dbName + "' \ 100 AND table_name = '" + tableName + "'" 101 102 try: 103 rs = self.executeQuery(sql) 104 rs.first() 105 if rs.getInt(1) > 0: return True 106 else: return False 107 except: 108 self.logger.errorPair("Could not check if table exists", sql) 109 pass 110 111 return False 91 112 92 113 ''' … … 151 172 152 173 ''' 174 Drops a column from a table 175 ''' 176 def dropColumn(self, table, column): 177 178 sql = "ALTER TABLE " + table + " DROP " + column 179 try: self.execute(sql) 180 except: return 181 182 ''' 183 Drops a list of tables 184 ''' 185 def dropTables(self, tables): 186 for table in tables: self.dropTable(table) 187 188 ''' 153 189 Drops a table 154 190 ''' … … 185 221 #self.logger.warn("Index already in place on '" + column + "' for table '" + table + "'") 186 222 ''' 223 Changes the database engine for this table to InnoDb 224 ''' 225 def changeEngineToInnoDB(self, table): 226 self.changeEngine(table, "InnoDB") 227 228 ''' 229 Changes the database engine for this table 230 ''' 231 def changeEngine(self, table, engine): 232 233 sql = "ALTER TABLE " + table + " ENGINE=" + engine 234 try: 235 self.execute(sql) 236 except: 237 self.logger.errorPair("Could not change table engine", sql) 238 return False 239 240 return True 241 242 ''' 187 243 Adds an index to the supplied table and column 188 244 ''' … … 196 252 except: pass 197 253 #self.logger.warn("Index already in place on '" + column + "' for table '" + table + "'") 254 255 ''' 256 Checks that we have a connection 257 ''' 258 def isConnected(self): 259 260 try: 261 self.con 262 self.connected = True 263 except: 264 self.logger.errorPair("Not connected to", self.url) 265 self.connected = False 266 267 return self.connected 268 198 269 ''' 199 270 TODO 200 271 ''' 201 272 def execute(self, sql): 273 274 if not self.isConnected(): raise 202 275 203 276 stmt = self.con.createStatement() … … 210 283 def executeQuery(self, sql): 211 284 285 if not self.isConnected(): raise 286 212 287 stmt = self.con.createStatement() 213 288 rs = stmt.executeQuery(sql) … … 336 411 return -1 337 412 338 413 ''' 414 Adds a column to this table which contains a row count 415 ''' 416 def addRowCountColumn(self, table, colName): 417 418 self.execute("ALTER TABLE " + table + " ADD COLUMN " + colName + " INTEGER FIRST") 419 self.execute("SET @i = 0") 420 self.execute("UPDATE " + table + " SET " + colName + "=(@i:=@i+1)") 421 self.execute("ALTER TABLE " + table + " ADD PRIMARY KEY (" + colName + ")") 422
Note:
See TracChangeset
for help on using the changeset viewer.
