Skip to content

Latest commit

 

History

History
328 lines (238 loc) · 8.3 KB

Notes.md

File metadata and controls

328 lines (238 loc) · 8.3 KB

How to properly construct builder pattern in rust

https://aturon.github.io/ownership/builders.html

##Postgres specific foreign key *This is needed because information_schema is a lot slower

http://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys

SELECT
  o.conname AS constraint_name,
  (SELECT nspname FROM pg_namespace WHERE oid=m.relnamespace) AS source_schema,
  m.relname AS source_table,
  (SELECT a.attname FROM pg_attribute a WHERE a.attrelid = m.oid AND a.attnum = o.conkey[1] AND a.attisdropped = false) AS source_column,
  (SELECT nspname FROM pg_namespace WHERE oid=f.relnamespace) AS target_schema,
  f.relname AS target_table,
  (SELECT a.attname FROM pg_attribute a WHERE a.attrelid = f.oid AND a.attnum = o.confkey[1] AND a.attisdropped = false) AS target_column
FROM
  pg_constraint o LEFT JOIN pg_class c ON c.oid = o.conrelid
  LEFT JOIN pg_class f ON f.oid = o.confrelid LEFT JOIN pg_class m ON m.oid = o.conrelid
WHERE
  o.contype = 'f' AND o.conrelid IN (SELECT oid FROM pg_class c WHERE c.relkind = 'r');
 

##Show tables in postgresql

select 
  tablename as table 
from 
  pg_tables  
where schemaname = 'public'

A simple Good resource for reading about lifetimes http://hermanradtke.com/2015/05/03/string-vs-str-in-rust-functions.html

##Select the parent table

select relname,  
( select relname from pg_class where oid = pg_inherits.inhparent ) as parent 
					 from pg_class  
					 left join pg_inherits  
					 on pg_class.oid = pg_inherits.inhrelid 
					 where relname = 'product'

##Select the subclass

select relname, 
( select relname from pg_class where oid = pg_inherits.inhrelid ) as subclass 
					 from pg_inherits 
					 left join pg_class on pg_class.oid = pg_inherits.inhparent 
					 where relname = 'base' ;

Useful projects:

text editor

https://github.com/gchp/iota

csv parsing

https://github.com/BurntSushi/rust-csv

window tiling for the win

https://github.com/Kintaro/wtftw

for text base interface, a wrapper for termbox

https://github.com/gchp/rustbox

##How to deal with nullable columns in the database *Most database columns has null values, can be optional *If nullable make the field Optional

Check to see if extension are already installed in the database

select * from pg_extension

You may need to create the schema before installing the extensions

releasing:

cargo publish

Make sure license, github code, documentation is properly linked in the Cargo.toml file

Publishing the documents

cargo clean
cargo doc --no-deps
cd target/doc
git init
git add . -A
git commit -m "Commiting docs to github pages"
git remote add origin https://github.com/ivanceras/rustorm
git checkout -b gh-pages
git push --force origin gh-pages


Using vim coding rust

  • Install pathogen
mkdir -p ~/.vim/autoload ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
  • Add this to ~/.vimrc
execute pathogen#infect()
syntax on
filetype plugin indent on
  • Install syntax highlighting in rust
cd ~/.vim/bundle
git clone https://github.com/rust-lang/rust.vim.git
  • Install vim racer plugin
cd .vim/bundle/
git clone https://github.com/ebfe/vim-racer
  • Add this to ~/.vimrc
set hidden
let g:racer_cmd = "/home/lee/Developer/racer/target/release/racer"
let $RUST_SRC_PATH="/home/lee/Developer/rust-1.0.0/src"

  • Vim number toggle
cd ~/.vim/bundle
git clone git://github.com/jeffkreeftmeijer/vim-numbertoggle.git
  • Install nerdtree for displaying files in a tabs
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git
  • Add this to ~/.vimrc
autocmd VimEnter * NERDTree
autocmd BufEnter * NERDTreeMirror

"CTRL-N to toggle tree view with CTRL-N
nmap <silent> <c-n> :NERDTreeToggle<CR>		
"Set F2 to put the cursor to the nerdtree
nmap <silent> <F2> :NERDTreeFind<CR>

You can use to switch in between windows

vim cheatsheet at http://vim.rtorr.com/

Screen capturing the desktop

sudo apt-get install byzanz

Install ScreenRuler/Kruler as well

Record using

byzanz-record --duration=30 --x=2 --y=50 --width=1095 --height=595 out.gif
byzanz-record --duration=15 --x=2 --y=50 --width=1095 --height=595 out.gif

put the window to the top of the OS toolbar, this is 50px including the window of the terminal put a margin of around 2px from the side

git compare file changes

git config diff.tool vimdiff git difftool

Count the number of lines in git project

git ls-files | xargs wc -l

Show postgresql version

https://blog.sleeplessbeastie.eu/2014/04/04/how-to-remotely-check-postgresql-version/

SHOW server_version; select version();

Uuid

  • uuid has 16 bytes -> (128 bits/8)
  • 32 characters (hex digit)
  • 36 when including hyphens
  • 22 character when encododed to base64

gotcha: need to put & to borrow immutable from mutable

fn execute_with_return(&mut self, query:&Query)->DaoResult{
        let sql_frag = &self.build_query(query);

Get view columns

SELECT
                pg_attribute.attnum AS number,
                pg_attribute.attname AS name,
                pg_attribute.attnotnull AS notnull,
                pg_catalog.format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS data_type,
                CASE
                WHEN pg_constraint.contype = 'p' THEN true
                ELSE false
                END AS is_primary,
                CASE
                WHEN pg_constraint.contype = 'u' THEN true
                ELSE false
                END AS is_unique,
                CASE
                WHEN pg_constraint.contype = 'f' THEN g.relname
                END AS foreign_table,
                CASE
                WHEN pg_attribute.atthasdef = true THEN pg_attrdef.adsrc
                END as default
                ,pg_description.description as comment
                ,(SELECT nspname FROM pg_namespace WHERE oid=g.relnamespace) AS foreign_schema
                ,(SELECT pg_attribute.attname FROM pg_attribute
                WHERE pg_attribute.attrelid = pg_constraint.confrelid
                AND pg_attribute.attnum = pg_constraint.confkey[1]
                AND pg_attribute.attisdropped = false) AS foreign_column
                ,pg_constraint.conname

            FROM pg_attribute
                JOIN pg_class
                    ON pg_class.oid = pg_attribute.attrelid
                JOIN pg_type
                    ON pg_type.oid = pg_attribute.atttypid
                LEFT JOIN pg_attrdef
                    ON pg_attrdef.adrelid = pg_class.oid
                    AND pg_attrdef.adnum = pg_attribute.attnum
                LEFT JOIN pg_namespace
                    ON pg_namespace.oid = pg_class.relnamespace
                LEFT JOIN pg_constraint
                    ON pg_constraint.conrelid = pg_class.oid
                    AND pg_attribute.attnum = ANY (pg_constraint.conkey)
                LEFT JOIN pg_class AS g
                    ON pg_constraint.confrelid = g.oid
                LEFT JOIN pg_description
                    ON pg_description.objoid = pg_class.oid
                    AND pg_description.objsubid = pg_attribute.attnum
            WHERE pg_class.relkind = 'v'::char
                AND pg_namespace.nspname = 'views'
                AND pg_class.relname = 'vw_device_ssh'
                AND pg_attribute.attnum > 0
                ORDER BY number

Sqlite meta data extractions

https://www.sqlite.org/pragma.html#pragma_foreign_key_list

extract table columns

PRAGMA database.table_info(table-name); PRAGMA table_info(product);

foreign keys

PRAGMA foreign_key_list(table-name); PRAGMA foreign_key_list(product_availability);

indexes

PRAGMA index_list(table-name);

CREATE TABLE product_availability ( product_id uuid NOT NULL, available boolean, always_available boolean, stocks numeric DEFAULT 1, available_from timestamp with time zone, available_until timestamp with time zone, available_day json, open_time time with time zone, close_time time with time zone, FOREIGN KEY(product_id) REFERENCES product(product_id) )

extract columns in sqlite,

http://stackoverflow.com/questions/2785702/use-javascript-regex-to-extract-column-names-from-sqlite-create-table-sql

Run test in the project using

 cargo test --features "mysql sqlite"