1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import sys
19
20 from spacewalk.common.usix import raise_with_tb
21 from spacewalk.common.rhnLog import log_debug
22 from spacewalk.common.rhnConfig import CFG, initCFG
23 from spacewalk.common.rhnException import rhnException
24 from spacewalk.common.rhnTB import add_to_seclist
25
26
27 import sql_table
28 import sql_row
29 import sql_sequence
30 import dbi
31 import sql_types
32 types = sql_types
33
34 from const import ORACLE, POSTGRESQL, SUPPORTED_BACKENDS
35
36
37 from sql_base import SQLError, SQLSchemaError, SQLConnectError, \
38 SQLStatementPrepareError, Statement, ModifiedRowError
39
40
41
42
43
44
45 -def __init__DB(backend, host, port, username, password, database, sslmode, sslrootcert):
46 """
47 Establish and check the connection so we can wrap it and handle
48 exceptions.
49 """
50
51 global __DB
52 try:
53 my_db = __DB
54 except NameError:
55 db_class = dbi.get_database_class(backend=backend)
56 __DB = db_class(host, port, username, password, database, sslmode, sslrootcert)
57 __DB.connect()
58 return
59 else:
60 del my_db
61
62 if __DB.is_connected_to(backend, host, port, username, password,
63 database, sslmode, sslrootcert):
64 __DB.check_connection()
65 return
66
67 __DB.commit()
68 __DB.close()
69
70 __DB = dbi.get_database_class(backend=backend)(
71 host, port, username, password, database, sslmode, sslrootcert)
72 __DB.connect()
73 return 0
74
75
76 -def __init__DB2(backend, host, port, username, password, database, sslmode, sslrootcert):
77 """
78 Establish and check the connection so we can wrap it and handle
79 exceptions.
80 """
81
82 global __DB2
83 try:
84 my_db = __DB2
85 except NameError:
86 db_class = dbi.get_database_class(backend=backend)
87 __DB2 = db_class(host, port, username, password, database, sslmode, sslrootcert)
88 __DB2.connect()
89 return
90 else:
91 del my_db
92
93 if __DB2.is_connected_to(backend, host, port, username, password,
94 database, sslmode, sslrootcert):
95 __DB2.check_connection()
96 return
97
98 __DB2.commit()
99 __DB2.close()
100
101 __DB2 = dbi.get_database_class(backend=backend)(
102 host, port, username, password, database, sslmode, sslrootcert)
103 __DB2.connect()
104 return 0
105
106
107 -def initDB(backend=None, host=None, port=None, username=None,
108 password=None, database=None, sslmode=None, sslrootcert=None, initsecond=False):
109 """
110 Initialize the database.
111
112 Either we get backend and all parameter which means the caller
113 knows what they are doing, or we populate everything from the
114 config files.
115
116 initsecond: If set to True it initialize a second DB connection.
117 By default only one DB connection is needed.
118 """
119
120 if backend is None:
121 if CFG is None or not CFG.is_initialized():
122 initCFG('server')
123 backend = CFG.DB_BACKEND
124 host = CFG.DB_HOST
125 port = CFG.DB_PORT
126 database = CFG.DB_NAME
127 username = CFG.DB_USER
128 password = CFG.DB_PASSWORD
129 sslmode = None
130 sslrootcert = None
131 if CFG.DB_SSL_ENABLED:
132 sslmode = 'verify-full'
133 sslrootcert = CFG.DB_SSLROOTCERT
134
135 if backend not in SUPPORTED_BACKENDS:
136 raise rhnException("Unsupported database backend", backend)
137
138 if port:
139 port = int(port)
140
141
142 add_to_seclist(password)
143 try:
144 if initsecond == False:
145 __init__DB(backend, host, port, username, password, database, sslmode, sslrootcert)
146 else:
147 __init__DB2(backend, host, port, username, password, database, sslmode, sslrootcert)
148
149
150
151
152 except SQLConnectError:
153 e = sys.exc_info()[1]
154 try:
155 closeDB()
156 except NameError:
157 pass
158 raise_with_tb(e, sys.exc_info()[2])
159 except:
160 raise
161
162
163
164 return 0
165
166
168 global __DB2
169 try:
170 my_db = __DB2
171 except NameError:
172 return
173 else:
174 del my_db
175
176 if not __DB2:
177 del __DB2
178 return
179 __DB2.commit()
180 __DB2.close()
181 del __DB2
182 return
183
184
185
186
188 global __DB
189 try:
190 my_db = __DB
191 except NameError:
192 __closeDB2()
193 return
194 else:
195 del my_db
196 __DB.commit()
197 __DB.close()
198 del __DB
199 __closeDB2()
200 return
201
202
203
205 global __DB
206 try:
207 return __DB
208 except NameError:
209 raise_with_tb(SystemError("Not connected to any database!"), sys.exc_info()[2])
210
211
213 global __DB2
214 try:
215 return __DB2
216 except NameError:
217
218 try:
219 initDB(initsecond=True)
220 return __DB2
221 except NameError:
222 raise_with_tb(SystemError("Not connected to secondary database!"), sys.exc_info()[2])
223
224
225
226
230
231
232
236
237
238
242
243
244
245 -def Row(table, hash_name, hash_value=None):
248
249
250
251 -def Table(table, hash_name, local_cache=0):
254
255
256
257
258
259
260
264
265
267 db = __test_DB()
268 if isinstance(sql, Statement):
269 sql = sql.statement
270 return db.prepare(sql, blob_map=blob_map)
271
272
278
279
283
284
288
289
294
295
300
301
305
306
310
311
315
316
320
321
325
326
330
331
332 -def Date(*args, **kwargs):
335
336
340
341
345
346
356
357
363
364
366
368 self._name = name
369 self._implementor = None
370
372 return self.__class__("%s.%s" % (self._name, name))
373
375 proc = self._implementor.__call__(self._name)
376 return proc(*args)
377
378
384
385
391
392
394
396 self._wrapped = wrapped
397
399 return self._wrapped(x)
400
401 procedure = _CallableWrapper(_Procedure)
402 function = _CallableWrapper(_Function)
403