- Timestamp:
- May 4, 2011, 3:20:38 PM (15 years ago)
- Location:
- branches/czw_branch/20110406
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
ippToPsps/jython/ipptopspsdb.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/czw_branch/20110406
- Property svn:mergeinfo changed
-
branches/czw_branch/20110406/ippToPsps/jython/ipptopspsdb.py
r31253 r31434 6 6 import logging 7 7 8 from mysql import MySql 8 9 from java.sql import * 9 from xml.etree.ElementTree import ElementTree10 10 11 11 ''' 12 12 Class for ippToPsps database connectivity 13 13 ''' 14 class IppToPspsDb(object): 15 16 driverName="com.mysql.jdbc.Driver" 14 class IppToPspsDb(MySql): 17 15 18 16 ''' 19 17 Constructor 20 21 18 ''' 22 19 def __init__(self, logger): 23 24 # setup logging 25 self.logger = logger 26 self.logger.debug("IppToPspsDb Constructor") 27 # open config 28 doc = ElementTree(file="config.xml") 29 30 # set up JDBC connection 31 dbName = doc.find("ipptopspsdatabase/name").text 32 dbHost = doc.find("ipptopspsdatabase/host").text 33 dbUser = doc.find("ipptopspsdatabase/user").text 34 dbPass = doc.find("ipptopspsdatabase/password").text 35 self.url = "jdbc:mysql://"+dbHost+"/"+dbName+"?user="+dbUser+"&password="+dbPass 36 self.con = DriverManager.getConnection(self.url) 37 self.stmt = self.con.createStatement() 20 super(IppToPspsDb, self).__init__(logger,"ipptopspsdatabase") 38 21 39 22 ''' 40 23 Creates a new batch 41 24 ''' 42 def createNewBatch(self, expID, surveyType, batchType, dvoDb, datastoreProduct):25 def createNewBatch(self, batchType, survey, dvoDb, datastoreProduct): 43 26 44 sql = "SELECT batch_id FROM batches ORDER BY batch_id DESC LIMIT 1" 27 sql = "INSERT INTO batch ( \ 28 batch_type, \ 29 survey, \ 30 dvo_db, \ 31 datastore_product \ 32 ) VALUES ( \ 33 '" + batchType + "', \ 34 '" + survey + "', \ 35 '" + dvoDb + "', \ 36 '" + datastoreProduct + "' \ 37 )" 38 39 self.stmt.execute(sql) 40 41 sql = "SELECT MAX(batch_id) FROM batch" 45 42 46 43 batchID = -1; … … 53 50 self.logger.exception("Unable to get batch ID") 54 51 55 if batchID > 0: 56 batchID = batchID + 1 57 58 sql = "INSERT INTO batches \ 59 (batch_id, \ 60 exp_id, \ 61 survey_id, \ 62 batch_type, \ 63 dvo_db, \ 64 datastore_product) \ 65 VALUES \ 66 ("+str(batchID)+", \ 67 " + str(expID) + ", \ 68 '"+surveyType+"', \ 69 '"+batchType+"', \ 70 '"+dvoDb+"', \ 71 '"+datastoreProduct+"')" 72 73 self.stmt.execute(sql) 74 75 self.logger.info("Creating new batch in ippToPsps database with batchID = %d" % batchID) 52 self.logger.info("Created new batch in ippToPsps database with batchID = %d" % batchID) 76 53 77 54 return batchID; 55 56 ''' 57 Updates min/max object ID on this table and batch 58 ''' 59 def updateMinMaxObjID(self, batchID, minObjID, maxObjID): 60 61 sql = "UPDATE batch SET \ 62 min_obj_id = " + str(minObjID) + ", \ 63 max_obj_id = " + str(maxObjID) + " \ 64 WHERE batch_id = " + str(batchID) 65 66 self.stmt.execute(sql) 67 68 ''' 69 Updates batch processed field 70 ''' 71 def updateProcessed(self, batchID, processed): 72 73 sql = "UPDATE batch \ 74 SET processed = " + str(processed) + " \ 75 WHERE batch_id = " + str(batchID) 76 77 self.stmt.execute(sql) 78 79 ''' 80 Updates batch loaded_to_datastore field 81 ''' 82 def updateLoadedToDatastore(self, batchID, loadedToDatastore): 83 84 sql = "UPDATE batch \ 85 SET loaded_to_datastore = " + str(loadedToDatastore) + " \ 86 WHERE batch_id = " + str(batchID) 87 88 self.stmt.execute(sql) 89 90 ''' 91 Have we already processed and published this batch? 92 ''' 93 def alreadyProcessed(self, table, col, value): 94 95 sql = "SELECT COUNT(*) FROM \ 96 " + table + " \ 97 JOIN batch USING(batch_id) \ 98 WHERE " + col + " = " + str(value) + " \ 99 AND processed \ 100 AND loaded_to_datastore" 101 102 try: 103 rs = self.stmt.executeQuery(sql) 104 rs.first() 105 if rs.getInt(1) > 0: 106 self.logger.error("Batch with "+col+" = "+str(value)+" has already been processed and published to datastore") 107 return True 108 else: 109 return False 110 except: 111 self.logger.exception("Unable to check whether this batch has already been processed") 112 113 114 115 ''' 116 Inserts some detection metadata for this batch ID 117 ''' 118 def insertDetectionMeta(self, batchID, expID, filter): 119 120 sql = "INSERT INTO detection ( \ 121 batch_id \ 122 ,exp_id \ 123 ,filter \ 124 ) VALUES ( \ 125 " + str(batchID) + " \ 126 ," + str(expID) + " \ 127 ,'" + filter + "' \ 128 )" 129 130 self.stmt.execute(sql) 131 132 ''' 133 Inserts some stack metadata for this batch ID 134 ''' 135 def insertStackMeta(self, batchID, skyID, stackID, filter, stackType): 136 137 sql = "INSERT INTO stack ( \ 138 batch_id \ 139 ,sky_id \ 140 ,stack_id \ 141 ,filter \ 142 ,stack_type \ 143 ) VALUES ( \ 144 " + str(batchID) + " \ 145 ," + str(skyID) + " \ 146 ," + str(stackID) + " \ 147 ,'" + filter + "' \ 148 ,'" + stackType + "' \ 149 )" 150 151 self.stmt.execute(sql) 152 78 153 79 154 '''
Note:
See TracChangeset
for help on using the changeset viewer.
