Project

General

Profile

1
# XML-database conversion
2

    
3
import re
4
from xml.dom import Node
5

    
6
import sql
7
import strings
8
import util
9
import xml_dom
10

    
11
def name_of(node): return re.sub(r'^.*\.', r'', node.tagName)
12

    
13
ptr_suffix = '_id'
14

    
15
def is_ptr(node_name): return node_name.lower().endswith(ptr_suffix)
16

    
17
def ptr_type(node_name):
18
    assert is_ptr(node_name)
19
    return node_name[:-len(ptr_suffix)]
20

    
21
def ptr_target(node):
22
    assert is_ptr(name_of(node))
23
    return xml_dom.first_elem(node)
24

    
25
def find_by_name(node, name):
26
    for parent in xml_dom.NodeParentIter(node):
27
        if name_of(parent) == name: return parent
28
        else:
29
            for child in xml_dom.NodeElemIter(parent):
30
                child_name = name_of(child)
31
                if is_ptr(child_name):
32
                    if ptr_type(child_name) == name: return ptr_target(child)
33
                elif child_name == name: return child
34
    return None
35

    
36
def get(db, node, pkeys=None, limit=None):
37
    if pkeys == None: pkeys = {}
38
    def pkey(table): return sql.pkey(db, pkeys, table)
39
    
40
    node = node.firstChild
41
    table = name_of(node)
42
    pkey_ = pkey(table)
43
    
44
    fields = []
45
    conds = {}
46
    for child in xml_dom.NodeElemIter(node):
47
        child_name = name_of(child)
48
        if xml_dom.is_empty(child): fields.append(child_name)
49
        elif xml_dom.is_text(child): conds[child_name] = xml_dom.value(child)
50
        else: raise Exception('Joins not supported yet')
51
    id_ = xml_dom.get_id(node)
52
    if id_ != '': conds[pkey(table)] = id_ # replace any existing pkey value
53
    if fields == []: fields.append(pkey_)
54
    
55
    return sql.select(db, table, fields, conds, limit)
56

    
57
def put(db, node, store_ids=False, row_ct_ref=None, pkeys=None, parent_id=None):
58
    '''store_ids enables searching the tree for missing fields'''
59
    if pkeys == None: pkeys = {}
60
    def pkey(table): return sql.pkey(db, pkeys, table)
61
    
62
    table = name_of(node)
63
    pkey_ = pkey(table)
64
    row = {}
65
    children = []
66
    
67
    # Divide children into fields and children with fkeys to parent
68
    for child in xml_dom.NodeElemIter(node):
69
        child_name = name_of(child)
70
        if xml_dom.is_text(child):
71
            row[child_name] = strings.to_unicode(xml_dom.value(child))
72
        elif is_ptr(child_name): row[child_name] = put(db, ptr_target(child),
73
            store_ids, row_ct_ref, pkeys)
74
        else: children.append(child)
75
    try: del row[pkey_]
76
    except KeyError: pass
77
    
78
    # Add fkey to parent
79
    if parent_id != None:
80
        parent_ptr = node.getAttribute('fkey')
81
        if parent_ptr == '': parent_ptr = pkey(name_of(node.parentNode))
82
        row[parent_ptr] = parent_id
83
    
84
    # Insert node
85
    for try_num in xrange(2):
86
        try:
87
            id_ = sql.get(db, table, row, pkey_, True, row_ct_ref)
88
            if store_ids: xml_dom.set_id(node, id_)
89
            break
90
        except sql.NullValueException, ex:
91
            if try_num > 0: raise # exception still raised after retry
92
            if store_ids and is_ptr(ex.col):
93
                # Search for required column in ancestors and their children
94
                target = find_by_name(node, ptr_type(ex.col))
95
                if target == None: raise
96
                row[ex.col] = xml_dom.get_id(target)
97
            else: raise
98
    
99
    # Insert children with fkeys to parent
100
    for child in children: put(db, child, store_ids, row_ct_ref, pkeys, id_)
101
    
102
    return id_
103

    
104
def xml2db(db, node, row_ct_ref=None):
105
    iter_ = xml_dom.NodeElemIter(node)
106
    util.skip(iter_, xml_dom.is_text) # skip metadata
107
    for child in iter_: put(db, child, False, row_ct_ref)
(2-2/11)