ATLAS Offline Software
Public Member Functions | Public Attributes | List of all members
AtlTriggerDBCopy.OracleExporter Class Reference
Collaboration diagram for AtlTriggerDBCopy.OracleExporter:

Public Member Functions

def __init__ (self, connection)
 
def getTables (self, exceptTables)
 
def executeQuery (self, query, data={})
 
def tableSize (self, tableName)
 
def columnNames (self, tableName)
 
def getIndexes (self)
 
def getPrimaryKeys (self)
 
def getForeignKeys (self)
 
def sqliteType (self, oraType)
 
def tableCreationCommand (self, tableName, primaryKeys, foreignKeys, indexes)
 
def extractSchema (self, creationFileName)
 
def copyTable (self, tableName, sqliteInserter, size)
 

Public Attributes

 connection
 
 primaryKeys
 
 foreignKeys
 
 indexes
 
 allTables
 
 ignoreTablesCreate
 
 ignoreTablesFill
 

Detailed Description

Definition at line 102 of file AtlTriggerDBCopy.py.

Constructor & Destructor Documentation

◆ __init__()

def AtlTriggerDBCopy.OracleExporter.__init__ (   self,
  connection 
)

Definition at line 104 of file AtlTriggerDBCopy.py.

104  def __init__(self, connection):
105  self.connection = connection
106  self.primaryKeys = None
107  self.foreignKeys = None
108  self.indexes = None
109  self.allTables = None
110  self.ignoreTablesCreate = [ 'TT_WRITELOCK', 'HLT_SMT_TO_HRE', 'HLT_PARAMETER', 'HLT_RELEASE', 'TRIGGER_LOG',
111  'DBCOPY_SOURCE_DATABASE', 'HLT_RULE_SET', 'TEMP', 'HLT_RULE_PARAMETER', 'HLT_RULE_COMPONENT',
112  'HLT_SETUP', 'TT_USERS', 'HLT_RULE', "HLT_HRC_TO_HRP", "HLT_HRE_TO_HRS",
113  "HLT_HRS_TO_HRU", "HLT_HRU_TO_HRC", "HLT_PRESCALE_SET_ALIAS", "HLT_PRESCALE_SET_COLL",
114  "PRESCALE_SET_ALIAS", "TRIGGER_ALIAS", "L1_PRESCALE_SET_ALIAS", "L1_CALO_SIN_COS",
115  "L1_CI_TO_CSC", "L1_JET_INPUT", "L1_MUON_THRESHOLD_SET"]
116  self.ignoreTablesFill = self.ignoreTablesCreate + [ ]
117  print("Tables to ignore creation: %r" % len(self.ignoreTablesCreate))
118  print("Tables to ignore filling: %r" % len(self.ignoreTablesFill))
119 
120 

Member Function Documentation

◆ columnNames()

def AtlTriggerDBCopy.OracleExporter.columnNames (   self,
  tableName 
)

Definition at line 148 of file AtlTriggerDBCopy.py.

148  def columnNames(self,tableName):
149  query_ColumnNames = "SELECT column_name, data_type FROM sys.all_tab_columns WHERE owner = :SCHEMA AND table_name = :TABLE_NAME ORDER BY column_id"
150  data = { "SCHEMA" : self.connection.schema, "TABLE_NAME" : tableName }
151  result = self.executeQuery( query = query_ColumnNames, data = data )
152  colNames, colTypes = zip(*result)
153  return colNames, colTypes
154 

◆ copyTable()

def AtlTriggerDBCopy.OracleExporter.copyTable (   self,
  tableName,
  sqliteInserter,
  size 
)

Definition at line 248 of file AtlTriggerDBCopy.py.

248  def copyTable(self, tableName, sqliteInserter, size):
249  # build insert statement
250  schema = self.connection.schema
251  colNames,_ = self.columnNames(tableName)
252  colNameList = ",".join(colNames)
253  bindVarList = ",".join(len(colNames)*["?"])
254  insertStatement = f"INSERT INTO {tableName} ({colNameList}) VALUES ({bindVarList})"
255  # select entries from oracle
256  selectQuery = f"SELECT {colNameList} FROM {schema}.{tableName}"
257  #print(selectQuery)
258  result = self.executeQuery( selectQuery )
259  useBulkInsert = True
260  if useBulkInsert:
261  sqliteInserter.insertBulk(insertStatement, result)
262  else:
263  c = 0
264  for data in result:
265  if c%10000==0:
266  print("%i / %i" % (c, size))
267  c+=1
268  sqliteInserter.insert(insertStatement, data)
269  if c==size:
270  break # just doing some timing measurements
271  sqliteInserter.commit()
272 
273 

◆ executeQuery()

def AtlTriggerDBCopy.OracleExporter.executeQuery (   self,
  query,
  data = {} 
)

Definition at line 135 of file AtlTriggerDBCopy.py.

135  def executeQuery(self, query, data = {}):
136  cursor = self.connection.cursor()
137  cursor.arraysize = 1000
138  cursor.execute( query, **data )
139  return cursor
140 

◆ extractSchema()

def AtlTriggerDBCopy.OracleExporter.extractSchema (   self,
  creationFileName 
)

Definition at line 239 of file AtlTriggerDBCopy.py.

239  def extractSchema(self, creationFileName):
240  fk = self.getForeignKeys()
241  pk = self.getPrimaryKeys()
242  indexes = self.getIndexes()
243  with open(creationFileName, "w") as fh:
244  print("Creating schema file for sqlite: %s" % creationFileName)
245  for tableName in self.getTables(exceptTables = self.ignoreTablesCreate):
246  print(self.tableCreationCommand( tableName, pk[tableName], fk.get(tableName, list()), indexes.get(tableName, list())), file = fh)
247 

◆ getForeignKeys()

def AtlTriggerDBCopy.OracleExporter.getForeignKeys (   self)

Definition at line 190 of file AtlTriggerDBCopy.py.

190  def getForeignKeys(self):
191  if self.foreignKeys is not None:
192  return self.foreignKeys
193  print("retrieving foreign key constraints from Oracle")
194  query_ForeignKeys = """
195  SELECT c.table_name, c.constraint_name, a.column_name, c.r_constraint_name, r.table_name, r.column_name
196  FROM sys.all_constraints c
197  INNER JOIN
198  sys.all_cons_columns a
199  on (c.constraint_type='R' and c.constraint_name = a.constraint_name and c.owner = :SCHEMA and a.owner = :SCHEMA)
200  INNER JOIN
201  sys.all_cons_columns r
202  on (c.r_constraint_name = r.constraint_name and r.owner = :SCHEMA)
203  """
204  data = { "SCHEMA" : self.connection.schema }
205  fk = {}
206  result = self.executeQuery( query = query_ForeignKeys, data = data )
207  for table, _, fkcol, _, rtable, rcol in result:
208  if table not in fk:
209  fk[table] = []
210  fk[table] += [ { "col" : fkcol, "rtab" : rtable, "rcol" : rcol } ]
211  self.foreignKeys = fk
212  return self.foreignKeys
213 

◆ getIndexes()

def AtlTriggerDBCopy.OracleExporter.getIndexes (   self)

Definition at line 155 of file AtlTriggerDBCopy.py.

155  def getIndexes(self):
156  if self.indexes is not None:
157  return self.indexes
158  self.indexes = {}
159  print("retrieving indexes from Oracle")
160  query_Indexes = """
161  SELECT table_name, index_name, column_name FROM sys.all_ind_columns WHERE table_owner = :SCHEMA
162  """
163  data = { "SCHEMA" : self.connection.schema }
164  result = self.executeQuery( query = query_Indexes, data = data )
165  for table_name, index_name, column_name in result:
166  if table_name not in self.indexes:
167  self.indexes[table_name] = {}
168  self.indexes[table_name][index_name] = column_name
169  return self.indexes
170 

◆ getPrimaryKeys()

def AtlTriggerDBCopy.OracleExporter.getPrimaryKeys (   self)

Definition at line 171 of file AtlTriggerDBCopy.py.

171  def getPrimaryKeys(self):
172  if self.primaryKeys is not None:
173  return self.primaryKeys
174  print("retrieving primary key constraints from Oracle")
175  query_PrimaryKeys = """
176  SELECT table_name, column_name, position FROM sys.all_cons_columns WHERE owner = :SCHEMA AND constraint_name in
177  (SELECT constraint_name FROM sys.all_constraints WHERE owner = :SCHEMA AND constraint_type='P')
178  ORDER BY table_name, position
179  """
180  data = { "SCHEMA" : self.connection.schema }
181  pk = {}
182  result = self.executeQuery( query = query_PrimaryKeys, data = data )
183  for table_name, column_name, _ in result:
184  if table_name not in pk:
185  pk[table_name] = []
186  pk[table_name] += [ column_name ]
187  self.primaryKeys = pk
188  return self.primaryKeys
189 

◆ getTables()

def AtlTriggerDBCopy.OracleExporter.getTables (   self,
  exceptTables 
)

Definition at line 121 of file AtlTriggerDBCopy.py.

121  def getTables(self, exceptTables):
122  if self.allTables is None:
123  query_ListAllTables = "SELECT table_name FROM all_tables WHERE owner=:SCHEMA"
124  data = { "SCHEMA" : self.connection.schema }
125  result = self.executeQuery(query_ListAllTables, data)
126  self.allTables = [x[0] for x in result]
127  print("All tables: %i" % len(self.allTables))
128  tables = deepcopy(self.allTables)
129  if exceptTables is not None:
130  for x in exceptTables:
131  if x in tables:
132  tables.remove(x)
133  return tables
134 

◆ sqliteType()

def AtlTriggerDBCopy.OracleExporter.sqliteType (   self,
  oraType 
)

Definition at line 214 of file AtlTriggerDBCopy.py.

214  def sqliteType(self, oraType):
215  if oraType.startswith("TIMESTAMP"):
216  return "TEXT"
217  d = { "VARCHAR2" : "TEXT",
218  "CHAR" : "TEXT",
219  "NUMBER" : "INTEGER",
220  "CLOB" : "TEXT"
221  }
222  return d[oraType]
223 

◆ tableCreationCommand()

def AtlTriggerDBCopy.OracleExporter.tableCreationCommand (   self,
  tableName,
  primaryKeys,
  foreignKeys,
  indexes 
)

Definition at line 224 of file AtlTriggerDBCopy.py.

224  def tableCreationCommand(self, tableName, primaryKeys, foreignKeys, indexes):
225  colNames, colTypes = self.columnNames(tableName)
226  lines = []
227  for colName, colType in zip(colNames,colTypes):
228  lines.append( "%s %s" % (colName, self.sqliteType(colType)) )
229  lines.append( "PRIMARY KEY (%s)" % ",".join(primaryKeys) )
230  for fk in foreignKeys:
231  lines.append( "FOREIGN KEY (%s) REFERENCES %s (%s)" % (fk["col"], fk["rtab"], fk["rcol"]) )
232  creationCommand = f"CREATE TABLE IF NOT EXISTS {tableName} (\n "
233  creationCommand += ",\n ".join(lines)
234  creationCommand += "\n);\n"
235  for index_name in indexes:
236  creationCommand += "CREATE INDEX %s on %s(%s);\n" % (index_name, tableName, indexes[index_name])
237  return creationCommand
238 

◆ tableSize()

def AtlTriggerDBCopy.OracleExporter.tableSize (   self,
  tableName 
)

Definition at line 141 of file AtlTriggerDBCopy.py.

141  def tableSize(self,tableName):
142  query_TableSize = "SELECT COUNT(*) FROM {schema}.{tableName}"
143  qdict = { "schema" : self.connection.schema, "tableName" : tableName }
144  result = self.executeQuery( query_TableSize.format(**qdict) )
145  row = result.fetchone()
146  return row[0]
147 

Member Data Documentation

◆ allTables

AtlTriggerDBCopy.OracleExporter.allTables

Definition at line 109 of file AtlTriggerDBCopy.py.

◆ connection

AtlTriggerDBCopy.OracleExporter.connection

Definition at line 105 of file AtlTriggerDBCopy.py.

◆ foreignKeys

AtlTriggerDBCopy.OracleExporter.foreignKeys

Definition at line 107 of file AtlTriggerDBCopy.py.

◆ ignoreTablesCreate

AtlTriggerDBCopy.OracleExporter.ignoreTablesCreate

Definition at line 110 of file AtlTriggerDBCopy.py.

◆ ignoreTablesFill

AtlTriggerDBCopy.OracleExporter.ignoreTablesFill

Definition at line 116 of file AtlTriggerDBCopy.py.

◆ indexes

AtlTriggerDBCopy.OracleExporter.indexes

Definition at line 108 of file AtlTriggerDBCopy.py.

◆ primaryKeys

AtlTriggerDBCopy.OracleExporter.primaryKeys

Definition at line 106 of file AtlTriggerDBCopy.py.


The documentation for this class was generated from the following file:
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
Trk::open
@ open
Definition: BinningType.h:40
query_example.cursor
cursor
Definition: query_example.py:21
python.AtlRunQuerySFO.executeQuery
def executeQuery(run, cu, q, cachekey)
Definition: AtlRunQuerySFO.py:115