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_guess(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
                    target = ptr_target(child)
33
                    if target.tagName == name: return target
34
                elif child_name == name: return child
35
    return None
36

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

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

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