|
| 1 | +# The following resets the data for use with the update section |
| 2 | +# For both tables: |
| 3 | +# Table data is removed. |
| 4 | +# The identity column is set to start with the id after the starting data. |
| 5 | +# Using the executemany function an array of starting data is inserted into the table. |
| 6 | +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ |
| 7 | +# section titled "Resetting the data" |
| 8 | + |
| 9 | +require 'oci8' |
| 10 | +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database |
| 11 | +con = OCI8.new(connectString) |
| 12 | + |
| 13 | +# Delete rows |
| 14 | +cursor = con.parse('delete from lcs_pets') |
| 15 | +cursor.exec |
| 16 | + |
| 17 | +# Reset Identity Coulmn |
| 18 | +cursor = con.parse('alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 8)') |
| 19 | +cursor.exec |
| 20 | + |
| 21 | +# Delete rows |
| 22 | +cursor = con.parse('delete from lcs_people') |
| 23 | +cursor.exec |
| 24 | + |
| 25 | +# Reset Identity Coulmn |
| 26 | +cursor = con.parse('alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 8)') |
| 27 | +cursor.exec |
| 28 | + |
| 29 | +# Insert default people rows |
| 30 | +cursor = con.parse('INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)') |
| 31 | +cursor.max_array_size = 7 |
| 32 | +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) |
| 33 | +cursor.bind_param_array(:name, %w[Bob Kim Cheryl Bob Stacey Pete Pat]) |
| 34 | +cursor.bind_param_array(:age, [35, 27, 23, 27, 45, 23, 36]) |
| 35 | +cursor.bind_param_array(:notes, ['I like dogs', 'I like birds', 'I like horses', 'I like rabbits', 'I like snakes', 'I like cats', 'I like dogs']) |
| 36 | +people_row_count = cursor.exec_array |
| 37 | +printf " %d people rows inserted\n", people_row_count |
| 38 | + |
| 39 | +# Insert default pet rows |
| 40 | +cursor = con.parse('INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)') |
| 41 | +cursor.max_array_size = 7 |
| 42 | +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) |
| 43 | +cursor.bind_param_array(:name, %w[Duke Dragon Sneaky Red Red Buster Fido]) |
| 44 | +cursor.bind_param_array(:owner, [1, 2, 5, 2, 3, 1, 7]) |
| 45 | +cursor.bind_param_array(:type, %w[dog bird snake bird horse dog cat]) |
| 46 | +pet_row_count = cursor.exec_array |
| 47 | +printf " %d pet rows inserted\n", pet_row_count |
| 48 | + |
| 49 | +con.commit |
0 commit comments