rdbi-driver-odbc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ RDBI-DRIVER-ODBC 0.1.0
2
+ ======================
3
+
4
+ Features
5
+ --------
6
+ - Initial release. Please experiment and report and bugs you find.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Shane Emmons
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ ODBC driver for RDBI
2
+ ====================
3
+
4
+ This gem gives you the ability to query ODBC connections with RDBI.
5
+
6
+ Usage
7
+ -----
8
+
9
+ > gem install rdbi-driver-odbc
10
+ > irb
11
+ > require 'rdbi-driver-odbc'
12
+ > dbh = RDBI.connect :ODBC, :db => "MY_DSN", :user => "USERNAME",
13
+ * :password => "PASSWORD"
14
+ > rs = dbh.execute "SELECT * FROM MY_TABLE"
15
+ > rs.as(:Struct).fetch(:first)
16
+
17
+
18
+ Copyright
19
+ ---------
20
+
21
+ Copyright (c) 2010 Shane Emmons. See LICENSE for details.
@@ -0,0 +1,2 @@
1
+ require 'rdbi'
2
+ require 'rdbi/driver/odbc'
@@ -0,0 +1,170 @@
1
+ require 'rdbi'
2
+ require 'rubygems'
3
+ gem 'ruby-odbc', '= 0.99992'
4
+ require 'odbc'
5
+
6
+ class RDBI::Driver::ODBC < RDBI::Driver
7
+ def initialize(*args)
8
+ super Database, *args
9
+ end
10
+ end
11
+
12
+ class RDBI::Driver::ODBC < RDBI::Driver
13
+ class Database < RDBI::Database
14
+
15
+ attr_accessor :handle
16
+
17
+ def initialize(*args)
18
+ super *args
19
+
20
+ database = @connect_args[:database] || @connect_args[:dbname] ||
21
+ @connect_args[:db]
22
+ username = @connect_args[:username] || @connect_args[:user]
23
+ password = @connect_args[:password] || @connect_args[:pass]
24
+
25
+ @handle = ::ODBC.connect(database, username, password)
26
+
27
+ self.database_name = @handle.get_info("SQL_DATABASE_NAME")
28
+ end
29
+
30
+ def disconnect
31
+ @handle.rollback
32
+ @handle.disconnect
33
+ super
34
+ end
35
+
36
+ def transaction(&block)
37
+ raise NotImplementedError, "#transaction"
38
+ end
39
+
40
+ def rollback
41
+ @handle.rollback
42
+ super
43
+ end
44
+
45
+ def commit
46
+ @handle.commit
47
+ super
48
+ end
49
+
50
+ def new_statement(query)
51
+ Statement.new(query, self)
52
+ end
53
+
54
+ def table_schema(table_name)
55
+ raise NotImplementedError, "#table_schema"
56
+ end
57
+
58
+ def schema
59
+ raise NotImplementedError, "#schema"
60
+ end
61
+
62
+ def ping
63
+ @handle.connected?
64
+ end
65
+ end
66
+
67
+ class Cursor < RDBI::Cursor
68
+
69
+ # only #fetch works reliably with ODBC, so we just build the array upfront.
70
+ def initialize(handle)
71
+ super handle
72
+ @index = 0
73
+ @rs = []
74
+ while r = @handle.fetch
75
+ @rs << r
76
+ end
77
+ end
78
+
79
+ def next_row
80
+ val = @rs[@index]
81
+ @index += 1
82
+ val
83
+ end
84
+
85
+ def result_count
86
+ @rs.size
87
+ end
88
+
89
+ def affected_count
90
+ 0
91
+ end
92
+
93
+ def first
94
+ @rs.first
95
+ end
96
+
97
+ def last
98
+ @rs.last
99
+ end
100
+
101
+ def rest
102
+ @rs[@index+1..-1]
103
+ end
104
+
105
+ def all
106
+ @rs
107
+ end
108
+
109
+ def fetch(count = 1)
110
+ return [] if last_row?
111
+ @rs[@index, count]
112
+ end
113
+
114
+ def [](index)
115
+ @rs[index]
116
+ end
117
+
118
+ def last_row?
119
+ @index == @rs.size
120
+ end
121
+
122
+ def empty?
123
+ @rs.empty?
124
+ end
125
+
126
+ def rewind
127
+ @index = 0
128
+ end
129
+
130
+ def size
131
+ @rs.length
132
+ end
133
+
134
+ def finish
135
+ @handle.drop
136
+ end
137
+
138
+ def coerce_to_array
139
+ @rs
140
+ end
141
+ end
142
+
143
+ class Statement < RDBI::Statement
144
+
145
+ attr_accessor :handle
146
+
147
+ def initialize(query, dbh)
148
+ super
149
+
150
+ @handle = @dbh.handle.prepare(query)
151
+ @output_type_map = RDBI::Type.create_type_hash(RDBI::Type::Out)
152
+ end
153
+
154
+ def new_execution(*binds)
155
+ @handle.execute(*binds)
156
+
157
+ columns = @handle.columns(true).collect do |col|
158
+ newcol = RDBI::Column.new
159
+ newcol.name = col.name.to_sym
160
+ newcol
161
+ end
162
+
163
+ return Cursor.new(@handle), RDBI::Schema.new(columns), @output_type_map
164
+ end
165
+
166
+ def finish
167
+ @handle.drop
168
+ end
169
+ end
170
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rdbi-driver-odbc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Shane Emmons
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-08 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rdbi
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 9
31
+ version: "0.9"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: ruby-odbc
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - "="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 99992
45
+ version: "0.99992"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 2
58
+ - 2
59
+ version: "2.2"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ type: :development
74
+ version_requirements: *id004
75
+ description: This gem gives you the ability to query ODBC connections with RDBI.
76
+ email: semmons99@gmail.com
77
+ executables: []
78
+
79
+ extensions: []
80
+
81
+ extra_rdoc_files: []
82
+
83
+ files:
84
+ - lib/rdbi/driver/odbc.rb
85
+ - lib/rdbi-driver-odbc.rb
86
+ - CHANGELOG.md
87
+ - LICENSE
88
+ - README.md
89
+ has_rdoc: true
90
+ homepage: https://github.com/semmons99/rdbi-driver-odbc
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ segments:
112
+ - 1
113
+ - 3
114
+ - 6
115
+ version: 1.3.6
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: ODBC driver for RDBI
123
+ test_files: []
124
+