Example #1
0
    def run(self):
        """
        Extracts table objects from the scope, then creates new representation
        objects which are placed into the DWRepresentation
        :return: A DWRepresentation object for the given scope
        """

        # Gets all table objects in the scope
        pygrametl = self.scope['pygrametl']
        tables = pygrametl._alltables

        # Creates representation objects
        for table in tables:

            # If the table is a dimension.
            if self.check_table_type(table, DIM_CLASSES):
                if isinstance(table, TypeOneSlowlyChangingDimension):
                    dim = SCDType1DimRepresentation(table, self.dw_conn)
                elif isinstance(table, SlowlyChangingDimension):
                    dim = SCDType2DimRepresentation(table, self.dw_conn)
                else:
                    dim = DimRepresentation(table, self.dw_conn)
                self.dim_reps.append(dim)

            # If the table is a fact table
            elif self.check_table_type(table, FT_CLASSES):
                    ft = FTRepresentation(table, self.dw_conn)
                    self.fts_reps.append(ft)

        # From the scope, gets all SnowflakedDimensions.
        # These are used to re-create the referencing structure of the DW,
        # when instantiating DWRepresentation.
        snowflakes = []
        for x, value in self.scope.items():
            if isinstance(value, SnowflakedDimension):
                snowflakes.append(value)

        dw_rep = DWRepresentation(self.dim_reps, self.dw_conn, self.fts_reps,
                                  snowflakes)

        # Clears the list of tables as its contents may otherwise be retained,
        # when a new Case is executed. This is because the list is mutable.
        pygrametl._alltables.clear()

        return dw_rep
Example #2
0
special_snowflake = SnowflakedDimension(references=[(dim1,
                                                     [dim2, dim3]), (dim2,
                                                                     dim4)])

for row in data:
    special_snowflake.insert(row)

conn.commit()

dim1_rep = DimRepresentation(dim1, conn)
dim2_rep = DimRepresentation(dim2, conn)
dim3_rep = DimRepresentation(dim3, conn)
dim4_rep = DimRepresentation(dim4, conn)

snow_dw_rep = DWRepresentation([dim1_rep, dim2_rep, dim3_rep, dim4_rep],
                               conn,
                               snowflakeddims=(special_snowflake, ))

snow_dw_rep.connection.cursor().execute("DELETE FROM dim2 WHERE key2 > 3")

# quick overview of the keys we have left
for dim in snow_dw_rep.dims:
    allatts = dim.all.copy()
    lookupatts = dim.lookupatts.copy()
    for attr in lookupatts:
        allatts.remove(attr)

    for row in dim.itercolumns(allatts):
        print(dim.name, row)

ref_tester = ReferentialIntegrityPredicate(refs={
Example #3
0
}, {
    'attr1': 25
}, {
    'attr1': None
}, {
    'attr1': 5
}, {
    'attr1': None
}, {
    'attr1': None
}]

wrapper = pygrametl.ConnectionWrapper(connection=null_conn)

dim1 = Dimension(
    name='dim1',
    key='key1',
    attributes=['attr1'],
)

for row in data:
    dim1.insert(row)

dim_rep = DimRepresentation(dim1, null_conn)
notnull_tester = ColumnNotNullPredicate('dim1')
null_rep = DWRepresentation([dim_rep], null_conn)

print(notnull_tester.run(null_rep))

null_conn.close()
Example #4
0
    AGE            INT    NOT NULL,
    ADDRESS        CHAR(50),
    VERSION         INT);''')

company_info = [('Anders', 43, 'Denmark', 1.0), ('Charles', 50, 'Texas', 1),
                ('Wolf', 28, 'Sweden', 1), ('Hannibal', 45, 'America', 1),
                ('Anders', 43, 'Denmark', 2.0)]

# ... and inserting the necessary data.
c.executemany(
    "INSERT INTO COMPANY (NAME,AGE,ADDRESS,VERSION) VALUES (?,?,?,?)",
    company_info)
conn.commit()

ConnectionWrapper(conn)

s = SlowlyChangingDimension('COMPANY', 'ID',
                            ['NAME', 'AGE', 'ADDRESS', 'VERSION'],
                            ['NAME', 'AGE'], 'VERSION')

dim = SCDType2DimRepresentation(s, conn)

dw = DWRepresentation([dim], conn)

a = SCDVersionPredicate('COMPANY', {'NAME': 'Anders', 'AGE': 43}, 2)
b = SCDVersionPredicate('COMPANY', {'NAME': 'Anders', 'AGE': 43}, 3)
print(a.run(dw))
print(b.run(dw))

conn.close()
Example #5
0
c.executemany("INSERT INTO FACTTABLE (BOOK,ISSUE) VALUES (?,?)", company_info)

conn.commit()

ConnectionWrapper(conn)

dim = Dimension('COMPANY', 'ID', ['NAME', 'AGE', 'ADDRESS', 'SALARY'], ['NAME'])
dim_rep = DimRepresentation(dim, conn)

dim2 = Dimension('LASTNAME', 'NAME', ['LAST'])
dim_rep2 = DimRepresentation(dim2, conn)

ft = FactTable('FACTTABLE', ['Book'], ['Issue'])
ft_rep = FTRepresentation(ft, conn)

dw = DWRepresentation([dim_rep, ft_rep, dim_rep2], conn)

expected_list1 = [
    {'NAME': 'Anders', 'AGE': 43, 'SALARY': 21000.0, 'ADDRESS': 'Denmark',
     'ID': 1},
    {'NAME': 'Charles', 'AGE': 50, 'SALARY': 25000.0, 'ADDRESS': 'Texas',
     'ID': 2},
    {'NAME': 'Wolf', 'AGE': 28, 'SALARY': 19000.0, 'ADDRESS': 'Sweden',
     'ID': 3},
    {'NAME': 'Hannibal', 'AGE': 45, 'SALARY': 65000.0, 'ADDRESS': 'America',
     'ID': 4},
    {'NAME': 'Buggy', 'AGE': 67, 'SALARY': 2000.0, 'ADDRESS': 'America',
     'ID': 5}
]

expected_list2 = expected_list1.copy()
Example #6
0
    SALARY         REAL);''')

company_info = [('Anders', 43, 'Denmark', 21000.00),
                ('Charles', 50, 'Texas', 25000.00),
                ('Wolf', 28, 'Sweden', 19000.00),
                ('Hannibal', 45, 'America', 65000.00),
                ('Hannibal', 45, 'America', 65000.00),
                ('Buggy', 67, 'America', 2000), ('Buggy', 67, 'America', 2000),
                ('Buggy', 67, 'America', 2000), ('Buggy', 67, 'America', 2000),
                ('Buggy', 67, 'America', 2000), ('Buggy', 67, 'America', 2000)]

# ... and inserting the necessary data.
c.executemany("INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?)",
              company_info)
conn.commit()
columns = ['NAME', 'AGE', 'ADDRESS', 'SALARY']

wrapper = ConnectionWrapper(conn)
dim = Dimension(name='COMPANY',
                key='ID',
                attributes=['NAME', 'AGE', 'ADDRESS', 'SALARY'],
                lookupatts=['NAME'])

dim_rep = DimRepresentation(dim, conn)
dw = DWRepresentation([dim_rep], conn)

dup_predicate = DuplicatePredicate('company', ['ID'], True)
print(dup_predicate.run(dw))

conn.close()