Package glue :: Package segmentdb :: Module query_engine
[hide private]
[frames] | no frames]

Source Code for Module glue.segmentdb.query_engine

  1  # 
  2  # Copyright (C) 2009  Larne Pekowsky 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify it 
  5  # under the terms of the GNU General Public License as published by the 
  6  # Free Software Foundation; either version 3 of the License, or (at your 
  7  # option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, but 
 10  # WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 12  # Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License along 
 15  # with this program; if not, write to the Free Software Foundation, Inc., 
 16  # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
 17   
 18   
 19  # 
 20  # ============================================================================= 
 21  # 
 22  #                                  Preamble 
 23  # 
 24  # ============================================================================= 
 25  # 
 26   
 27   
 28  """ 
 29  Provides a unified interface for querying sqlite databases and a DB2 database 
 30  behind a ldbd interface 
 31  """ 
 32   
 33  import os 
 34  try: 
 35      import pyRXP 
 36  except ImportError: 
 37      import pyRXPU as pyRXP 
 38  from glue import ldbd 
 39   
 40  from glue import git_version 
 41  __date__ = git_version.date 
 42  __version__ = git_version.id 
 43  __author__  = "Larne Pekowsky <lppekows@physics.syr.edu>" 
 44   
 45   
46 -class QueryEngine:
47 """Abstract class. Provides query() method that returns something that 48 behaves like an array of tuples of results and a close() method that 49 does any needed cleanup.""" 50
51 - def query(sql):
52 return None
53
54 - def close():
55 pass
56 57
58 -class SqliteQueryEngine(QueryEngine):
59 """QueryEngine for sqlite databases. Really just a minimal wrapper 60 around cursor""" 61 62 connection = None 63 cursor = None 64
65 - def __init__(self, connection):
67
68 - def query(self, sql):
69 self.cursor = self.connection.cursor().execute(sql) 70 ret = [] 71 72 for row in self.cursor: 73 ret.append(row) 74 75 return ret
76
77 - def close(self):
78 self.cursor.close() 79 del self.cursor
80 81
82 -class LdbdQueryEngine(QueryEngine):
83 """QueryEngine for databses behind ldbd. Parses ligolw that a query 84 returns into rows""" 85 86 xmlparser = None 87 lwtparser = None 88 ligomd = None 89 90 rows = None 91 92 93
94 - def __init__(self, client):
95 def dtd_uri_callback(uri): 96 if uri == 'http://ldas-sw.ligo.caltech.edu/doc/ligolwAPI/html/ligolw_dtd.txt': 97 return 'file://localhost' + os.path.join( os.environ["GLUE_PREFIX"], 'etc/ligolw_dtd.txt' ) 98 else: 99 return uri
100 101 self.client = client 102 self.xmlparser = pyRXP.Parser() 103 self.xmlparser.eoCB = dtd_uri_callback 104 self.lwtparser = ldbd.LIGOLwParser() 105 self.ligomd = ldbd.LIGOMetadata(self.xmlparser, self.lwtparser, None)
106 107
108 - def query(self, sql):
109 xml = self.client.query(sql) 110 111 # This is a kludge around bug 2317 112 try: 113 self.client.__disconnect__() 114 self.client.__connect__(self.client.host, self.client.port, self.client.identity) 115 except: 116 pass 117 118 self.ligomd.parse(xml) 119 res = self.ligomd.table 120 self.rows = self.ligomd.table[list(res.keys())[0]]['stream'] 121 122 return self.rows
123
124 - def close(self):
125 del self.rows
126