Skip to content

Instantly share code, notes, and snippets.

WITH RECURSIVE genres_materialized_path AS (
SELECT id, name, ARRAY[]::INTEGER[] AS path
FROM genres WHERE parent_id IS NULL
UNION ALL
SELECT genres.id, genres.name, genres_materialized_path.path || genres.parent_id
FROM genres, genres_materialized_path
WHERE genres.parent_id = genres_materialized_path.id
) SELECT * FROM genres_materialized_path WHERE 15 = genres_materialized_path.path[array_upper(genres_materialized_path.path,1)];
id | name | path
----+--------------------+---------
16 | Hard Boiled | {14,15}
17 | Police Procedurals | {14,15}
(2 rows)
CREATE OR REPLACE FUNCTION get_children(genre_id integer)
RETURNS json AS $$
DECLARE
result json;
BEGIN
SELECT array_to_json(array_agg(row_to_json(t))) INTO result -- inject output into result variable
FROM ( -- same CTE as above
WITH RECURSIVE genres_materialized_path AS (
SELECT id, name, ARRAY[]::INTEGER[] AS path
FROM genres WHERE parent_id IS NULL
[
{
"id": 1,
"name": "Arts & Photography",
"children": [
{
"id": 2,
"name": "Architecture",
"children": [
CREATE OR REPLACE FUNCTION test_func(data json) RETURNS json AS $$
return JSON.stringify(data);
$$ LANGUAGE PLV8;
SELECT test_func('{"a": {"b":"foo"}}'::json);
test_func
-------------------
{"a":{"b":"foo"}}
CREATE OR REPLACE FUNCTION get_tree(data json) RETURNS json AS $$
var root = [];
for(var i in data) {
build_tree(data[i]['id'], data[i]['name'], data[i]['children']);
}
function build_tree(id, name, children) {
var exists = getObject(root, id);
WITH data AS(
select array_to_json(array_agg(row_to_json(t))) as data
from (
SELECT id, name, COALESCE(get_children(id), '[]') as children from genres
) t
) SELECT get_tree(data) from data;
@badri
badri / decoupal.make.yaml
Created February 24, 2016 11:11
decoupal make yaml first cut
core: 7.x
api: '2'
projects:
drupal:
version: ~
cdn:
type: module
subdir: contrib
version: ~
@badri
badri / Dockerfile
Last active March 8, 2016 07:18
buildpack doccker image
FROM progrium/cedarish:cedar14
RUN curl https://github.com/gliderlabs/herokuish/releases/download/v0.3.1/herokuish_0.3.1_linux_x86_64.tgz \
--silent -L | tar -xzC /bin
# install herokuish supported buildpacks and entrypoints
RUN /bin/herokuish buildpack install \
&& ln -s /bin/herokuish /build \
&& ln -s /bin/herokuish /start \
&& ln -s /bin/herokuish /exec
@badri
badri / py.py
Created March 8, 2016 09:56
ansible 2.0 run
#!/usr/bin/python2
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.inventory import Inventory
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'remote_user', 'private_key_file', 'ssh_common_args', 'sftp_extra_args', 'scp_extra_args', 'ssh_extra_args', 'verbosity'])