Esempio n. 1
0
    def insert(self):
        """插入当前对象"""
        cls = self.__class__
        if cls.__shard_field__:
            db_conf, tb_shard = get_db_table(getattr(self, cls.__shard_field__))
        else:
            print cls.__insert_counter__
            db_conf, tb_shard = get_db_table(cls.__insert_counter__)
            cls.__insert_counter__ = cls.__insert_counter__ + 1
            if cls.__insert_counter__ >= settings.DB_TABLES_LEN:
                cls.__insert_counter__ = 0
        cnn = Connection(*db_conf)
        print 'db = %s ' % db_conf[1]
        try:
            tb_name = '%s_%d' % (cls.__tablename__, tb_shard,)

            sql = "UPDATE `seq` SET `id`=LAST_INSERT_ID(`id`+%d) WHERE `tb`=%%s" % settings.DB_TABLES_LEN
            print sql % tb_name 
            id = cnn.execute(sql, tb_name)
            print 'lastid %d' % id
            setattr(self, cls.__pk__, id)
            attrs = self.__dict__
           
            sql = "INSERT INTO `%s` (%s) VALUES (%s)" % (
                    tb_name,
                    ', '.join(map(lambda x: '`%s`' % x, attrs)),
                    ', '.join(['%s' for _ in attrs]),
                )
            print sql
            parameters = [attrs[x] for x in attrs]
            cnn.execute(sql, *parameters)
			
            return getattr(self, cls.__pk__)
        finally:
            cnn.close()
Esempio n. 2
0
 def get_user_id(cls, field_name, field_value):
     db_conf, tb_shard = models.get_db_table(field_value)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (cls.__tablename__, tb_shard,)
         sql = 'SELECT `user_id` FROM `%s` WHERE `field_name`=%%s AND `field_value`=%%s' % (tb_name,)
         row = cnn.get(sql, field_name, field_value)
         return row.user_id
     finally:
         cnn.close()
Esempio n. 3
0
 def get(cls, id):
     db_conf, tb_shard = get_db_table(id)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (cls.__tablename__, tb_shard,)
         sql = 'SELECT * FROM `%s` WHERE `%s`=%%s' % (tb_name, cls.__pk__,)
         row = cnn.get(sql, id)
         return cls(**row)
     finally:
         cnn.close()
Esempio n. 4
0
 def get_friends(cls, user_id):
     """返回好友列表"""
     db_conf, tb_shard = models.get_db_table(user_id)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (cls.__tablename__, tb_shard,)
         sql = 'SELECT `id`, `user_id`, `friend_id`, `created_at` FROM `%s` WHERE `user_id`=%%s' % (tb_name,)
         iter = cnn.iter(sql, user_id)
         return [cls(**row) for row in iter]
     finally:
         cnn.close()
Esempio n. 5
0
 def delete(self):
     """删除当前对象"""
     cls = self.__class__
     id = getattr(self, cls.__pk__)
     db_conf, tb_shard = get_db_table(id)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (cls.__tablename__, tb_shard,)
         sql = 'DELETE FROM `%s` WHERE `%s`=%%s' % (tb_name, cls.__pk__,)
         return cnn.execute(sql, id)
     finally:
         cnn.close()
Esempio n. 6
0
 def get_user_id(cls, field_name, field_value):
     db_conf, tb_shard = models.get_db_table(field_value)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (
             cls.__tablename__,
             tb_shard,
         )
         sql = 'SELECT `user_id` FROM `%s` WHERE `field_name`=%%s AND `field_value`=%%s' % (
             tb_name, )
         row = cnn.get(sql, field_name, field_value)
         return row.user_id
     finally:
         cnn.close()
Esempio n. 7
0
 def get(cls, id):
     db_conf, tb_shard = get_db_table(id)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (
             cls.__tablename__,
             tb_shard,
         )
         sql = 'SELECT * FROM `%s` WHERE `%s`=%%s' % (
             tb_name,
             cls.__pk__,
         )
         row = cnn.get(sql, id)
         return cls(**row)
     finally:
         cnn.close()
Esempio n. 8
0
 def delete(self):
     """删除当前对象"""
     cls = self.__class__
     id = getattr(self, cls.__pk__)
     db_conf, tb_shard = get_db_table(id)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (
             cls.__tablename__,
             tb_shard,
         )
         sql = 'DELETE FROM `%s` WHERE `%s`=%%s' % (
             tb_name,
             cls.__pk__,
         )
         return cnn.execute(sql, id)
     finally:
         cnn.close()
Esempio n. 9
0
 def update(self):
     """修改当前对象"""
     cls = self.__class__
     id = getattr(self, cls.__pk__)
     db_conf, tb_shard = get_db_table(id)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (cls.__tablename__, tb_shard,)
         attrs = self.__dict__
         sql = 'UPDATE `%s` SET %s WHERE `%s`=%%s' % (
                 tb_name,
                 ', '.join(['%s=%%s' % x for x in attrs if x != cls.__pk__]),
                 cls.__pk__,
             )
         parameters = [attrs[x] for x in attrs if x != cls.__pk__]
         parameters.append(getattr(self, cls.__pk__))
         return cnn.execute(sql, *parameters)
     finally:
         cnn.close()
Esempio n. 10
0
 def update(self):
     """修改当前对象"""
     cls = self.__class__
     id = getattr(self, cls.__pk__)
     db_conf, tb_shard = get_db_table(id)
     cnn = Connection(*db_conf)
     try:
         tb_name = '%s_%d' % (
             cls.__tablename__,
             tb_shard,
         )
         attrs = self.__dict__
         sql = 'UPDATE `%s` SET %s WHERE `%s`=%%s' % (
             tb_name,
             ', '.join(['%s=%%s' % x for x in attrs if x != cls.__pk__]),
             cls.__pk__,
         )
         parameters = [attrs[x] for x in attrs if x != cls.__pk__]
         parameters.append(getattr(self, cls.__pk__))
         return cnn.execute(sql, *parameters)
     finally:
         cnn.close()
Esempio n. 11
0
    def insert(self):
        """插入当前对象"""
        cls = self.__class__
        if cls.__shard_field__:
            db_conf, tb_shard = get_db_table(getattr(self,
                                                     cls.__shard_field__))
        else:
            print cls.__insert_counter__
            db_conf, tb_shard = get_db_table(cls.__insert_counter__)
            cls.__insert_counter__ = cls.__insert_counter__ + 1
            if cls.__insert_counter__ >= settings.DB_TABLES_LEN:
                cls.__insert_counter__ = 0
        cnn = Connection(*db_conf)
        print 'db = %s ' % db_conf[1]
        try:
            tb_name = '%s_%d' % (
                cls.__tablename__,
                tb_shard,
            )

            sql = "UPDATE `seq` SET `id`=LAST_INSERT_ID(`id`+%d) WHERE `tb`=%%s" % settings.DB_TABLES_LEN
            print sql % tb_name
            id = cnn.execute(sql, tb_name)
            print 'lastid %d' % id
            setattr(self, cls.__pk__, id)
            attrs = self.__dict__

            sql = "INSERT INTO `%s` (%s) VALUES (%s)" % (
                tb_name,
                ', '.join(map(lambda x: '`%s`' % x, attrs)),
                ', '.join(['%s' for _ in attrs]),
            )
            print sql
            parameters = [attrs[x] for x in attrs]
            cnn.execute(sql, *parameters)

            return getattr(self, cls.__pk__)
        finally:
            cnn.close()