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 obj(node, parent_id=None):
42
        table = name_of(node)
43
        pkey_ = pkey(table)
44
        row = {}
45
        children = []
46
        
47
        # Divide children into fields and children with fkeys to parent
48
        for child in xml_util.NodeElemIter(node):
49
            child_name = name_of(child)
50
            if xml_util.is_text(child): row[child_name] = xml_util.value(child)
51
            elif is_ptr(child_name): row[child_name] = obj(ptr_target(child))
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: row[pkey(name_of(node.parentNode))] = parent_id
58
        
59
        # Insert node
60
        for try_num in range(2):
61
            try:
62
                id_ = db_util.get(db, table, row, pkey_, create, row_ct_ref)
63
                if store_ids: xml_util.set_id(node, id_)
64
                break
65
            except db_util.NullValueException, ex:
66
                if try_num > 0: raise # exception still raised after retry
67
                # Search for required column in ancestors and their children
68
                target = find_by_name(node, ptr_type(ex.col))
69
                if target == None: raise
70
                row[ex.col] = xml_util.get_id(target)
71
        
72
        # Insert children with fkeys to parent
73
        for child in children: obj(child, id_)
74
        
75
        return id_
76
    
77
    return obj(node)
78

    
79
def xml2db(db, node, row_ct_ref=None):
80
    for child in xml_util.NodeElemIter(node):
81
        if not xml_util.is_text(child): # not XML metadata
82
            get(db, child, True, True, row_ct_ref)
(11-11/12)