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