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),
@jl-
jl- / sql-builderl.js
Created January 23, 2016 15:12
postgresql sql-builder
import without from 'lodash/array/without';
const FIELD_SEPERATOR = ', ';
// table public fields
export const ACCOUNT_FIELDS = ['id', 'username', 'email', 'phone', 'password', 'status', 'type', 'created_at'];
export const PARENT_FIELDS = ['account_id', 'activated_at'];
export const EXPERT_FIELDS = ['account_id', 'activated_at'];
export const PROFILE_FIELDS = ['account_id', 'display_name', 'avatar_url', 'location_id', 'updated_at'];
export const LOCATION_FIELDS = ['id', 'country', 'province', 'city', 'district', 'street', 'street_number', 'lat', 'lng', 'created_at'];
export const COMMUNITY_FIELDS = ['id', 'location_id', 'name', 'description', 'signature', 'notice', 'cover_image_url', 'member_counts', 'rules', 'join_restrict', 'join_review', 'join_review_question', 'join_review_question', 'visible_on_search', 'status', 'created_at'];
-- 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- / schema.sql
Last active December 4, 2015 04:46
xixi
CREATE DATABASE xixi;
/**
* accounts
*/
CREATE TABLE accounts (
id bigserial UNIQUE NOT NULL,
-- UNIQUE is needed
-- http://www.postgresql.org/docs/9.5/static/datatype-numeric.html#DATATYPE-SERIAL
--