Skip to content

Instantly share code, notes, and snippets.

@pateketrueke
Created September 6, 2012 16:11
Show Gist options
  • Save pateketrueke/3657960 to your computer and use it in GitHub Desktop.
Save pateketrueke/3657960 to your computer and use it in GitHub Desktop.
Fancy db-handle specs with migrations (unnamed project)
<?php
# through DSN
$db = connect(...);
$tbl = 'my_table';
$col = 'my_column';
serialize($db); # export scheme
unserialize($str); # import scheme
$db[$tbl]; # retrieve the table handle for $tbl, warn if not exists
$db[$tbl] = array(); # create the table with the given columns, warn if exists already
$db[$tbl] = ''; # rename the table or drop if empty; warn if not exists (from) or already exists (to)
isset($db[$tbl]); # check table existence without warnings
unset($db[$tbl]); # drop the table, warn if not exists
$db[$tbl]->$method(...); # delegate table-method, i.e. drop($tbl, ...) or rename($tbl, ...)
$db[$tbl]->select($fields, $where, $options); # => select($tbl, ...)
$db[$tbl]->insert($values, $pk_for_pgsql?); # => insert($tbl, ...)
$db[$tbl]->update($fields, $where, $limit); # => update($tbl, ...)
$db[$tbl]->delete($where, $limit); # => delete($tbl, ...)
$db[$tbl]->rename($to); # => rename($tbl, ...)
$db[$tbl]->drop(); # => drop($tbl)
$db[$tbl]->select()->$method(...); # delegate result-method, i.e. fetch_all($res, ...) or result(...)
$db[$tbl]->add_column($col, $name, $type); # => add_column($tbl, ...)
$db[$tbl]->rename_column($col, $to); # => rename_column($tbl, ...)
$db[$tbl]->change_column($col, $type); # => change_column($tbl, ...)
$db[$tbl]->remove_column($col); # => remove_column($tbl, ...)
serialize($db[$tbl]); # export data-only
unserialize($db[$tbl]); # import data-only
$db[$tbl][$col]; # retrieve field handle for $col, warn if not exists
$db[$tbl][$col] = ''; # rename the column or remove if empty; warn if not exists (from) or already exists (to)
$db[$tbl][$col] = array(); # add or change column, warn if already exists or cannot be crafted
isset($db[$tbl][$col]); # check column existence without warnings
unset($db[$tbl][$col]); # remove column, warn if not exists
$db[$tbl][$col]->$method(...); # delegate column-method, rename($tbl, $col, ...) => rename_column($tbl, $col, ...)
# special cases, the $hash is generated from given arguments, i.e. $hash = "$tbl.$col.$name"
$db[$tbl][$col]->index($name, $unique); # => add_index($tbl, $hash, $col, ...)
$db[$tbl][$col]->unindex($name); # => remove_index($hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment