Class: FluidDb::Base

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

Direct Known Subclasses

Firebird, Mock, Mysql, Mysql2, Pgsql, SQLite, TinyTds

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Base

Constructor.

Parameters:

  • uri (String)

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/FluidDb.rb', line 36

def initialize(uri)
    if uri.kind_of? String then
        @uri = URI.parse( uri )
        else
        @uri = uri
    end
    
    self.connect
    
    @verbose = !ENV["VERBOSE"].nil?
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



27
28
29
# File 'lib/FluidDb.rb', line 27

def connection
  @connection
end

#verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



26
27
28
# File 'lib/FluidDb.rb', line 26

def verbose=(value)
  @verbose = value
end

Instance Method Details

#BeginObject

Transaction Semantics



168
169
170
# File 'lib/FluidDb.rb', line 168

def Begin
    @connection.execute( "BEGIN", [] )
end

#closeObject

Raises:

  • (NotImplementedError)


125
126
127
# File 'lib/FluidDb.rb', line 125

def close
    raise NotImplementedError.new("You must implement 'close'.")
end

#CommitObject

Transaction Semantics



173
174
175
# File 'lib/FluidDb.rb', line 173

def Commit
    @connection.execute( "COMMIT", [] )
end

#connectObject

Raises:

  • (NotImplementedError)


121
122
123
# File 'lib/FluidDb.rb', line 121

def connect
    raise NotImplementedError.new("You must implement 'connect'.")
end

#convertTupleToHash(fields, tuple, j) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/FluidDb.rb', line 112

def convertTupleToHash( fields, tuple, j )
    hash = Hash.new
    0.upto( fields.length-1 ).each do |i|
        hash[fields[i].to_s] = tuple.getvalue(j, i)
    end
    
    return hash
end

#escape_string(input) ⇒ Object



67
68
69
# File 'lib/FluidDb.rb', line 67

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

#execute(sql, params, expected_affected_rows) ⇒ Object

Execute an insert, update or delete, then check the impact that statement has on the data.

Parameters:

  • sql (String)

    The SELECT statement to run

  • parama (Array)

    The parameters to be added to the sql query. Ruby types are used to determine formatting and escaping.

  • expected_affected_rows (String)

    The number of rows that should have been updated.

Raises:

  • (NotImplementedError)


163
164
165
# File 'lib/FluidDb.rb', line 163

def execute( sql, params, expected_affected_rows )
    raise NotImplementedError.new("You must implement 'execute'.")
end

#format_to_sql(sql, params = nil) ⇒ Object



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
106
107
108
109
110
# File 'lib/FluidDb.rb', line 71

def format_to_sql( sql, params=nil )
    if params.nil? || params.count == 0 then
        return sql
    end
    
    params.each_with_index do |v, idx|
        if v.kind_of? String then
            v = "'" + self.escape_string( v ) + "'"
            #v = "'" + v.sub( "'", "\'" ) + "'"
        elsif v.is_a? DateTime then
            v = "'" + v.strftime( "%Y-%m-%d %H:%M:%S.%6N %z" ) + "'"
        elsif v.is_a? Time then
            v = "'" + v.strftime( "%Y-%m-%d %H:%M:%S.%6N %z" ) + "'"
        elsif v.kind_of? Date then
            v = "'" + v.to_s + "'"
        elsif v.is_a? Numeric then
            v = v.to_s
        elsif v.is_a? TrueClass then
            v = "true"
        elsif v.is_a? FalseClass then
            v = "false"
        elsif v.nil? then
            v = 'NULL'
        else
            raise ParamTypeNotSupportedError.new( "Name of unknown param type, #{v.class.name}, for sql, #{sql}" )
        end
        params[idx] = v
    end
    
    sql_out = self.splice_sql( sql, params )
    if @verbose == true then
        puts self.class.name
        puts sql
        puts params.join(",")
        puts sql_out
        #puts "#{self.class.name}\n#{sql}\n#{params.join(',')\n#{sql_out}}"
    end
    
    return sql_out
end

#queryForArray(sql, params) ⇒ Object

Return a single row from the database, given the sql parameter. Throws an error for no data. Throws an error for more than 1 row

Parameters:

  • sql (String)

    The SELECT statement to run

  • parama (Array)

    The parameters to be added to the sql query. Ruby types are used to determine formatting and escaping.

Raises:

  • (NotImplementedError)


140
141
142
# File 'lib/FluidDb.rb', line 140

def queryForArray( sql, params )
    raise NotImplementedError.new("You must implement 'queryForArray'.")
end

#queryForResultset(sql, params) ⇒ Object

Raises:

  • (NotImplementedError)


154
155
156
# File 'lib/FluidDb.rb', line 154

def queryForResultset( sql, params )
    raise NotImplementedError.new("You must implement 'queryForResultset'.")
end

#queryForValue(sql, params) ⇒ Object

Return a single value is returned from a single row from the database, given the sql parameter. Throws an error for no data. Throws an error for more than 1 row

Parameters:

  • sql (String)

    The SELECT statement to run

  • parama (Array)

    The parameters to be added to the sql query. Ruby types are used to determine formatting and escaping.

Raises:

  • (NotImplementedError)


150
151
152
# File 'lib/FluidDb.rb', line 150

def queryForValue( sql, params )
    raise NotImplementedError.new("You must implement 'queryForValue'.")
end

#reconnectObject



129
130
131
132
# File 'lib/FluidDb.rb', line 129

def reconnect
    self.close
    self.connect
end

#RollbackObject

Transaction Semantics



178
179
180
# File 'lib/FluidDb.rb', line 178

def Rollback
    @connection.execute( "ROLLBACK", [] )
end

#splice_sql(sql, params) ⇒ Object



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

def splice_sql( sql, params )
    
    if params.length != sql.count( "?" ) then
        raise IncorrectNumberOfParametersError.new
    end

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

#verboseLog(string) ⇒ Object



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

def verboseLog( string )
    puts string if @verbose == true
end