# Path: canary/dtuple.py
# class TupleDescriptor:
# class DatabaseTuple:
#   def __init__(self, desc):
#   def __len__(self):
#   def __repr__(self):
#   def __str__(self):
#   def __init__(self, desc, data):
#   def __str__(self):
#   def __repr__(self):
#   def __cmp__(self, other):
#   def __getattr__(self, name):
#   def __setattr__(self, name, value):
#   def __getitem__(self, key):
#   def __setitem__(self, key, value):
#   def __len__(self):
#   def __getslice__(self, i, j):
#   def __setslice__(self, i, j, list):
#   def _keys_(self):
#   def _has_key_(self, key):
#   def _items_(self):
#   def _count_(self, item):
#   def _index_(self, item):
#   def _getvalue_(self,name):
#   def asMapping(self):
#   def asTuple(self):
#   def asList(self):
# 
# 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 random
import sha
import canary
from canary import dtuple
from canary.utils import DTable
                    (uid, user_id, record_id, notes, date_created)
                    VALUES 
                    (NULL, %s, %s, %s, NOW())
                    """, (self.user_id, self.record_id, self.notes)
                    )
                self.uid = self.get_new_uid(context)
                context.logger.info('UserRecord %s created with uid %s', self.record_id, self.uid)
            else:
                # NOTE: only updates notes field!
                update_phrase = 'UPDATE %s ' % self.TABLE_NAME
                cursor.execute(update_phrase + """
                    SET notes=%s
                    WHERE uid = %s
                    """, (self.notes,
                    self.uid)
                    )
                context.logger.info('UserRecord %s updated', self.uid)
        except Exception, e:
            context.logger.error(e)

    def delete (self, context):
        cursor = context.get_cursor();
        try:
            # First delete all sets to which this record was assigned
            cursor.execute("""
                DELETE FROM user_set_records
                WHERE record_id = %s
                """, self.record_id)
            
            # Then delete the record itself

# Path: canary/dtuple.py
# class TupleDescriptor:
# class DatabaseTuple:
#   def __init__(self, desc):
#   def __len__(self):
#   def __repr__(self):
#   def __str__(self):
#   def __init__(self, desc, data):
#   def __str__(self):
#   def __repr__(self):
#   def __cmp__(self, other):
#   def __getattr__(self, name):
#   def __setattr__(self, name, value):
#   def __getitem__(self, key):
#   def __setitem__(self, key, value):
#   def __len__(self):
#   def __getslice__(self, i, j):
#   def __setslice__(self, i, j, list):
#   def _keys_(self):
#   def _has_key_(self, key):
#   def _items_(self):
#   def _count_(self, item):
#   def _index_(self, item):
#   def _getvalue_(self,name):
#   def asMapping(self):
#   def asTuple(self):
#   def asList(self):
# 
# 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/user.py
import logging
import random
import sha
import canary
from canary import dtuple
from canary.utils import DTable

                    (uid, user_id, record_id, notes, date_created)
                    VALUES 
                    (NULL, %s, %s, %s, NOW())
                    """, (self.user_id, self.record_id, self.notes)
                    )
                self.uid = self.get_new_uid(context)
                context.logger.info('UserRecord %s created with uid %s', self.record_id, self.uid)
            else:
                # NOTE: only updates notes field!
                update_phrase = 'UPDATE %s ' % self.TABLE_NAME
                cursor.execute(update_phrase + """
                    SET notes=%s
                    WHERE uid = %s
                    """, (self.notes,
                    self.uid)
                    )
                context.logger.info('UserRecord %s updated', self.uid)
        except Exception, e:
            context.logger.error(e)

    def delete (self, context):
        cursor = context.get_cursor();
        try:
            # First delete all sets to which this record was assigned
            cursor.execute("""
                DELETE FROM user_set_records
                WHERE record_id = %s
                """, self.record_id)
            
            # Then delete the record itself

            DTable.delete(self, context)