-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_restart.py
199 lines (181 loc) · 7.14 KB
/
query_restart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import fdb
from fdb.ibase import *
from fdb.fbcore import *
fbClient = '/home/roman/prj/fb/firebird/gen/Debug/firebird/lib/libfbclient.so'
database = 'localhost:/home/roman/qr/qr.fdb'
def qe(query, traOpt = ISOLATION_LEVEL_READ_COMMITED):
conn = fdb.connect(dsn=database, user='sysdba', password='masterkey', fb_library_name=fbClient)
conn.begin(traOpt)
cursor = conn.cursor()
tr = conn.transaction_info(isc_info_tra_id, 'i')
try:
print(f'T{tr} EXECUTING: {query}')
cursor.execute(query)
if query.startswith('select'):
res = []
try:
res = cursor.fetchall()
print(f'T{tr} RESULT OF: {query}')
for r in res:
print(r)
except Exception as e:
print(f'T{tr} FETCH ERROR: {e}')
else:
print(f'T{tr} EXECUTED: {query}')
except Exception as e:
print(f'T{tr} EXECUTE ERROR: {e}')
return conn
def qec(query, traOpt = ISOLATION_LEVEL_READ_COMMITED):
conn = qe(query, traOpt)
tr = conn.transaction_info(isc_info_tra_id, 'i')
conn.commit()
print(f'T{tr} COMMITTED')
return conn
def qecc(query, traOpt = ISOLATION_LEVEL_READ_COMMITED):
qec(query, traOpt).close()
def prepare_db():
try:
conn = fdb.connect(dsn=database, user='sysdba', password='masterkey', fb_library_name=fbClient)
conn.drop_database()
except Exception as e:
print(e)
conn = fdb.create_database("create database '" + database + "' user 'sysdba' password 'masterkey'", fb_library_name=fbClient)
cursor = conn.cursor()
metadata = ("create table t (i int, v int)",
"create table log (i int, tn int, sn int, o int, n int)",
"insert into t values (1,0)",
"insert into t values (2,0)",
"insert into t values (3,0)",
"insert into t values (4,0)",
"insert into t values (5,0)",
'''
create trigger bu_t for t before update
as
declare cn integer;
declare tn integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
in autonomous transaction do
insert into log values(old.i, :tn, :cn, old.v, new.v);
end
''',
'''
create trigger bd_t for t before delete
as
declare cn integer;
declare tn integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
in autonomous transaction do
insert into log values(old.i, :tn, :cn, old.v, null);
end
'''
,
'''
create procedure p_upd ()
as
declare cn integer;
declare tn integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
in autonomous transaction do
insert into log values(null, -:tn, :cn, null, null);
update t set v=1 where v=-1 or i in (2, 3);
end
''',
'''
create procedure p_s returns (v int)
as
declare cn integer;
declare tn integer;
declare i integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
for select i, v from t where v=-1 or i in (2, 3) into :i, :v do
begin
in autonomous transaction do
insert into log values(:i, -:tn, :cn, :v, null);
suspend;
end
end
''',
'''
create procedure p_slock returns (v int)
as
declare cn integer;
declare tn integer;
declare i integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
for select i, v from t where v=-1 or i in (2, 3) with lock into :i, :v do
begin
in autonomous transaction do
insert into log values(:i, -:tn, :cn, :v, null);
suspend;
end
end
''',
'''
create procedure p_nos
as
declare cn integer;
declare tn integer;
declare i integer;
declare v integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
for select i, v from t where v=-1 or i in (2, 3) with lock into :i, :v do
begin
in autonomous transaction do
insert into log values(:i, -:tn, :cn, :v, null);
end
end
''',
'''
create procedure p_cur
as
begin
for select * from t where v=-1 or i in (2, 3) as cursor cur do
begin
update t set v=1 where current of cur;
end
end
''',
'create view vt as select * from t',
'create view trig_v as select distinct * from t where i>0',
'''
create trigger bu_trig_v for trig_v before update
as
declare cn integer;
declare tn integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
in autonomous transaction do
insert into log values(old.i, -:tn, :cn, old.v, new.v);
update t set v=new.v where i=old.i;
end
''',
'''
create trigger bd_trig_v for trig_v before delete
as
declare cn integer;
declare tn integer;
begin
cn = null; --rdb$get_context('SYSTEM', 'SNAPSHOT_NUMBER');
tn = CURRENT_TRANSACTION;
in autonomous transaction do
insert into log values(old.i, -:tn, :cn, old.v, null);
delete from t where i=old.i;
end
'''
)
for q in metadata:
cursor.execute(q)
conn.commit()