def fetch_value_query(vo_class_instance, ar_data): """ Mehtod for fetch data in vo :param vo_class_instance: class for vo :param ar_data: to fetch :return: ar data for fetches values """ ar_response = [] if ar_data is not None: for data in ar_data: vo_instance = vo_class_instance() for key in data: old_instance = vo_instance ar_key_split = RFUtilsStr.split(key, FIELD_TABLE_SEPARATOR) if RFUtilsArray.is_not_empty(ar_key_split): for field in ar_key_split: # Not use default alias if field == DEFAULT_ALIAS: continue if RFUtilsBuilt.has_attr(old_instance, field): old_tpm_instance = RFUtilsBuilt.get_attr( old_instance, field) if old_tpm_instance is None: rf_column = RFContext.get_column_table( old_instance.__class__.__name__, field) if rf_column is not None and RFUtilsStr.is_not_emtpy( rf_column.join_vo_class_name ) is True: old_tpm_instance = RFContext.instance_vo( vo_class_name=rf_column. join_vo_class_name) RFUtilsBuilt.set_attr( old_instance, field, old_tpm_instance) old_instance = old_tpm_instance else: RFUtilsBuilt.set_attr( old_instance, field, data[key]) else: old_instance = old_tpm_instance else: break ar_response.append(vo_instance) return ar_response
def __get_pk_filters__(self, ar_pks_values): """ Method for get pk filters :param ar_pks_values: to set in filters :return: array pk filters """ ar_filters = [] for index, pk in enumerate(self.vo_class.__ar_pk_fields__): rf_column = RFContext.get_column_table(self.vo_class.__name__, pk) if RFUtilsStr.is_not_emtpy(rf_column.join_table): ar_filters.append( Filter(field=pk + DOT + rf_column.column_name, value=ar_pks_values[index])) else: ar_filters.append(Filter(field=pk, value=ar_pks_values[index])) return ar_filters
def json_data_to_vo(self, vo_class_name, data): """ Mehtod for fetch data in vo :param vo_class_name: class name for vo :param ar_data: to fetch :return: ar data for fetches values """ vo_instance = RFContext.instance_vo(vo_class_name) if data is not None: for key, data_vo in data.items(): rf_column = RFContext.get_column_table(vo_class_name, key) if data_vo is not None and rf_column is not None: if isinstance(data_vo, Mapping): RFUtilsBuilt.set_attr(vo_instance, key, self.json_data_to_vo(rf_column.join_vo_class_name, data_vo)) elif isinstance(data_vo, float): RFUtilsBuilt.set_attr(vo_instance, key, Decimal(data_vo)) else: RFUtilsBuilt.set_attr(vo_instance, key, data_vo) return vo_instance
def build_update_query(vo_instance=None, dic_params_query={}): """ Method for build update query :param vo_instance: to update :param dic_params_query: to set update query :return:to get build query """ query_builder = "UPDATE " + vo_instance.__table_name__ + " " dic_rf_columns = RFContext.get_columns_table( vo_instance.__class__.__name__) first: bool = True for key, rf_column in dic_rf_columns.items(): if rf_column.updatable: column_value = None if first is not True: query_builder = query_builder + " , " else: query_builder = query_builder + " SET " column_name = rf_column.column_name if RFUtilsStr.is_not_emtpy( rf_column.join_table) else rf_column.name if RFUtilsStr.is_not_emtpy(rf_column.join_table): join_instance = RFUtilsBuilt.get_attr( vo_instance, rf_column.name) if join_instance is not None: column_value = RFUtilsBuilt.get_attr( join_instance, rf_column.join_table_column) else: column_value = RFUtilsBuilt.get_attr( vo_instance, column_name) query_builder = query_builder + " " + column_name + " = " dic_params_query[column_name] = column_value query_builder = query_builder + " %(" + column_name + ")s " first = False query_builder = query_builder + " WHERE " first: bool = True for pk in vo_instance.__ar_pk_fields__: if first is not True: query_builder = query_builder + " AND " column_value = None rf_column = RFContext.get_column_table( vo_instance.__class__.__name__, pk) column_name = rf_column.column_name if RFUtilsStr.is_not_emtpy( rf_column.join_table) else rf_column.name if RFUtilsStr.is_not_emtpy(rf_column.join_table): join_instance = RFUtilsBuilt.get_attr(vo_instance, rf_column.name) if join_instance is not None: column_value = RFUtilsBuilt.get_attr( join_instance, rf_column.join_table_column) else: column_value = RFUtilsBuilt.get_attr(vo_instance, column_name) query_builder = query_builder + " " + column_name + " = " dic_params_query["keyPK_" + pk] = column_value query_builder = query_builder + " %(" + "keyPK_" + pk + ")s " first = False return query_builder
def build_select_query( ar_fields_query=None, ar_joins_query=None, db_engine_type: EnumDbEngineType = EnumDbEngineType.RF_MYSQL_POOL, ar_default_fields_table=None, vo_class_name=None): """ Method for build select query :param ar_fields_query: to get in query :param ar_joins_query: joins for query. Is necessary if has join is fetch :param db_engine_type for database :param ar_default_fields_table for table :param vo_class_name for query :return: select query """ query_builder = "" # Engine EnumDbEngineType.RF_MYSQL if db_engine_type == EnumDbEngineType.RF_MYSQL or db_engine_type == EnumDbEngineType.RF_MYSQL_POOL: query_builder = " SELECT " # Load selected fields if RFUtilsArray.is_not_empty(ar_fields_query): first = True for field in ar_fields_query: if not first: query_builder = query_builder + " , " if RFUtilsStr.is_not_emtpy(field.alias_table): query_builder = query_builder + " " + field.alias_table.strip( ) + "." else: query_builder = query_builder + " " + DEFAULT_ALIAS + "." ar_field = RFUtilsStr.split(field.name, DOT) field_name = field.name field_name_alias = field.name rf_column = None old_vo_class_name = vo_class_name for field_ar in ar_field: rf_column = RFContext.get_column_table( vo_class_name=old_vo_class_name, column_name=field_ar) if rf_column is not None and RFUtilsStr.is_not_emtpy( rf_column.join_table): old_vo_class_name = rf_column.join_vo_class_name field_name = rf_column.column_name field_name_alias = field_name_alias + DOT + rf_column.join_table_column else: break query_builder = query_builder + field_name if RFUtilsStr.is_not_emtpy(field.alias_field): query_builder = query_builder + " " + field.alias_field.strip( ) + " " else: query_builder = query_builder + " " + \ RFUtilsStr.replace(field_name_alias, DOT, FIELD_TABLE_SEPARATOR) + " " first = False # Load all fields elif RFUtilsArray.is_not_empty(ar_default_fields_table): first = True for field in ar_default_fields_table: if not first: query_builder = query_builder + " , " rf_column = RFContext.get_column_table( vo_class_name=vo_class_name, column_name=field.name) if RFUtilsStr.is_empty(rf_column.join_table): query_builder = query_builder + " " + DEFAULT_ALIAS + "." + field.name.strip() + " " + \ RFUtilsStr.replace(field.name.strip(), DOT, FIELD_TABLE_SEPARATOR) + " " else: query_builder = query_builder + " " + DEFAULT_ALIAS + "." + rf_column.join_table_column + " " + \ RFUtilsStr.replace(field.name.strip(), DOT, FIELD_TABLE_SEPARATOR) + rf_column.join_table_column + " " first = False # Check joins for get data if RFUtilsArray.is_not_empty(ar_joins_query): # For each join add if join fetch ar_joins_fetch = [ EnumJoinType.INNER_JOIN_FETCH, EnumJoinType.LEFT_JOIN_FETCH, EnumJoinType.RIGHT_JOIN_FETCH ] for join in ar_joins_query: # Add join if fetch for join table. Only join for non alias table by the moment if join.join_type in ar_joins_fetch: old_vo_class_name = vo_class_name ar_join_fields = RFUtilsStr.split(join.field, DOT) for join_field in ar_join_fields: rf_column = RFContext.get_column_table( vo_class_name=old_vo_class_name, column_name=join_field) if rf_column is not None and RFUtilsStr.is_not_emtpy( rf_column.join_table) is True: old_vo_class_name = rf_column.join_vo_class_name ar_rf_columns = RFContext.get_columns_table( old_vo_class_name) field_alias = RFUtilsStr.replace( join.field, DOT, FIELD_TABLE_SEPARATOR) alias = DEFAULT_ALIAS + JOIN_ASSOCIATION_SEPARATOR + RFUtilsStr.replace( join.field, DOT, JOIN_ASSOCIATION_SEPARATOR) for key_rf_column_apply in ar_rf_columns: rf_column_apply = ar_rf_columns[ key_rf_column_apply] query_builder = query_builder + " , " column_name = rf_column_apply.name if RFUtilsStr.is_empty( rf_column_apply.join_table ) else rf_column_apply.column_name query_builder = query_builder + " " + alias + "." + column_name + " " + \ field_alias + FIELD_TABLE_SEPARATOR + column_name query_builder = query_builder + " " return query_builder
def build_joins_query( db_engine_type: EnumDbEngineType = EnumDbEngineType.RF_MYSQL_POOL, ar_joins_query=None, vo_class_name=None): """ Method for build join query :param db_engine_type: for build joins :param ar_joins_query: to build :param vo_class_name for get columns and field :return: build whit joins """ query_builder = "" if RFUtilsArray.is_not_empty(ar_joins_query): for join in ar_joins_query: if db_engine_type == EnumDbEngineType.RF_MYSQL or db_engine_type == EnumDbEngineType.RF_MYSQL_POOL: if RFUtilsStr.is_not_emtpy(join.custom_query_join): query_builder = query_builder + " " + join.custom_query_join else: join_type = " INNER JOIN " if join.join_type == EnumJoinType.RIGHT_JOIN or \ join.join_type == EnumJoinType.RIGHT_JOIN_FETCH: join_type = " INNER JOIN " elif join.join_type == EnumJoinType.LEFT_JOIN or \ join.join_type == EnumJoinType.LEFT_JOIN_FETCH: join_type = " LEFT JOIN " elif join.join_type == EnumJoinType.RIGHT_JOIN or \ join.join_type == EnumJoinType.RIGHT_JOIN_FETCH: join_type = " RIGHT JOIN " join_table = None join_table_field = None ar_join_split_values = RFUtilsStr.split( join.field, DOT) column_name = None alias_join = RFUtilsStr.replace( join.field, DOT, JOIN_ASSOCIATION_SEPARATOR) vo_class_name_join = vo_class_name len_split_values = len(ar_join_split_values) - 1 origin_alias_table = DEFAULT_ALIAS for index, join_split_value in enumerate( ar_join_split_values): rf_column = RFContext.get_column_table( vo_class_name=vo_class_name_join, column_name=join_split_value) vo_class_name_join = rf_column.join_vo_class_name join_table = rf_column.join_table join_table_field = rf_column.join_table_column column_name = rf_column.column_name if index != len_split_values: origin_alias_table = origin_alias_table + join_split_value query_builder = query_builder + " " + join_type + " " + join_table if RFUtilsStr.is_not_emtpy(join.alias): query_builder = query_builder + " AS " + join.alias query_builder = query_builder + " ON " + join.alias + "." + join_table_field \ + " = " + origin_alias_table + "." + column_name else: query_builder = query_builder + " AS " + DEFAULT_ALIAS + JOIN_ASSOCIATION_SEPARATOR + \ alias_join + " " query_builder = query_builder + " ON " + DEFAULT_ALIAS + JOIN_ASSOCIATION_SEPARATOR + \ alias_join + "." + join_table_field \ + " = " + origin_alias_table + "." + column_name return query_builder