From 60a05bb7458df63c05b61b5ab023ecc8a025db2a Mon Sep 17 00:00:00 2001 From: David Greenberg Date: Mon, 11 May 2015 23:24:16 -0400 Subject: [PATCH] Add missing queries --- resources/sql/queries.sql | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 resources/sql/queries.sql diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql new file mode 100644 index 0000000..d319834 --- /dev/null +++ b/resources/sql/queries.sql @@ -0,0 +1,50 @@ +-- name: lookup-node-name->id* +-- Given a node name, finds its id +SELECT rowid FROM node_names WHERE name = :nodename + +-- name: lookup-node-id->name* +-- Given a node id, finds its name +SELECT name FROM node_names WHERE rowid = :nodeid + +-- name: get-all-states* +-- Returns all the state data, joined with node names +SELECT * +FROM primary_state, node_names +WHERE node_names.rowid = primary_state.node +ORDER BY primary_state.step + +-- name: get-all-events* +-- Returns all the event data, joined with state data & names +-- TODO make this be a few queries driven by the client +SELECT * FROM primary_events + +-- name: latest-state-id-for-node* +-- Finds the rowid of the most recent state for a node as of a given time +SELECT primary_state.rowid +FROM primary_state, node_names +WHERE time = (SELECT max(time) + FROM primary_state, node_names + WHERE primary_state.node = node_names.rowid + AND node_names.name = :nodename + AND primary_state.time <= :time) +AND primary_state.node = node_names.rowid +AND node_names.name = :nodename + +-- name: get-state-by-state-id +-- returns the state for the given state id +SELECT * +FROM primary_state +WHERE primary_state.rowid = :id + +-- name: add-reciever-to-event! +-- Updates the given event with the target reciever +UPDATE primary_events +SET reciever_state = :reciever_state_id +WHERE rowid = :id + +-- name: get-logs +-- Retrieves all the logs by node & time +SELECT msg +FROM user_logs +WHERE user_logs.time = :time +AND user_logs.node = :node