Created
August 14, 2020 08:01
-
-
Save rodloboz/18fc83d51d2f2bca03ba3e1135b25aec to your computer and use it in GitHub Desktop.
SQL CRUD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gem/Library to allow Ruby to interact with the DB | |
require 'sqlite3' | |
require 'byebug' | |
require_relative 'doctor' | |
# Create a DB connection | |
DB = SQLite3::Database.new("doctors.db") | |
#[[1, "John Smith", 39, "Anesthesiologist"], [2, "Emma Reale", 31, "Cardiologist"]] | |
# We want our individual records/rows to be Hashes | |
DB.results_as_hash = true | |
# Let's create an instance of Doctor | |
# with the first row: id = 1 | |
row = DB.execute("SELECT * FROM doctors WHERE id = 1") | |
.first | |
# Our Doctor initiaze expects hash with symbol keys | |
# Convert keys to symbols | |
attrs = row.transform_keys(&:to_sym) | |
# Create a Doctor instance | |
doctor = Doctor.new(attrs) | |
p doctor | |
# Let's read all instances of Doctor | |
doctors = Doctor.all | |
p doctors | |
p doctors.first.name | |
# Let's read Doctor with id 2 | |
doctor = Doctor.find(2) | |
puts "My name is #{doctor.name}" | |
# Let's create a new Doctor | |
doctor = Doctor.create(name: "Foo", age: 45) | |
# ActiveRecord => on Moday/Tuesday | |
# Doctor.where(id: ids).find_each(&:destroy) | |
# Doctor.destroy_all | |
# doctor.update | |
# doctor.delete | |
puts "Done!" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MVC - M model | |
# Given a DB row | |
# attrs = {"id"=>1, "name"=>"John Smith", "age"=>39, "specialty"=>"Anesthesiologist"} | |
# Doctir.new(attrs) | |
class Doctor | |
attr_accessor :id, :name, :age, :specialty | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@name = attributes[:name] | |
@age = attributes[:age] | |
@specialty = attributes[:specialty] | |
end | |
# Class methods: | |
# Doctor.all => return all instances of Doctor | |
def self.all | |
# rows is an Array of hashes with Doctor data form the DB | |
rows = DB.execute("SELECT * FROM doctors") | |
# Converts rows to an array of Doctor instances | |
rows.map do |row| | |
# Convert string keys to symbol keys | |
attrs = row.transform_keys(&:to_sym) | |
# Doctor.new(attrs) => won't work | |
# self.new(attrs) => self is Doctor | |
new(attrs) | |
end | |
end | |
def self.find(id) | |
row = DB.execute("SELECT * FROM doctors WHERE id = #{id}") | |
.first | |
# convert string keys to symbol keys | |
attrs = row.transform_keys(&:to_sym) | |
# Doctor.new(attrs) => won't work | |
# self.new(attrs) => self is Doctor | |
new(attrs) | |
end | |
# Doctor.create(name: "Doogie Houser", age: 18) | |
def self.create(attributes = {}) | |
doctor = new(attributes) | |
doctor.save | |
end | |
# Update the instance varitable | |
# Also save it to the DB | |
def update(attributes = {}) | |
@name = attributes[:name] || @name | |
@age = attributes[:age] || @age | |
@specialty = attributes[:specialty] || @specialty | |
save | |
end | |
def save | |
if @id.nil? | |
# create | |
else | |
# update | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment