Class: FluidDb2::Mysql

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

Overview

Mysql

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



18
19
20
# File 'lib/fluiddb2/mysql.rb', line 18

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

def connect
  uri = @uri
  database = uri.path.sub('/', '')

  @connection = ::Mysql.new uri.host, uri.user, uri.password, database, nil, nil, ::Mysql::CLIENT_FOUND_ROWS
end

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



75
76
77
78
79
80
81
82
# File 'lib/fluiddb2/mysql.rb', line 75

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
    fail ExpectedAffectedRowsError, "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{@connection.affected_rows}"
  end
end

#insert(sql, params) ⇒ Object



84
85
86
87
# File 'lib/fluiddb2/mysql.rb', line 84

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

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fluiddb2/mysql.rb', line 22

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

  case results.num_rows
  when -1
    fail FluidDb2::ConnectionError
  when 0
    fail FluidDb2::NoDataFoundError
  when 1
    r = results.fetch_hash
    return r
  else
    fail FluidDb2::TooManyRowsError
  end
end

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fluiddb2/mysql.rb', line 59

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

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

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluiddb2/mysql.rb', line 39

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

  case results.num_rows
  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