Class: MiniSql::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_sql/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(raw_connection, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mini_sql/connection.rb', line 6

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

Instance Method Details

#build(sql) ⇒ Object



56
57
58
# File 'lib/mini_sql/connection.rb', line 56

def build(sql)
  Builder.new(self, sql)
end

#escape_string(str) ⇒ Object

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/mini_sql/connection.rb', line 68

def escape_string(str)
  raise NotImplementedError, "must be implemented by child connection"
end

#exec(sql, *params) ⇒ Object

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/mini_sql/connection.rb', line 52

def exec(sql, *params)
  raise NotImplementedError, "must be implemented by child connection"
end

#query(sql, *params) ⇒ Object

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/mini_sql/connection.rb', line 32

def query(sql, *params)
  raise NotImplementedError, "must be implemented by child connection"
end

#query_decorator(sql, *params) ⇒ Object

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/mini_sql/connection.rb', line 40

def query_decorator(sql, *params)
  raise NotImplementedError, "must be implemented by child connection"
end

#query_each(sql, *params) ⇒ Object

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/mini_sql/connection.rb', line 44

def query_each(sql, *params)
  raise NotImplementedError, "must be implemented by child connection"
end

#query_each_hash(sql, *params) ⇒ Object

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/mini_sql/connection.rb', line 48

def query_each_hash(sql, *params)
  raise NotImplementedError, "must be implemented by child connection"
end

#query_hash(sql, *params) ⇒ Object

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/mini_sql/connection.rb', line 36

def query_hash(sql, *params)
  raise NotImplementedError, "must be implemented by child connection"
end

#query_single(sql, *params) ⇒ Object

Returns a flat array containing all results. Note, if selecting multiple columns array will be flattened

Parameters:

  • sql (String)

    the query to run

  • params (Array or Hash)

    , params to apply to query

Returns:

  • (Object)

    a flat array containing all results

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/mini_sql/connection.rb', line 28

def query_single(sql, *params)
  raise NotImplementedError, "must be implemented by child connection"
end

#to_sql(sql, *params) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/mini_sql/connection.rb', line 60

def to_sql(sql, *params)
  if params.empty?
    sql
  else
    param_encoder.encode(sql, *params)
  end
end