ruby-plsql Cheat Sheet Cheat Sheet
by
Jacek Gebal (jgebal)
via
Session connect
Insert into table (cont)
plsql( : de f au l t).co n nect! {:username => 'hr', :password => 'hr', :database
plsql.e m p l oy e es.insert {:empl o yee_id => 2, :name => 'Tony Stark'}
=> 'xe'}
# inserts one row into the employees table, with partial column list
# opens a default connection to database
plsql.e m p l oy e es.insert [ {:empl o yee_id => 3, :name => 'Darth Vader'},
plsql.c o n n ect! {:username => 'hr', :password => 'hr', :database => 'xe'}
{:empl o yee_id => 4, :name => 'Luke Skywal k er'}]
# opens a default connection to database
# inserts multiple rows into the employees table using Array of Hashes
plsql( : an o th e r).co n nect! {:username => 'hr', :password => 'hr', :database
plsql.e m p l oy e es.in s er t _v a lues *[ [ 5, 'Batman', Time.l o ca l (1 9 90 , 01 , 01)],
=> 'xe'}
[6,'Sp i de r ma n ', T im e .l o ca l (1 9 99 , 02 , 02)] ]
# opens a second connection (refer e nced by Symbol :another)
# inserts multiple rows, specifying Array of Array of values
plsql( : an o th e r).logoff
plsql.e m p l oy e es.in s er t _v a lues [:empl o ye e _id, :name], *[ [ 7, 'Super m an'],
[8, 'Hulk'] ]
# discon n ects connection (refer e nced by symbol :another)
# inserts multiple rows, specifying columns first and subset of values
Transa c tion
plsql.e m p l oy e es.in s er t _v a lues [ 9, 'Thor', Time.l o ca l (1 9 90 , 09 , 09)]
# inserts one row, specifying only Array of values
plsql( : an o th e r).co n ne c ti o n.a u t o commit = false
#disables auto commit in :another connection
plsql.e m p l oy e es.in s er t _v a lues [:empl o ye e _id, :name], [ 10, 'Sandman' ]
# inserts one row, specifying subset of columns (Array) and Array of
plsql.a u t o co m mit?
values
# returns the current status of autocommit
plsql.c o mmit
Select statements
#commits a transa c tion in :default connection
plsql.s e l e ct ( :f i rst, " S ELECT * FROM employ e es " )
plsql( : an o th e r).ro l lback
{:empl o yee_id => 1, :name => 'James bond', :hire_date => '0007- 0 7-
#rollbacks changes for :another connection
07'}
# returns first row of a query as a Ruby Hash
plsql.s a v e point " s av e _t h is _ po i nt "
plsql.s e l e ct _ on e ("S E LECT count(*) FROM employ e es " )
#sets a transa c tion savepoint in :default connection
10
plsql.r o l l ba c k_to " s av e _t h is _ po i nt "
# returns a scalar value from a first row from single column query
#rollbacks to specified savepoint in :default connection
plsql.s e l e ct _ on e ("S E LECT employ e e_id FROM employees WHERE 1=2")
nil
Insert into table
# returns nil Object (NULL) when no data found
plsql.e m p l oy e es.insert {:empl o yee_id => 1, :name => 'James bond',
:hire_date => Time.l o ca l (0 0 07 , 07 , 07)}
# inserts one row into the employees table using key-value pairs (Ruby
Hash object)
By Jacek Gebal (jgebal)
Published 19th July, 2015.
Sponsored by
Last updated 19th July, 2015.
Learn to solve cryptic crosswords!
Page 1 of 4.