Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first implementation of 3store and relational queries for data …
…retrieval. Should collate data into identical hierarchical format.
- Loading branch information
1 parent
760550a
commit f9c9aef
Showing
2 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
__author__="majorerror" | ||
__date__ ="$31-Oct-2010 09:42:52$" | ||
|
||
from MySQLdb import * | ||
from itemtypes import TYPES | ||
|
||
DB = connect(user="root", db="3store") | ||
|
||
def choose_owners(num=10): | ||
c = DB.cursor() | ||
c.execute("""SELECT DISTINCT Owner.ownerid FROM Owner | ||
LEFT JOIN Item ON Owner.ownerid = Item.ownerid | ||
GROUP BY Owner.ownerid | ||
ORDER BY COUNT(itemid) DESC LIMIT %d""" % num) | ||
return [str(o[0]) for o in c.fetchall()] | ||
|
||
def get_data_3store(owners): | ||
c = DB.cursor() | ||
c.execute("""SELECT Item.itemid, Item.type, ownerid, predicate, value | ||
FROM Item LEFT JOIN ItemDetails ON Item.itemid = ItemDetails.itemid | ||
WHERE Item.Ownerid IN (%s)""" % ', '.join(owners)) | ||
data = {} | ||
for row in c.fetchall(): | ||
itemid, type, ownerid, key, val = row | ||
if not key in TYPES[type]: | ||
continue | ||
if not str(ownerid) in data: | ||
data[str(ownerid)] = {} | ||
if not str(itemid) in data[str(ownerid)]: | ||
data[str(ownerid)][str(itemid)] = {} | ||
data[str(ownerid)][str(itemid)]['TYPE'] = type | ||
data[str(ownerid)][str(itemid)][key] = val | ||
return data | ||
|
||
def get_data_rel(owners): | ||
c = DB.cursor() | ||
c.execute("""SELECT Item.itemid, type FROM Item WHERE Item.Ownerid IN (%s)""" % ', '.join(owners)) | ||
items = {} | ||
for row in c.fetchall(): | ||
itemid, type = row | ||
if not type in items: | ||
items[type] = [] | ||
items[type].append(itemid) | ||
data = {} | ||
for type in items: | ||
keys = TYPES[type].keys() | ||
c = DB.cursor() | ||
c.execute('SELECT Item.itemid, ownerid, %s FROM Item LEFT JOIN %s ON Item.itemid = %s.itemid WHERE Item.itemid IN (%s)' % | ||
(', '.join(keys), type, type, ', '.join([str(x) for x in items[type]]))) | ||
for row in c.fetchall(): | ||
itemid = row[0] | ||
ownerid = row[1] | ||
if not str(ownerid) in data: | ||
data[str(ownerid)] = {} | ||
data[str(ownerid)][str(itemid)] = dict([(keys[i], row[i + 2]) for i in xrange(0, len(keys) - 1)]) | ||
data[str(ownerid)][str(itemid)]['TYPE'] = type | ||
return data |