Skip to content

Instantly share code, notes, and snippets.

@rskelley9
Created September 12, 2013 01:47
Show Gist options
  • Save rskelley9/6532269 to your computer and use it in GitHub Desktop.
Save rskelley9/6532269 to your computer and use it in GitHub Desktop.
require 'sqlite3'
require_relative 'setup_revised'
class Contact
attr_accessor :first_name, :last_name, :company, :phone, :email
def initialize(hash)
@id = hash[:id]
@first_name = hash[:first_name]
@last_name = hash[:last_name]
@company = hash[:company]
@phone = hash[:phone]
@email = hash[:email]
end
def save
$db.execute(
<<-SQL
INSERT INTO contacts
(first_name, last_name, company, phone, email)
VALUES
(?,?,?,?,?);
SQL
)
end
#each contact is a row in results array
def self.all
results = $db.execute("SELECT * FROM contacts")
results_hash = results.map do |contact|
{
:first_name => contact[1],
:last_name => contact[2],
:company => contact[3],
:phone => contact[4],
:email => contact[5]
}
results_hash
end
end
# def self.select_all
# results = []
# $db.results_as_hash = true
# $db.execute("SELECT * FROM students") do |row|
# results << Students.new(row)
# end
# results
# end
def self.all_where_condition(condition, arguments)
results = $db.execute("SELECT * FROM contacts WHERE {condition}", arguments)
results.map do |contact|
attributes = {
:first_name => contact[1],
:last_name => contact[2],
:company => contact[3],
:phone => contact[4],
:email => contact[5]
}
end
end
def change_column_value!(field, value, new_value)
$db.execute("CASE WHEN #{field} = #{value} THEN #{new_value}")
end
def self.update!(value, field, condition)
$db.execute("UPDATE contacts SET #{field} = #{new_value} VALUES where #{condition};")
end
# def self.delete!(condition, *arguments)
# $db.execute("DELETE FROM contacts WHERE #{condition}", arguments)
# end
def self.delete_by_id!(id)
$db.execute("DELETE FROM contacts WHERE id = #{id}")
end
def self.delete(condition, *arguments)
query = "DELETE FROM contacts WHERE #{condition}"
$db.execute(query, arguments)
end
def self.where_name(name, *arguments)
results = $db.execute("SELECT * FROM contacts WHERE #{name} = ", arguments)
results.map do |contact|
attributes = {
:first_name => contact[1],
:last_name => contact[2],
:company => contact[3],
:phone => contact[4],
:email => contact[5]
}
end
end
end
class Group
attr_reader :groupname, :id
def initialize(hash)
@group_id = hash[:id]
@group_name = hash[:group_name]
end
def self.update!(condition, new_name)
query = "UPDATE groups SET group_name = '#{new_name}' WHERE #{condition}"
$db.execute(query)
end
def self.delete!(condition, *arguments)
query = "DELETE FROM groups WHERE #{condition}"
$db.execute(query, arguments)
end
end
class Groups_Contacts
# def self.all
# contact_id_all = $db.execute("SELECT id FROM contacts")
# groupd_id_all = $db.execute("SELECT id from groups")
# contact_id_all.map do |contact|
# {contact =>}
# results_hash = results.map do |contact|
# {
# :first_name => contact[1],
# :last_name => contact[2],
# :company => contact[3],
# :phone => contact[4],
# :email => contact[5]
# }
# results_hash
# end
#add one link between a contact and a group into groups_contacts.
def add_to_groups_contacts(first, last, group_name)
contact_id = db.execute("SELECT id FROM contacts WHERE first_name = ?
AND last_name = ?", first, last).flatten[0]
group_id = db.execute("SELECT id FROM groups WHERE group_name = ?", group_name)
.flatten[0]
db.execute("INSERT INTO groups_contacts (contact_id, group_id, created_at,
updated_at)
VALUES (?, ?, ?, ?)", contact_id, group_id, DateTime.now.to_s,
DateTime.now.to_s)
end
end
$contacts = []
$one_input = {:first_name => "Steve", :last_name => "mark", :company => "kep", :phone => "565464554654", :email => "rgeggregreerg"}
$all_input = [{:first_name => "Steve", :last_name => "mark", :company => "kefp", :phone => "56546f4554654", :email => "rgeggregreerg"},
{:first_name => "Steve", :last_name => "mark", :company => "ke1p", :phone => "565464522654", :email => "rge@eerg"},
{:first_name => "Stave", :last_name => "mrak", :company => "k1ep", :phone => "56546455334654", :email => "rgegg123reerg"},
{:first_name => "Stove", :last_name => "maik", :company => "k2ep", :phone => "565467654", :email => "rg123egreerg"},
{:first_name => "Steev", :last_name => 'maek', :company => "joob", :phone => "123656444", :email => "[email protected]"}]
def my_address_book
$all_input.each do |row|
$contacts << Contact.new(row)
end
end
puts address_book
puts "What's the name of of my first contact?"
puts "It's #{$contacts[0].first_name}"
puts "I can create a new contact object with name 'Alex'"
contact = Contact.new({:first_name => "Alex"})
puts "poof I just created Alex. Check it out..."
puts "My first contact is #{contact.first_name}."
puts contact.first_name == "Alex"
puts $contacts[0].first_name == "Steve"
require 'sqlite3'
$db = SQLite3::Database.new("addressbook.db")
module AddressBook
def self.setup
$db.execute(
<<-SQL
CREATE TABLE contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(20),
last_name VARCHAR(20),
company VARCHAR(20),
phone VARCHAR(15),
email VARCHAR(50)
);
SQL
)
$db.execute(
<<-SQL
CREATE TABLE groups (
id INTEGER PRIMARY KEY AUTOINCREMENT
group_name, VARCHAR(30)
);
SQL
)
$db.execute(
<<-SQL
CREATE TABLE groups_contacts (
id INTEGER
group_id INTEGER
contact_id INTEGER
FOREIGN KEY (group_id)
FOREIGN KEY (contact_id)
);
SQL
)
end
def self.seed
$db.execute(
<<-SQL
INSERT INTO contacts
(first_name, last_name, company, phone, email)
VALUES
('Steve', 'Stevenson', BlueCross, '994-232-3333', '[email protected]');
SQL
)
$db.execute(
<<-SQL
INSERT INTO groups
(group_name)
VALUES
('Pets')
('Work')
('The Steves');
SQL
)
$db.execute(
<<-SQL
INSERT INTO groups_contacts
(id, group_id)
VALUES
(1, 2)
(2, 1)
(2, 2);
SQL
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment