def values_from_db(warehouse_id, field): """Get allowed field values from the dimension_values table. If warehouse_id is `None` the warehouse_id is defaulted to the value of `zillion.field.FIELD_VALUE_DEFAULT_WAREHOUSE_ID`. This allows pulling dimension values even when a `Warehouse` has not been saved. **Parameters:** * **warehouse_id** - (*int*) A zillion warehouse ID * **field** - (*Field*) A zillion Dimension object **Returns:** (*list or None*) - A list of valid values or None if no row is found for this dimension. """ if warehouse_id is None: warehouse_id = FIELD_VALUE_DEFAULT_WAREHOUSE_ID s = sa.select(DimensionValues.c).where( sa.and_( DimensionValues.c.warehouse_id == warehouse_id, DimensionValues.c.name == field.name, )) conn = zillion_engine.connect() try: result = conn.execute(s) row = result.fetchone() if not row: return None return json.loads(row["values"]) finally: conn.close()
def delete(cls, id): """Delete a saved warehouse. Note that this does not delete any report specs that reference this warehouse ID. **Parameters:** * **id** - (*int*) The ID of a Warehouse to delete """ s = Warehouses.delete().where(Warehouses.c.id == id) conn = zillion_engine.connect() try: conn.execute(s) finally: conn.close()
def update_report(warehouse, report_id, meta=None, **kwargs): # Workaround until wh.update_report() is added report = Report(warehouse, **kwargs) conn = zillion_engine.connect() try: conn.execute( ReportSpecs.update().where(ReportSpecs.columns.id == report_id), warehouse_id=warehouse.id, params=report.get_json(), meta=json.dumps(meta), ) finally: conn.close() report.spec_id = report_id report.meta = meta return report
def _load_warehouse(cls, id): """Get a Warehouse row from a Warehouse ID **Parameters:** * **id** - (*int*) The ID of the Warehouse to load **Returns:** (*dict*) - A Warehouse row """ s = sa.select(Warehouses.c).where(Warehouses.c.id == id) conn = zillion_engine.connect() try: result = conn.execute(s) row = result.fetchone() return row finally: conn.close()
def save(self, name, config_url, meta=None): """Save the warehouse config and return the ID **Parameters:** * **name** - (*str*) A name to give the Warehouse * **config_url** - (*str*) A URL pointing to a config file that can be used to recreate the warehouse * **meta** - (*object, optional*) A metadata object to be serialized as JSON and stored with the warehouse **Returns:** (*int*) - The ID of the saved Warehouse """ raiseifnot(name, "A unique name must be specified to save a Warehouse") raiseifnot( # TODO: better check for valid URL config_url and isinstance(config_url, str), "A config URL must be specified to save a Warehouse", ) params = dict(ds_priority=self.ds_priority, config=config_url) conn = zillion_engine.connect() try: result = conn.execute( Warehouses.insert(), name=name, params=json.dumps(params), meta=json.dumps(meta), ) wh_id = result.inserted_primary_key[0] raiseifnot(wh_id, "No warehouse ID found") finally: conn.close() self.id = wh_id self.meta = meta self.name = name return wh_id