Skip to content

Instantly share code, notes, and snippets.

@crispincornett
Last active August 29, 2015 14:10
Show Gist options
  • Save crispincornett/4d258a63a65982b41d09 to your computer and use it in GitHub Desktop.
Save crispincornett/4d258a63a65982b41d09 to your computer and use it in GitHub Desktop.
# Gather your inputs, cast as string and strip whitespace
name = str(event.source.parent.getComponent('nameFilter').text).strip()
farm = str(event.source.parent.getComponent('farmFilter').text).strip()
item = str(event.source.parent.getComponent('itemFilter').text).strip()
cluster = str(event.source.parent.getComponent('clusterFilter').text).strip()
start_date = event.source.parent.getComponent('startDate').text
end_date = event.source.parent.getComponent('endDate').text
# Empty array to contain conditions
conditions = []
# If input isn't an empty string, add the SQL fragment to the conditions array
if len(name) > 0:
# 'ILIKE' is Postgres specific version of a case insensitive SQL 'LIKE'
conditions.append("cl.name ILIKE '%" + name + "%'")
if len(farm) > 0:
conditions.append("names.farm ILIKE '%" + farm + "%'")
if len(item) > 0:
conditions.append("cl.item_name ILIKE '%" + item + "%'")
if len(cluster) > 0:
conditions.append("names.cluster ILIKE '%" + cluster + "%'")
# Append date to query
conditions.append("DATE(cl.time_stamp) BETWEEN '%s' AND '%s'" % (start_date, end_date))
# Joins conditions w/ "AND" into a single string
cond_string = " AND ".join(conditions)
# Add the "WHERE" clause
query = "WHERE %s" % cond_string
# Populate the components custom 'query' property
event.source.parent.getComponent('Table').query = query
## Then your component data binding to SQL Query would like something like:
# SELECT
# cl.time_stamp,
# cl.name,
# names.farm,
# names.cluster,
# cl.item_name,
# cl.value
# FROM
# change_log cl
# JOIN
# name_lookup names ON names.name = cl.name
# {Root Container.Table.query}
# ORDER BY
# cl.time_stamp DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment