Project

General

Profile

« Previous | Next » 

Revision 40

xml2db: Started refactoring xml2db() to support getting as well as inserting data

View differences:

scripts/xml2db/xml2db
27 27
    db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
28 28
    try:
29 29
        doc = xml.dom.minidom.parse(sys.stdin)
30
        print 'Inserted '+str(xml_db.xml2db(doc.documentElement, db))+' rows'
30
        row_ct_ref = [0]
31
        xml_db.xml2db(db, doc.documentElement, row_ct_ref)
32
        print 'Inserted '+str(row_ct_ref[0])+' rows'
31 33
        if commit: db.commit()
32 34
    finally:
33 35
        db.rollback()
scripts/xml2db/db_util.py
92 92
    check_name(table)
93 93
    return col(run_query(db, 'SELECT * FROM '+table+' LIMIT 0'), 0)
94 94

  
95
def insert_or_get(db, table, row, pkey, row_ct_ref=None):
95
def get(db, table, row, pkey, create=False, row_ct_ref=None):
96 96
    try: return value(select(db, table, [pkey], row))
97 97
    except StopIteration:
98
        if not create: raise
99
        # Insert new row
98 100
        try:
99 101
            row_ct = try_insert(db, table, row).rowcount
100 102
            if row_ct_ref != None and row_ct >= 0: row_ct_ref[0] += row_ct
scripts/xml2db/xml_db.py
31 31
                elif child_name == name: return child
32 32
    return None
33 33

  
34
def xml2db(node, db):
35
    pkeys = {}
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 = {}
36 37
    def pkey(table):
37 38
        if table not in pkeys: pkeys[table] = db_util.pkey(db, table)
38 39
        return pkeys[table]
......
62 63
        # Insert node
63 64
        for try_num in range(2):
64 65
            try:
65
                id_ = db_util.insert_or_get(db, table, row, pkey_, row_ct_ref)
66
                xml_util.set_id(node, id_)
66
                id_ = db_util.get(db, table, row, pkey_, create, row_ct_ref)
67
                if store_ids: xml_util.set_id(node, id_)
67 68
                break
68 69
            except db_util.NullValueException, ex:
69 70
                if try_num > 0: raise # exception still raised after retry
......
77 78
        
78 79
        return id_
79 80
    
80
    row_ct_ref = [0]
81
    main(node)
82
    return row_ct_ref[0]
81
    return main(node)
82

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

Also available in: Unified diff