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 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 insert(self, table, condition): try: value_param = [] value_params = [] condition_params = ReaPy.presenter().sql_condition_presenter( condition, ReaPy.inspect().stack()[0][3]) key_len = int(len(condition_params['insert_key'])) value_len = int(len(condition_params['insert_value'])) condition_len = int(value_len / key_len) for _ in range(condition_len): for _ in range(0, key_len): value_param.append('%s') value_params.append('(' + ','.join(value_param) + ')') value_param = [] insert_keys = ','.join(condition_params['insert_key']) insert_clause = 'INSERT INTO ' + table + ' (' + insert_keys + ') VALUES ' + ','.join( value_params) self.query({ 'query': insert_clause, 'params': condition_params['insert_value'] }) return self except Exception as e: print(e)
def set_cache(cache_hash, data): data = ReaPy.json().dumps(data, default=ReaPy.presenter().datetime_handler) #TODO: bazi datalarda json load edemeyip patladigi icin try cachede replace kaldirmak sorunu cozdu try: epsg_json = ReaPy.json().loads(data.replace("\'", '"')) except Exception as e: epsg_json = ReaPy.json().loads(data) sys.stdout.flush() insert_cache = Model('ReaRedis').insert(11, { 0: {'Local_Model_Cache:' + cache_hash: {'data': epsg_json, }}}).data() sys.stdout.flush() return insert_cache
def delete(self, table, condition): if condition is None: conditions = '' conditions_value = [] else: condition_params = ReaPy.presenter().sql_condition_presenter( condition, ReaPy.inspect().stack()[0][3]) conditions = condition_params['condition'] conditions_value = condition_params['values'] delete_clause = 'DELETE FROM ' + table + ' ' + conditions self.query({'query': delete_clause, 'params': conditions_value}) return self
def select(self, table, field=None, condition=None, sort=None, top=None, offset=None): try: sort_param = '' top_param = '' offset_param = '' conditions = '' conditions_value = [] if 'pg_function.' in table: action_param = 'SELECT ' + table.replace('pg_function.', '') else: action_param = 'SELECT * FROM ' + table if field is not None: action_param = 'SELECT ' + str(field) + ' FROM ' + table if field == 'COUNT(*)': action_param = 'SELECT COUNT(*) FROM ' + table if sort is not None: if sort.startswith('group by'): sort_param = sort else: sort_param = 'order by ' + sort if top is not None: top_param = ' limit ' + str(top) if offset is not None: offset_param = ' offset ' + str(offset) if condition is not None and condition != '': condition_params = ReaPy.presenter().sql_condition_presenter( condition, ReaPy.inspect().stack()[0][3]) conditions = condition_params['condition'] conditions_value = condition_params['values'] select_clause = action_param + ' ' + conditions + sort_param + top_param + offset_param if self._count_data: select_clause_count = "SELECT COUNT(*) as count FROM " + table + ' ' + conditions self.query_count( { 'query': select_clause_count, 'params': conditions_value }, True) self.query({ 'query': select_clause, 'params': conditions_value }, True) return self except Exception as e: print(e)
def update(self, table, condition): condition_params = ReaPy.presenter().sql_condition_presenter( condition, ReaPy.inspect().stack()[0][3]) if 'CONDITION' not in condition and not condition['CONDITION']: conditions = '' condition_values = [] else: conditions = ' ' + condition_params['condition'] condition_values = condition_params['condition_value'] values = condition_params['set_value'] + condition_values update_clause = 'UPDATE ' + table + ' SET ' + ', '.join( condition_params['set_key']) + conditions self.query({'query': update_clause, 'params': values}) 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
def select(self, table, field=None, condition=None, sort=None, top=None): conditions_value = [] sort_param = '' top_param = '' if sort is not None: sort_param = 'order by ' + sort if top is not None: top_param = ' limit ' + str(top) action_param = 'SELECT * FROM ' + table if field is not None: action_param = 'SELECT ' + str(field) + ' FROM ' + table if condition is None: conditions = '' else: condition_params = ReaPy.presenter().sql_condition_presenter( condition, ReaPy.inspect().stack()[0][3]) conditions = condition_params['condition'] conditions_value = condition_params['values'] select_clause = action_param + ' ' + conditions + sort_param + top_param self.query({'query': select_clause, 'params': conditions_value}, True) return self
def insert(self, topic, condition): condition_params = ReaPy.presenter().kafka_insert_condition(condition) self.query(topic, condition_params) return self