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 get(db, node, create=False, store_ids=False, row_ct_ref=None, pkeys=None):
35
    # store_ids enables searching the tree for missing fields
36
    if pkeys == None: pkeys = {}
37
    def pkey(table):
38
        if table not in pkeys: pkeys[table] = db_util.pkey(db, table)
39
        return pkeys[table]
40
    
41
    def main(node):
42
        for child in xml_util.NodeElemIter(node):
43
            if not xml_util.is_text(child): obj(child) # not XML metadata
44
    
45
    def obj(node, parent_id=None):
46
        table = name_of(node)
47
        pkey_ = pkey(table)
48
        row = {}
49
        children = []
50
        
51
        # Divide children into fields and children with fkeys to parent
52
        for child in xml_util.NodeElemIter(node):
53
            child_name = name_of(child)
54
            if xml_util.is_text(child): row[child_name] = xml_util.value(child)
55
            elif is_ptr(child_name): row[child_name] = obj(ptr_target(child))
56
            else: children.append(child)
57
        try: del row[pkey_]
58
        except KeyError: pass
59
        
60
        # Add fkey to parent
61
        if parent_id != None: row[pkey(name_of(node.parentNode))] = parent_id
62
        
63
        # Insert node
64
        for try_num in range(2):
65
            try:
66
                id_ = db_util.get(db, table, row, pkey_, create, row_ct_ref)
67
                if store_ids: xml_util.set_id(node, id_)
68
                break
69
            except db_util.NullValueException, ex:
70
                if try_num > 0: raise # exception still raised after retry
71
                # Search for required column in ancestors and their children
72
                target = find_by_name(node, ptr_type(ex.col))
73
                if target == None: raise
74
                row[ex.col] = xml_util.get_id(target)
75
        
76
        # Insert children with fkeys to parent
77
        for child in children: obj(child, id_)
78
        
79
        return id_
80
    
81
    return main(node)
82

    
83
def xml2db(db, node, row_ct_ref=None):
84
    return get(db, node, True, True, row_ct_ref)
(11-11/12)