Project

General

Profile

1
# XML-database conversion
2

    
3
import re
4
from xml.dom import Node
5

    
6
import db_util
7
import xml_util
8

    
9
def name_of(node): return re.sub(r'^.*\.', r'', xml_util.name_of(node))
10

    
11
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
                child_name = name_of(child)
29
                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
    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
        for child in xml_util.NodeElemIter(node):
42
            if not xml_util.is_text(child): obj(child) # not XML metadata
43
    
44
    def obj(node):
45
        table = name_of(node)
46
        pkey_ = pkey(table)
47
        row = {}
48
        children = []
49
        
50
        # Divide children into fields and children with fkeys to parent
51
        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
            else: children.append(child)
59
        try: del row[pkey_]
60
        except KeyError: pass
61
        
62
        # Add fkey to parent
63
        parent_id = xml_util.get_id(node.parentNode)
64
        if parent_id != '': row[pkey(name_of(node.parentNode))] = parent_id
65
        
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
                    pkey_, row_ct_ref))
71
                break
72
            except db_util.NullValueException, ex:
73
                if try_num > 0: raise # exception still raised after retry
74
                # Search for required column in ancestors and their children
75
                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
        for child in children: obj(child)
81
    
82
    row_ct_ref = [0]
83
    main(node)
84
    return row_ct_ref[0]
(8-8/9)