Class: FluidDb::TinyTds

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

Instance Attribute Summary

Attributes inherited from Base

#connection, #verbose

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from FluidDb::Base

Instance Method Details

#closeObject



63
64
65
# File 'lib/FluidDb/TinyTds.rb', line 63

def close
    @connection.close
end

#connectObject

Connect to Db.

Parameters:

  • uri (String)

    a location for the resource to which we will attach, eg tinytds://<user>:<pass>@<dataserver>/<database>



12
13
14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
# File 'lib/FluidDb/TinyTds.rb', line 12

def connect()
    uri = @uri

    dataserver = uri.host
    database = uri.path.sub( "/", "" )
    username = URI.unescape( uri.user )
    password = uri.password

    
    if dataserver == "" ||
        database == "" then
        raise "*** You need to specify both a dataserver and a database for the tinytds driver. Expected format: tinytds://<user>:<pass>@<dataserver>/<database>\n" +
        "*** The specified dataserver should have an entry in /etc/freetds/freetds.conf"
    end
    
    if username == "" ||
        password == "" then
        puts "*** Warning - you will normally need to specify both a username and password for the tinytds driver to work correctly."
    end
    
    hash = Hash[:username, username, :password, password, :database, database, :dataserver, dataserver]
    failOverDataServer = nil
    if !uri.query.nil? then
        cgi = CGI.parse( uri.query )
        hash[:timeout] = cgi["timeout"][0].to_i if cgi.has_key?( "timeout" )
        
        failOverDataServer = cgi["failoverdataserver"][0] if cgi.has_key?( "failoverdataserver" )
    end

    begin
        @connection = ::TinyTds::Client.new( hash )
        rescue ::TinyTds::Error=>e
    #Message for an incorrect password,
        #Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
    #Message for unavailable db
        #Cannot open user default database. Login failed.
        if e.message == "Cannot open user default database. Login failed." &&
            !failOverDataServer.nil? then
            hash[:dataserver] = failOverDataServer
            @connection = ::TinyTds::Client.new( hash )
        else
            raise e
        end
    end
    
    if !@connection.active? then
        raise "Unable to connect to the database"
    end
    
end

#escape_string(input) ⇒ Object



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

def escape_string( input )
    return @connection.escape( input )
end

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/FluidDb/TinyTds.rb', line 120

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

#insert(sql, params) ⇒ Object



131
132
133
134
135
# File 'lib/FluidDb/TinyTds.rb', line 131

def insert( sql, params )
    raise "Pgsql uses SEQUENCES, so possibly easier to use 2 executes"
    #            self.execute( sql, params )
    #return @connection.last_id
end

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/FluidDb/TinyTds.rb', line 71

def queryForArray( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    results = @connection.execute(sql)
    
    count = 0
    tuple = ""
    results.each do |row|
        count = count + 1
        raise FluidDb::TooManyRowsError.new if count > 1
        
        tuple = row
    end
    
    raise FluidDb::NoDataFoundError.new if count == 0
    
    return tuple
end

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/FluidDb/TinyTds.rb', line 107

def queryForResultset( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    results = @connection.execute(sql)
    
    list = Array.new
    results.each do |row|
        list << row
    end
    
    return list
end

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/FluidDb/TinyTds.rb', line 89

def queryForValue( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    results = @connection.execute(sql)
    
    count = 0
    value = ""
    results.each do |row|
        count = count + 1
        raise FluidDb::TooManyRowsError.new if count > 1
        
        value = row[results.fields[0]]
    end
    
    raise FluidDb::NoDataFoundError.new if count == 0
    
    return value
end