Project

General

Profile

1 13 aaronmk
# XML-database conversion
2
3 16 aaronmk
import re
4 294 aaronmk
import traceback
5 13 aaronmk
from xml.dom import Node
6
7 46 aaronmk
import sql
8 84 aaronmk
import strings
9 133 aaronmk
import util
10 46 aaronmk
import xml_dom
11 13 aaronmk
12 139 aaronmk
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
13 16 aaronmk
14 13 aaronmk
ptr_suffix = '_id'
15
16 138 aaronmk
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
17 13 aaronmk
18 172 aaronmk
def ptr_type_guess(node_name):
19 13 aaronmk
    assert is_ptr(node_name)
20
    return node_name[:-len(ptr_suffix)]
21
22
def ptr_target(node):
23
    assert is_ptr(name_of(node))
24 46 aaronmk
    return xml_dom.first_elem(node)
25 13 aaronmk
26
def find_by_name(node, name):
27 46 aaronmk
    for parent in xml_dom.NodeParentIter(node):
28 13 aaronmk
        if name_of(parent) == name: return parent
29
        else:
30 46 aaronmk
            for child in xml_dom.NodeElemIter(parent):
31 16 aaronmk
                child_name = name_of(child)
32 13 aaronmk
                if is_ptr(child_name):
33 172 aaronmk
                    target = ptr_target(child)
34
                    if target.tagName == name: return target
35 13 aaronmk
                elif child_name == name: return child
36
    return None
37
38 146 aaronmk
def get(db, node, pkeys=None, limit=None):
39 126 aaronmk
    if pkeys == None: pkeys = {}
40
    def pkey(table): return sql.pkey(db, pkeys, table)
41
42 141 aaronmk
    node = node.firstChild
43 135 aaronmk
    table = name_of(node)
44
    pkey_ = pkey(table)
45
46
    fields = []
47
    conds = {}
48 133 aaronmk
    for child in xml_dom.NodeElemIter(node):
49 135 aaronmk
        child_name = name_of(child)
50
        if xml_dom.is_empty(child): fields.append(child_name)
51
        elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child)
52
        else: raise Exception('Joins not supported yet')
53
    id_ = xml_dom.get_id(node)
54
    if id_ != '': conds[pkey(table)] = id_ # replace any existing pkey value
55
    if fields == []: fields.append(pkey_)
56 133 aaronmk
57 146 aaronmk
    return sql.select(db, table, fields, conds, limit)
58 126 aaronmk
59 48 aaronmk
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
60 138 aaronmk
    '''store_ids enables searching the tree for missing fields'''
61 40 aaronmk
    if pkeys == None: pkeys = {}
62 126 aaronmk
    def pkey(table): return sql.pkey(db, pkeys, table)
63 15 aaronmk
64 48 aaronmk
    table = name_of(node)
65
    pkey_ = pkey(table)
66
    row = {}
67
    children = []
68 13 aaronmk
69 48 aaronmk
    # Divide children into fields and children with fkeys to parent
70
    for child in xml_dom.NodeElemIter(node):
71
        child_name = name_of(child)
72 84 aaronmk
        if xml_dom.is_text(child):
73
            row[child_name] = strings.to_unicode(xml_dom.value(child))
74 48 aaronmk
        elif is_ptr(child_name): row[child_name] = put(db, ptr_target(child),
75
            store_ids, row_ct_ref, pkeys)
76
        else: children.append(child)
77
    try: del row[pkey_]
78
    except KeyError: pass
79
80
    # Add fkey to parent
81 59 aaronmk
    if parent_id != None:
82
        parent_ptr = node.getAttribute('fkey')
83
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
84
        row[parent_ptr] = parent_id
85 48 aaronmk
86
    # Insert node
87 70 aaronmk
    for try_num in xrange(2):
88 48 aaronmk
        try:
89
            id_ = sql.get(db, table, row, pkey_, True, row_ct_ref)
90
            if store_ids: xml_dom.set_id(node, id_)
91
            break
92
        except sql.NullValueException, ex:
93
            if try_num > 0: raise # exception still raised after retry
94 138 aaronmk
            if store_ids and is_ptr(ex.col):
95 73 aaronmk
                # Search for required column in ancestors and their children
96 172 aaronmk
                target = find_by_name(node, ptr_type_guess(ex.col))
97 73 aaronmk
                if target == None: raise
98
                row[ex.col] = xml_dom.get_id(target)
99
            else: raise
100 48 aaronmk
101
    # Insert children with fkeys to parent
102
    for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
103
104
    return id_
105 40 aaronmk
106 295 aaronmk
def xml2db(db, node, commit=False, row_ct_ref=None):
107 48 aaronmk
    iter_ = xml_dom.NodeElemIter(node)
108 133 aaronmk
    util.skip(iter_, xml_dom.is_text) # skip metadata
109 294 aaronmk
    for child in iter_:
110 295 aaronmk
        try:
111
            put(db, child, False, row_ct_ref)
112
            if commit: db.commit()
113
        except Exception:
114
            db.rollback()
115
            traceback.print_exc()