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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
"""Database tools."""
import os
from cStringIO import StringIO
from skytools.quoting import quote_copy, quote_literal
import skytools.installer_config
__all__ = [
"fq_name_parts", "fq_name", "get_table_oid", "get_table_pkeys",
"get_table_columns", "exists_schema", "exists_table", "exists_type",
"exists_function", "exists_language", "Snapshot", "magic_insert",
"db_copy_from_dict", "db_copy_from_list", "CopyPipe", "full_copy",
"DBObject", "DBSchema", "DBTable", "DBFunction", "DBLanguage",
"db_install"
]
#
# Fully qualified table name
#
def fq_name_parts(tbl):
"Return fully qualified name parts."
tmp = tbl.split('.')
if len(tmp) == 1:
return ('public', tbl)
elif len(tmp) == 2:
return tmp
else:
raise Exception('Syntax error in table name:'+tbl)
def fq_name(tbl):
"Return fully qualified name."
return '.'.join(fq_name_parts(tbl))
#
# info about table
#
def get_table_oid(curs, table_name):
schema, name = fq_name_parts(table_name)
q = """select c.oid from pg_namespace n, pg_class c
where c.relnamespace = n.oid
and n.nspname = %s and c.relname = %s"""
curs.execute(q, [schema, name])
res = curs.fetchall()
if len(res) == 0:
raise Exception('Table not found: '+table_name)
return res[0][0]
def get_table_pkeys(curs, tbl):
oid = get_table_oid(curs, tbl)
q = "SELECT k.attname FROM pg_index i, pg_attribute k"\
" WHERE i.indrelid = %s AND k.attrelid = i.indexrelid"\
" AND i.indisprimary AND k.attnum > 0 AND NOT k.attisdropped"\
" ORDER BY k.attnum"
curs.execute(q, [oid])
return map(lambda x: x[0], curs.fetchall())
def get_table_columns(curs, tbl):
oid = get_table_oid(curs, tbl)
q = "SELECT k.attname FROM pg_attribute k"\
" WHERE k.attrelid = %s"\
" AND k.attnum > 0 AND NOT k.attisdropped"\
" ORDER BY k.attnum"
curs.execute(q, [oid])
return map(lambda x: x[0], curs.fetchall())
#
# exist checks
#
def exists_schema(curs, schema):
q = "select count(1) from pg_namespace where nspname = %s"
curs.execute(q, [schema])
res = curs.fetchone()
return res[0]
def exists_table(curs, table_name):
schema, name = fq_name_parts(table_name)
q = """select count(1) from pg_namespace n, pg_class c
where c.relnamespace = n.oid and c.relkind = 'r'
and n.nspname = %s and c.relname = %s"""
curs.execute(q, [schema, name])
res = curs.fetchone()
return res[0]
def exists_type(curs, type_name):
schema, name = fq_name_parts(type_name)
q = """select count(1) from pg_namespace n, pg_type t
where t.typnamespace = n.oid
and n.nspname = %s and t.typname = %s"""
curs.execute(q, [schema, name])
res = curs.fetchone()
return res[0]
def exists_function(curs, function_name, nargs):
# this does not check arg types, so may match several functions
schema, name = fq_name_parts(function_name)
q = """select count(1) from pg_namespace n, pg_proc p
where p.pronamespace = n.oid and p.pronargs = %s
and n.nspname = %s and p.proname = %s"""
curs.execute(q, [nargs, schema, name])
res = curs.fetchone()
return res[0]
def exists_language(curs, lang_name):
q = """select count(1) from pg_language
where lanname = %s"""
curs.execute(q, [lang_name])
res = curs.fetchone()
return res[0]
#
# Support for PostgreSQL snapshot
#
class Snapshot(object):
"Represents a PostgreSQL snapshot."
def __init__(self, str):
"Create snapshot from string."
self.sn_str = str
tmp = str.split(':')
if len(tmp) != 3:
raise Exception('Unknown format for snapshot')
self.xmin = int(tmp[0])
self.xmax = int(tmp[1])
self.txid_list = []
if tmp[2] != "":
for s in tmp[2].split(','):
self.txid_list.append(int(s))
def contains(self, txid):
"Is txid visible in snapshot."
txid = int(txid)
if txid < self.xmin:
return True
if txid >= self.xmax:
return False
if txid in self.txid_list:
return False
return True
#
# Copy helpers
#
def _gen_dict_copy(tbl, row, fields):
tmp = []
for f in fields:
v = row[f]
tmp.append(quote_copy(v))
return "\t".join(tmp)
def _gen_dict_insert(tbl, row, fields):
tmp = []
for f in fields:
v = row[f]
tmp.append(quote_literal(v))
fmt = "insert into %s (%s) values (%s);"
return fmt % (tbl, ",".join(fields), ",".join(tmp))
def _gen_list_copy(tbl, row, fields):
tmp = []
for i in range(len(fields)):
v = row[i]
tmp.append(quote_copy(v))
return "\t".join(tmp)
def _gen_list_insert(tbl, row, fields):
tmp = []
for i in range(len(fields)):
v = row[i]
tmp.append(quote_literal(v))
fmt = "insert into %s (%s) values (%s);"
return fmt % (tbl, ",".join(fields), ",".join(tmp))
def magic_insert(curs, tablename, data, fields = None, use_insert = 0):
"""Copy/insert a list of dict/list data to database.
If curs == None, then the copy or insert statements are returned
as string. For list of dict the field list is optional, as its
possible to guess them from dict keys.
"""
if len(data) == 0:
return
# decide how to process
if type(data[0]) == type({}):
if fields == None:
fields = data[0].keys()
if use_insert:
row_func = _gen_dict_insert
else:
row_func = _gen_dict_copy
else:
if fields == None:
raise Exception("Non-dict data needs field list")
if use_insert:
row_func = _gen_list_insert
else:
row_func = _gen_list_copy
# init processing
buf = StringIO()
if curs == None and use_insert == 0:
fmt = "COPY %s (%s) FROM STDIN;\n"
buf.write(fmt % (tablename, ",".join(fields)))
# process data
for row in data:
buf.write(row_func(tablename, row, fields))
buf.write("\n")
# if user needs only string, return it
if curs == None:
if use_insert == 0:
buf.write("\\.\n")
return buf.getvalue()
# do the actual copy/inserts
if use_insert:
curs.execute(buf.getvalue())
else:
buf.seek(0)
hdr = "%s (%s)" % (tablename, ",".join(fields))
curs.copy_from(buf, hdr)
def db_copy_from_dict(curs, tablename, dict_list, fields = None):
"""Do a COPY FROM STDIN using list of dicts as source."""
if len(dict_list) == 0:
return
if fields == None:
fields = dict_list[0].keys()
buf = StringIO()
for dat in dict_list:
row = []
for k in fields:
row.append(quote_copy(dat[k]))
buf.write("\t".join(row))
buf.write("\n")
buf.seek(0)
hdr = "%s (%s)" % (tablename, ",".join(fields))
curs.copy_from(buf, hdr)
def db_copy_from_list(curs, tablename, row_list, fields):
"""Do a COPY FROM STDIN using list of lists as source."""
if len(row_list) == 0:
return
if fields == None or len(fields) == 0:
raise Exception('Need field list')
buf = StringIO()
for dat in row_list:
row = []
for i in range(len(fields)):
row.append(quote_copy(dat[i]))
buf.write("\t".join(row))
buf.write("\n")
buf.seek(0)
hdr = "%s (%s)" % (tablename, ",".join(fields))
curs.copy_from(buf, hdr)
#
# Full COPY of table from one db to another
#
class CopyPipe(object):
"Splits one big COPY to chunks."
def __init__(self, dstcurs, tablename, limit = 512*1024, cancel_func=None):
self.tablename = tablename
self.dstcurs = dstcurs
self.buf = StringIO()
self.limit = limit
self.cancel_func = None
self.total_rows = 0
self.total_bytes = 0
def write(self, data):
"New data from psycopg"
self.total_bytes += len(data)
self.total_rows += data.count("\n")
if self.buf.tell() >= self.limit:
pos = data.find('\n')
if pos >= 0:
# split at newline
p1 = data[:pos + 1]
p2 = data[pos + 1:]
self.buf.write(p1)
self.flush()
data = p2
self.buf.write(data)
def flush(self):
"Send data out."
if self.cancel_func:
self.cancel_func()
if self.buf.tell() > 0:
self.buf.seek(0)
self.dstcurs.copy_from(self.buf, self.tablename)
self.buf.seek(0)
self.buf.truncate()
def full_copy(tablename, src_curs, dst_curs, column_list = []):
"""COPY table from one db to another."""
if column_list:
hdr = "%s (%s)" % (tablename, ",".join(column_list))
else:
hdr = tablename
buf = CopyPipe(dst_curs, hdr)
src_curs.copy_to(buf, hdr)
buf.flush()
return (buf.total_bytes, buf.total_rows)
#
# SQL installer
#
class DBObject(object):
"""Base class for installable DB objects."""
name = None
sql = None
sql_file = None
def __init__(self, name, sql = None, sql_file = None):
self.name = name
self.sql = sql
self.sql_file = sql_file
def create(self, curs, log = None):
if log:
log.info('Installing %s' % self.name)
if self.sql:
sql = self.sql
elif self.sql_file:
fn = self.find_file()
if log:
log.info(" Reading from %s" % fn)
sql = open(fn, "r").read()
else:
raise Exception('object not defined')
curs.execute(sql)
def find_file(self):
full_fn = None
if self.sql_file[0] == "/":
full_fn = self.sql_file
else:
dir_list = skytools.installer_config.dir_list
contrib_list = [
"/opt/pgsql/share/contrib",
"/usr/share/postgresql/8.0/contrib",
"/usr/share/postgresql/8.0/contrib",
"/usr/share/postgresql/8.1/contrib",
"/usr/share/postgresql/8.2/contrib",
]
for dir in contrib_list:
fn = os.path.join(dir, self.sql_file)
if os.path.isfile(fn):
full_fn = fn
break
if not full_fn:
raise Exception('File not found: '+self.sql_file)
return full_fn
class DBSchema(DBObject):
"""Handles db schema."""
def exists(self, curs):
return exists_schema(curs, self.name)
class DBTable(DBObject):
"""Handles db table."""
def exists(self, curs):
return exists_table(curs, self.name)
class DBFunction(DBObject):
"""Handles db function."""
def __init__(self, name, nargs, sql = None, sql_file = None):
DBObject.__init__(self, name, sql, sql_file)
self.nargs = nargs
def exists(self, curs):
return exists_function(curs, self.name, self.nargs)
class DBLanguage(DBObject):
"""Handles db language."""
def __init__(self, name):
DBObject.__init__(self, name, sql = "create language %s" % name)
def exists(self, curs):
return exists_language(curs, self.name)
def db_install(curs, list, log = None):
"""Installs list of objects into db."""
for obj in list:
if not obj.exists(curs):
obj.create(curs, log)
else:
if log:
log.info('%s is installed' % obj.name)
|