Class: FluidDb2::Mysql2

Inherits:
Base
  • Object
show all
Defined in:
lib/fluiddb2/mysql2.rb

Overview

Mysql2

Instance Attribute Summary

Attributes inherited from Base

#connection, #verbose

Instance Method Summary collapse

Methods inherited from Base

#begin, #commit, #initialize, #reconnect, #rollback, #verbose_log

Constructor Details

This class inherits a constructor from FluidDb2::Base

Instance Method Details

#closeObject



20
21
22
# File 'lib/fluiddb2/mysql2.rb', line 20

def close
  @connection.close
end

#connectObject

Connect to Db.

Parameters:

  • uri (String)

    a location for the resource to which we will attach, eg mysql://user:[email protected]/foo



11
12
13
14
15
16
17
18
# File 'lib/fluiddb2/mysql2.rb', line 11

def connect
  uri = @uri
  @connection = ::Mysql2::Client.new(:host => uri.host,
                                     :database => uri.path.sub('/', ''),
                                     :username => uri.user,
                                     :password => uri.password,
                                     :flags => ::Mysql2::Client::FOUND_ROWS)
end

#execute(sql, params = [], expected_affected_rows = nil) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/fluiddb2/mysql2.rb', line 80

def execute(sql, params = [], expected_affected_rows = nil)
  sql = FluidDb2.format_to_sql(sql, params)
  @connection.query(sql)

  if !expected_affected_rows.nil? && @connection.affected_rows != expected_affected_rows
    raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{@connection.affected_rows}")
  end
end

#insert(sql, params) ⇒ Object



89
90
91
92
# File 'lib/fluiddb2/mysql2.rb', line 89

def insert(sql, params)
  execute(sql, params)
  @connection.last_id
end

#query_for_array(sql, params = []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fluiddb2/mysql2.rb', line 24

def query_for_array(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.query(sql)

  case results.count
  when -1
    fail FluidDb2::ConnectionError
  when 0
    fail FluidDb2::NoDataFoundError
  when 1
    r = nil
    results.each do |row|
      r = row
    end
    return r
  else
    fail FluidDb2::TooManyRowsError
  end
end

#query_for_resultset(sql, params = []) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluiddb2/mysql2.rb', line 64

def query_for_resultset(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.query(sql)

  case results.count
  when -1
    fail FluidDb2::ConnectionError
  else
    list = []
    results.each do |row|
      list.push row
    end
    return list
  end
end

#query_for_value(sql, params = []) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fluiddb2/mysql2.rb', line 44

def query_for_value(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.query(sql, :as => :array)

  case results.count
  when -1
    fail FluidDb2::ConnectionError
  when 0
    fail FluidDb2::NoDataFoundError
  when 1
    r = nil
    results.each do |row|
      r = row
    end
    return r[0]
  else
    fail FluidDb2::TooManyRowsError
  end
end