My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
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
62
63
64
65
66
67
68
69
70
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require 'mscorlib'
require 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
require 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
require File.dirname(__FILE__) + '/Workarounds.dll'

module DBI
module DBD
module ADONET

VERSION = "0.1"
USED_DBD_VERSION = "0.1"
PROVIDERS = {
:odbc => "System.Data.Odbc",
:oledb => "System.Data.OleDb",
:oracle => "System.Data.OracleClient",
:mssql => "System.Data.SqlClient",
:sqlce => "System.Data.SqlServerCe.3.5",
:mysql => "MySql.Data.MySqlClient",
:sqlite => "System.Data.SQLite"
}

class Driver < DBI::BaseDriver

include System::Data::Common

def initialize
super(USED_DBD_VERSION)
end

def connect(driver_url, user, auth, attr)
provider, connection = parse_connection_string(driver_url)
load_factory PROVIDERS[provider.downcase.to_sym]
conn = @factory.create_connection
conn.connection_string = connection
conn.open

return Database.new(conn, attr);

rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

private

def parse_connection_string(connection_string)
if connection_string =~ /^([^:]+)(:(.*))$/
[$1, $3]
else
raise InterfaceError, "Invalid provider name"
end
end

def load_factory(provider_name)
return nil if defined? @factory

provider = (provider_name.nil? || provider_name.empty?) ? DEFAULT_PROVIDER : provider_name
@factory = DbProviderFactories.get_factory provider
end

end

class Database < DBI::BaseDatabase

def initialize(dbd_db, attr)
super
@trans = dbd_db.begin_transaction
end

def disconnect
@trans.rollback
@handle.close
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def prepare(statement)
Statement.new(statement, self)
end

def ping
cmd = @handle.create_command
cmd.command_text = "Select 1"
begin
cmd.execute_non_query
return true
rescue
return false
end
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def commit
@trans.commit
@trans = @handle.begin_transaction
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def rollback
@trans.rollback
@trans = @handle.begin_transaction
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def current_transaction
@trans
end

def current_connection
@handle
end

def []=(attr, value)
if attr == 'AutoCommit' then
# TODO: commit current transaction?
@attr[attr] = value
else
super
end
end

end # class Database

class Statement < DBI::BaseStatement
include Workarounds

CLR_TYPES = {
:TINYINT => "byte",
:SMALLINT => "short",
:BIGINT => "long",
:INT => "int",
:FLOAT => "double",
:REAL => "float",
:SMALLMONEY => "decimal",
:MONEY => "decimal",
:NUMERIC => "decimal",
:DECIMAL => "decimal",
:BIT => "bool",
:UNIQUEIDENTIFIER => "Guid",
:VARCHAR => "string",
:NVARCHAR => "string",
:TEXT => "string",
:NTEXT => "string",
:CHAR => "char",
:NCHAR => "char",
:VARBINARY => "byte[]",
:IMAGE => "byte[]",
:DATETIME => "DateTime"
}

SQL_TYPE_NAMES = {
:BIT => "BIT",
:TINYINT => "TINYINT",
:SMALLINT => "SMALLINT",
:INTEGER => "INTEGER",
:INT => "INTEGER",
:BIGINT => "BIGINT",
:FLOAT => "FLOAT",
:REAL => "REAL",
:DOUBLE => "DOUBLE",
:NUMERIC => "NUMERIC",
:DECIMAL => "DECIMAL",
:CHAR => "CHAR",
:NCHAR => "CHAR",
:VARCHAR => "VARCHAR",
:NVARCHAR => "VARCHAR",
:LONGVARCHAR => "LONG VARCHAR",
:TEXT => "LONG VARCHAR",
:NTEXT => "LONG VARCHAR",
:DATE => "DATE",
:DATETIME => "DATETIME",
:TIME => "TIME",
:TIMESTAMP => "TIMESTAMP",
:BINARY => "BINARY",
:VARBINARY => "VARBINARY",
:LONGVARBINARY => "LONG VARBINARY",
:IMAGE => "BLOB",
:BLOB => "BLOB",
:CLOB => "CLOB",
:OTHER => "",
:BOOLEAN => "BOOLEAN",
:UNIQUEIDENTIFIER => "VARCHAR"
}

def initialize(statement, db)
@statement = statement
@connection = db.current_connection;
@command = @connection.create_command
@command.command_text = statement
@command.transaction = db.current_transaction
@current_index = 0
@db = db
end

def bind_param(name, value, attribs)
parameter = @command.create_parameter
parameter.ParameterName = name
parameter.Value = value
@command.parameters.add parameter
end

def execute
@current_index = 0
@rows = []
@schema = nil
@reader = @command.execute_reader

finish if not SQL.query?(@statement)
# TODO: SELECT and AutoCommit finishes the result-set
# what to do?
if @db['AutoCommit'] == true and not SQL.query?(@statement) then
@db.commit
end
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def fetch
@reader.read ? read_row(@reader) : nil
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def finish
@reader.close

rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def schema
@schema ||= @reader.get_schema_table
end

def column_info

infos = schema.rows.collect do |row|
name = DataWorkarounds.get_row_value row, "ColumnName"
dtn = DataWorkarounds.get_row_value(row, "DataTypeName").to_s
{
:name => name.to_s,
:sql_type => SQL_TYPE_NAMES[dtn.upcase.to_sym],
:type_name => CLR_TYPES[dtn.upcase.to_sym],
:precision => DataWorkarounds.get_row_value(row, "NumericPrecision"),
:default => DataWorkarounds.get_default_value(row, name),
:scale => DataWorkarounds.get_row_value(row, "NumericScale"),
:nullable => DataWorkarounds.get_row_value(row, "AllowDBNull"),
:primary => schema.primary_key.select { |pk| pk.column_name.to_s == name.to_s }.size > 0,
:unique => DataWorkarounds.get_row_value(row, "IsUnique")
}
end
infos
rescue RuntimeError => err
raise DBI::DatabaseError.new(err.message)
end

def rows
return 0 if @reader.nil?
@reader.records_affected
end

private

def read_row(record)
(0...schema.columns.count).collect do |i|
res = record.get_value(i)
if res.is_a?(System::Guid)
res.to_string.to_s
elsif res.is_a?(System::DBNull)
nil
else
res
end
end
end

end

end
end
end

Change log

r14 by i...@flanders.co.nz on Dec 28, 2008   Diff
some refactoring and preparing to create a
package
Go to: 
Project members, sign in to write a code review

Older revisions

r13 by i...@flanders.co.nz on Dec 28, 2008   Diff
Some cleanup we're stuck with 2
methods in C# until indexers are fixed
on IronRuby
r12 by i...@flanders.co.nz on Dec 28, 2008   Diff
moved implementation to ruby code..
r11 by i...@flanders.co.nz on Dec 28, 2008   Diff
It is kind of working
All revisions of this file

File info

Size: 8630 bytes, 283 lines
Powered by Google Project Hosting