Revision 15
Added by Aaron Marcuse-Kubitza about 13 years ago
scripts/xml2db/db_util.py | ||
---|---|---|
36 | 36 |
raise |
37 | 37 |
return cur |
38 | 38 |
|
39 |
def col(cur, idx): return cur.description[idx][0] |
|
40 |
|
|
39 | 41 |
def row(cur): return iter(lambda: cur.fetchone(), None).next() |
40 | 42 |
|
41 | 43 |
def value(cur): return row(cur)[0] |
... | ... | |
52 | 54 |
return return_val |
53 | 55 |
|
54 | 56 |
def select(db, table, fields, conds): |
55 |
for field in fields: check_name(field) |
|
56 |
for col in conds.keys(): check_name(col) |
|
57 |
check_name(table) |
|
58 |
map(check_name, fields) |
|
59 |
map(check_name, conds.keys()) |
|
57 | 60 |
def cond(entry): |
58 | 61 |
col, value = entry |
59 | 62 |
cond_ = col+' ' |
... | ... | |
67 | 70 |
def insert(db, table, row): |
68 | 71 |
check_name(table) |
69 | 72 |
cols = row.keys() |
70 |
for col in cols: check_name(col)
|
|
73 |
map(check_name, cols)
|
|
71 | 74 |
return run_query(db, 'INSERT INTO '+table+' ('+', '.join(cols) |
72 | 75 |
+') VALUES ('+', '.join(['%s']*len(cols))+')', row.values()) |
73 | 76 |
|
... | ... | |
85 | 88 |
if match: raise NullValueException(match.group(1), ex) |
86 | 89 |
raise # no specific exception raised |
87 | 90 |
|
91 |
def pkey(db, table): # Assumed to be first column in table |
|
92 |
check_name(table) |
|
93 |
return col(run_query(db, 'SELECT * FROM '+table+' LIMIT 0'), 0) |
|
94 |
|
|
88 | 95 |
def insert_or_get(db, table, row, pkey, row_ct_ref=None): |
89 | 96 |
try: return value(select(db, table, [pkey], row)) |
90 | 97 |
except StopIteration: |
scripts/xml2db/vegbank.sql | ||
---|---|---|
6777 | 6777 |
-- PostgreSQL database dump complete |
6778 | 6778 |
-- |
6779 | 6779 |
|
6780 |
-- Modifications |
|
6781 |
|
|
6782 |
ALTER TABLE aux_role RENAME role_id TO aux_role_id; |
|
6783 |
ALTER TABLE place RENAME plotplace_id TO place_id; |
scripts/xml2db/xml_db.py | ||
---|---|---|
20 | 20 |
|
21 | 21 |
def pkey_name(node_name): return node_name+ptr_suffix |
22 | 22 |
|
23 |
def is_field(node_name): return node_name.find('.') >= 0
|
|
23 |
def isfield(node_name): return node_name.find('.') >= 0 |
|
24 | 24 |
|
25 | 25 |
def field_name(node_name): |
26 |
if is_field(node_name): return node_name.partition('.')[2]
|
|
26 |
if isfield(node_name): return node_name.partition('.')[2] |
|
27 | 27 |
else: return node_name |
28 | 28 |
|
29 | 29 |
def is_db_export(node_name): return node_name.find('.') >= 1 # has prefix |
... | ... | |
40 | 40 |
return None |
41 | 41 |
|
42 | 42 |
def xml2db(node, db): |
43 |
def _main(node): |
|
43 |
pkeys = {} |
|
44 |
def pkey(table): |
|
45 |
if table not in pkeys: pkeys[table] = db_util.pkey(db, table) |
|
46 |
return pkeys[table] |
|
47 |
|
|
48 |
def main(node): |
|
44 | 49 |
for child in xml_util.NodeElemIter(node): |
45 |
if not xml_util.is_text(child): _obj(child) # not XML metadata
|
|
50 |
if not xml_util.is_text(child): obj(child) # not XML metadata |
|
46 | 51 |
|
47 |
def _obj(node):
|
|
52 |
def obj(node): |
|
48 | 53 |
table = name_of(node) |
49 | 54 |
db_util.check_name(table) |
50 |
pkey = pkey_name(table)
|
|
55 |
pkey_ = pkey(table)
|
|
51 | 56 |
row = {} |
52 | 57 |
children = [] |
53 | 58 |
iter_ = xml_util.NodeElemIter(node) |
... | ... | |
59 | 64 |
|
60 | 65 |
# Divide children into fields and children with fkeys to parent |
61 | 66 |
for child in iter_: |
62 |
if is_field(name_of(child)): row.update([_field(child)])
|
|
67 |
if isfield(name_of(child)): row.update([field(child)])
|
|
63 | 68 |
else: children.append(child) |
64 | 69 |
|
65 | 70 |
# Add fkey to parent |
66 | 71 |
parent_id = xml_util.get_id(node.parentNode) |
67 |
if parent_id != '': row[pkey_name(name_of(node.parentNode))] = parent_id
|
|
72 |
if parent_id != '': row[pkey(name_of(node.parentNode))] = parent_id |
|
68 | 73 |
|
69 | 74 |
# Insert node |
70 | 75 |
for try_num in range(2): |
71 | 76 |
try: |
72 | 77 |
xml_util.set_id(node, db_util.insert_or_get(db, table, row, |
73 |
pkey, row_ct_ref)) |
|
78 |
pkey_, row_ct_ref))
|
|
74 | 79 |
break |
75 | 80 |
except db_util.NullValueException, ex: |
76 | 81 |
if try_num > 0: raise # exception still raised after retry |
... | ... | |
80 | 85 |
row[ex.col] = xml_util.get_id(target) |
81 | 86 |
|
82 | 87 |
# Insert children with fkeys to parent |
83 |
for child in children: _obj(child)
|
|
88 |
for child in children: obj(child) |
|
84 | 89 |
|
85 |
def _field(node):
|
|
90 |
def field(node): |
|
86 | 91 |
if xml_util.is_text(node): value = xml_util.value(node) |
87 | 92 |
else: |
88 | 93 |
child = xml_util.first_elem(node) |
89 |
_obj(child)
|
|
94 |
obj(child) |
|
90 | 95 |
value = xml_util.get_id(child) |
91 | 96 |
return (field_name(name_of(node)), value) |
92 | 97 |
|
93 | 98 |
row_ct_ref = [0] |
94 |
_main(node)
|
|
99 |
main(node) |
|
95 | 100 |
return row_ct_ref[0] |
Also available in: Unified diff
Changed xml2db to use the first column in a table as its primary key