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, 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 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, 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 connect(self, payload): self.__count = 0 self.__error = False self.__result = [] self.coll = None request = ReaPy.requests().post(self.__mediation_server, data=ReaPy.json().dumps(payload), headers=self.__header) return request
def __init__(self, source_id=None): if source_id is None: source = ReaPgsql configurations = None else: if source_id in self._local_sources: source = getattr(ReaPy.sys().modules[__name__], source_id) configurations = None else: configurations = Sources.get_source(source_id) source = getattr(ReaPy.sys().modules[__name__], configurations['driver']) self._db = source.get_instance(configurations)
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 connect(db): try: if ReaRedis.__db is None: if ReaRedis.__configurations is None: configurations = ReaPy.configuration().get_configuration( )['system']['tcp_server']['redis'] else: configurations = ReaRedis.__configurations ReaRedis.__db = ReaPy.redis( host=configurations['host'], port=configurations['port'], db=db, password=configurations['redis_auth']) except (Exception, ReaPy.kafka_error()) as error: print(error)
def connect(): try: if ReaKafka.__db is None: if ReaKafka.__configurations is None: configurations = ReaPy.configuration().get_configuration( )['system']['tcp_server']['kafka'] else: configurations = ReaKafka.__configurations ReaKafka.__db = ReaPy.kafka_producer( value_serializer=lambda m: ReaPy.json().dumps(m).encode( 'ascii'), bootstrap_servers=configurations['host'] + ':' + str(configurations['port']), retries=3) except (Exception, ReaPy.kafka_error()) as error: print(error)
def connect(): try: if ReaMysql.__configurations is None: configurations = ReaPy.configuration().get_configuration( )['system']['tcp_server']['my_sql'] else: configurations = ReaMysql.__configurations try: ReaMysql.__db = ReaPy.my_sql().connect( host=configurations['host'], port=configurations['port'], user=configurations['user'], passwd=configurations['password'], db=configurations['database']) except Exception as exc: print(exc) except (Exception, ReaPy.my_sql().DatabaseError) as error: print(error)
def connect(): try: if ReaMssql.__configurations is None: configurations = ReaPy.configuration().get_configuration( )['system']['tcp_server']['ms_sql'] else: configurations = ReaMssql.__configurations try: connection_string = 'Driver={ODBC Driver 17 for SQL Server};Server=' + configurations['host'] + ',' + str(configurations[ 'port']) + ';Database=' + \ configurations['database'] + ';UID=' + configurations['user'] + ';PWD=' + \ configurations['password'] + ';' ReaMssql.__db = ReaPy.ms_sql().connect(connection_string) except Exception as exc: print(exc) except (Exception, ReaPy.ms_sql().DatabaseError) as error: print(error)
def select(self, table, field=None, condition=None, sort=None, top=None, cache=False, offset=None): data = None payload = { 'method': 'select', 'source_id': self.__source_id, 'table': table, 'field': field, 'condition': condition, 'sort': sort, 'top': top, 'offset': offset } if cache is True: hash_payload = ReaPy.hash().md5(ReaPy.json().dumps(payload)) cache = self.get_cache(hash_payload) if cache is not None: cache_data = cache[0][1] data_json = ReaPy.json().loads(cache_data) data = data_json['data'] else: request = self.connect(payload) if request is not None: if request.status_code == 200: data = request.json()['data'] self.set_cache(hash_payload, data) else: request = self.connect(payload) if request is not None: if request.status_code == 200: data = request.json()['data'] if data is not None: self.__count = data['count'] self.__result = data['result'] self.__error = data['error'] return self
def connect(): try: if ReaPgsql.__configurations is None: configurations = ReaPy.configuration().get_configuration( )['system']['tcp_server']['pg_sql'] else: configurations = ReaPgsql.__configurations try: ReaPgsql.__db = ReaPy.pg_sql().connect( user=configurations['user'], password=configurations['password'], host=configurations['host'], port=configurations['port'], database=configurations['database']) ReaPgsql.__db.autocommit = True except Exception as exc: print('pge1', exc) ReaPgsql.__db.close() ReaPgsql.__instance = None except (Exception, ReaPy.pg_sql().DatabaseError) as error: print('pge2', error)
def exec(self, proc, condition): self.__count = 0 self.__error = False self.__result = [] try: cur = self.__db.cursor() cur.callproc(proc, condition) self.__count = cur.rowcount self.__result = cur.fetchall() self.__db.commit() except (Exception, ReaPy.ms_sql().DatabaseError) as error: print(error)
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 query(self, topic, condition): self.__count = 0 self.__error = False self.__result = [] try: i = 0 for c_key, c_value in condition.items(): self.__db.send(topic, c_value) i += 1 self.__count = i self.__db.flush() except (Exception, ReaPy.kafka_error()) as error: self.__error = error
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 query(self, sql, fetch=False): self.__count = 0 self.__error = False self.__result = [] self.coll = None try: cur = self.__db.cursor() cur.execute(sql['query'], sql['params']) self.__count = cur.rowcount if fetch is True: self.coll = [column[0] for column in cur.description] self.__result = cur.fetchall() self.__db.commit() except (Exception, ReaPy.my_sql().DatabaseError) as error: self.__error = error
def get_source(source_id): configurations = {} check_cache = Sources().get_cache(source_id) if check_cache is not None: configurations = ReaPy.json().loads(check_cache.decode('utf-8')) else: db = ReaPgsql.get_instance().select('public.get_source', None, {0: {'col': 'source_id', 'operator': '=', 'value': source_id}} ).first() configurations['driver'] = db[0][1] configurations['host'] = db[0][2] configurations['port'] = db[0][3] configurations['user'] = db[0][4] configurations['password'] = db[0][5] configurations['database'] = db[0][6] Sources().set_cache(source_id, configurations) return configurations
def find_key(self, condition): condition_namespace = condition['key'] condition_value = condition['value'] condition_operator = condition['operator'] keys_list = [] scan_operators = ['=', '>', '<', '>=', '<=', '!='] try: namespace = condition_namespace + ':' if condition_namespace else condition_namespace if isinstance(condition_value, dict): condition_value = ReaPy.json().dumps(condition_value) if condition_operator in scan_operators: self.condition_scan(namespace, condition_value, condition_operator, keys_list) if condition_operator == 'LIKE': key = namespace + str(condition_value) result = self.key_scan(key) if result is not None: keys_list += result except ValueError: pass return keys_list if keys_list else None
def condition_scan(self, namespace, condition_value, condition_operator, keys_lists): ops = { '=': operator.eq, '!=': operator.ne, '<=': operator.le, '>=': operator.ge, '>': operator.gt, '<': operator.lt } key = namespace + '*' if namespace else '*' result = self.key_scan(key) for keys in result: convert_key = keys if key != '*': convert_key = keys.split(':') convert_key = convert_key[len(convert_key) - 1] type_convert = ReaPy.transformer().type_converter( condition_value, convert_key) if type_convert is not None and ops[condition_operator]( type_convert, condition_value) is True: keys_lists.append(keys) return keys_lists if keys_lists else None
def __init__(self): super(Sources).__init__() cache_configurations = ReaPy.configuration().get_configuration()['system']['tcp_server']['redis'] self.cache_db = ReaPy.redis(host=cache_configurations['host'], port=cache_configurations['port'], db=13, password=cache_configurations['redis_auth'])
# # user_token = reader2['data']['payload']['user_token'] # # client.send_obj({"token": token, "method": "check_user_token", # # "data": {"user_token": user_token}}) # # readers = client.read_obj() # # print(readers) # # else: # # print(reader) import datetime from core.utilities.rest import jsonify import datetime from core.reapy import ReaPy try: use_mediation = ReaPy.configuration().get_configuration()['system']['use_mediation'] if use_mediation is True: from core.mediation_model import MediationModel as Model else: from core.model import Model except ImportError: from core.model import Model query = Model(ReaPy.get_reactor_source('yeten')).update('paydas', { 'SET': {'guncelleme_tarihi': str(datetime.datetime.now())}, 'CONDITION': { 0: {'col': 'paydas_id', 'operator': '=', 'value': 'b3f67bc8-ded0-4d2a-b662-08e732e59396' }}}).data() print(query)
def test_rea_py_json_dumps(self): self.assertTrue(ReaPy.json().dumps({"test": "test"}))
def insert(self, topic, condition): condition_params = ReaPy.presenter().kafka_insert_condition(condition) self.query(topic, condition_params) return self
def set_cache(self, cache_hash, data): self.cache_db.set(cache_hash, ReaPy.json().dumps(data))
def select(self, table, field=None, condition=None, sort=None, top=None, is_first=False, cache=False, offset=None): if self._pagination is not None: top = self._pagination[1] offset = self._pagination[0] try: cache_payload = {"table": table, "field": field, "condition": condition, "sort": sort, "top": top, "offset": offset, "is_first": is_first} #database aktif_mi add condition if self._aktif_mi is not None: if condition is None: condition = [] combiner_number = len(condition) if combiner_number>0: condition[combiner_number-1]['combiner'] = 'AND' condition[combiner_number] = {'col': 'aktif_mi', 'operator': '=', 'value': self._aktif_mi} else: condition = {0:{'col': 'aktif_mi', 'operator': '=', 'value': self._aktif_mi}} if cache is True: hash_payload = ReaPy.hash().md5(ReaPy.json().dumps(cache_payload)) cache = self.get_cache(hash_payload) sys.stdout.flush() if cache is not None: try: cache_data = cache[0][1] data_json = ReaPy.json().loads(cache_data) data = data_json['data'] count = data['count'] self._error = data['error'] self._db.coll = None if count: data_len = len(data['result']) if data_len>0: self._data = data['result'] else: self._data = None else: self._data = None try: self._error = data.error() except Exception as e: print(e) return self except Exception as e: print(e) else: data = self._db.select(table, field, condition, sort, top, offset) if data is None: self._data = None return self if data.error()==False: if is_first is True: if data.count(): self._data = data.first() else: self._data = None else: data_len = len(data.result()) sys.stdout.flush() if data_len>0: self._data = data.result() else: self._data = None cache_data = {"result": self.data(), "error": self.error(), "count": data.count()} self.set_cache(hash_payload, cache_data) sys.stdout.flush() else: self._data = None self._error = data.error() return self else: data = self._db.select(table, field, condition, sort, top, offset) if data is None: self._data = None return self if data.error()==False: if is_first is True: if data.count(): self._data = data.first() else: self._data = None else: data_len = len(data.result()) if data_len>0: self._data = data.result() else: self._data = None else: self._data = None self._error = data.error() return self except Exception as e: print(e)