1 from __future__
import print_function
2 import sys, os, datetime
14 suffixes = [
'tags',
'iovs',
'tag_iovs',
'data']
19 db=
'icarus_online_prd'
24 conn = psycopg2.connect(host=host, port=port, dbname=db, user=user, password=pw)
31 print(
'Calibration database %s.' % calib)
35 sqlite_database =
'%s.db' % calib
36 if os.path.exists(sqlite_database):
37 os.remove(sqlite_database)
38 sqlite_conn = sqlite3.connect(sqlite_database)
39 sqlite_cur = sqlite_conn.cursor()
47 for suffix
in suffixes:
51 table_name =
'%s_%s' % (calib, suffix)
52 schema[table_name] = []
53 print(
'Processing table %s.' % table_name)
57 qtbl =
'CREATE TABLE IF NOT EXISTS %s (' % table_name
61 q =
'select column_name, data_type, is_nullable from information_schema.columns where table_name=%s'
62 cur.execute(q, (table_name,))
70 schema[table_name].append(column_name)
78 if data_type ==
'integer':
79 sqlite_type =
'integer'
80 elif data_type ==
'bigint':
81 sqlite_type =
'integer'
82 elif data_type ==
'text':
84 elif data_type ==
'timestamp without time zone':
85 sqlite_type =
'integer'
86 elif data_type ==
'boolean':
87 sqlite_type =
'integer'
88 elif data_type ==
'real':
91 print(
'Unknown type %s' % data_type)
96 qtbl +=
'%s %s' % (column_name, sqlite_type)
100 if first
and (suffix ==
'tags' or suffix ==
'iovs'):
101 qtbl +=
' PRIMARY KEY'
108 if suffix ==
'tag_iovs':
109 qtbl +=
', FOREIGN KEY (iov_id) REFERENCES %s_iovs(iov_id)' % calib
110 qtbl +=
', FOREIGN KEY (tag) REFERENCES %s_tags(tag)' % calib
111 elif suffix ==
'data':
112 qtbl +=
', FOREIGN KEY (__iov_id) REFERENCES %s_iovs(iov_id)' % calib
118 sqlite_cur.execute(qtbl)
126 q =
'SELECT MAX(__iov_id) FROM calib_prd.%s' % table_name
130 print(
'Maximum iov_id = %d' % max_iov_id)
134 for iov_id
in range(max_iov_id + 1):
140 for column
in schema[table_name]:
146 q +=
' FROM calib_prd.%s' % table_name
151 print(
'iov id = %d' % iov_id)
152 q +=
' WHERE __iov_id = %d' % iov_id
157 rows = cur.fetchall()
158 print(
'%d rows fetched.' % len(rows))
159 now = datetime.datetime.now()
164 q =
'INSERT INTO %s (' % table_name
168 for column
in schema[table_name]:
176 values.append(int(element.strftime(
'%s')))
178 values.append(element)
184 sqlite_cur.execute(q, tuple(values))
do one_file $F done echo for F in find $TOP name CMakeLists txt print