Project

General

Profile

1
# XML-database conversion
2

    
3
import re
4
from xml.dom import Node
5

    
6
import sql
7
import xml_dom
8

    
9
def name_of(node): return re.sub(r'^.*\.', r'', xml_dom.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_dom.first_elem(node)
22

    
23
def find_by_name(node, name):
24
    for parent in xml_dom.NodeParentIter(node):
25
        if name_of(parent) == name: return parent
26
        else:
27
            for child in xml_dom.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 put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=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] = sql.pkey(db, table)
39
        return pkeys[table]
40
    
41
    table = name_of(node)
42
    pkey_ = pkey(table)
43
    row = {}
44
    children = []
45
    
46
    # Divide children into fields and children with fkeys to parent
47
    for child in xml_dom.NodeElemIter(node):
48
        child_name = name_of(child)
49
        if xml_dom.is_text(child): row[child_name] = xml_dom.value(child)
50
        elif is_ptr(child_name): row[child_name] = put(db, ptr_target(child),
51
            store_ids, row_ct_ref, pkeys)
52
        else: children.append(child)
53
    try: del row[pkey_]
54
    except KeyError: pass
55
    
56
    # Add fkey to parent
57
    if parent_id != None:
58
        parent_ptr = node.getAttribute('fkey')
59
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
60
        row[parent_ptr] = parent_id
61
    
62
    # Insert node
63
    for try_num in xrange(2):
64
        try:
65
            id_ = sql.get(db, table, row, pkey_, True, row_ct_ref)
66
            if store_ids: xml_dom.set_id(node, id_)
67
            break
68
        except sql.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_dom.get_id(target)
74
    
75
    # Insert children with fkeys to parent
76
    for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
77
    
78
    return id_
79

    
80
def xml2db(db, node, row_ct_ref=None):
81
    iter_ = xml_dom.NodeElemIter(node)
82
    while xml_dom.is_text(iter_.curr()): iter_.next() # skip metadata
83
    for child in iter_: put(db, child, True, row_ct_ref)
(2-2/9)