def insert(self, db, condition): ttl = None selected_db = db self.__count = 0 self.__error = False self.__result = [] if isinstance(db, list): selected_db = db[0] ttl = db[1] self.connect(selected_db) self.__db.execute_command('SELECT ' + str(selected_db)) i = 0 ins_condition_params = ReaPy.presenter().redis_insert_condition( condition) for c_key, c_value in ins_condition_params.items(): for i_key, i_value in c_value.items(): try: if isinstance(i_value, dict): i_value = ReaPy.json().dumps(i_value) self.__db.set(str(i_key), i_value, ttl) i += 1 except (Exception, ReaPy.redis_error()) as error: self.__error = error self.__count = i return self
def update(self, db, condition): selected_db = db self.__count = 0 self.__error = False self.__result = [] if isinstance(db, list): selected_db = db[0] ttl = db[1] else: ttl = None self.connect(selected_db) self.__db.execute_command('SELECT ' + str(selected_db)) update_value = condition['SET'] if isinstance(update_value, dict): update_value = ReaPy.json().dumps(update_value) else: update_value = ', '.join(update_value) i = 0 try: if condition['CONDITION'] is not None: condition_params = ReaPy.presenter().redis_where_condition( condition['CONDITION']) for c_key in condition_params: db_key = self.find_key(c_key) if db_key is not None: for key in db_key: self.__db.set(key, str(update_value), ttl) i += 1 except (Exception, ReaPy.redis_error()) as error: self.__error = error self.__count = i return self
def delete(self, db, condition): self.__count = 0 self.__error = False self.__result = [] self.connect(db) self.__db.execute_command('SELECT ' + str(db)) i = 0 try: if condition is not None: condition_params = ReaPy.presenter().redis_where_condition( condition) for c_key in condition_params: db_key = self.find_key(c_key) if db_key is not None: for key in db_key: self.__db.delete(key) i += 1 else: for key in self.__db.scan_iter(): self.__db.delete(key) i += 1 except (Exception, ReaPy.redis_error()) as error: self.__error = error self.__count = i return self
def select(self, db, field=None, condition=None, sort=None, top=None, is_first=False): self.__pep8 = {str(field), sort, top, is_first} self.__count = 0 self.__error = False self.__result = [] self.connect(db) self.__db.execute_command('SELECT ' + str(db)) i = 0 try: if condition is not None: condition_params = ReaPy.presenter().redis_where_condition( condition) for c_key in condition_params: db_key = self.find_key(c_key) if db_key is not None: for key in db_key: result = self.__db.get(key) ttl = self.__db.ttl(key) self.__result.append((key, result.decode('utf-8'))) self.__result.append(ttl) i += 1 else: for key in self.__db.scan_iter(): result = self.__db.get(key) ttl = self.__db.ttl(key) self.__result.append( (key.decode('utf-8'), result.decode('utf-8'))) self.__result.append(ttl) i += 1 except (Exception, ReaPy.redis_error()) as error: print(error) self.__error = error self.__count = i return self