Skip to content

Commit

Permalink
Use an real UNIQUE index for "unq" prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgol committed Nov 28, 2014
1 parent 7fe6978 commit 52328db
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions commands/export_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ def search_dict(list, key, val):
for row in list:
if row[key] == val: return row

def getUniqueIndex(conn, tableName):
# return the first unique index
idxCursor = conn.cursor()
unqIndex = []
for idxRow in idxCursor.execute("PRAGMA index_list('%s')" % tableName):
if idxRow['unique']:
# it's a unique index
unqCount = 0
unqCursor = conn.cursor()
for unqRow in unqCursor.execute("PRAGMA index_info('%s')" % idxRow['name']):
# count columns and remember the last name
unqCount += 1
unqIndex.append(unqRow['name'])
return unqIndex
return unqIndex

######################################################################
# Perform query and populate result set

Expand Down Expand Up @@ -114,6 +130,9 @@ def run(results, cmdenv, tdb):
bindValues = []
tableStmt = ''

# prefix for unique columns
uniquePfx = "unq:"

tableCursor = conn.cursor()
for row in tableCursor.execute("""
SELECT name
Expand Down Expand Up @@ -159,6 +178,7 @@ def run(results, cmdenv, tdb):
stmtColumn = []
stmtTable = [ tableName ]
stmtOrder = []
unqIndex = getUniqueIndex(conn, tableName)

# iterate over all columns of the table
for col in cur.execute("PRAGMA table_info('%s')" % tableName):
Expand All @@ -169,7 +189,11 @@ def run(results, cmdenv, tdb):
key = search_dict(keyList, 'column', col['name'])
if key:
# there must be a "name" column in the referenced table
csvHead += [ "name@{}.{}".format(key['table'], key['column']) ]
if col['name'] in unqIndex:
# column is part of an unique index
csvHead += [ uniquePfx + "name@{}.{}".format(key['table'], key['column']) ]
else:
csvHead += [ "name@{}.{}".format(key['table'], key['column']) ]
stmtColumn += [ "{}.name".format(key['table']) ]
if col['notnull']:
stmtTable += [ 'INNER JOIN {} USING({})'.format(key['table'], key['column']) ]
Expand All @@ -178,9 +202,9 @@ def run(results, cmdenv, tdb):
stmtOrder += [ "{}.name".format(key['table']) ]
else:
# ordinary column
if col['name'] == 'name':
# name columns must be unique
csvHead += [ 'unq:name' ]
if col['name'] in unqIndex:
# column is part of an unique index
csvHead += [ uniquePfx + col['name'] ]
stmtOrder += [ "{}.{}".format(tableName, col['name']) ]
else:
csvHead += [ col['name'] ]
Expand Down

0 comments on commit 52328db

Please sign in to comment.