Skip to content

Instantly share code, notes, and snippets.

@jl-
jl- / json_manipulator.sql
Created March 3, 2016 14:24 — forked from matheusoliveira/json_manipulator.sql
Simple PostgreSQL functions to manipulate json objects. (Note: performance is not a concern for those functions)
CREATE OR REPLACE FUNCTION public.json_append(data json, insert_data json)
RETURNS json
IMMUTABLE
LANGUAGE sql
AS $$
SELECT ('{'||string_agg(to_json(key)||':'||value, ',')||'}')::json
FROM (
SELECT * FROM json_each(data)
UNION ALL
SELECT * FROM json_each(insert_data)
@jl-
jl- / vigilante.sql
Created February 28, 2016 13:45 — forked from karmatr0n/vigilante.sql
SQL and PLPGSQL code to store ftp logs into a table in PostgreSQL (From rsyslog)
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
@jl-
jl- / dynamic_query_inside_sproc.sql
Created February 28, 2016 13:41 — forked from lmineiro/dynamic_query_inside_sproc.sql
Example of dynamic queries inside plpgsql stored procedures
CREATE TABLE IF NOT EXISTS sample_table (
id serial,
field1 text,
field2 int,
field3 date
);
INSERT INTO sample_table (field1, field2, field3)
SELECT
md5(random()::text),
-- Demo of PostgreSQL 9.5 features for Wellington PostgreSQL User's Group
-- The following is not necessarily in the right order or exactly how I ran it,
-- I mixed it up a bit when presenting!
DROP TABLE IF EXISTS country CASCADE;
DROP TABLE IF EXISTS sales_per_person CASCADE;
DROP TABLE IF EXISTS film CASCADE;
@jl-
jl- / gist:2be89cec2be3bb05a60e
Created December 31, 2015 18:07 — forked from j4k/gist:7133787
PostgreSQL fast distance query
WITH in_radius AS (
SELECT id
FROM table
WHERE earth_box( ll_to_earth( 51.534901, -0.204573), 5000.00) @> ll_to_earth(table.lat, table.lng)
), results AS (
SELECT *
FROM spaces
WHERE id IN (SELECT id FROM in_radius)
)
SELECT *,
@jl-
jl- / recursiveReactChildrenToArray.js
Created December 30, 2015 18:39 — forked from jsdf/recursiveReactChildrenToArray.js
Utility function to walk react element children and expand to an array of every element in tree (without removing children from elements)
import React from 'react';
export default function flattenReactChildrenToArray(nodeChildren, accumulated = []) {
React.Children.forEach(nodeChildren, (childNode) => {
accumulated.push(childNode);
if (childNode && childNode.props && childNode.props.children) {
flattenReactChildrenToArray(childNode.props.children, accumulated);
}
});
return accumulated;
@jl-
jl- / example.sql
Created December 12, 2015 15:04 — forked from mateuszwenus/example.sql
DB view dependencies in PostgreSQL
create table departments
(
dept_id integer primary key,
dept_name varchar(255)
);
create table employees
(
emp_id integer primary key,
emp_name varchar(255),
@jl-
jl- / Dockerfile
Created November 12, 2015 03:26 — forked from spujadas/Dockerfile
Dockerfile for Flarum commit d5229bd3
# Dockerfile for Flarum, based on Flarum's Vagrantfile
# (https://github.com/flarum/flarum/blob/master/Vagrantfile)
# which uses scripts from Vaprobash
# (https://github.com/fideloper/Vaprobash)
# Run with:
# docker-compose up flarum
# docker-compose run --service-ports flarum <custom command, e.g. /bin/bash>
FROM phusion/baseimage
@jl-
jl- / LearningSQLExample.sql
Created November 7, 2015 15:53 — forked from mahmoudhossam/LearningSQLExample.sql
"Learning SQL" example file converted to PostgreSQL.
/* begin table creation */
create table department
(department_id serial primary key,
name varchar(20) not null
);
create table branch
(branch_id serial primary key,
name varchar(20) not null,
@jl-
jl- / gist:08de5257f93f88446570
Created November 7, 2015 15:41 — forked from Kronos11/gist:1039510
account table
delimiter $$
CREATE TABLE `accounts` (
`account_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Account ID',
`account_username` char(32) NOT NULL DEFAULT '' COMMENT 'Account username',
`account_password` char(64) NOT NULL COMMENT 'Account password',
`account_station_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT 'Account STATION_ID',
`account_level` smallint(6) NOT NULL DEFAULT '0' COMMENT 'Account - CSR Flag',
`account_banned` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Account - Banned Status',
`account_email` char(64) NOT NULL DEFAULT '' COMMENT 'Account - User email',