Class: FluidDb::Mysql2

Inherits:
Base
  • Object
show all
Defined in:
lib/FluidDb/Mysql2.rb

Instance Attribute Summary

Attributes inherited from Base

#connection, #verbose

Instance Method Summary collapse

Methods inherited from Base

#Begin, #Commit, #Rollback, #convertTupleToHash, #escape_string, #format_to_sql, #initialize, #reconnect, #splice_sql, #verboseLog

Constructor Details

This class inherits a constructor from FluidDb::Base

Instance Method Details

#closeObject



23
24
25
# File 'lib/FluidDb/Mysql2.rb', line 23

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
19
20
21
# File 'lib/FluidDb/Mysql2.rb', line 11

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

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



102
103
104
105
106
107
108
109
110
# File 'lib/FluidDb/Mysql2.rb', line 102

def execute( sql, params=[], expected_affected_rows=nil )
    sql = self.format_to_sql( sql, params )
    @connection.query( sql );
    
    if !expected_affected_rows.nil? and
        @connection.affected_rows != expected_affected_rows then
        raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{@connection.affected_rows}")
    end
end

#insert(sql, params) ⇒ Object



113
114
115
116
# File 'lib/FluidDb/Mysql2.rb', line 113

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

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



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/FluidDb/Mysql2.rb', line 27

def queryForArray( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    results = @connection.query(sql)
    
    #        if ( $result === false ) then
    #    $message = pg_last_error( $this->connection );
    #    throw new Fluid_ConnectionException( $message );
    #end
    
    case results.count
        when -1
        raise FluidDb::ConnectionError.new
        when 0
        raise FluidDb::NoDataFoundError.new
        when 1
        r=nil;
        results.each do |row|
            r=row
        end
        return r
        else
        raise FluidDb::TooManyRowsError.new
    end
    
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/FluidDb/Mysql2.rb', line 80

def queryForResultset( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    results = @connection.query(sql)
    
    #        if ( $result === false ) then
    #    $message = pg_last_error( $this->connection );
    #    throw new Fluid_ConnectionException( $message );
    #end
    
    case results.count
        when -1
        raise FluidDb::ConnectionError.new
        else
        list = Array.new
        results.each do |row|
            list.push row
        end
        
        return list
    end
end

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/FluidDb/Mysql2.rb', line 53

def queryForValue( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    results = @connection.query(sql, :as => :array)
    
    #        if ( $result === false ) then
    #    $message = pg_last_error( $this->connection );
    #    throw new Fluid_ConnectionException( $message );
    #end
    
    case results.count
        when -1
        raise FluidDb::ConnectionError.new
        when 0
        raise FluidDb::NoDataFoundError.new
        when 1
        r=nil;
        results.each do |row|
            r=row
        end
        return r[0]
        else
        raise FluidDb::TooManyRowsError.new
    end
    
end