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
import db_util
7
import xml_util
8
9 16 aaronmk
def name_of(node): return re.sub(r'^.*\.', r'', xml_util.name_of(node))
10
11 13 aaronmk
ptr_suffix = '_id'
12
13
def is_ptr(node_name): return node_name.endswith(ptr_suffix)
14
15
def ptr_type(node_name):
16
    assert is_ptr(node_name)
17
    return node_name[:-len(ptr_suffix)]
18
19
def ptr_target(node):
20
    assert is_ptr(name_of(node))
21
    return xml_util.first_elem(node)
22
23
def find_by_name(node, name):
24
    for parent in xml_util.NodeParentIter(node):
25
        if name_of(parent) == name: return parent
26
        else:
27
            for child in xml_util.NodeElemIter(parent):
28 16 aaronmk
                child_name = name_of(child)
29 13 aaronmk
                if is_ptr(child_name):
30
                    if ptr_type(child_name) == name: return ptr_target(child)
31
                elif child_name == name: return child
32
    return None
33
34
def xml2db(node, db):
35 15 aaronmk
    pkeys = {}
36
    def pkey(table):
37
        if table not in pkeys: pkeys[table] = db_util.pkey(db, table)
38
        return pkeys[table]
39
40
    def main(node):
41 13 aaronmk
        for child in xml_util.NodeElemIter(node):
42 15 aaronmk
            if not xml_util.is_text(child): obj(child) # not XML metadata
43 13 aaronmk
44 15 aaronmk
    def obj(node):
45 13 aaronmk
        table = name_of(node)
46 15 aaronmk
        pkey_ = pkey(table)
47 13 aaronmk
        row = {}
48
        children = []
49
50
        # Divide children into fields and children with fkeys to parent
51 16 aaronmk
        for child in xml_util.NodeElemIter(node):
52
            child_name = name_of(child)
53
            if xml_util.is_text(child): row[child_name] = xml_util.value(child)
54
            elif is_ptr(child_name):
55
                child = ptr_target(child)
56
                obj(child)
57
                row[child_name] = xml_util.get_id(child)
58 13 aaronmk
            else: children.append(child)
59 16 aaronmk
        try: del row[pkey_]
60
        except KeyError: pass
61 13 aaronmk
62
        # Add fkey to parent
63
        parent_id = xml_util.get_id(node.parentNode)
64 15 aaronmk
        if parent_id != '': row[pkey(name_of(node.parentNode))] = parent_id
65 13 aaronmk
66
        # Insert node
67
        for try_num in range(2):
68
            try:
69
                xml_util.set_id(node, db_util.insert_or_get(db, table, row,
70 15 aaronmk
                    pkey_, row_ct_ref))
71 13 aaronmk
                break
72
            except db_util.NullValueException, ex:
73
                if try_num > 0: raise # exception still raised after retry
74 14 aaronmk
                # Search for required column in ancestors and their children
75 13 aaronmk
                target = find_by_name(node, ptr_type(ex.col))
76
                if target == None: raise
77
                row[ex.col] = xml_util.get_id(target)
78
79
        # Insert children with fkeys to parent
80 15 aaronmk
        for child in children: obj(child)
81 13 aaronmk
82
    row_ct_ref = [0]
83 15 aaronmk
    main(node)
84 13 aaronmk
    return row_ct_ref[0]