示例#1
0
 def test_update_where_multi(self):
     _database = DbConnection('localhost', 'KartonBot', 'root', 'zxc123', 3306)
     _first = _database.select_where('users', {'id': 1, 'vk_id': 376359640})
     _database.update_where('users', {'id': 1}, {'association': 'dima', 'lvl_exp': 0.9})
     _last = _database.select_where('users', {'id': 1, 'vk_id': 376359640})
     print(_first, " != ", _last)
     self.assertNotEqual(_first, _last)
     self.assertEqual(_last, [(1, 1, 376359640, 'dima', 0.9)])
示例#2
0
 def test_update_where(self):
     _database = DbConnection('localhost', 'KartonBot', 'root', 'zxc123', 3306)
     _first = _database.select_where('users', {'id': 1})
     _database.update_where('users', {'id': 1}, {'association': 'дима'})
     _last = _database.select_where('users', {'id': 1})
     print(_first, " != ", _last)
     self.assertNotEqual(_first, _last)
     self.assertEqual(_last, [(1, 1, 376359640, 'дима', 1000.9)])
示例#3
0
class CommandDbWorker:

    def __init__(self):
        self.db = DbConnection('localhost', 'KartonBot', 'root', 'zxc123', 3306, DbConnVersion.SYNC)
        self.table_name = 'categories'

    def select_all(self):
        data = self.db.select_all_table(self.table_name, ['access_level', 'name', 'value', 'attachment'])
        items = []
        for item in data:
            items.append({
                'access_level': item[0],
                'name': item[1],
                'value': item[2],
                'attachment': item[3]})

        return items

    def insert(self, access_lvl: int, comm_name: str, comm_value: str = '', comm_attachment: str = None):
        if comm_attachment is not None:
            return self.db.insert_into(self.table_name, {'access_level': access_lvl,
                                                         'name': comm_name,
                                                         'value': comm_value,
                                                         'attachment': comm_attachment})
        else:
            return self.db.insert_into(self.table_name, {'access_level': access_lvl,
                                                         'name': comm_name,
                                                         'value': comm_value})

    def delete(self, comm_name: str):
        return self.db.delete_where(self.table_name, {'name': comm_name})

    def update(self,  comm_name: str, access_lvl: int = None, value: str = None, attachment: str = None) -> bool:

        args = locals()
        if any(args.values()) is not None:
            dict_of_updates = {}
            if access_lvl is not None:
                dict_of_updates.update({'access_level': access_lvl})
            if value is not None:
                dict_of_updates.update({'value': value})
            if attachment is not None:
                dict_of_updates.update({'attachment': attachment})
            return self.db.update_where(self.table_name, {'name': comm_name}, dict_of_updates)
        else:
            logging.warning('taken zero params and cannot be any update..')
            return False
示例#4
0
class OsuDbWorker:
    def __init__(self):
        self.db = DbConnection('localhost', 'KartonBot', 'root', 'zxc123',
                               3306, DbConnVersion.SYNC)
        self.table_name = 'osu'

    def select_all(self) -> Iterable:
        data = self.db.select_all_table(self.table_name,
                                        ['vk_id', 'nickname', 'mode'])
        items = []
        for item in data:
            items.append({
                'vk_id': item[0],
                'nickname': item[1],
                'mode': item[2]
            })

        return items

    def select_one(self, osu_vk_id: int) -> object:
        return self.db.select_where(self.table_name, {'vk_id': osu_vk_id})

    def insert(self, osu_vk_id: int, osu_nickname: str, mode: int = 1):
        return self.db.insert_into(self.table_name, {
            'vk_id': osu_vk_id,
            'nickname': osu_nickname,
            'mode': mode
        })

    def delete(self, osu_vk_id: int) -> bool:
        return self.db.delete_where(self.table_name, {'vk_id': osu_vk_id})

    def update(self, vk_id, nickname: str = None, mode: int = None) -> bool:
        args = locals()
        if any(args.values()) is not None:
            dict_of_updates = {}
            if nickname is not None:
                dict_of_updates.update({'nickname': nickname})
            if mode is not None:
                dict_of_updates.update({'mode': mode})

            return self.db.update_where(self.table_name, {'vk_id': vk_id},
                                        dict_of_updates)
        else:
            logging.warning('taken zero params and cannot be any update..')
            return False
示例#5
0
class UserDbWorker:
    def __init__(self):
        self.table_name = 'users'
        self.db = DbConnection('localhost', 'KartonBot', 'root', 'zxc123',
                               3306, DbConnVersion.SYNC)

    def select_all(self) -> Iterable:
        data = self.db.select_all_table(
            self.table_name,
            ['access_level', 'vk_id', 'association', 'lvl_exp'])
        items = []
        for item in data:
            items.append({
                'access_level': item[0],
                'vk_id': item[1],
                'association': item[2],
                'lvl_exp': item[3]
            })

        return items

    def first_or_default(self, vk_id: int):
        query = self.db.select_where(self.table_name, {'vk_id': vk_id})
        return query[0] if len(query) > 0 else None

    def insert(self,
               access_level: int,
               vk_id: int,
               association: str,
               level_exp=0) -> bool:
        return self.db.insert_into(
            self.table_name, {
                'access_level': access_level,
                'vk_id': vk_id,
                'association': association,
                'lvl_exp': level_exp
            })

    def delete(self, vk_id: int) -> bool:
        return self.db.delete_where(self.table_name, {'vk_id': vk_id})

    def update(self,
               vk_id,
               association: str = None,
               level: int = None,
               exp: float = None) -> bool:

        args = locals()
        if any(args.values()) is not None:
            dict_of_updates = {}
            if association is not None:
                dict_of_updates.update({'association': association})
            if level is not None:
                dict_of_updates.update({'access_level': level})
            if exp is not None:
                dict_of_updates.update({'lvl_exp': exp})
            return self.db.update_where(self.table_name, {'vk_id': vk_id},
                                        dict_of_updates)
        else:
            logging.warning('taken zero params and cannot be any update..')
            return False

    def contains(self, vk_id: int) -> bool:
        return len(self.db.select_where(self.table_name,
                                        {'vk_id': vk_id})) != 0