# Path: canary/utils.py
# class DTable:
#     """
#     Convenience class for rdbms-persisted object types.  Allows simple
#     loading of data for single-valued fields using set() and for multi-valued
#     fields using append().
#     """
#     
#     def set (self, field, value):
#         """
#         Dynamic field setting for simpler database lookups
#         """
#         if value == None:
#             setattr(self, str(field), str(''))
#         else:
#             setattr(self, str(field), value)
#        
#     #def append (self, field, value):
#     #    list_attr = getattr(self, str(field))
#     #    if not value in list_attr:
#     #        list_attr.append(value)
#     
#        
#     def get_new_uid (self, context):
#         cursor = context.get_cursor()
#         try:
#             cursor.execute('SELECT LAST_INSERT_ID() AS new_uid')
#             row = cursor.fetchone()
#             new_uid = int(row[0])
#         except:
#             new_uid = -1
#         return new_uid
#     
# 
#     def load (self, context):
#         cursor = context.get_cursor()
#         if self.uid:
#             cursor.execute("""SELECT * FROM """ + self.TABLE_NAME +
#                 """
#                 WHERE uid LIKE %s
#                 """, (self.uid))
#             fields = [d[0] for d in cursor.description]
#             desc = dtuple.TupleDescriptor([[f] for f in fields])
#             row = cursor.fetchone()
#             if row:
#                 row = dtuple.DatabaseTuple(desc, row)
#                 for field in fields:
#                     self.set(field, row[field])
#             else:
#                 context.logger.debug('No %s "%s"', self.TABLE_NAME, self.uid)
#                 print 'No %s "%s"' % (self.TABLE_NAME, self.uid)
#                 raise 'InvalidId'
#                 
# 
#     def delete (self, context):
#         cursor = context.get_cursor()
#         try:
#             if self.uid >= 0:
#                 cursor.execute("""DELETE FROM """ + self.TABLE_NAME +
#                     """
#                     WHERE uid = %s
#                     """, self.uid)
#         except Exception, e:
#             context.logger.error(e)


import logging
import traceback
import types
import canary.context
import dtuple
from canary.utils import DTable
            
            for group in self.groups:
                group.category_id = self.uid
                group.save(context)
        
        else:
            cursor.execute("""
                UPDATE categories
                SET name = %s, concept_types = %s
                WHERE uid = %s
                """, (self.name, self.get_types(shorthand=True), self.uid))
        

def load_categories (context):
    cursor = context.get_cursor()
    categories = []
    cursor.execute("""
        SELECT uid
        FROM categories
        ORDER BY name
        """)
    rows = cursor.fetchall()
    for row in rows:
        category = Category(uid=row[0])
        category.load(context)
        categories.append(category)
    
    return categories



# Path: canary/utils.py
# class DTable:
#     """
#     Convenience class for rdbms-persisted object types.  Allows simple
#     loading of data for single-valued fields using set() and for multi-valued
#     fields using append().
#     """
#     
#     def set (self, field, value):
#         """
#         Dynamic field setting for simpler database lookups
#         """
#         if value == None:
#             setattr(self, str(field), str(''))
#         else:
#             setattr(self, str(field), value)
#        
#     #def append (self, field, value):
#     #    list_attr = getattr(self, str(field))
#     #    if not value in list_attr:
#     #        list_attr.append(value)
#     
#        
#     def get_new_uid (self, context):
#         cursor = context.get_cursor()
#         try:
#             cursor.execute('SELECT LAST_INSERT_ID() AS new_uid')
#             row = cursor.fetchone()
#             new_uid = int(row[0])
#         except:
#             new_uid = -1
#         return new_uid
#     
# 
#     def load (self, context):
#         cursor = context.get_cursor()
#         if self.uid:
#             cursor.execute("""SELECT * FROM """ + self.TABLE_NAME +
#                 """
#                 WHERE uid LIKE %s
#                 """, (self.uid))
#             fields = [d[0] for d in cursor.description]
#             desc = dtuple.TupleDescriptor([[f] for f in fields])
#             row = cursor.fetchone()
#             if row:
#                 row = dtuple.DatabaseTuple(desc, row)
#                 for field in fields:
#                     self.set(field, row[field])
#             else:
#                 context.logger.debug('No %s "%s"', self.TABLE_NAME, self.uid)
#                 print 'No %s "%s"' % (self.TABLE_NAME, self.uid)
#                 raise 'InvalidId'
#                 
# 
#     def delete (self, context):
#         cursor = context.get_cursor()
#         try:
#             if self.uid >= 0:
#                 cursor.execute("""DELETE FROM """ + self.TABLE_NAME +
#                     """
#                     WHERE uid = %s
#                     """, self.uid)
#         except Exception, e:
#             context.logger.error(e)


# Path: canary/concept.py
import logging
import traceback
import types
import canary.context
import dtuple
from canary.utils import DTable

            
            for group in self.groups:
                group.category_id = self.uid
                group.save(context)
        
        else:
            cursor.execute("""
                UPDATE categories
                SET name = %s, concept_types = %s
                WHERE uid = %s
                """, (self.name, self.get_types(shorthand=True), self.uid))
        

def load_categories (context):
    cursor = context.get_cursor()
    categories = []
    cursor.execute("""
        SELECT uid
        FROM categories
        ORDER BY name
        """)
    rows = cursor.fetchall()
    for row in rows:
        category = Category(uid=row[0])
        category.load(context)
        categories.append(category)
    
    return categories



class CategoryGroup (DTable):