skydb/sky — pull request #121 by benbjohnson (2013-08-23 22:23:08+00:00)
Title: Variables

Added support for variable declaration within queries and variable assignment.

Variables are declared using the `DECLARE` statement:

```
DECLARE myVar AS INTEGER
```

And assigned with the `SET` statement:

```
SET myVar = 20
```

And then they can be used like normal properties:

```
SELECT count() AS count GROUP BY myVar
```

Only `float`, `integer` and `boolean` types are currently supported. `factor` types are a little trickier so they're not in there yet. Strings are a big pain and they're really slow so I probably won't support those.

You can do cool things like create finite state machines based on event data. Lets say you have states represented by integers:

```
UNREGISTERED 0
TRIALING 1
PAYING 2
CANCELLED 3
```

Then you can transition between states based on previous state and some event data:

```
DECLARE state AS INTEGER

WHEN state == 0 && action == "Signup" THEN
SET state = 1
END
WHEN state == 1 && action == "Payment" THEN
SET state = 2
END
WHEN state == 2 && action == "Cancel Account" THEN
SET state = 3
END

SELECT count() AS count GROUP BY state, action
```

That query will show the count of each action performed by users at each state within their lifecycle.

/cc: @orenmazor @boourns @snormore