" pg_apprentice's vim shortcuts - paste at end of .vimrc
" This function is needed for what follows:
" from :help abbreviations, then /consume the space
func Eatchar(pat)
let b = nr2char(getchar())
return (b =~ a:pat) ? '' : b
endfunc
" This sets us up with a nice CREATE TABLE with parentheses
iab ct CREATE TABLE<CR>(<CR> <CR><ESC>xi);<ESC>kkkA
" This sets us up with a nice table level primary key constraint, labeling the constraint
" table_name_pkey by searching backwords for the first CREATE TABLE statement
iab <silent> cpk CONSTRAINT <ESC>mp?CREATE TABLE<CR>2wye`ppa_pkey PRIMARY KEY()<ESC>:silent noh<CR><ESC>A<Left><C-R>=Eatchar('\s')<CR>
" This sets up column level foreign key constraints by searching through your file and grabbing
" whatever is set up as column_name .*PRIMARY.*KEY, and also grabbing the associated table name
" for that column.
iab rf REFERENCES <ESC>mp^?<C-r><C-w>.*PRIMARY.*KEY<CR>?CREATE TABLE<CR>2wye`ppa(<ESC>:silent noh<CR><ESC>mp^ye`ppa),