def __init__( self, vo_class=None, db_engine_type: EnumDbEngineType = EnumDbEngineType.RF_MYSQL_POOL): """ Constructor for dao :param vo_class is a class vo data :param db_engine_type: default value is EnumDbEngineType.RF_MYSQL """ self.vo_class = vo_class self.db_engine_type = db_engine_type self._table_name = self.vo_class.__table_name__ self._columns_table = None self._fields_table = [] self._vo_class_name = self.vo_class.__name__ # Load fields for db engine if RFUtilsStr.is_not_emtpy( self._vo_class_name) and self.db_engine_type is not None: self._columns_table = RFContext.get_columns_table( self._vo_class_name) if self._columns_table is not None: for key_column in self._columns_table: column = self._columns_table[key_column] self._fields_table.append(Field(column.name))
def build_insert_query(vo_instance=None, dic_params_query={}): """ Method for insert build query :param table_name: for build insert query :param vo_instance: for build insert query :param dic_params_query: for build params :return: query for insert """ query_builder = "INSERT INTO " + vo_instance.__table_name__ + " " query_builder_columns = "" query_builder_values = "" 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.insertable: column_value = None if first is not True: query_builder_columns = query_builder_columns + " , " query_builder_values = query_builder_values + " , " column_name = rf_column.column_name if RFUtilsStr.is_not_emtpy( rf_column.join_table) else rf_column.name query_builder_columns = query_builder_columns + 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) dic_params_query[column_name] = column_value query_builder_values = query_builder_values + " %(" + column_name + ")s " first = False # Build columns query_builder = query_builder + " ( " query_builder = query_builder + query_builder_columns query_builder = query_builder + " ) " # Build values query_builder = query_builder + " VALUES ( " query_builder = query_builder + query_builder_values query_builder = query_builder + " ) " return query_builder
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