Method: PLSQL::Table#insert_values
- Defined in:
- lib/plsql/table.rb
#insert_values(*args) ⇒ Object
Insert record or records in table using array of values. Examples:
# with values for all columns
plsql.employees.insert_values [1, 'First', 'Last', Time.local(2000,01,31)]
# => INSERT INTO employees VALUES (1, 'First', 'Last', ...)
# with values for specified columns
plsql.employees.insert_values [:employee_id, :first_name, :last_name], [1, 'First', 'Last']
# => INSERT INTO employees (employee_id, first_name, last_name) VALUES (1, 'First', 'Last')
# with values for many records
plsql.employees.insert_values [:employee_id, :first_name, :last_name], [1, 'First', 'Last'], [2, 'Second', 'Last']
# => INSERT INTO employees (employee_id, first_name, last_name) VALUES (1, 'First', 'Last')
# => INSERT INTO employees (employee_id, first_name, last_name) VALUES (2, 'Second', 'Last')
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/plsql/table.rb', line 197 def insert_values(*args) raise ArgumentError, "no arguments given" unless args.first # if first argument is array of symbols then use it as list of fields if args.first.all?{|a| a.instance_of?(Symbol)} fields = args.shift # otherwise use all columns as list of fields else fields = column_names end args.each do |record| raise ArgumentError, "record should be Array of values" unless record.is_a?(Array) raise ArgumentError, "wrong number of column values" unless record.size == fields.size insert(ArrayHelpers::to_hash(fields, record)) end end |