-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_sql_object.rb
More file actions
executable file
·121 lines (98 loc) · 2.24 KB
/
01_sql_object.rb
File metadata and controls
executable file
·121 lines (98 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require_relative 'db_connection'
require 'active_support/inflector'
# NB: the attr_accessor we wrote in phase 0 is NOT used in the rest
# of this project. It was only a warm up.
class SQLObject
def self.columns
return @cols if @cols
all = DBConnection.execute2(<<-SQL)
SELECT
*
FROM
#{table_name}
SQL
@cols = all[0].map {|item| item.to_sym}
end
def self.finalize!
self.columns.each do |col|
define_method(col) do
attributes[col]
end
define_method("#{col}=") do |arg|
attributes[col] = arg
end
end
end
def self.table_name=(table_name)
@table_name = table_name
end
def self.table_name
@table_name ||= (self.name == "Human" ? "humans" : self.name.tableize)
end
def self.all
all = DBConnection.execute(<<-SQL)
SELECT
*
FROM
#{table_name}
SQL
parse_all(all)
end
def self.parse_all(results)
results.map {|hash| self.new(hash)}
end
def self.find(id)
result = DBConnection.execute(<<-SQL, id)
SELECT
*
FROM
#{table_name}
WHERE id = ?
SQL
return nil if result.empty?
self.new(result.first)
end
def initialize(params = {})
params.each do |key, value|
if self.class.columns.include?(key.to_sym)
send("#{key}=", value)
else
raise "unknown attribute '#{key}'"
end
end
end
def attributes
@attributes ||= {}
end
def attribute_values
attributes.values
end
def insert
cols = attributes.keys
col_names = cols.join(', ')
ques_marks = (['?'] * cols.length).join(', ')
result = DBConnection.execute(<<-SQL, *attribute_values)
INSERT INTO
#{self.class.table_name} (#{col_names})
VALUES
(#{ques_marks})
SQL
# self.send(:id=, DBConnection.last_insert_row_id)
self.id = DBConnection.last_insert_row_id
end
def update
cols = attributes.keys
col_names = cols.map {|col| "#{col} = ?"}.join(', ')
result = DBConnection.execute(<<-SQL, *attribute_values, self.id)
UPDATE
#{self.class.table_name}
SET
#{col_names}
WHERE
id = ?
SQL
end
def save
self.id ? self.update : self.insert
end
end