Revision 1850
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
176 | 176 |
|
177 | 177 |
##### Database structure queries |
178 | 178 |
|
179 |
def pkey(db, cache, table, recover=None):
|
|
179 |
def pkey(db, table, recover=None): |
|
180 | 180 |
'''Assumed to be first column in table''' |
181 | 181 |
check_name(table) |
182 |
if table not in cache:
|
|
183 |
cache[table] = col_names(run_query(db,
|
|
182 |
if table not in db.pkeys:
|
|
183 |
db.pkeys[table] = col_names(run_query(db,
|
|
184 | 184 |
'SELECT * FROM '+table+' LIMIT 0', recover=recover)).next() |
185 |
return cache[table]
|
|
185 |
return db.pkeys[table]
|
|
186 | 186 |
|
187 | 187 |
def index_cols(db, table, index): |
188 | 188 |
'''Can also use this for UNIQUE constraints, because a UNIQUE index is |
lib/db_xml.py | ||
---|---|---|
36 | 36 |
elif child_name == name: return child |
37 | 37 |
return None |
38 | 38 |
|
39 |
def get(db, node, pkeys=None, limit=None, start=None): |
|
40 |
if pkeys == None: pkeys = {} |
|
41 |
def pkey(table): return sql.pkey(db, pkeys, table) |
|
39 |
def get(db, node, limit=None, start=None): |
|
40 |
def pkey(table): return sql.pkey(db, table) |
|
42 | 41 |
|
43 | 42 |
node = node.firstChild |
44 | 43 |
table = name_of(node) |
... | ... | |
57 | 56 |
|
58 | 57 |
return sql.select(db, table, fields, conds, limit, start) |
59 | 58 |
|
60 |
def put(db, node, pkeys=None, row_ct_ref=None, on_error=exc.raise_,
|
|
61 |
store_ids=False, parent_id=None):
|
|
59 |
def put(db, node, row_ct_ref=None, on_error=exc.raise_, store_ids=False,
|
|
60 |
parent_id=None): |
|
62 | 61 |
'''store_ids enables searching the tree for missing fields''' |
63 |
if pkeys == None: pkeys = {} |
|
64 |
def pkey(table): return sql.pkey(db, pkeys, table, True) |
|
62 |
def pkey(table): return sql.pkey(db, table, True) |
|
65 | 63 |
|
66 | 64 |
def put_(node, parent_id=None): |
67 |
return put(db, node, pkeys, row_ct_ref, on_error, store_ids, parent_id)
|
|
65 |
return put(db, node, row_ct_ref, on_error, store_ids, parent_id) |
|
68 | 66 |
|
69 | 67 |
def on_error_(e): |
70 | 68 |
exc.add_msg(e, 'node:\n'+str(node)) |
bin/map | ||
---|---|---|
237 | 237 |
assert in_is_xpaths |
238 | 238 |
|
239 | 239 |
in_db = connect_db(in_db_config) |
240 |
in_pkeys = {} |
|
241 | 240 |
cur = sql.select(in_db, table=in_root, fields=None, conds=None, |
242 | 241 |
limit=end, start=0) |
243 | 242 |
row_ct = map_table(list(sql.col_names(cur)), sql.rows(cur)) |
... | ... | |
288 | 287 |
import db_xml |
289 | 288 |
|
290 | 289 |
out_db = connect_db(out_db_config) |
291 |
out_pkeys = {} |
|
292 | 290 |
try: |
293 | 291 |
if redo: sql.empty_db(out_db) |
294 | 292 |
row_ins_ct_ref = [0] |
... | ... | |
306 | 304 |
try: |
307 | 305 |
sql.with_savepoint(out_db, |
308 | 306 |
lambda: db_xml.put(out_db, root.firstChild, |
309 |
out_pkeys, row_ins_ct_ref, on_error))
|
|
307 |
row_ins_ct_ref, on_error)) |
|
310 | 308 |
if commit: out_db.db.commit() |
311 | 309 |
except sql.DatabaseErrors, e: on_error(e) |
312 | 310 |
prep_root() |
Also available in: Unified diff
sql.py: pkey(): Use pkeys cache from db object instead of parameter