Module: FluidDb2

Defined in:
lib/fluiddb2.rb,
lib/fluiddb2/mock.rb,
lib/fluiddb2/mysql.rb,
lib/fluiddb2/pgsql.rb,
lib/fluiddb2/mysql2.rb,
lib/fluiddb2/sqlite.rb,
lib/fluiddb2/firebird.rb,
lib/fluiddb2/tiny_tds.rb

Overview

FluidDb2

Defined Under Namespace

Classes: Base, ConnectionError, DuplicateKeyError, ExpectedAffectedRowsError, Firebird, IncorrectNumberOfParametersError, Mock, Mysql, Mysql2, NoDataFoundError, ParamTypeNotSupportedError, Pgsql, SQLite, SqlNotMatchedError, TinyTds, TooManyRowsError

Class Method Summary collapse

Class Method Details

.convert_tuple_to_hash(fields, tuple, j) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/fluiddb2.rb', line 107

def self.convert_tuple_to_hash(fields, tuple, j)
  hash = {}
  0.upto(fields.length - 1).each do |i|
    hash[fields[i].to_s] = tuple.getvalue(j, i)
  end

  hash
end

.db(uri) ⇒ Object



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
# File 'lib/fluiddb2.rb', line 22

def self.db(uri)
  uri = URI.parse(uri) if uri.is_a? String

  case uri.scheme
  when 'mysql'
    require 'fluiddb2/mysql'
    return FluidDb2::Mysql.new(uri)
  when 'mysql2'
    require 'fluiddb2/mysql2'
    return FluidDb2::Mysql2.new(uri)
  when 'pgsql'
    require 'fluiddb2/pgsql'
    return FluidDb2::Pgsql.new(uri)
  when 'fb'
    require 'fluiddb2/firebird'
    return FluidDb2::Firebird.new(uri)
  when 'mock'
    require 'fluiddb2/Mock'
    return FluidDb2::Mock.new(uri)
  when 'tinytds'
    require 'fluiddb2/tiny_tds'
    return FluidDb2::TinyTds.new(uri)
  when 'sqlite'
    require 'fluiddb2/sqlite'
    return FluidDb2::SQLite.new(uri)
  else
    abort("Scheme, #{uri.scheme}, not recognised when configuring creating " \
           'db connection')
  end
end

.escape_string(input) ⇒ Object



65
66
67
# File 'lib/fluiddb2.rb', line 65

def self.escape_string(input)
  input.split("'").join("''")
end

.format_to_sql(sql, params = nil) ⇒ Object



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
# File 'lib/fluiddb2.rb', line 69

def self.format_to_sql(sql, params = nil)
  return sql if params.nil? || params.count == 0

  params.each_with_index do |v, idx|
    if v.is_a? String
      v = "'#{escape_string(v)}'"
    elsif v.is_a? DateTime
      v = "'" + v.strftime('%Y-%m-%d %H:%M:%S.%6N %z') + "'"
    elsif v.is_a? Time
      v = "'" + v.strftime('%Y-%m-%d %H:%M:%S.%6N %z') + "'"
    elsif v.is_a? Date
      v = "'#{v}'"
    elsif v.is_a? Numeric
      v = v.to_s
    elsif v.is_a? TrueClass
      v = 'true'
    elsif v.is_a? FalseClass
      v = 'false'
    elsif v.nil?
      v = 'NULL'
    else
      fail ParamTypeNotSupportedError,
           "Name of unknown param type, #{v.class.name}, for sql, #{sql}"
    end
    params[idx] = v
  end

  sql_out = splice_sql(sql, params)
  if @verbose == true
    puts self.class.name
    puts sql
    puts params.join(',')
    puts sql_out
  end

  sql_out
end

.splice_sql(sql, params) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluiddb2.rb', line 53

def self.splice_sql(sql, params)
  fail IncorrectNumberOfParametersError if params.length != sql.count('?')

  sql_out = ''
  sql.split('?').each_with_index do |s, idx|
    sql_out += s
    sql_out += params[idx] unless params[idx].nil?
  end

  sql_out
end