From dcf20a79b99c708fb55e292e202cf6f2c0dff3e7 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 14:43:43 -0600 Subject: [PATCH 01/10] added ruby crud directories and starter files --- ruby/CRUD-examples/README.md | 1 + ruby/CRUD-examples/createDBObjects.sql | 49 ++++++++++++++++++++++++++ ruby/CRUD-examples/dropDBObjects.sql | 8 +++++ ruby/CRUD-examples/insert/template.rb | 29 +++++++++++++++ ruby/CRUD-examples/reset_data.rb | 46 ++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 ruby/CRUD-examples/README.md create mode 100644 ruby/CRUD-examples/createDBObjects.sql create mode 100644 ruby/CRUD-examples/dropDBObjects.sql create mode 100644 ruby/CRUD-examples/insert/template.rb create mode 100644 ruby/CRUD-examples/reset_data.rb diff --git a/ruby/CRUD-examples/README.md b/ruby/CRUD-examples/README.md new file mode 100644 index 00000000..8155ac50 --- /dev/null +++ b/ruby/CRUD-examples/README.md @@ -0,0 +1 @@ +The scripts in this directory follow the [BASIC CRUD OPERATIONS USING RUBY-OCI8](https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/) tutorial found on [LearnCodeShare.net](https://learncodeshare.net). diff --git a/ruby/CRUD-examples/createDBObjects.sql b/ruby/CRUD-examples/createDBObjects.sql new file mode 100644 index 00000000..5ba85175 --- /dev/null +++ b/ruby/CRUD-examples/createDBObjects.sql @@ -0,0 +1,49 @@ +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ +# The following creates and populates the tables used for the tutorial +# These scripts use identity columns available in Oracle 12.1. If you are using an older version you will need +# to add your own trigger/sequence functionality to generate the id values. + +CREATE TABLE LCS_PEOPLE ( + id NUMBER GENERATED BY DEFAULT AS identity, + name VARCHAR2(20), + age NUMBER, + notes VARCHAR2(100) +) +/ + +ALTER TABLE LCS_PEOPLE +ADD CONSTRAINT PK_LCS_PEOPLE PRIMARY KEY ("ID") +/ + +CREATE TABLE LCS_PETS ( + id NUMBER GENERATED BY DEFAULT AS IDENTITY, + name VARCHAR2(20), + owner NUMBER, + type VARCHAR2(100) +) +/ + +ALTER TABLE LCS_PETS ADD CONSTRAINT PK_LCS_PETS PRIMARY KEY ("ID") +/ + +ALTER TABLE LCS_PETS ADD CONSTRAINT FK_LCS_PETS_OWNER FOREIGN KEY ("OWNER") REFERENCES "LCS_PEOPLE" ("ID") +/ + +INSERT INTO LCS_PEOPLE (name, age, notes) + VALUES ('Bob', 35, 'I like dogs') +/ + +INSERT INTO LCS_PEOPLE (name, age, notes) + VALUES ('Kim', 27, 'I like birds') +/ + +INSERT INTO LCS_PETS (name, owner, type) + VALUES ('Duke', 1, 'dog') +/ + +INSERT INTO LCS_PETS (name, owner, type) + VALUES ('Pepe', 2, 'bird') +/ + +COMMIT +/ diff --git a/ruby/CRUD-examples/dropDBObjects.sql b/ruby/CRUD-examples/dropDBObjects.sql new file mode 100644 index 00000000..ef872788 --- /dev/null +++ b/ruby/CRUD-examples/dropDBObjects.sql @@ -0,0 +1,8 @@ +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ +# The following drops the tables created for the tutorial + +drop table LCS_PETS +/ + +drop table LCS_PEOPLE +/ diff --git a/ruby/CRUD-examples/insert/template.rb b/ruby/CRUD-examples/insert/template.rb new file mode 100644 index 00000000..6add996c --- /dev/null +++ b/ruby/CRUD-examples/insert/template.rb @@ -0,0 +1,29 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ +# section titled "Boilerplate template" +# The following code is used as the base template for the other examples. + +require 'oci8' +connectString = ENV['db_connect'] + +def get_all_rows (label) + connectString = ENV['db_connect'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch() {|row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + } + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Your code here + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/reset_data.rb b/ruby/CRUD-examples/reset_data.rb new file mode 100644 index 00000000..090b538c --- /dev/null +++ b/ruby/CRUD-examples/reset_data.rb @@ -0,0 +1,46 @@ +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ +# The following resets the data to the initial state for the tutorial. + +# Query all rows +require 'oci8' +connectString = ENV['DB_CONNECT'] +printf connectString +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse("delete from lcs_pets") +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse("alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 3)") +cursor.exec + +# Delete rows +cursor = con.parse("delete from lcs_people") +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse("alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 3)") +cursor.exec + +# Insert default people rows +cursor = con.parse("INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)") +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, ["Bob", "Kim"]) +cursor.bind_param_array(:age, [35, 27]) +cursor.bind_param_array(:notes, ["I like dogs", "I like birds"]) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse("INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)") +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, ["Duke", "Pepe"]) +cursor.bind_param_array(:owner, [1, 2]) +cursor.bind_param_array(:type, ["dog", "bird"]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit From a0a641aa7784fe74abc3431517bea7750f3611e7 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 15:36:51 -0600 Subject: [PATCH 02/10] added insert examples --- ruby/CRUD-examples/insert/extra_fun_1.rb | 41 ++++++++++++++++ ruby/CRUD-examples/insert/extra_fun_2.rb | 40 ++++++++++++++++ ruby/CRUD-examples/insert/extra_fun_3.rb | 44 +++++++++++++++++ ruby/CRUD-examples/insert/multiple.rb | 37 +++++++++++++++ ruby/CRUD-examples/insert/reset_data.rb | 50 ++++++++++++++++++++ ruby/CRUD-examples/insert/return_data.rb | 60 ++++++++++++++++++++++++ ruby/CRUD-examples/insert/simple.rb | 35 ++++++++++++++ ruby/CRUD-examples/insert/template.rb | 26 +++++----- 8 files changed, 320 insertions(+), 13 deletions(-) create mode 100644 ruby/CRUD-examples/insert/extra_fun_1.rb create mode 100644 ruby/CRUD-examples/insert/extra_fun_2.rb create mode 100644 ruby/CRUD-examples/insert/extra_fun_3.rb create mode 100644 ruby/CRUD-examples/insert/multiple.rb create mode 100644 ruby/CRUD-examples/insert/reset_data.rb create mode 100644 ruby/CRUD-examples/insert/return_data.rb create mode 100644 ruby/CRUD-examples/insert/simple.rb diff --git a/ruby/CRUD-examples/insert/extra_fun_1.rb b/ruby/CRUD-examples/insert/extra_fun_1.rb new file mode 100644 index 00000000..63cd58bb --- /dev/null +++ b/ruby/CRUD-examples/insert/extra_fun_1.rb @@ -0,0 +1,41 @@ +# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Extra Fun 1 & 2" +# Using the base template, the example code executes a simple insert using positional bind variables. +# The same statement is executed twice each using different bind variable values. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Rob') +cursor.bind_param(:age, 37) +cursor.bind_param(:notes, 'I like snakes') +cursor.exec + +cursor.bind_param(:name, 'Cheryl') +cursor.bind_param(:age, 41) +cursor.bind_param(:notes, 'I like monkeys') +cursor.exec +con.commit + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/extra_fun_2.rb b/ruby/CRUD-examples/insert/extra_fun_2.rb new file mode 100644 index 00000000..838411bc --- /dev/null +++ b/ruby/CRUD-examples/insert/extra_fun_2.rb @@ -0,0 +1,40 @@ +# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Extra Fun 1 & 2" +# Using the base template, the example code executes a simple insert using positional bind variables. +# The get_all_rows function is modified to use a second connection to show how the data is seen +# by different connections before and after a commit. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, con) + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +con = OCI8.new(connectString) +con2 = OCI8.new(connectString) + +get_all_rows('Original Data', con) + +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Suzy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like rabbits') +cursor.exec + +get_all_rows('New connection after insert', con2) +get_all_rows('Same connection', con) + +con.commit + +get_all_rows('New connection after commit', con2) +get_all_rows('New Data', con) diff --git a/ruby/CRUD-examples/insert/extra_fun_3.rb b/ruby/CRUD-examples/insert/extra_fun_3.rb new file mode 100644 index 00000000..df9cb7dd --- /dev/null +++ b/ruby/CRUD-examples/insert/extra_fun_3.rb @@ -0,0 +1,44 @@ +# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Extra Fun 3" +# Using the base template, the example code executes a simple insert using positional bind variables. +# Cursor variables are used to accept the insert statements returning values. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes) returning id, name into :id, :name_out' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Sandy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like horses') +cursor.bind_param(:id, Integer) +cursor.bind_param(:name_out, String) +cursor.exec + +new_id = cursor[:id] +name_out = cursor[:name_out] + +con.commit + +printf " Our new id is: %d name: %s\n\n", new_id, name_out + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/multiple.rb b/ruby/CRUD-examples/insert/multiple.rb new file mode 100644 index 00000000..1d146fe2 --- /dev/null +++ b/ruby/CRUD-examples/insert/multiple.rb @@ -0,0 +1,37 @@ +# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Insert more than 1 row" +# Using the base template, the example code executes a simple insert using positional bind variables. +# Using the executemany function an array of data is inserted into the table. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +cursor = con.parse('INSERT INTO lcs_people(name, age, notes) VALUES (:name, :age, :notes)') +cursor.max_array_size = 2 +cursor.bind_param_array(:name, %w[Sandy Suzy]) +cursor.bind_param_array(:age, [31, 29]) +cursor.bind_param_array(:notes, ['I like horses', 'I like rabbits']) +people_row_count = cursor.exec_array +con.commit +printf " Successfully inserted %d records\n\n", people_row_count + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/reset_data.rb b/ruby/CRUD-examples/insert/reset_data.rb new file mode 100644 index 00000000..4389a024 --- /dev/null +++ b/ruby/CRUD-examples/insert/reset_data.rb @@ -0,0 +1,50 @@ +# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Resetting the data" +# The following resets the data for use with the update section +# For both tables: +# Table data is removed. +# The identity column is set to start with the id after the starting data. +# Using the executemany function an array of starting data is inserted into the table. + +# Query all rows +require 'oci8' +connectString = ENV['DB_CONNECT'] +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse('delete from lcs_pets') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 3)') +cursor.exec + +# Delete rows +cursor = con.parse('delete from lcs_people') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 3)') +cursor.exec + +# Insert default people rows +cursor = con.parse('INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)') +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, %w[Bob Kim]) +cursor.bind_param_array(:age, [35, 27]) +cursor.bind_param_array(:notes, ['I like dogs', 'I like birds']) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse('INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)') +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, %w[Duke Pepe]) +cursor.bind_param_array(:owner, [1, 2]) +cursor.bind_param_array(:type, %w[dog bird]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit diff --git a/ruby/CRUD-examples/insert/return_data.rb b/ruby/CRUD-examples/insert/return_data.rb new file mode 100644 index 00000000..eca43975 --- /dev/null +++ b/ruby/CRUD-examples/insert/return_data.rb @@ -0,0 +1,60 @@ +# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Returning data after an insert" +# Using the base template, the example code executes a simple insert using positional bind variables. +# A cursor variable is used to accept the insert statements returning value. This value is then +# used as the parent key value to insert a child record. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes) returning id into :id' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Sandy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like horses') +cursor.bind_param(:id, Integer) +cursor.exec + +new_id = cursor[:id] + +statement = 'insert into lcs_pets (name, owner, type) values (:name, :owner, :type)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Big Red') +cursor.bind_param(:owner, new_id) +cursor.bind_param(:type, 'horse') +cursor.exec + +con.commit + +printf " Our new value is: %d\n", new_id + +statement = 'select name, owner, type from lcs_pets where owner = :owner' +cursor = con.parse(statement) +cursor.bind_param(:owner, new_id) +cursor.exec +printf "\n Sandy\'s pets:\n" +cursor.fetch do |row| + printf " Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2] +end +printf "\n" + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/simple.rb b/ruby/CRUD-examples/insert/simple.rb new file mode 100644 index 00000000..7d446653 --- /dev/null +++ b/ruby/CRUD-examples/insert/simple.rb @@ -0,0 +1,35 @@ +# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Simple insert" +# Using the base template, the example code executes a simple insert using positional bind variables. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Sandy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like horses') +cursor.exec +con.commit + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/template.rb b/ruby/CRUD-examples/insert/template.rb index 6add996c..87182c80 100644 --- a/ruby/CRUD-examples/insert/template.rb +++ b/ruby/CRUD-examples/insert/template.rb @@ -3,21 +3,21 @@ # The following code is used as the base template for the other examples. require 'oci8' -connectString = ENV['db_connect'] +connectString = ENV['DB_CONNECT'] -def get_all_rows (label) - connectString = ENV['db_connect'] - con = OCI8.new(connectString) +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) - # Query all rows - statement = 'select id, name, age, notes from lcs_people order by id' - cursor = con.parse(statement) - cursor.exec - printf " %s:\n", label - cursor.fetch() {|row| - printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] - } - printf "\n" + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" end con = OCI8.new(connectString) From 4960d5f4347e7e0185f163e9e869dbcdda76fb9d Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 15:39:35 -0600 Subject: [PATCH 03/10] changed http to https --- ruby/CRUD-examples/insert/extra_fun_1.rb | 2 +- ruby/CRUD-examples/insert/extra_fun_2.rb | 2 +- ruby/CRUD-examples/insert/extra_fun_3.rb | 2 +- ruby/CRUD-examples/insert/multiple.rb | 2 +- ruby/CRUD-examples/insert/reset_data.rb | 2 +- ruby/CRUD-examples/insert/return_data.rb | 2 +- ruby/CRUD-examples/insert/simple.rb | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ruby/CRUD-examples/insert/extra_fun_1.rb b/ruby/CRUD-examples/insert/extra_fun_1.rb index 63cd58bb..ae9b613d 100644 --- a/ruby/CRUD-examples/insert/extra_fun_1.rb +++ b/ruby/CRUD-examples/insert/extra_fun_1.rb @@ -1,4 +1,4 @@ -# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Extra Fun 1 & 2" # Using the base template, the example code executes a simple insert using positional bind variables. # The same statement is executed twice each using different bind variable values. diff --git a/ruby/CRUD-examples/insert/extra_fun_2.rb b/ruby/CRUD-examples/insert/extra_fun_2.rb index 838411bc..2d9766d8 100644 --- a/ruby/CRUD-examples/insert/extra_fun_2.rb +++ b/ruby/CRUD-examples/insert/extra_fun_2.rb @@ -1,4 +1,4 @@ -# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Extra Fun 1 & 2" # Using the base template, the example code executes a simple insert using positional bind variables. # The get_all_rows function is modified to use a second connection to show how the data is seen diff --git a/ruby/CRUD-examples/insert/extra_fun_3.rb b/ruby/CRUD-examples/insert/extra_fun_3.rb index df9cb7dd..212e54f2 100644 --- a/ruby/CRUD-examples/insert/extra_fun_3.rb +++ b/ruby/CRUD-examples/insert/extra_fun_3.rb @@ -1,4 +1,4 @@ -# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Extra Fun 3" # Using the base template, the example code executes a simple insert using positional bind variables. # Cursor variables are used to accept the insert statements returning values. diff --git a/ruby/CRUD-examples/insert/multiple.rb b/ruby/CRUD-examples/insert/multiple.rb index 1d146fe2..b62f8d9e 100644 --- a/ruby/CRUD-examples/insert/multiple.rb +++ b/ruby/CRUD-examples/insert/multiple.rb @@ -1,4 +1,4 @@ -# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Insert more than 1 row" # Using the base template, the example code executes a simple insert using positional bind variables. # Using the executemany function an array of data is inserted into the table. diff --git a/ruby/CRUD-examples/insert/reset_data.rb b/ruby/CRUD-examples/insert/reset_data.rb index 4389a024..d8ce60e5 100644 --- a/ruby/CRUD-examples/insert/reset_data.rb +++ b/ruby/CRUD-examples/insert/reset_data.rb @@ -1,4 +1,4 @@ -# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Resetting the data" # The following resets the data for use with the update section # For both tables: diff --git a/ruby/CRUD-examples/insert/return_data.rb b/ruby/CRUD-examples/insert/return_data.rb index eca43975..48e5fc19 100644 --- a/ruby/CRUD-examples/insert/return_data.rb +++ b/ruby/CRUD-examples/insert/return_data.rb @@ -1,4 +1,4 @@ -# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Returning data after an insert" # Using the base template, the example code executes a simple insert using positional bind variables. # A cursor variable is used to accept the insert statements returning value. This value is then diff --git a/ruby/CRUD-examples/insert/simple.rb b/ruby/CRUD-examples/insert/simple.rb index 7d446653..a7974066 100644 --- a/ruby/CRUD-examples/insert/simple.rb +++ b/ruby/CRUD-examples/insert/simple.rb @@ -1,4 +1,4 @@ -# Code Sample from the tutorial at http://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Simple insert" # Using the base template, the example code executes a simple insert using positional bind variables. From c349a58826fb9318c2164d4d16ff9328dd8943d1 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 15:54:47 -0600 Subject: [PATCH 04/10] added select examples --- ruby/CRUD-examples/select/bind_variables.rb | 18 ++++++++++++++++++ ruby/CRUD-examples/select/extra_fun_1.rb | 15 +++++++++++++++ ruby/CRUD-examples/select/extra_fun_2.rb | 16 ++++++++++++++++ ruby/CRUD-examples/select/simple.rb | 17 +++++++++++++++++ ruby/CRUD-examples/select/template.rb | 8 ++++++++ 5 files changed, 74 insertions(+) create mode 100644 ruby/CRUD-examples/select/bind_variables.rb create mode 100644 ruby/CRUD-examples/select/extra_fun_1.rb create mode 100644 ruby/CRUD-examples/select/extra_fun_2.rb create mode 100644 ruby/CRUD-examples/select/simple.rb create mode 100644 ruby/CRUD-examples/select/template.rb diff --git a/ruby/CRUD-examples/select/bind_variables.rb b/ruby/CRUD-examples/select/bind_variables.rb new file mode 100644 index 00000000..72ce8756 --- /dev/null +++ b/ruby/CRUD-examples/select/bind_variables.rb @@ -0,0 +1,18 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Select specific rows" +# Using the base template, the example code executes a simple query using named bind variables, +# uses fetchall to retrieve the data and displays the results. + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Query for Kim +person_name = 'Kim' +statement = 'select id, name, age, notes from lcs_people where name=:name' +cursor = con.parse(statement) +cursor.bind_param('name', person_name) +cursor.exec +cursor.fetch do |row| + printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] +end diff --git a/ruby/CRUD-examples/select/extra_fun_1.rb b/ruby/CRUD-examples/select/extra_fun_1.rb new file mode 100644 index 00000000..9f783db6 --- /dev/null +++ b/ruby/CRUD-examples/select/extra_fun_1.rb @@ -0,0 +1,15 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Extra Fun 1" +# Using the base template, the example code executes a simple query, uses fetchall to retrieve the data +# and displays the results. + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +statement = 'select id, name, age, notes from lcs_people order by age' +cursor = con.parse(statement) +cursor.exec +cursor.fetch do |row| + printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] +end diff --git a/ruby/CRUD-examples/select/extra_fun_2.rb b/ruby/CRUD-examples/select/extra_fun_2.rb new file mode 100644 index 00000000..46d3fe8e --- /dev/null +++ b/ruby/CRUD-examples/select/extra_fun_2.rb @@ -0,0 +1,16 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Extra Fun 2" +# Using the base template, the example code executes a simple query using named bind variables, +# uses fetchall to retrieve the data and displays the results. + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +statement = 'select id, name, age, notes from lcs_people where age > :age' +cursor = con.parse(statement) +cursor.bind_param('age', 30) +cursor.exec +cursor.fetch do |row| + printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] +end diff --git a/ruby/CRUD-examples/select/simple.rb b/ruby/CRUD-examples/select/simple.rb new file mode 100644 index 00000000..10cce33b --- /dev/null +++ b/ruby/CRUD-examples/select/simple.rb @@ -0,0 +1,17 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Simple query" +# Using the base template, the example code executes a simple query, uses fetchall to retrieve the data +# and displays the results. + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Query all rows +con = OCI8.new(connectString) +statement = 'select id, name, age, notes from lcs_people' +cursor = con.parse(statement) +cursor.exec +cursor.fetch do |row| + print row +end diff --git a/ruby/CRUD-examples/select/template.rb b/ruby/CRUD-examples/select/template.rb new file mode 100644 index 00000000..c373437c --- /dev/null +++ b/ruby/CRUD-examples/select/template.rb @@ -0,0 +1,8 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# The following code is used as the base template for the other examples. + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Your code here From a818f1eacf438c6d043ba6c96e94adc46016a2d8 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 16:02:32 -0600 Subject: [PATCH 05/10] added update examples --- ruby/CRUD-examples/update/extra_fun_1.rb | 43 ++++++++++++++++++ ruby/CRUD-examples/update/extra_fun_2.rb | 47 ++++++++++++++++++++ ruby/CRUD-examples/update/reset_data.rb | 49 +++++++++++++++++++++ ruby/CRUD-examples/update/simple.rb | 43 ++++++++++++++++++ ruby/CRUD-examples/update/specific_where.rb | 44 ++++++++++++++++++ ruby/CRUD-examples/update/template.rb | 38 ++++++++++++++++ ruby/CRUD-examples/update/verify_number.rb | 46 +++++++++++++++++++ 7 files changed, 310 insertions(+) create mode 100644 ruby/CRUD-examples/update/extra_fun_1.rb create mode 100644 ruby/CRUD-examples/update/extra_fun_2.rb create mode 100644 ruby/CRUD-examples/update/reset_data.rb create mode 100644 ruby/CRUD-examples/update/simple.rb create mode 100644 ruby/CRUD-examples/update/specific_where.rb create mode 100644 ruby/CRUD-examples/update/template.rb create mode 100644 ruby/CRUD-examples/update/verify_number.rb diff --git a/ruby/CRUD-examples/update/extra_fun_1.rb b/ruby/CRUD-examples/update/extra_fun_1.rb new file mode 100644 index 00000000..305c2677 --- /dev/null +++ b/ruby/CRUD-examples/update/extra_fun_1.rb @@ -0,0 +1,43 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Extra Fun 1" +# Using the base template, the example code executes a simple update using positional bind variables. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +statement = 'update lcs_people set notes = :notes where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:notes, 'I like cats') +cursor.bind_param(:id, 1) +cursor.exec +con.commit + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/extra_fun_2.rb b/ruby/CRUD-examples/update/extra_fun_2.rb new file mode 100644 index 00000000..8aee5aae --- /dev/null +++ b/ruby/CRUD-examples/update/extra_fun_2.rb @@ -0,0 +1,47 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Extra Fun 2" +# Using the base template, the example code executes a simple update using positional bind variables +# and displays the number of affected rows. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +statement = 'update lcs_pets set owner = :newOwner where type = :type and owner != :oldOwner' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:type, 'bird') +cursor.bind_param(:oldOwner, 2) +changed = cursor.exec +con.commit + +printf "Number of rows updated: %d\n\n", changed + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/update/reset_data.rb b/ruby/CRUD-examples/update/reset_data.rb new file mode 100644 index 00000000..9c42754d --- /dev/null +++ b/ruby/CRUD-examples/update/reset_data.rb @@ -0,0 +1,49 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Resetting the data" +# The following resets the data for use with the update section +# For both tables: +# Table data is removed. +# The identity column is set to start with the id after the starting data. +# Using the executemany function an array of starting data is inserted into the table. + +require 'oci8' +connectString = ENV['DB_CONNECT'] +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse('delete from lcs_pets') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 8)') +cursor.exec + +# Delete rows +cursor = con.parse('delete from lcs_people') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 3)') +cursor.exec + +# Insert default people rows +cursor = con.parse('INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)') +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, %w[Bob Kim]) +cursor.bind_param_array(:age, [35, 27]) +cursor.bind_param_array(:notes, ['I like dogs', 'I like birds']) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse('INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)') +cursor.max_array_size = 7 +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) +cursor.bind_param_array(:name, %w[Duke Pepe Princess Polly Rollo Buster Fido]) +cursor.bind_param_array(:owner, [1, 2, 1, 1, 1, 1, 1]) +cursor.bind_param_array(:type, %w[dog bird snake bird horse dog cat]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit diff --git a/ruby/CRUD-examples/update/simple.rb b/ruby/CRUD-examples/update/simple.rb new file mode 100644 index 00000000..1d1acf9b --- /dev/null +++ b/ruby/CRUD-examples/update/simple.rb @@ -0,0 +1,43 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Simple update" +# Using the base template, the example code executes a simple update using positional bind variables. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +statement = 'update lcs_people set age = :age where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:age, 31) +cursor.bind_param(:id, 1) +cursor.exec +con.commit + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/specific_where.rb b/ruby/CRUD-examples/update/specific_where.rb new file mode 100644 index 00000000..8df08788 --- /dev/null +++ b/ruby/CRUD-examples/update/specific_where.rb @@ -0,0 +1,44 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Make sure your where clause is specific" +# Using the base template, the example code executes a simple update using positional bind variables. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +statement = 'update lcs_pets set owner = :newOwner where owner = :oldOwner and type = :type' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:oldOwner, 1) +cursor.bind_param(:type, 'dog') +cursor.exec +con.commit + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/update/template.rb b/ruby/CRUD-examples/update/template.rb new file mode 100644 index 00000000..663b9e3b --- /dev/null +++ b/ruby/CRUD-examples/update/template.rb @@ -0,0 +1,38 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Boilerplate template" +# The following code is used as the base template for the other examples. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Your code here + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/verify_number.rb b/ruby/CRUD-examples/update/verify_number.rb new file mode 100644 index 00000000..67d470d3 --- /dev/null +++ b/ruby/CRUD-examples/update/verify_number.rb @@ -0,0 +1,46 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Verify the number of affected rows" +# Using the base template, the example code executes a simple update using positional bind variables +# and displays the number of affected rows. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +statement = 'update lcs_pets set owner = :newOwner where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:id, 6) +changed = cursor.exec +con.commit + +printf "Number of rows updated: %d\n\n", changed + +get_all_rows('New Data', 'pets') From 048d357604a6da782daafca08335fdea37ade38e Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 16:11:44 -0600 Subject: [PATCH 06/10] added delete examples --- ruby/CRUD-examples/delete/extra_fun_1.rb | 42 +++++++++++++++++++ ruby/CRUD-examples/delete/extra_fun_2.rb | 50 ++++++++++++++++++++++ ruby/CRUD-examples/delete/foreign_keys.rb | 46 ++++++++++++++++++++ ruby/CRUD-examples/delete/handle_pets.rb | 51 +++++++++++++++++++++++ ruby/CRUD-examples/delete/reset_data.rb | 49 ++++++++++++++++++++++ ruby/CRUD-examples/delete/simple.rb | 42 +++++++++++++++++++ ruby/CRUD-examples/delete/template.rb | 38 +++++++++++++++++ 7 files changed, 318 insertions(+) create mode 100644 ruby/CRUD-examples/delete/extra_fun_1.rb create mode 100644 ruby/CRUD-examples/delete/extra_fun_2.rb create mode 100644 ruby/CRUD-examples/delete/foreign_keys.rb create mode 100644 ruby/CRUD-examples/delete/handle_pets.rb create mode 100644 ruby/CRUD-examples/delete/reset_data.rb create mode 100644 ruby/CRUD-examples/delete/simple.rb create mode 100644 ruby/CRUD-examples/delete/template.rb diff --git a/ruby/CRUD-examples/delete/extra_fun_1.rb b/ruby/CRUD-examples/delete/extra_fun_1.rb new file mode 100644 index 00000000..cea066c8 --- /dev/null +++ b/ruby/CRUD-examples/delete/extra_fun_1.rb @@ -0,0 +1,42 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Extra Fun 1" +# Using the base template, the example code executes a simple delete using named bind variables. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +statement = 'delete from lcs_pets where type = :type' +cursor = con.parse(statement) +cursor.bind_param(:type, 'bird') +cursor.exec +con.commit + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/delete/extra_fun_2.rb b/ruby/CRUD-examples/delete/extra_fun_2.rb new file mode 100644 index 00000000..3cbb732d --- /dev/null +++ b/ruby/CRUD-examples/delete/extra_fun_2.rb @@ -0,0 +1,50 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Extra Fun 2" +# Using the base template, the example code executes two simple deletes using named bind variables. +# The child records are removed, followed by the parent record. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original People Data', 'people') +get_all_rows('Original Pet Data', 'pets') + +statement = 'delete from lcs_pets where owner = :owner' +cursor = con.parse(statement) +cursor.bind_param(:owner, 5) +cursor.exec + +statement = 'delete from lcs_people where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 5) +cursor.exec +con.commit + +get_all_rows('New People Data', 'people') +get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/foreign_keys.rb b/ruby/CRUD-examples/delete/foreign_keys.rb new file mode 100644 index 00000000..8a9d8e3e --- /dev/null +++ b/ruby/CRUD-examples/delete/foreign_keys.rb @@ -0,0 +1,46 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Deleting records referenced by Foreign Keys" 1st example +# Using the base template, the example code executes a simple delete using named bind variables. +# When following the tutorial with default data this section intentionally throws an error +# to demonstrate foreign key functionality. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original People Data', 'people') +get_all_rows('Original Pet Data', 'pets') + +statement = 'delete from lcs_people where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 1) +cursor.exec +con.commit + +get_all_rows('New People Data', 'people') +get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/handle_pets.rb b/ruby/CRUD-examples/delete/handle_pets.rb new file mode 100644 index 00000000..046fadf3 --- /dev/null +++ b/ruby/CRUD-examples/delete/handle_pets.rb @@ -0,0 +1,51 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Deleting records referenced by Foreign Keys" 2nd example +# Using the base template, the example code executes two simple deletes using named bind variables. +# The child records are removed, followed by the parent record. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original People Data', 'people') +get_all_rows('Original Pet Data', 'pets') + +statement = 'update lcs_pets set owner = :newOwner where owner = :oldOwner' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:oldOwner, 1) +cursor.exec + +statement = 'delete from lcs_people where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 1) +cursor.exec +con.commit + +get_all_rows('New People Data', 'people') +get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/reset_data.rb b/ruby/CRUD-examples/delete/reset_data.rb new file mode 100644 index 00000000..5f5b2cf5 --- /dev/null +++ b/ruby/CRUD-examples/delete/reset_data.rb @@ -0,0 +1,49 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Resetting the data" +# The following resets the data for use with the update section +# For both tables: +# Table data is removed. +# The identity column is set to start with the id after the starting data. +# Using the executemany function an array of starting data is inserted into the table. + +require 'oci8' +connectString = ENV['DB_CONNECT'] +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse('delete from lcs_pets') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 8)') +cursor.exec + +# Delete rows +cursor = con.parse('delete from lcs_people') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 8)') +cursor.exec + +# Insert default people rows +cursor = con.parse('INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)') +cursor.max_array_size = 7 +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) +cursor.bind_param_array(:name, %w[Bob Kim Cheryl Bob Stacey Pete Pat]) +cursor.bind_param_array(:age, [35, 27, 23, 27, 45, 23, 36]) +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']) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse('INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)') +cursor.max_array_size = 7 +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) +cursor.bind_param_array(:name, %w[Duke Dragon Sneaky Red Red Buster Fido]) +cursor.bind_param_array(:owner, [1, 2, 5, 2, 3, 1, 7]) +cursor.bind_param_array(:type, %w[dog bird snake bird horse dog cat]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit diff --git a/ruby/CRUD-examples/delete/simple.rb b/ruby/CRUD-examples/delete/simple.rb new file mode 100644 index 00000000..e4794623 --- /dev/null +++ b/ruby/CRUD-examples/delete/simple.rb @@ -0,0 +1,42 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Simple delete" +# Using the base template, the example code executes a simple delete using named bind variables. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +statement = 'delete from lcs_pets where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 1) +cursor.exec +con.commit + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/delete/template.rb b/ruby/CRUD-examples/delete/template.rb new file mode 100644 index 00000000..0bdeb343 --- /dev/null +++ b/ruby/CRUD-examples/delete/template.rb @@ -0,0 +1,38 @@ +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Boilerplate template" +# The following code is used as the base template for the other examples. + +require 'oci8' +connectString = ENV['DB_CONNECT'] + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +# Your code here + +get_all_rows('New Data', 'pets') From 03aa994ec66ae1183124f05c34a99ac8aa1abe7c Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 16:18:15 -0600 Subject: [PATCH 07/10] added environment vairable comment --- ruby/CRUD-examples/delete/extra_fun_1.rb | 4 ++-- ruby/CRUD-examples/delete/extra_fun_2.rb | 4 ++-- ruby/CRUD-examples/delete/foreign_keys.rb | 4 ++-- ruby/CRUD-examples/delete/handle_pets.rb | 4 ++-- ruby/CRUD-examples/delete/reset_data.rb | 2 +- ruby/CRUD-examples/delete/simple.rb | 4 ++-- ruby/CRUD-examples/delete/template.rb | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ruby/CRUD-examples/delete/extra_fun_1.rb b/ruby/CRUD-examples/delete/extra_fun_1.rb index cea066c8..4b10b7f9 100644 --- a/ruby/CRUD-examples/delete/extra_fun_1.rb +++ b/ruby/CRUD-examples/delete/extra_fun_1.rb @@ -3,10 +3,10 @@ # Using the base template, the example code executes a simple delete using named bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/delete/extra_fun_2.rb b/ruby/CRUD-examples/delete/extra_fun_2.rb index 3cbb732d..a9e7bbe3 100644 --- a/ruby/CRUD-examples/delete/extra_fun_2.rb +++ b/ruby/CRUD-examples/delete/extra_fun_2.rb @@ -4,10 +4,10 @@ # The child records are removed, followed by the parent record. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/delete/foreign_keys.rb b/ruby/CRUD-examples/delete/foreign_keys.rb index 8a9d8e3e..96f31493 100644 --- a/ruby/CRUD-examples/delete/foreign_keys.rb +++ b/ruby/CRUD-examples/delete/foreign_keys.rb @@ -5,10 +5,10 @@ # to demonstrate foreign key functionality. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/delete/handle_pets.rb b/ruby/CRUD-examples/delete/handle_pets.rb index 046fadf3..1be0d132 100644 --- a/ruby/CRUD-examples/delete/handle_pets.rb +++ b/ruby/CRUD-examples/delete/handle_pets.rb @@ -4,10 +4,10 @@ # The child records are removed, followed by the parent record. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/delete/reset_data.rb b/ruby/CRUD-examples/delete/reset_data.rb index 5f5b2cf5..cc9b8615 100644 --- a/ruby/CRUD-examples/delete/reset_data.rb +++ b/ruby/CRUD-examples/delete/reset_data.rb @@ -7,7 +7,7 @@ # Using the executemany function an array of starting data is inserted into the table. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Delete rows diff --git a/ruby/CRUD-examples/delete/simple.rb b/ruby/CRUD-examples/delete/simple.rb index e4794623..af1f02d1 100644 --- a/ruby/CRUD-examples/delete/simple.rb +++ b/ruby/CRUD-examples/delete/simple.rb @@ -3,10 +3,10 @@ # Using the base template, the example code executes a simple delete using named bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/delete/template.rb b/ruby/CRUD-examples/delete/template.rb index 0bdeb343..0c839f55 100644 --- a/ruby/CRUD-examples/delete/template.rb +++ b/ruby/CRUD-examples/delete/template.rb @@ -3,10 +3,10 @@ # The following code is used as the base template for the other examples. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows From 5ea6e85b759d64435784145ee8376b3c2a410c6c Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Thu, 20 Jul 2017 16:19:14 -0600 Subject: [PATCH 08/10] added environment vairable comment --- ruby/CRUD-examples/insert/extra_fun_1.rb | 4 ++-- ruby/CRUD-examples/insert/extra_fun_2.rb | 2 +- ruby/CRUD-examples/insert/extra_fun_3.rb | 4 ++-- ruby/CRUD-examples/insert/multiple.rb | 4 ++-- ruby/CRUD-examples/insert/reset_data.rb | 2 +- ruby/CRUD-examples/insert/return_data.rb | 4 ++-- ruby/CRUD-examples/insert/simple.rb | 4 ++-- ruby/CRUD-examples/insert/template.rb | 4 ++-- ruby/CRUD-examples/reset_data.rb | 2 +- ruby/CRUD-examples/update/extra_fun_1.rb | 4 ++-- ruby/CRUD-examples/update/extra_fun_2.rb | 4 ++-- ruby/CRUD-examples/update/reset_data.rb | 2 +- ruby/CRUD-examples/update/simple.rb | 4 ++-- ruby/CRUD-examples/update/specific_where.rb | 4 ++-- ruby/CRUD-examples/update/template.rb | 4 ++-- ruby/CRUD-examples/update/verify_number.rb | 4 ++-- 16 files changed, 28 insertions(+), 28 deletions(-) diff --git a/ruby/CRUD-examples/insert/extra_fun_1.rb b/ruby/CRUD-examples/insert/extra_fun_1.rb index ae9b613d..e552acdb 100644 --- a/ruby/CRUD-examples/insert/extra_fun_1.rb +++ b/ruby/CRUD-examples/insert/extra_fun_1.rb @@ -4,10 +4,10 @@ # The same statement is executed twice each using different bind variable values. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/insert/extra_fun_2.rb b/ruby/CRUD-examples/insert/extra_fun_2.rb index 2d9766d8..b5761df7 100644 --- a/ruby/CRUD-examples/insert/extra_fun_2.rb +++ b/ruby/CRUD-examples/insert/extra_fun_2.rb @@ -5,7 +5,7 @@ # by different connections before and after a commit. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, con) # Query all rows diff --git a/ruby/CRUD-examples/insert/extra_fun_3.rb b/ruby/CRUD-examples/insert/extra_fun_3.rb index 212e54f2..d39f2d1e 100644 --- a/ruby/CRUD-examples/insert/extra_fun_3.rb +++ b/ruby/CRUD-examples/insert/extra_fun_3.rb @@ -4,10 +4,10 @@ # Cursor variables are used to accept the insert statements returning values. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/insert/multiple.rb b/ruby/CRUD-examples/insert/multiple.rb index b62f8d9e..6b490ed4 100644 --- a/ruby/CRUD-examples/insert/multiple.rb +++ b/ruby/CRUD-examples/insert/multiple.rb @@ -4,10 +4,10 @@ # Using the executemany function an array of data is inserted into the table. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/insert/reset_data.rb b/ruby/CRUD-examples/insert/reset_data.rb index d8ce60e5..1c3f8c75 100644 --- a/ruby/CRUD-examples/insert/reset_data.rb +++ b/ruby/CRUD-examples/insert/reset_data.rb @@ -8,7 +8,7 @@ # Query all rows require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Delete rows diff --git a/ruby/CRUD-examples/insert/return_data.rb b/ruby/CRUD-examples/insert/return_data.rb index 48e5fc19..118b3039 100644 --- a/ruby/CRUD-examples/insert/return_data.rb +++ b/ruby/CRUD-examples/insert/return_data.rb @@ -5,10 +5,10 @@ # used as the parent key value to insert a child record. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/insert/simple.rb b/ruby/CRUD-examples/insert/simple.rb index a7974066..70e451af 100644 --- a/ruby/CRUD-examples/insert/simple.rb +++ b/ruby/CRUD-examples/insert/simple.rb @@ -3,10 +3,10 @@ # Using the base template, the example code executes a simple insert using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/insert/template.rb b/ruby/CRUD-examples/insert/template.rb index 87182c80..2d682301 100644 --- a/ruby/CRUD-examples/insert/template.rb +++ b/ruby/CRUD-examples/insert/template.rb @@ -3,10 +3,10 @@ # The following code is used as the base template for the other examples. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/reset_data.rb b/ruby/CRUD-examples/reset_data.rb index 090b538c..49ecbf9e 100644 --- a/ruby/CRUD-examples/reset_data.rb +++ b/ruby/CRUD-examples/reset_data.rb @@ -3,7 +3,7 @@ # Query all rows require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database printf connectString con = OCI8.new(connectString) diff --git a/ruby/CRUD-examples/update/extra_fun_1.rb b/ruby/CRUD-examples/update/extra_fun_1.rb index 305c2677..43e3c44b 100644 --- a/ruby/CRUD-examples/update/extra_fun_1.rb +++ b/ruby/CRUD-examples/update/extra_fun_1.rb @@ -3,10 +3,10 @@ # Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/update/extra_fun_2.rb b/ruby/CRUD-examples/update/extra_fun_2.rb index 8aee5aae..eec1dcd9 100644 --- a/ruby/CRUD-examples/update/extra_fun_2.rb +++ b/ruby/CRUD-examples/update/extra_fun_2.rb @@ -4,10 +4,10 @@ # and displays the number of affected rows. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/update/reset_data.rb b/ruby/CRUD-examples/update/reset_data.rb index 9c42754d..bfcc19ab 100644 --- a/ruby/CRUD-examples/update/reset_data.rb +++ b/ruby/CRUD-examples/update/reset_data.rb @@ -7,7 +7,7 @@ # Using the executemany function an array of starting data is inserted into the table. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Delete rows diff --git a/ruby/CRUD-examples/update/simple.rb b/ruby/CRUD-examples/update/simple.rb index 1d1acf9b..184cb0d0 100644 --- a/ruby/CRUD-examples/update/simple.rb +++ b/ruby/CRUD-examples/update/simple.rb @@ -3,10 +3,10 @@ # Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/update/specific_where.rb b/ruby/CRUD-examples/update/specific_where.rb index 8df08788..71113b61 100644 --- a/ruby/CRUD-examples/update/specific_where.rb +++ b/ruby/CRUD-examples/update/specific_where.rb @@ -3,10 +3,10 @@ # Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/update/template.rb b/ruby/CRUD-examples/update/template.rb index 663b9e3b..1a5571c4 100644 --- a/ruby/CRUD-examples/update/template.rb +++ b/ruby/CRUD-examples/update/template.rb @@ -3,10 +3,10 @@ # The following code is used as the base template for the other examples. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows diff --git a/ruby/CRUD-examples/update/verify_number.rb b/ruby/CRUD-examples/update/verify_number.rb index 67d470d3..9c90e657 100644 --- a/ruby/CRUD-examples/update/verify_number.rb +++ b/ruby/CRUD-examples/update/verify_number.rb @@ -4,10 +4,10 @@ # and displays the number of affected rows. require 'oci8' -connectString = ENV['DB_CONNECT'] +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') - connectString = ENV['DB_CONNECT'] + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) # Query all rows From d6fd11d316365e150ae18f3477a437c619471523 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Wed, 26 Jul 2017 12:03:49 -0600 Subject: [PATCH 09/10] Moved the connectString variable from the top of the file down near where it's used. --- ruby/CRUD-examples/delete/extra_fun_1.rb | 2 +- ruby/CRUD-examples/delete/extra_fun_2.rb | 2 +- ruby/CRUD-examples/delete/foreign_keys.rb | 2 +- ruby/CRUD-examples/delete/handle_pets.rb | 2 +- ruby/CRUD-examples/delete/simple.rb | 2 +- ruby/CRUD-examples/delete/template.rb | 2 +- ruby/CRUD-examples/insert/extra_fun_1.rb | 2 +- ruby/CRUD-examples/insert/extra_fun_2.rb | 2 +- ruby/CRUD-examples/insert/extra_fun_3.rb | 2 +- ruby/CRUD-examples/insert/multiple.rb | 2 +- ruby/CRUD-examples/insert/return_data.rb | 2 +- ruby/CRUD-examples/insert/simple.rb | 2 +- ruby/CRUD-examples/insert/template.rb | 2 +- ruby/CRUD-examples/reset_data.rb | 1 - ruby/CRUD-examples/update/extra_fun_1.rb | 2 +- ruby/CRUD-examples/update/extra_fun_2.rb | 2 +- ruby/CRUD-examples/update/simple.rb | 2 +- ruby/CRUD-examples/update/specific_where.rb | 2 +- ruby/CRUD-examples/update/template.rb | 2 +- ruby/CRUD-examples/update/verify_number.rb | 2 +- 20 files changed, 19 insertions(+), 20 deletions(-) diff --git a/ruby/CRUD-examples/delete/extra_fun_1.rb b/ruby/CRUD-examples/delete/extra_fun_1.rb index 4b10b7f9..34c635a8 100644 --- a/ruby/CRUD-examples/delete/extra_fun_1.rb +++ b/ruby/CRUD-examples/delete/extra_fun_1.rb @@ -3,7 +3,6 @@ # Using the base template, the example code executes a simple delete using named bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -29,6 +28,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data', 'pets') diff --git a/ruby/CRUD-examples/delete/extra_fun_2.rb b/ruby/CRUD-examples/delete/extra_fun_2.rb index a9e7bbe3..d780de78 100644 --- a/ruby/CRUD-examples/delete/extra_fun_2.rb +++ b/ruby/CRUD-examples/delete/extra_fun_2.rb @@ -4,7 +4,6 @@ # The child records are removed, followed by the parent record. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -30,6 +29,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original People Data', 'people') diff --git a/ruby/CRUD-examples/delete/foreign_keys.rb b/ruby/CRUD-examples/delete/foreign_keys.rb index 96f31493..d39ed852 100644 --- a/ruby/CRUD-examples/delete/foreign_keys.rb +++ b/ruby/CRUD-examples/delete/foreign_keys.rb @@ -5,7 +5,6 @@ # to demonstrate foreign key functionality. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -31,6 +30,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original People Data', 'people') diff --git a/ruby/CRUD-examples/delete/handle_pets.rb b/ruby/CRUD-examples/delete/handle_pets.rb index 1be0d132..06585050 100644 --- a/ruby/CRUD-examples/delete/handle_pets.rb +++ b/ruby/CRUD-examples/delete/handle_pets.rb @@ -4,7 +4,6 @@ # The child records are removed, followed by the parent record. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -30,6 +29,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original People Data', 'people') diff --git a/ruby/CRUD-examples/delete/simple.rb b/ruby/CRUD-examples/delete/simple.rb index af1f02d1..a12df409 100644 --- a/ruby/CRUD-examples/delete/simple.rb +++ b/ruby/CRUD-examples/delete/simple.rb @@ -3,7 +3,6 @@ # Using the base template, the example code executes a simple delete using named bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -29,6 +28,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data', 'pets') diff --git a/ruby/CRUD-examples/delete/template.rb b/ruby/CRUD-examples/delete/template.rb index 0c839f55..e2482522 100644 --- a/ruby/CRUD-examples/delete/template.rb +++ b/ruby/CRUD-examples/delete/template.rb @@ -3,7 +3,6 @@ # The following code is used as the base template for the other examples. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -29,6 +28,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data', 'pets') diff --git a/ruby/CRUD-examples/insert/extra_fun_1.rb b/ruby/CRUD-examples/insert/extra_fun_1.rb index e552acdb..616f059a 100644 --- a/ruby/CRUD-examples/insert/extra_fun_1.rb +++ b/ruby/CRUD-examples/insert/extra_fun_1.rb @@ -4,7 +4,6 @@ # The same statement is executed twice each using different bind variable values. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -21,6 +20,7 @@ def get_all_rows(label) printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/insert/extra_fun_2.rb b/ruby/CRUD-examples/insert/extra_fun_2.rb index b5761df7..f5116342 100644 --- a/ruby/CRUD-examples/insert/extra_fun_2.rb +++ b/ruby/CRUD-examples/insert/extra_fun_2.rb @@ -5,7 +5,6 @@ # by different connections before and after a commit. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, con) # Query all rows @@ -19,6 +18,7 @@ def get_all_rows(label, con) printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) con2 = OCI8.new(connectString) diff --git a/ruby/CRUD-examples/insert/extra_fun_3.rb b/ruby/CRUD-examples/insert/extra_fun_3.rb index d39f2d1e..f87f3541 100644 --- a/ruby/CRUD-examples/insert/extra_fun_3.rb +++ b/ruby/CRUD-examples/insert/extra_fun_3.rb @@ -4,7 +4,6 @@ # Cursor variables are used to accept the insert statements returning values. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -21,6 +20,7 @@ def get_all_rows(label) printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/insert/multiple.rb b/ruby/CRUD-examples/insert/multiple.rb index 6b490ed4..6b426c1b 100644 --- a/ruby/CRUD-examples/insert/multiple.rb +++ b/ruby/CRUD-examples/insert/multiple.rb @@ -4,7 +4,6 @@ # Using the executemany function an array of data is inserted into the table. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -21,6 +20,7 @@ def get_all_rows(label) printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/insert/return_data.rb b/ruby/CRUD-examples/insert/return_data.rb index 118b3039..a4ad5a8a 100644 --- a/ruby/CRUD-examples/insert/return_data.rb +++ b/ruby/CRUD-examples/insert/return_data.rb @@ -5,7 +5,6 @@ # used as the parent key value to insert a child record. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -22,6 +21,7 @@ def get_all_rows(label) printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/insert/simple.rb b/ruby/CRUD-examples/insert/simple.rb index 70e451af..ac138179 100644 --- a/ruby/CRUD-examples/insert/simple.rb +++ b/ruby/CRUD-examples/insert/simple.rb @@ -3,7 +3,6 @@ # Using the base template, the example code executes a simple insert using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -20,6 +19,7 @@ def get_all_rows(label) printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/insert/template.rb b/ruby/CRUD-examples/insert/template.rb index 2d682301..8ee7762c 100644 --- a/ruby/CRUD-examples/insert/template.rb +++ b/ruby/CRUD-examples/insert/template.rb @@ -3,7 +3,6 @@ # The following code is used as the base template for the other examples. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label) connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -20,6 +19,7 @@ def get_all_rows(label) printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/reset_data.rb b/ruby/CRUD-examples/reset_data.rb index 49ecbf9e..d87038f9 100644 --- a/ruby/CRUD-examples/reset_data.rb +++ b/ruby/CRUD-examples/reset_data.rb @@ -4,7 +4,6 @@ # Query all rows require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database -printf connectString con = OCI8.new(connectString) # Delete rows diff --git a/ruby/CRUD-examples/update/extra_fun_1.rb b/ruby/CRUD-examples/update/extra_fun_1.rb index 43e3c44b..8ba2ae93 100644 --- a/ruby/CRUD-examples/update/extra_fun_1.rb +++ b/ruby/CRUD-examples/update/extra_fun_1.rb @@ -3,7 +3,6 @@ # Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -29,6 +28,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/update/extra_fun_2.rb b/ruby/CRUD-examples/update/extra_fun_2.rb index eec1dcd9..9c0f9306 100644 --- a/ruby/CRUD-examples/update/extra_fun_2.rb +++ b/ruby/CRUD-examples/update/extra_fun_2.rb @@ -4,7 +4,6 @@ # and displays the number of affected rows. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -30,6 +29,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data', 'pets') diff --git a/ruby/CRUD-examples/update/simple.rb b/ruby/CRUD-examples/update/simple.rb index 184cb0d0..9b42baff 100644 --- a/ruby/CRUD-examples/update/simple.rb +++ b/ruby/CRUD-examples/update/simple.rb @@ -3,7 +3,6 @@ # Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -29,6 +28,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/update/specific_where.rb b/ruby/CRUD-examples/update/specific_where.rb index 71113b61..a8bee84b 100644 --- a/ruby/CRUD-examples/update/specific_where.rb +++ b/ruby/CRUD-examples/update/specific_where.rb @@ -3,7 +3,6 @@ # Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -29,6 +28,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data', 'pets') diff --git a/ruby/CRUD-examples/update/template.rb b/ruby/CRUD-examples/update/template.rb index 1a5571c4..316b5416 100644 --- a/ruby/CRUD-examples/update/template.rb +++ b/ruby/CRUD-examples/update/template.rb @@ -3,7 +3,6 @@ # The following code is used as the base template for the other examples. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -29,6 +28,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data') diff --git a/ruby/CRUD-examples/update/verify_number.rb b/ruby/CRUD-examples/update/verify_number.rb index 9c90e657..4f7d90d4 100644 --- a/ruby/CRUD-examples/update/verify_number.rb +++ b/ruby/CRUD-examples/update/verify_number.rb @@ -4,7 +4,6 @@ # and displays the number of affected rows. require 'oci8' -connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database def get_all_rows(label, data_type = 'people') connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database @@ -30,6 +29,7 @@ def get_all_rows(label, data_type = 'people') printf "\n" end +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) get_all_rows('Original Data', 'pets') From e60286acbdc0734f4634531d72d50dd318027805 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 18 Aug 2017 14:45:22 -0600 Subject: [PATCH 10/10] Fixed and added comments to clarify the examples. --- ruby/CRUD-examples/createDBObjects.sql | 2 +- ruby/CRUD-examples/delete/extra_fun_1.rb | 4 +++- ruby/CRUD-examples/delete/extra_fun_2.rb | 6 ++++-- ruby/CRUD-examples/delete/foreign_keys.rb | 9 ++++++--- ruby/CRUD-examples/delete/handle_pets.rb | 7 +++++-- ruby/CRUD-examples/delete/reset_data.rb | 4 ++-- ruby/CRUD-examples/delete/simple.rb | 4 +++- ruby/CRUD-examples/delete/template.rb | 3 ++- ruby/CRUD-examples/dropDBObjects.sql | 2 +- ruby/CRUD-examples/insert/extra_fun_1.rb | 6 ++++-- ruby/CRUD-examples/insert/extra_fun_2.rb | 9 ++++++--- ruby/CRUD-examples/insert/extra_fun_3.rb | 6 ++++-- ruby/CRUD-examples/insert/multiple.rb | 8 +++++--- ruby/CRUD-examples/insert/reset_data.rb | 4 ++-- ruby/CRUD-examples/insert/return_data.rb | 8 +++++--- ruby/CRUD-examples/insert/simple.rb | 4 +++- ruby/CRUD-examples/insert/template.rb | 3 ++- ruby/CRUD-examples/reset_data.rb | 2 +- ruby/CRUD-examples/select/bind_variables.rb | 7 ++++--- ruby/CRUD-examples/select/extra_fun_1.rb | 6 ++++-- ruby/CRUD-examples/select/extra_fun_2.rb | 6 ++++-- ruby/CRUD-examples/select/simple.rb | 7 ++++--- ruby/CRUD-examples/select/template.rb | 3 ++- ruby/CRUD-examples/update/extra_fun_1.rb | 4 +++- ruby/CRUD-examples/update/extra_fun_2.rb | 7 +++++-- ruby/CRUD-examples/update/reset_data.rb | 4 ++-- ruby/CRUD-examples/update/simple.rb | 4 +++- ruby/CRUD-examples/update/specific_where.rb | 4 +++- ruby/CRUD-examples/update/template.rb | 3 ++- ruby/CRUD-examples/update/verify_number.rb | 7 +++++-- 30 files changed, 100 insertions(+), 53 deletions(-) diff --git a/ruby/CRUD-examples/createDBObjects.sql b/ruby/CRUD-examples/createDBObjects.sql index 5ba85175..7de64a26 100644 --- a/ruby/CRUD-examples/createDBObjects.sql +++ b/ruby/CRUD-examples/createDBObjects.sql @@ -1,7 +1,7 @@ -# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ # The following creates and populates the tables used for the tutorial # These scripts use identity columns available in Oracle 12.1. If you are using an older version you will need # to add your own trigger/sequence functionality to generate the id values. +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ CREATE TABLE LCS_PEOPLE ( id NUMBER GENERATED BY DEFAULT AS identity, diff --git a/ruby/CRUD-examples/delete/extra_fun_1.rb b/ruby/CRUD-examples/delete/extra_fun_1.rb index 34c635a8..f2ffffd5 100644 --- a/ruby/CRUD-examples/delete/extra_fun_1.rb +++ b/ruby/CRUD-examples/delete/extra_fun_1.rb @@ -1,6 +1,6 @@ +# The example code below executes a simple delete using named bind variables. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ # section titled "Extra Fun 1" -# Using the base template, the example code executes a simple delete using named bind variables. require 'oci8' @@ -33,10 +33,12 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original Data', 'pets') +# Example code showing a simple delete using named bind variables. statement = 'delete from lcs_pets where type = :type' cursor = con.parse(statement) cursor.bind_param(:type, 'bird') cursor.exec con.commit +# End Example get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/delete/extra_fun_2.rb b/ruby/CRUD-examples/delete/extra_fun_2.rb index d780de78..74e0f220 100644 --- a/ruby/CRUD-examples/delete/extra_fun_2.rb +++ b/ruby/CRUD-examples/delete/extra_fun_2.rb @@ -1,7 +1,7 @@ +# The example code below executes two simple deletes using named bind variables. +# The child records are removed, followed by the parent record. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ # section titled "Extra Fun 2" -# Using the base template, the example code executes two simple deletes using named bind variables. -# The child records are removed, followed by the parent record. require 'oci8' @@ -35,6 +35,7 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original People Data', 'people') get_all_rows('Original Pet Data', 'pets') +# Example code showing two simple deletes using named bind variables. statement = 'delete from lcs_pets where owner = :owner' cursor = con.parse(statement) cursor.bind_param(:owner, 5) @@ -45,6 +46,7 @@ def get_all_rows(label, data_type = 'people') cursor.bind_param(:id, 5) cursor.exec con.commit +# End Example get_all_rows('New People Data', 'people') get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/foreign_keys.rb b/ruby/CRUD-examples/delete/foreign_keys.rb index d39ed852..fe8b8844 100644 --- a/ruby/CRUD-examples/delete/foreign_keys.rb +++ b/ruby/CRUD-examples/delete/foreign_keys.rb @@ -1,8 +1,8 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ -# section titled "Deleting records referenced by Foreign Keys" 1st example -# Using the base template, the example code executes a simple delete using named bind variables. +# The example code below executes a simple delete using named bind variables. # When following the tutorial with default data this section intentionally throws an error # to demonstrate foreign key functionality. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Deleting records referenced by Foreign Keys" 1st example require 'oci8' @@ -36,11 +36,14 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original People Data', 'people') get_all_rows('Original Pet Data', 'pets') +# Example code showing a simple delete using named bind variables. +# The FK will cause this to throw an error. statement = 'delete from lcs_people where id = :id' cursor = con.parse(statement) cursor.bind_param(:id, 1) cursor.exec con.commit +# End Example get_all_rows('New People Data', 'people') get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/handle_pets.rb b/ruby/CRUD-examples/delete/handle_pets.rb index 06585050..7ae09c31 100644 --- a/ruby/CRUD-examples/delete/handle_pets.rb +++ b/ruby/CRUD-examples/delete/handle_pets.rb @@ -1,7 +1,8 @@ +# The example code below executes two simple deletes using named bind variables. +# The child records are updated to a different parrent, +# followed by deleting the original parent record. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ # section titled "Deleting records referenced by Foreign Keys" 2nd example -# Using the base template, the example code executes two simple deletes using named bind variables. -# The child records are removed, followed by the parent record. require 'oci8' @@ -35,6 +36,7 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original People Data', 'people') get_all_rows('Original Pet Data', 'pets') +# Example code showing how to update existing child records before deleting the parent. statement = 'update lcs_pets set owner = :newOwner where owner = :oldOwner' cursor = con.parse(statement) cursor.bind_param(:newOwner, 2) @@ -46,6 +48,7 @@ def get_all_rows(label, data_type = 'people') cursor.bind_param(:id, 1) cursor.exec con.commit +# End Example get_all_rows('New People Data', 'people') get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/reset_data.rb b/ruby/CRUD-examples/delete/reset_data.rb index cc9b8615..951d1daf 100644 --- a/ruby/CRUD-examples/delete/reset_data.rb +++ b/ruby/CRUD-examples/delete/reset_data.rb @@ -1,10 +1,10 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ -# section titled "Resetting the data" # The following resets the data for use with the update section # For both tables: # Table data is removed. # The identity column is set to start with the id after the starting data. # Using the executemany function an array of starting data is inserted into the table. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Resetting the data" require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database diff --git a/ruby/CRUD-examples/delete/simple.rb b/ruby/CRUD-examples/delete/simple.rb index a12df409..7995cc62 100644 --- a/ruby/CRUD-examples/delete/simple.rb +++ b/ruby/CRUD-examples/delete/simple.rb @@ -1,6 +1,6 @@ +# The example code below executes a simple delete using named bind variables. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ # section titled "Simple delete" -# Using the base template, the example code executes a simple delete using named bind variables. require 'oci8' @@ -33,10 +33,12 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original Data', 'pets') +# Example code showing a simple delete using named bind variables. statement = 'delete from lcs_pets where id = :id' cursor = con.parse(statement) cursor.bind_param(:id, 1) cursor.exec con.commit +# End Example get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/delete/template.rb b/ruby/CRUD-examples/delete/template.rb index e2482522..ffb5fa62 100644 --- a/ruby/CRUD-examples/delete/template.rb +++ b/ruby/CRUD-examples/delete/template.rb @@ -1,6 +1,7 @@ +# The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ # section titled "Boilerplate template" -# The following code is used as the base template for the other examples. require 'oci8' diff --git a/ruby/CRUD-examples/dropDBObjects.sql b/ruby/CRUD-examples/dropDBObjects.sql index ef872788..53fec410 100644 --- a/ruby/CRUD-examples/dropDBObjects.sql +++ b/ruby/CRUD-examples/dropDBObjects.sql @@ -1,5 +1,5 @@ -# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ # The following drops the tables created for the tutorial +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ drop table LCS_PETS / diff --git a/ruby/CRUD-examples/insert/extra_fun_1.rb b/ruby/CRUD-examples/insert/extra_fun_1.rb index 616f059a..c0da8ddd 100644 --- a/ruby/CRUD-examples/insert/extra_fun_1.rb +++ b/ruby/CRUD-examples/insert/extra_fun_1.rb @@ -1,7 +1,7 @@ +# The example code below executes a simple insert using named bind variables. +# The same statement is executed twice each using different bind variable values. # Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Extra Fun 1 & 2" -# Using the base template, the example code executes a simple insert using positional bind variables. -# The same statement is executed twice each using different bind variable values. require 'oci8' @@ -25,6 +25,7 @@ def get_all_rows(label) get_all_rows('Original Data') +# Example code showing how to insert multiple rows with multiple database calls. statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' cursor = con.parse(statement) cursor.bind_param(:name, 'Rob') @@ -37,5 +38,6 @@ def get_all_rows(label) cursor.bind_param(:notes, 'I like monkeys') cursor.exec con.commit +# End Example get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/extra_fun_2.rb b/ruby/CRUD-examples/insert/extra_fun_2.rb index f5116342..ef01b0aa 100644 --- a/ruby/CRUD-examples/insert/extra_fun_2.rb +++ b/ruby/CRUD-examples/insert/extra_fun_2.rb @@ -1,8 +1,8 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ -# section titled "Extra Fun 1 & 2" -# Using the base template, the example code executes a simple insert using positional bind variables. +# The example code below executes a simple insert using named bind variables. # The get_all_rows function is modified to use a second connection to show how the data is seen # by different connections before and after a commit. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Extra Fun 1 & 2" require 'oci8' @@ -24,6 +24,8 @@ def get_all_rows(label, con) get_all_rows('Original Data', con) +# Example code showing how to insert multiple rows with a single database call. +# Extra calls to get_all_rows will demonstrate the state of the data before a commit. statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' cursor = con.parse(statement) cursor.bind_param(:name, 'Suzy') @@ -35,6 +37,7 @@ def get_all_rows(label, con) get_all_rows('Same connection', con) con.commit +# End Example get_all_rows('New connection after commit', con2) get_all_rows('New Data', con) diff --git a/ruby/CRUD-examples/insert/extra_fun_3.rb b/ruby/CRUD-examples/insert/extra_fun_3.rb index f87f3541..728265aa 100644 --- a/ruby/CRUD-examples/insert/extra_fun_3.rb +++ b/ruby/CRUD-examples/insert/extra_fun_3.rb @@ -1,7 +1,7 @@ +# The example code below executes a simple insert using named bind variables. +# Cursor variables are used to accept the insert statement's returning values. # Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Extra Fun 3" -# Using the base template, the example code executes a simple insert using positional bind variables. -# Cursor variables are used to accept the insert statements returning values. require 'oci8' @@ -25,6 +25,7 @@ def get_all_rows(label) get_all_rows('Original Data') +# Example code showing how to work with returning vales from the insert. statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes) returning id, name into :id, :name_out' cursor = con.parse(statement) cursor.bind_param(:name, 'Sandy') @@ -40,5 +41,6 @@ def get_all_rows(label) con.commit printf " Our new id is: %d name: %s\n\n", new_id, name_out +# End Example get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/multiple.rb b/ruby/CRUD-examples/insert/multiple.rb index 6b426c1b..c70727e8 100644 --- a/ruby/CRUD-examples/insert/multiple.rb +++ b/ruby/CRUD-examples/insert/multiple.rb @@ -1,7 +1,7 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# The example code below executes an insert using exec_array to create multiple rows with a single call. +# +# More details can be found in the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Insert more than 1 row" -# Using the base template, the example code executes a simple insert using positional bind variables. -# Using the executemany function an array of data is inserted into the table. require 'oci8' @@ -25,6 +25,7 @@ def get_all_rows(label) get_all_rows('Original Data') +# Example code showing how to insert multiple rows with a single database call. cursor = con.parse('INSERT INTO lcs_people(name, age, notes) VALUES (:name, :age, :notes)') cursor.max_array_size = 2 cursor.bind_param_array(:name, %w[Sandy Suzy]) @@ -33,5 +34,6 @@ def get_all_rows(label) people_row_count = cursor.exec_array con.commit printf " Successfully inserted %d records\n\n", people_row_count +# End Example get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/reset_data.rb b/ruby/CRUD-examples/insert/reset_data.rb index 1c3f8c75..4c50210a 100644 --- a/ruby/CRUD-examples/insert/reset_data.rb +++ b/ruby/CRUD-examples/insert/reset_data.rb @@ -1,10 +1,10 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ -# section titled "Resetting the data" # The following resets the data for use with the update section # For both tables: # Table data is removed. # The identity column is set to start with the id after the starting data. # Using the executemany function an array of starting data is inserted into the table. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Resetting the data" # Query all rows require 'oci8' diff --git a/ruby/CRUD-examples/insert/return_data.rb b/ruby/CRUD-examples/insert/return_data.rb index a4ad5a8a..b5a7b454 100644 --- a/ruby/CRUD-examples/insert/return_data.rb +++ b/ruby/CRUD-examples/insert/return_data.rb @@ -1,8 +1,8 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ -# section titled "Returning data after an insert" -# Using the base template, the example code executes a simple insert using positional bind variables. +# The example code below executes a simple insert using named bind variables. # A cursor variable is used to accept the insert statements returning value. This value is then # used as the parent key value to insert a child record. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Returning data after an insert" require 'oci8' @@ -26,6 +26,7 @@ def get_all_rows(label) get_all_rows('Original Data') +# Example code showing how to work with returning vales from the insert. statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes) returning id into :id' cursor = con.parse(statement) cursor.bind_param(:name, 'Sandy') @@ -56,5 +57,6 @@ def get_all_rows(label) printf " Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2] end printf "\n" +# End Example get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/simple.rb b/ruby/CRUD-examples/insert/simple.rb index ac138179..c48b6168 100644 --- a/ruby/CRUD-examples/insert/simple.rb +++ b/ruby/CRUD-examples/insert/simple.rb @@ -1,6 +1,6 @@ +# The example code below executes a simple insert using named bind variables. # Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ # section titled "Simple insert" -# Using the base template, the example code executes a simple insert using positional bind variables. require 'oci8' @@ -24,6 +24,7 @@ def get_all_rows(label) get_all_rows('Original Data') +# Example code showing a simple insert using named bind variables. statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' cursor = con.parse(statement) cursor.bind_param(:name, 'Sandy') @@ -31,5 +32,6 @@ def get_all_rows(label) cursor.bind_param(:notes, 'I like horses') cursor.exec con.commit +# End Example get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/template.rb b/ruby/CRUD-examples/insert/template.rb index 8ee7762c..88f74b74 100644 --- a/ruby/CRUD-examples/insert/template.rb +++ b/ruby/CRUD-examples/insert/template.rb @@ -1,6 +1,7 @@ +# The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. # Code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ # section titled "Boilerplate template" -# The following code is used as the base template for the other examples. require 'oci8' diff --git a/ruby/CRUD-examples/reset_data.rb b/ruby/CRUD-examples/reset_data.rb index d87038f9..a37515bf 100644 --- a/ruby/CRUD-examples/reset_data.rb +++ b/ruby/CRUD-examples/reset_data.rb @@ -1,5 +1,5 @@ -# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ # The following resets the data to the initial state for the tutorial. +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ # Query all rows require 'oci8' diff --git a/ruby/CRUD-examples/select/bind_variables.rb b/ruby/CRUD-examples/select/bind_variables.rb index 72ce8756..21e216e0 100644 --- a/ruby/CRUD-examples/select/bind_variables.rb +++ b/ruby/CRUD-examples/select/bind_variables.rb @@ -1,13 +1,13 @@ +# The example code below executes a simple query using named bind variables, +# uses fetch to retrieve the data and displays the results. # Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ # section titled "Select specific rows" -# Using the base template, the example code executes a simple query using named bind variables, -# uses fetchall to retrieve the data and displays the results. require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) -# Query for Kim +# Example code showing a simple query using named bind variables. person_name = 'Kim' statement = 'select id, name, age, notes from lcs_people where name=:name' cursor = con.parse(statement) @@ -16,3 +16,4 @@ cursor.fetch do |row| printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] end +# End Example diff --git a/ruby/CRUD-examples/select/extra_fun_1.rb b/ruby/CRUD-examples/select/extra_fun_1.rb index 9f783db6..a2fbd24d 100644 --- a/ruby/CRUD-examples/select/extra_fun_1.rb +++ b/ruby/CRUD-examples/select/extra_fun_1.rb @@ -1,15 +1,17 @@ +# The example code below executes a simple query, uses fetch to retrieve the data +# and displays the results. # Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ # section titled "Extra Fun 1" -# Using the base template, the example code executes a simple query, uses fetchall to retrieve the data -# and displays the results. require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) +# Example code showing a simple query. statement = 'select id, name, age, notes from lcs_people order by age' cursor = con.parse(statement) cursor.exec cursor.fetch do |row| printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] end +# End Example diff --git a/ruby/CRUD-examples/select/extra_fun_2.rb b/ruby/CRUD-examples/select/extra_fun_2.rb index 46d3fe8e..b84c5eba 100644 --- a/ruby/CRUD-examples/select/extra_fun_2.rb +++ b/ruby/CRUD-examples/select/extra_fun_2.rb @@ -1,12 +1,13 @@ +# The example code below executes a simple query using named bind variables, +# uses fetch to retrieve the data and displays the results. # Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ # section titled "Extra Fun 2" -# Using the base template, the example code executes a simple query using named bind variables, -# uses fetchall to retrieve the data and displays the results. require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) +# Example code showing a simple query using named bind variables. statement = 'select id, name, age, notes from lcs_people where age > :age' cursor = con.parse(statement) cursor.bind_param('age', 30) @@ -14,3 +15,4 @@ cursor.fetch do |row| printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] end +# End Example diff --git a/ruby/CRUD-examples/select/simple.rb b/ruby/CRUD-examples/select/simple.rb index 10cce33b..6440c28a 100644 --- a/ruby/CRUD-examples/select/simple.rb +++ b/ruby/CRUD-examples/select/simple.rb @@ -1,13 +1,13 @@ +# The example code below executes a simple query, uses fetch to retrieve the data +# and displays the results. # Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ # section titled "Simple query" -# Using the base template, the example code executes a simple query, uses fetchall to retrieve the data -# and displays the results. require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database con = OCI8.new(connectString) -# Query all rows +# Example code showing a simple query for all rows. con = OCI8.new(connectString) statement = 'select id, name, age, notes from lcs_people' cursor = con.parse(statement) @@ -15,3 +15,4 @@ cursor.fetch do |row| print row end +# End Example diff --git a/ruby/CRUD-examples/select/template.rb b/ruby/CRUD-examples/select/template.rb index c373437c..53f06a02 100644 --- a/ruby/CRUD-examples/select/template.rb +++ b/ruby/CRUD-examples/select/template.rb @@ -1,5 +1,6 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ # The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database diff --git a/ruby/CRUD-examples/update/extra_fun_1.rb b/ruby/CRUD-examples/update/extra_fun_1.rb index 8ba2ae93..1717747f 100644 --- a/ruby/CRUD-examples/update/extra_fun_1.rb +++ b/ruby/CRUD-examples/update/extra_fun_1.rb @@ -1,6 +1,6 @@ +# The example code below executes a simple update using named bind variables. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ # section titled "Extra Fun 1" -# Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' @@ -33,11 +33,13 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original Data') +# Example code showing a simple update using named bind variables. statement = 'update lcs_people set notes = :notes where id = :id' cursor = con.parse(statement) cursor.bind_param(:notes, 'I like cats') cursor.bind_param(:id, 1) cursor.exec con.commit +# End Example get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/extra_fun_2.rb b/ruby/CRUD-examples/update/extra_fun_2.rb index 9c0f9306..511fd57e 100644 --- a/ruby/CRUD-examples/update/extra_fun_2.rb +++ b/ruby/CRUD-examples/update/extra_fun_2.rb @@ -1,7 +1,7 @@ +# The example code below executes a simple update using named bind variables +# and displays the number of affected rows. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ # section titled "Extra Fun 2" -# Using the base template, the example code executes a simple update using positional bind variables -# and displays the number of affected rows. require 'oci8' @@ -34,6 +34,8 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original Data', 'pets') +# Example code showing a simple update using named bind variables +# and displays the number of affected rows. statement = 'update lcs_pets set owner = :newOwner where type = :type and owner != :oldOwner' cursor = con.parse(statement) cursor.bind_param(:newOwner, 2) @@ -41,6 +43,7 @@ def get_all_rows(label, data_type = 'people') cursor.bind_param(:oldOwner, 2) changed = cursor.exec con.commit +# End Example printf "Number of rows updated: %d\n\n", changed diff --git a/ruby/CRUD-examples/update/reset_data.rb b/ruby/CRUD-examples/update/reset_data.rb index bfcc19ab..c9094cb9 100644 --- a/ruby/CRUD-examples/update/reset_data.rb +++ b/ruby/CRUD-examples/update/reset_data.rb @@ -1,10 +1,10 @@ -# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ -# section titled "Resetting the data" # The following resets the data for use with the update section # For both tables: # Table data is removed. # The identity column is set to start with the id after the starting data. # Using the executemany function an array of starting data is inserted into the table. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Resetting the data" require 'oci8' connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database diff --git a/ruby/CRUD-examples/update/simple.rb b/ruby/CRUD-examples/update/simple.rb index 9b42baff..002be13c 100644 --- a/ruby/CRUD-examples/update/simple.rb +++ b/ruby/CRUD-examples/update/simple.rb @@ -1,6 +1,6 @@ +# The example code below executes a simple update using named bind variables. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ # section titled "Simple update" -# Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' @@ -33,11 +33,13 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original Data') +# Example code showing a simple update using named bind variables. statement = 'update lcs_people set age = :age where id = :id' cursor = con.parse(statement) cursor.bind_param(:age, 31) cursor.bind_param(:id, 1) cursor.exec con.commit +# End Example get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/specific_where.rb b/ruby/CRUD-examples/update/specific_where.rb index a8bee84b..48150a60 100644 --- a/ruby/CRUD-examples/update/specific_where.rb +++ b/ruby/CRUD-examples/update/specific_where.rb @@ -1,6 +1,6 @@ +# The example code below executes a simple update using named bind variables. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ # section titled "Make sure your where clause is specific" -# Using the base template, the example code executes a simple update using positional bind variables. require 'oci8' @@ -33,6 +33,7 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original Data', 'pets') +# Example code showing a simple update using named bind variables. statement = 'update lcs_pets set owner = :newOwner where owner = :oldOwner and type = :type' cursor = con.parse(statement) cursor.bind_param(:newOwner, 2) @@ -40,5 +41,6 @@ def get_all_rows(label, data_type = 'people') cursor.bind_param(:type, 'dog') cursor.exec con.commit +# End Example get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/update/template.rb b/ruby/CRUD-examples/update/template.rb index 316b5416..3296b89b 100644 --- a/ruby/CRUD-examples/update/template.rb +++ b/ruby/CRUD-examples/update/template.rb @@ -1,6 +1,7 @@ +# The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ # section titled "Boilerplate template" -# The following code is used as the base template for the other examples. require 'oci8' diff --git a/ruby/CRUD-examples/update/verify_number.rb b/ruby/CRUD-examples/update/verify_number.rb index 4f7d90d4..d3ee4d29 100644 --- a/ruby/CRUD-examples/update/verify_number.rb +++ b/ruby/CRUD-examples/update/verify_number.rb @@ -1,7 +1,7 @@ +# The example code below executes a simple update using named bind variables +# and displays the number of affected rows. # Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ # section titled "Verify the number of affected rows" -# Using the base template, the example code executes a simple update using positional bind variables -# and displays the number of affected rows. require 'oci8' @@ -34,12 +34,15 @@ def get_all_rows(label, data_type = 'people') get_all_rows('Original Data', 'pets') +# Example code showing a simple update using named bind variables +# and displays the number of affected rows. statement = 'update lcs_pets set owner = :newOwner where id = :id' cursor = con.parse(statement) cursor.bind_param(:newOwner, 2) cursor.bind_param(:id, 6) changed = cursor.exec con.commit +# End Example printf "Number of rows updated: %d\n\n", changed