-
Notifications
You must be signed in to change notification settings - Fork 18
/
connection.rb
73 lines (60 loc) · 2.29 KB
/
connection.rb
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
# frozen_string_literal: true
module MiniSql
class Connection
def self.get(raw_connection, options = {})
if (defined? ::PG::Connection) && (PG::Connection === raw_connection)
Postgres::Connection.new(raw_connection, options)
elsif (defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && (ActiveRecord::ConnectionAdapters::PostgreSQLAdapter === raw_connection)
ActiveRecordPostgres::Connection.new(raw_connection, options)
elsif (defined? ::ArJdbc)
Postgres::Connection.new(raw_connection, options)
elsif (defined? ::SQLite3::Database) && (SQLite3::Database === raw_connection)
Sqlite::Connection.new(raw_connection, options)
elsif (defined? ::Mysql2::Client) && (Mysql2::Client === raw_connection)
Mysql::Connection.new(raw_connection, options)
else
raise ArgumentError, 'unknown connection type!'
end
end
# Returns a flat array containing all results.
# Note, if selecting multiple columns array will be flattened
#
# @param sql [String] the query to run
# @param params [Array or Hash], params to apply to query
# @return [Object] a flat array containing all results
def query_single(sql, *params)
raise NotImplementedError, "must be implemented by child connection"
end
def query(sql, *params)
raise NotImplementedError, "must be implemented by child connection"
end
def query_hash(sql, *params)
raise NotImplementedError, "must be implemented by child connection"
end
def query_decorator(sql, *params)
raise NotImplementedError, "must be implemented by child connection"
end
def query_each(sql, *params)
raise NotImplementedError, "must be implemented by child connection"
end
def query_each_hash(sql, *params)
raise NotImplementedError, "must be implemented by child connection"
end
def exec(sql, *params)
raise NotImplementedError, "must be implemented by child connection"
end
def build(sql)
Builder.new(self, sql)
end
def to_sql(sql, *params)
if params.empty?
sql
else
param_encoder.encode(sql, *params)
end
end
def escape_string(str)
raise NotImplementedError, "must be implemented by child connection"
end
end
end