Skip to content

Instantly share code, notes, and snippets.

View chanmix51's full-sized avatar

Grégoire HUBERT chanmix51

View GitHub Profile
@chanmix51
chanmix51 / dump_join.sql
Created April 30, 2015 12:18
Table dump for articles about joins.
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
--
select
sale.seller_id,
wd.day_date::date as day,
count(sale.sale_id) as sales,
coalesce(sum(sale.total_price_ct)/100, 0) as total
from
(select generate_series('2015-04-13', '2015-04-20', '1 day'::interval) as day_date) wd
left join sale on sale.seller_id = 3 and wd.day_date = date_trunc('day', sale_ts)
group by 1,2
CREATE TABLE department (
department_id serial NOT NULL,
name character varying NOT NULL,
department_parent_id integer
);
COPY department (department_id, name, department_parent_id) FROM stdin;
1 my_company \N
2 direction 1
3 administrative 1
4 sales 3
@chanmix51
chanmix51 / fizzbuzz.php
Created March 10, 2015 14:53
FizzBuzz
<?php
interface RuleInterface
{
public function verify($i);
public function output($i);
}
class DefaultRule implements RuleInterface
{
@chanmix51
chanmix51 / projection.php
Created January 21, 2015 11:41
case when in a projection definition
<?php
// …
public function createProjection()
{
$age = <<<FIELD
case
when age(%published_at) > '1 year 1 day'::interval then date_trunc('year', age(%published_at))
when age(%published_at) > '1 month 1 day'::interval then date_trunc('month', age(%published_at))
when age(%published_at) > '1 day'::interval then date_trunc('day', age(%published_at))
when age(%published_at) > '1 hour'::interval then date_trunc('hour', age(%published_at))
@chanmix51
chanmix51 / create_pomm2_silex.sh
Last active August 29, 2015 14:11
Pomm2 Silex project creation script
#!/bin/bash
echo "Project setup";
echo -n "What is your project name ['Test']:> ";
read project_name;
echo "Postgresql setup";
echo -n "What is your Postgresql username [$USER]:> ";
read db_username;
echo -n "What is this user's password (empty if none) [] :> ";
read db_password;
@chanmix51
chanmix51 / .pomm_cli_bootstrap.php
Created November 17, 2014 16:05
blog article about CLI
<?php
use \PommProject\Foundation\Pomm;
$loader = require __DIR__.'/vendor/autoload.php';
return new Pomm(['my_db' =>
[
'dsn' => 'pgsql://user:pass@host:port/db_name'
]
@chanmix51
chanmix51 / .pomm_cli_bootstrap.php
Last active August 29, 2015 14:09
Pomm 2 examples
<?php
use \PommProject\Foundation\Pomm;
$loader = require __DIR__.'/vendor/autoload.php';
$loader->add(null, __DIR__.'/sources/lib');
// customize the DSN with your database parameters.
return new Pomm(['elcaro' =>
[
@chanmix51
chanmix51 / observer.php
Last active August 29, 2015 14:09
Using the observer with Pomm 2.
<?php // observer.php
require __DIR__.'/vendor/autoload.php';
use PommProject\Foundation\Pomm;
// Edit the dsn with your database settings and credentials
$pomm = new Pomm(['my_db' => ['dsn' => 'pgsql://greg/greg']]);
do {
@chanmix51
chanmix51 / gist:9de65b6290253cbb0280
Created October 14, 2014 16:24
Creating an UTF-8 postgresql database template
update pg_catalog.pg_database set datistemplate = false where datname = 'template1';
drop database template1;
create database template1 template template0 encoding 'utf8' lc_collate 'fr_FR.utf8' lc_ctype 'fr_FR.utf8';
update pg_catalog.pg_database set datistemplate = true where datname = 'template1';