Project

General

Profile

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