selects the src_cols from src_table and groups by dim_grain
        then inserts into newly created table dim_name the columns as 'dim_cols

def extract_dimension(self, dim_name, dim_cols, dim_key, dim_stag_table, src_table, src_cols, grain_cols, where_clause):
        """
        selects the src_cols from src_table and groups by dim_grain
        then inserts into newly created table dim_name the columns as 'dim_cols
        """
        
        self.ddl_text += '---------------------------------------------\n'
        self.ddl_text += '-- CREATE Dimension - ' + dim_name + '\n'
        self.ddl_text += '---------------------------------------------\n'
        self.ddl_text += 'DROP TABLE ' + dim_stag_table + ' CASCADE CONSTRAINTS;\n'
        self.ddl_text += 'CREATE TABLE ' + dim_stag_table + ' (\n'
        self.ddl_text += ' '.join([col + ' VARCHAR2(200), \n' for col in dim_cols])
        self.ddl_text += ' ' + self.date_updated_col + '  DATE \n' # + src_table + '; \n'
        self.ddl_text += ');\n'
        
        self.ddl_text += 'DROP TABLE ' + dim_name + ' CASCADE CONSTRAINTS;\n'
        self.ddl_text += 'CREATE TABLE ' + dim_name + ' (\n'
        self.ddl_text += ' ' + dim_key + ' NUMBER, \n'
        self.ddl_text += ' '.join([col + ' VARCHAR2(200), \n' for col in dim_cols])
        self.ddl_text += ' REC_SOURCE_SYSTEM  VARCHAR2(100), \n' # + src_table + '; \n'
        self.ddl_text += ' ' + self.date_updated_col + '  DATE \n' # + src_table + '; \n'
        self.ddl_text += ');\n'
        self.ddl_text += 'CREATE OR REPLACE VIEW U' + dim_name[1:] + ' AS SELECT * FROM ' + dim_name + ';\n'
        self.ddl_text += 'GRANT SELECT ON U' + dim_name[1:] + ' TO ALL_USERS;\n'
        self.ddl_text += '\n'
        self.ddl_text += 'DROP SEQUENCE SEQ_' + dim_name + ';\n'
        self.ddl_text += 'CREATE SEQUENCE SEQ_' + dim_name + ';\n\n'
        
        
        self.sql_text += '---------------------------------------------\n'
        self.sql_text += '-- Populate Dimension - ' + dim_name + '\n'
        self.sql_text += '---------------------------------------------\n'
        self.sql_text += "DELETE FROM " + dim_stag_table + ";\n"
        self.sql_text += "COMMIT;\n"
        self.sql_text += "INSERT INTO " + dim_stag_table + " (\n"
        self.sql_text += ", ".join([col for col in dim_cols])
        self.sql_text += ")\n (SELECT \n"
        self.sql_text += ", ".join([col for col in src_cols])
        self.sql_text += "\nFROM " + src_table + "\n"
        if where_clause != '':
            self.sql_text += "WHERE " + where_clause + "\n"
        if len(grain_cols) > 0:
            self.sql_text += "GROUP BY " + ", ".join([col for col in grain_cols]) + "\n"
        self.sql_text += "); \n"
        self.sql_text += "COMMIT;\n"
        
        
        self.sql_text += "DELETE FROM " + dim_name + ";\n"
        self.sql_text += "COMMIT;\n"
        self.sql_text += "INSERT INTO " + dim_name + " (\n"
        self.sql_text += ", ".join([col for col in dim_cols])
        self.sql_text += ", REC_SOURCE_SYSTEM, " + self.date_updated_col + " "
        
        self.sql_text += ") \n(SELECT \n"
        self.sql_text += ", ".join([col for col in src_cols])
        self.sql_text += ", '" + src_table + "', sysdate "
        self.sql_text += "\nFROM " + dim_stag_table + "\n"
        self.sql_text += "); \n"
        self.sql_text += "COMMIT;\n"
        
        self.sql_text += "UPDATE " + dim_name + " SET " + dim_key + " = SEQ_" + dim_name + ".nextval;\n"
        self.sql_text += "COMMIT;\n\n"
        
        
        print(self.ddl_text)
        print(self.sql_text)