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 39 aaronmk
    def obj(node, parent_id=None):
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 39 aaronmk
            elif is_ptr(child_name): row[child_name] = obj(ptr_target(child))
55 13 aaronmk
            else: children.append(child)
56 16 aaronmk
        try: del row[pkey_]
57
        except KeyError: pass
58 13 aaronmk
59
        # Add fkey to parent
60 39 aaronmk
        if parent_id != None: row[pkey(name_of(node.parentNode))] = parent_id
61 13 aaronmk
62
        # Insert node
63
        for try_num in range(2):
64
            try:
65 39 aaronmk
                id_ = db_util.insert_or_get(db, table, row, pkey_, row_ct_ref)
66
                xml_util.set_id(node, id_)
67 13 aaronmk
                break
68
            except db_util.NullValueException, ex:
69
                if try_num > 0: raise # exception still raised after retry
70 14 aaronmk
                # Search for required column in ancestors and their children
71 13 aaronmk
                target = find_by_name(node, ptr_type(ex.col))
72
                if target == None: raise
73
                row[ex.col] = xml_util.get_id(target)
74
75
        # Insert children with fkeys to parent
76 39 aaronmk
        for child in children: obj(child, id_)
77
78
        return id_
79 13 aaronmk
80
    row_ct_ref = [0]
81 15 aaronmk
    main(node)
82 13 aaronmk
    return row_ct_ref[0]