-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_class.rb
More file actions
60 lines (48 loc) · 1.28 KB
/
base_class.rb
File metadata and controls
60 lines (48 loc) · 1.28 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
require 'pg'
class Base
DB_NAME = 'Users'
DB_USER = 'abrakad_22'
class << self
def connection
PG.connect :dbname => "#{DB_NAME}" , :user => "#{DB_USER}"
end
def add(**options)
query = "INSERT INTO #{table_name} VALUES("
options.each{ |k,v|
query = query << "'#{v}'" <<','}
query = query.chomp(",")
connection.exec "#{query<<')'}"
close_connection
end
def select(logic_operation=nil, **options)
query = "SELECT * FROM #{table_name} WHERE"
options.each{ | k,v |
query = query << " #{k}" << " = " << "'#{v}'" << " #{logic_operation}" }
query = query.chomp(logic_operation)
return connection.exec "#{query}"
close_connection
end
def update(name, **options )
query = "UPDATE #{table_name} SET"
options.each{ | k,v |
query = query << " #{k}" << " = " << "'#{v}'" << " , " }
query = query.chomp(' , ')
query = query << "WHERE "<< "name='#{name}'"
connection.exec "#{query}"
close_connection
end
def delete(**options)
query = "DELETE FROM #{table_name} WHERE"
options.each{ | k,v |
query = query << " #{k}" << " = " << "'#{v}'"}
response = connection.exec "#{query}"
close_connection
end
def close_connection
connection.close
end
def table_name
self.name << 's'
end
end
end