Skip to content

Commit

Permalink
We know item names are going to be unique in future
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Smith committed Dec 5, 2014
1 parent b4f411d commit fc35b35
Showing 1 changed file with 7 additions and 41 deletions.
48 changes: 7 additions & 41 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,44 +263,13 @@ def getCategoriesByNameIndex(cur):
return { name: ID for (ID, name) in cur }


def testItemNamesUniqueAcrossCategories(cur):
def getItemByNameIndex(cur):
"""
Return True if no single item name appears twice.
In previous betas we had some items which appeared
in more than one category.
"""
cur.execute("""
SELECT COUNT(*)
FROM (
SELECT name
FROM Item
GROUP BY name
HAVING COUNT(*) > 1
)
""")
nonUniqueItemCount = cur.fetchone()[0]
return (nonUniqueItemCount == 0)


def getItemByNameIndex(cur, itemNamesAreUnique):
"""
Generate item name index. If item names are not
Generate item name index.
unique, prefix the name with the category id.
"""
if itemNamesAreUnique:
cur.execute("SELECT item_id, name FROM item")
return {
name: itemID
for (itemID, name)
in cur
}
else:
cur.execute("SELECT category_id, item_id, name FROM item")
return {
"{}:{}".format(catID, name): itemID
for (catID, itemID, name)
in cur
}
cur.execute("SELECT item_id, name FROM item")
return { name: itemID for (itemID, name) in cur }


def genSQLFromPriceLines(tdenv, priceFile, db, defaultZero):
Expand All @@ -316,8 +285,7 @@ def genSQLFromPriceLines(tdenv, priceFile, db, defaultZero):
systemByName = getSystemByNameIndex(cur)
categoriesByName = getCategoriesByNameIndex(cur)

itemNamesAreUnique = testItemNamesUniqueAcrossCategories(cur)
itemByName = getItemByNameIndex(cur, itemNamesAreUnique)
itemByName = getItemByNameIndex(cur)

defaultUnits = -1 if not defaultZero else 0
defaultLevel = -1 if not defaultZero else 0
Expand All @@ -331,7 +299,6 @@ def genSQLFromPriceLines(tdenv, priceFile, db, defaultZero):
itemPrefix = ""
DELETED = corrections.DELETED


def changeStation(matches):
nonlocal categoryID, uiOrder, facility, stationID
nonlocal processedStations, processedItems
Expand Down Expand Up @@ -461,9 +428,8 @@ def processItemLine(matches):
modified = None # Use CURRENT_FILESTAMP

# Look up the item ID.
itemPrefix = "" if itemNamesAreUnique else "{}:".format(categoryID)
try:
itemID = itemByName[itemPrefix + itemName]
itemID = itemByName[itemName]
except KeyError:
itemID = -1
if itemID < 0:
Expand All @@ -473,7 +439,7 @@ def processItemLine(matches):
tdenv.DEBUG1("DELETED {}", oldName)
return
try:
itemID = itemByName[itemPrefix + itemName]
itemID = itemByName[itemName]
tdenv.DEBUG1("Renamed {} -> {}", oldName, itemName)
except KeyError:
ex = UnknownItemError(priceFile, lineNo, itemName)
Expand Down

0 comments on commit fc35b35

Please sign in to comment.