Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 58 lines (52 sloc) 2.089 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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))])
            data[str(ownerid)][str(itemid)]['TYPE'] = type
    return data
Something went wrong with that request. Please try again.