Index: trunk/ippToPsps/jython/mysql.py
===================================================================
--- trunk/ippToPsps/jython/mysql.py	(revision 31343)
+++ trunk/ippToPsps/jython/mysql.py	(revision 31343)
@@ -0,0 +1,166 @@
+#!/usr/bin/env jython
+
+import re
+import sys
+import os
+import logging
+
+from java.lang import *
+from java.sql import *
+from xml.etree.ElementTree import ElementTree
+
+'''
+Base class of all MySql database classes.
+'''
+class MySql(object):
+
+    driverName="com.mysql.jdbc.Driver"
+
+    '''
+    Constructor
+
+    '''
+    def __init__(self, logger, dbType):
+
+        # set up logging
+        self.logger = logger
+        self.logger.debug("MySql class constructor")
+
+        # open config and grab database parameters
+        doc = ElementTree(file="config.xml")
+        self.dbName = doc.find(dbType +"/name").text
+        self.dbHost = doc.find(dbType +"/host").text
+        self.dbUser = doc.find(dbType +"/user").text
+        self.dbPass = doc.find(dbType +"/password").text
+
+        # set up JDBC connection
+        self.url = "jdbc:mysql://"+self.dbHost+"/"+self.dbName+"?user="+self.dbUser+"&password="+self.dbPass
+        self.con = DriverManager.getConnection(self.url)
+        self.stmt = self.con.createStatement()
+
+    '''
+    Destructor
+    '''
+    def __del__(self):
+
+        self.logger.debug("MySql destructor")
+        self.stmt.close()
+        self.con.close()
+
+    '''
+    Updates all rows of this column of this table with this value
+    '''
+    def updateAllRows(self, table, column, value):
+
+        sql = "UPDATE " + table + " SET " + column + " = " + value
+        self.stmt.execute(sql)
+
+    '''
+    Drops a table
+    '''
+    def dropTable(self, table):
+
+        sql = "DROP TABLE " + table
+        try: self.stmt.execute(sql)
+        except: return
+
+    '''
+    Adds an index to the supplied table and column
+    '''
+    def createIndex(self, table, column):
+
+        self.logger.debug("Creating index on column '"+column+"' for table '"+table+"'")
+
+        sql = "CREATE INDEX "+table+"_index ON "+table+" ("+column+")"
+        try:
+            self.stmt.execute(sql)
+        except: pass
+            #self.logger.warn("Index already in place on '" + column + "' for table '" + table + "'")
+
+    '''
+    Returns a list of column names for this table
+    '''
+    def getColumnNames(self, tableName):
+
+       sql = "SHOW COLUMNS FROM " + tableName
+       rs = self.stmt.executeQuery(sql)
+       columns = []
+       while (rs.next()): columns.append(rs.getString(1))
+       rs.close()
+       
+       return columns
+
+    '''
+    Replaces all NULL values in the provided table with the provided substitute 
+    '''
+    def replaceNulls(self, tableName, sub):
+
+       # get list of columns
+       columns = self.getColumnNames(tableName)
+
+       # now loop through all columns and replace all NULLs with sub
+       for column in columns:
+          
+          sql = "UPDATE " + tableName + " SET " + column + " = " + sub + " WHERE " + column + " IS NULL"
+          self.stmt.execute(sql)
+
+    '''
+    Reports the number of rows with this column NULL and then deletes them 
+    '''
+    def reportAndDeleteRowsWithNULLS(self, tableName, columnName):
+
+        sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + columnName + " IS NULL"
+        rs = self.stmt.executeQuery(sql)
+        rs.first()
+        nBadFlux = rs.getInt(1)
+        self.logger.info("%d NULL %s values in table %s. Deleting." % (nBadFlux, columnName, tableName))
+
+        sql="DELETE from " + tableName + " WHERE " + columnName + " IS NULL"
+        self.stmt.execute(sql)
+
+    '''
+    Searches a table and reports the columns that are either partially or completely populated with NULLs
+    '''
+    def reportNulls(self, tableName, showPartials):
+
+       # first, count rows
+       sql = "SELECT COUNT(*) FROM " + tableName
+       rs = self.stmt.executeQuery(sql)
+       rs.first()
+       numRows = rs.getInt(1)
+
+       # get list of columns
+       columns = self.getColumnNames(tableName)
+
+       print "+----------------------+---------------+"
+       print "|  %25s           |" % tableName
+       print "+----------------------+---------------+"
+
+       # now see which columns are full of NULLS, with are partially NULL
+       for column in columns:
+          
+          sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + column + " IS NULL"
+          rs = self.stmt.executeQuery(sql)
+          rs.first()
+          if rs.getInt(1) == numRows:
+              print "| %20s | all NULL      |" % column
+          elif showPartials and rs.getInt(1) > 0:
+              print "| %20s | partial NULL  |" % column
+       rs.close()
+       print "+----------------------+---------------+"
+
+    '''
+    Returns a row count for this table
+    '''
+    def getRowCount(self, table):
+
+        sql = "SELECT COUNT(*) FROM " + table
+        try:
+            rs = self.stmt.executeQuery(sql)  
+            rs.first()
+            return rs.getInt(1)
+        except:
+            self.logger.exception("Could not count rows for table: '" + table + "'")
+            return -1
+
+
