dbd-jdbc 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +4 -0
- data/Rakefile +5 -5
- data/lib/dbd/Jdbc.rb +152 -36
- data/lib/dbd/JdbcTypeConversions.rb +69 -38
- data/lib/dbd/jdbc.rb +29 -0
- metadata +39 -38
data/README.txt
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
RECENT CHANGES
|
2
|
+
==============
|
3
|
+
The dbd-jdbc project has recently moved to Kenai and can be found at the following url (http://kenai.com/projects/dbd-jdbc). Please report any bugs found to the issue tracker listed on the Kenai project page.
|
4
|
+
|
1
5
|
OVERVIEW
|
2
6
|
========
|
3
7
|
The Jdbc driver for DBI runs only under JRuby, using JRuby's Java integration to act as a thin wrapper around JDBC Connections obtained through DriverManager. Theoretically this driver can support any database that has JDBC support, although the behavior of certain aspects of the DBI API will differ slightly based on the implementation of the JDBC driver and the underlying database (see LIMITATIONS)
|
data/Rakefile
CHANGED
@@ -28,12 +28,12 @@ Rake::Task['manifest'].invoke # Always regen manifest, so Hoe has up-to-date lis
|
|
28
28
|
|
29
29
|
begin
|
30
30
|
require 'hoe'
|
31
|
-
Hoe.new("dbd-jdbc", "0.0.
|
31
|
+
Hoe.new("dbd-jdbc", "0.0.2") do |p|
|
32
32
|
p.rubyforge_name = "jruby-extras"
|
33
|
-
p.url = "http://
|
34
|
-
p.author = "
|
35
|
-
p.email = "
|
36
|
-
p.summary = "JDBC driver for DBI, originally by Kristopher Schmidt"
|
33
|
+
p.url = "http://kenai.com/projects/dbd-jdbc"
|
34
|
+
p.author = "Chad Johnson"
|
35
|
+
p.email = "chad.j.johnson@gmail.com"
|
36
|
+
p.summary = "JDBC driver for DBI, originally by Kristopher Schmidt and Ola Bini"
|
37
37
|
end.spec.dependencies.delete_if { |dep| dep.name == "hoe" }
|
38
38
|
rescue LoadError
|
39
39
|
puts "You really need Hoe installed to be able to package this gem"
|
data/lib/dbd/Jdbc.rb
CHANGED
@@ -1,3 +1,31 @@
|
|
1
|
+
# (C) 2009 Chad Johnson <chad.j.johnson@gmail.com>.
|
2
|
+
# (C) 2008 Ola Bini <ola.bini@gmail.com>.
|
3
|
+
# (C) 2006 Kristopher Schmidt <krisschmidt@optonline.net>.
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions
|
9
|
+
# are met:
|
10
|
+
# 1. Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# 2. Redistributions in binary form must reproduce the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer in the
|
14
|
+
# documentation and/or other materials provided with the distribution.
|
15
|
+
# 3. The name of the author may not be used to endorse or promote products
|
16
|
+
# derived from this software without specific prior written permission.
|
17
|
+
#
|
18
|
+
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
+
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
+
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
21
|
+
# THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
22
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
23
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
24
|
+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
26
|
+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
27
|
+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
1
29
|
require 'java'
|
2
30
|
require File.dirname(__FILE__) + '/JdbcTypeConversions'
|
3
31
|
|
@@ -6,13 +34,42 @@ module DBI
|
|
6
34
|
module Jdbc
|
7
35
|
|
8
36
|
include_class 'java.sql.Connection'
|
9
|
-
|
10
|
-
|
11
|
-
|
37
|
+
include_class 'java.sql.ResultSet'
|
38
|
+
|
39
|
+
VERSION = "0.0.2"
|
40
|
+
|
41
|
+
module Type
|
42
|
+
class Timestamp < DBI::Type::Null
|
43
|
+
def self.parse(obj)
|
44
|
+
obj = super
|
45
|
+
return obj unless obj
|
46
|
+
|
47
|
+
if obj.kind_of?(::DateTime) || obj.kind_of?(::Time) || obj.kind_of?(::Date)
|
48
|
+
return obj
|
49
|
+
elsif obj.kind_of?(::String)
|
50
|
+
return ::DateTime.strptime(obj, "%Y-%m-%d %H:%M:%S")
|
51
|
+
else
|
52
|
+
return ::DateTime.parse(obj.to_s) if obj.respond_to? :to_s
|
53
|
+
return ::DateTime.parse(obj.to_str) if obj.respond_to? :to_str
|
54
|
+
return obj
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
12
60
|
def self.driver_name
|
13
61
|
"Jdbc"
|
14
62
|
end
|
15
63
|
|
64
|
+
#
|
65
|
+
# Bound values pass through this function before being handed to Statement.bind_param
|
66
|
+
#
|
67
|
+
# The false value prevents the default type conversion from occurring
|
68
|
+
#
|
69
|
+
DBI::TypeUtil.register_conversion(driver_name) do |obj|
|
70
|
+
[obj, false]
|
71
|
+
end
|
72
|
+
|
16
73
|
class Driver < DBI::BaseDriver
|
17
74
|
|
18
75
|
def initialize
|
@@ -63,7 +120,8 @@ module DBI
|
|
63
120
|
@connection = connection
|
64
121
|
@attributes = {
|
65
122
|
#works with Sybase and Mysql.
|
66
|
-
"nulltype" => java.sql.Types::VARCHAR
|
123
|
+
"nulltype" => java.sql.Types::VARCHAR,
|
124
|
+
"allow_scroll" => false
|
67
125
|
}
|
68
126
|
end
|
69
127
|
|
@@ -75,7 +133,11 @@ module DBI
|
|
75
133
|
end
|
76
134
|
|
77
135
|
def prepare(sql)
|
78
|
-
|
136
|
+
if self["allow_scroll"]
|
137
|
+
return Statement.new(@connection.prepareStatement(sql,ResultSet::TYPE_SCROLL_INSENSITIVE, ResultSet::CONCUR_READ_ONLY), self["nulltype"], self["allow_scroll"])
|
138
|
+
else
|
139
|
+
return Statement.new(@connection.prepareStatement(sql), self["nulltype"], self["allow_scroll"])
|
140
|
+
end
|
79
141
|
rescue NativeException => error
|
80
142
|
raise DBI::DatabaseError.new(error.message)
|
81
143
|
end
|
@@ -114,15 +176,17 @@ module DBI
|
|
114
176
|
rs = metaData.getColumns(nil, nil, table, nil)
|
115
177
|
columns = []
|
116
178
|
while(rs.next())
|
179
|
+
type_name, dbi_type = jdbc_to_dbi_sqltype(rs.getShort(5))
|
117
180
|
columns << {
|
118
181
|
"name" => rs.getString(4),
|
119
|
-
"sql_type" =>
|
182
|
+
"sql_type" => type_name,
|
120
183
|
"type_name" => rs.getString(6),
|
121
184
|
"precision" => rs.getInt(7),
|
122
185
|
"scale" => rs.getInt(9),
|
123
186
|
"default" => rs.getString(13),
|
124
187
|
"nullable" => (rs.getInt(11) == 1)
|
125
|
-
}
|
188
|
+
}
|
189
|
+
columns[-1]["dbi_type"] = dbi_type if dbi_type
|
126
190
|
end
|
127
191
|
return columns
|
128
192
|
rescue NativeException => error
|
@@ -130,7 +194,7 @@ module DBI
|
|
130
194
|
end
|
131
195
|
|
132
196
|
def [](attribute)
|
133
|
-
attribute.downcase
|
197
|
+
attribute = attribute.downcase
|
134
198
|
check_attribute(attribute)
|
135
199
|
case attribute
|
136
200
|
when "autocommit" then @connection.getAutoCommit()
|
@@ -142,7 +206,7 @@ module DBI
|
|
142
206
|
end
|
143
207
|
|
144
208
|
def []=(attribute, value)
|
145
|
-
attribute.downcase
|
209
|
+
attribute = attribute.downcase
|
146
210
|
check_attribute(attribute)
|
147
211
|
case attribute
|
148
212
|
when "autocommit" then @connection.setAutoCommit(value)
|
@@ -156,6 +220,10 @@ module DBI
|
|
156
220
|
def __get_java_connection
|
157
221
|
return @connection
|
158
222
|
end
|
223
|
+
|
224
|
+
def self.from_java_connection(java_connection, type_coercion = false)
|
225
|
+
DBI::DatabaseHandle.new(Database.new(java_connection), type_coercion)
|
226
|
+
end
|
159
227
|
|
160
228
|
private
|
161
229
|
|
@@ -169,9 +237,10 @@ module DBI
|
|
169
237
|
|
170
238
|
include JdbcTypeConversions
|
171
239
|
|
172
|
-
def initialize(statement, nulltype)
|
240
|
+
def initialize(statement, nulltype, allow_scroll = false)
|
173
241
|
@statement = statement
|
174
242
|
@nulltype = nulltype
|
243
|
+
@allow_scroll = allow_scroll
|
175
244
|
@rows = nil
|
176
245
|
@data = []
|
177
246
|
end
|
@@ -189,12 +258,12 @@ module DBI
|
|
189
258
|
#despite DBD spec saying Float->SQL Float, using Double gives
|
190
259
|
#better precision and passes tests that setFloat does not.
|
191
260
|
@statement.setDouble(param, value)
|
192
|
-
elsif value.is_a?(DBI::
|
193
|
-
@statement.
|
194
|
-
elsif value.is_a?(DBI::
|
195
|
-
@statement.
|
196
|
-
elsif value.is_a?(DBI::
|
197
|
-
@statement.
|
261
|
+
elsif value.is_a?(::DateTime) || value.is_a?(DBI::Timestamp)
|
262
|
+
@statement.setTimestamp(param, timestamp_to_jdbctimestamp(value))
|
263
|
+
elsif value.is_a?(::Date) || value.is_a?(DBI::Date)
|
264
|
+
@statement.setDate(param, date_to_jdbcdate(value))
|
265
|
+
elsif value.is_a?(::Time) || value.is_a?(DBI::Time)
|
266
|
+
@statement.setTime(param, time_to_jdbctime(value))
|
198
267
|
else
|
199
268
|
@statement.setObject(param, value)
|
200
269
|
end
|
@@ -225,12 +294,7 @@ module DBI
|
|
225
294
|
|
226
295
|
def fetch
|
227
296
|
if (@rs && @rs.next())
|
228
|
-
|
229
|
-
metaData = @rs.getMetaData()
|
230
|
-
(1..metaData.getColumnCount()).each do |columnNumber|
|
231
|
-
@data << get_value(columnNumber, @rs, metaData)
|
232
|
-
end
|
233
|
-
return @data
|
297
|
+
return fill_data
|
234
298
|
else
|
235
299
|
return nil
|
236
300
|
end
|
@@ -238,19 +302,65 @@ module DBI
|
|
238
302
|
raise DBI::DatabaseError.new(error.message)
|
239
303
|
end
|
240
304
|
|
305
|
+
def fill_data
|
306
|
+
@data.clear
|
307
|
+
metaData = @rs.getMetaData()
|
308
|
+
(1..metaData.getColumnCount()).each do |columnNumber|
|
309
|
+
@data << get_value(columnNumber, @rs, metaData)
|
310
|
+
end
|
311
|
+
return @data
|
312
|
+
end
|
313
|
+
|
314
|
+
#
|
315
|
+
# Unless "allow_scroll" was set on this connection this will default to the
|
316
|
+
# DBI::BaseStatement#fetch_scroll implementation.
|
317
|
+
#
|
318
|
+
# See DBI::BaseStatement#fetch_scroll. These additional constants are supported
|
319
|
+
# when "allow_scroll" is set on the connection.
|
320
|
+
#
|
321
|
+
# * DBI::SQL_FETCH_PRIOR: Fetch the row previous to the current one.
|
322
|
+
# * DBI::SQL_FETCH_FIRST: Fetch the first row.
|
323
|
+
# * DBI::SQL_FETCH_ABSOLUTE: Fetch the row at the offset provided.
|
324
|
+
# * DBI::SQL_FETCH_RELATIVE: Fetch the row at the current point + offset.
|
325
|
+
#
|
326
|
+
def fetch_scroll(direction, offset)
|
327
|
+
return super(direction, offset) unless @allow_scroll
|
328
|
+
|
329
|
+
case direction
|
330
|
+
when DBI::SQL_FETCH_NEXT
|
331
|
+
fill_data if @rs && @rs.next()
|
332
|
+
when DBI::SQL_FETCH_PRIOR
|
333
|
+
fill_data if @rs && @rs.previous()
|
334
|
+
when DBI::SQL_FETCH_FIRST
|
335
|
+
fill_data if @rs && @rs.first()
|
336
|
+
when DBI::SQL_FETCH_LAST
|
337
|
+
fill_data if @rs && @rs.last()
|
338
|
+
when DBI::SQL_FETCH_ABSOLUTE
|
339
|
+
fill_data if @rs && @rs.absolute(offset)
|
340
|
+
when DBI::SQL_FETCH_RELATIVE
|
341
|
+
fill_data if @rs && @rs.relative(offset)
|
342
|
+
else
|
343
|
+
raise DBI::NotSupportedError
|
344
|
+
end
|
345
|
+
rescue NativeException => error
|
346
|
+
raise DBI::NotSupportedError.new(error.message)
|
347
|
+
end
|
348
|
+
|
241
349
|
def column_info
|
242
350
|
info = Array.new
|
243
351
|
return info unless @rs
|
244
352
|
metaData = @rs.getMetaData()
|
245
353
|
(1..metaData.getColumnCount()).each do |columnNumber|
|
354
|
+
type_name, dbi_type = jdbc_to_dbi_sqltype(metaData.getColumnType(columnNumber))
|
246
355
|
info << {
|
247
356
|
"name" => metaData.getColumnName(columnNumber),
|
248
|
-
"sql_type" =>
|
357
|
+
"sql_type" => type_name,
|
249
358
|
"type_name" => metaData.getColumnTypeName(columnNumber),
|
250
359
|
"precision" => metaData.getPrecision(columnNumber),
|
251
360
|
"scale" => metaData.getScale(columnNumber),
|
252
361
|
"nullable" => (metaData.isNullable(columnNumber) == 1)
|
253
362
|
}
|
363
|
+
info[-1]["dbi_type"] = dbi_type if dbi_type
|
254
364
|
end
|
255
365
|
return info
|
256
366
|
rescue NativeException => error
|
@@ -261,25 +371,31 @@ module DBI
|
|
261
371
|
return @rows
|
262
372
|
end
|
263
373
|
|
374
|
+
def self.from_java_statement(java_statement, type_coercion = false, null_type = java.sql.Types::VARCHAR)
|
375
|
+
raise DBI::DatabaseError.new("Only java.sql.PreparedStatement instances accepted") unless java_statement.kind_of?(java.sql.PreparedStatement)
|
376
|
+
|
377
|
+
DBI::StatementHandle.new(Statement.new(java_statement, null_type), true, true, type_coercion)
|
378
|
+
end
|
379
|
+
|
264
380
|
private
|
265
381
|
|
266
382
|
def get_value(columnNumber, rs, metaData)
|
267
383
|
#note only map things that seem unlikely to coerce properly to jruby,
|
268
384
|
#since anything mapped as primitive type cannot be returned as null
|
269
385
|
return case metaData.getColumnType(columnNumber)
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
386
|
+
when java.sql.Types::BIT
|
387
|
+
rs.getBoolean(columnNumber)
|
388
|
+
when java.sql.Types::NUMERIC, java.sql.Types::DECIMAL
|
389
|
+
metaData.getScale(columnNumber) == 0 ? rs.getLong(columnNumber) : rs.getDouble(columnNumber)
|
390
|
+
when java.sql.Types::DATE
|
391
|
+
jdbcdate_to_date(rs.getDate(columnNumber))
|
392
|
+
when java.sql.Types::TIME
|
393
|
+
jdbctime_to_time(rs.getTime(columnNumber))
|
394
|
+
when java.sql.Types::TIMESTAMP
|
395
|
+
jdbctimestamp_to_timestamp(rs.getTimestamp(columnNumber))
|
396
|
+
else
|
397
|
+
rs.getObject(columnNumber)
|
398
|
+
end
|
283
399
|
end
|
284
400
|
|
285
401
|
end # class Statement
|
@@ -1,3 +1,31 @@
|
|
1
|
+
# (C) 2009 Chad Johnson <chad.j.johnson@gmail.com>.
|
2
|
+
# (C) 2008 Ola Bini <ola.bini@gmail.com>.
|
3
|
+
# (C) 2006 Kristopher Schmidt <krisschmidt@optonline.net>.
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions
|
9
|
+
# are met:
|
10
|
+
# 1. Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# 2. Redistributions in binary form must reproduce the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer in the
|
14
|
+
# documentation and/or other materials provided with the distribution.
|
15
|
+
# 3. The name of the author may not be used to endorse or promote products
|
16
|
+
# derived from this software without specific prior written permission.
|
17
|
+
#
|
18
|
+
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
+
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
+
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
21
|
+
# THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
22
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
23
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
24
|
+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
26
|
+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
27
|
+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
1
29
|
module JdbcTypeConversions
|
2
30
|
|
3
31
|
include_class 'java.util.Calendar'
|
@@ -5,79 +33,82 @@ module JdbcTypeConversions
|
|
5
33
|
|
6
34
|
def jdbc_to_dbi_sqltype(jdbctype)
|
7
35
|
return case jdbctype
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
else
|
28
|
-
DBI::SQL_OTHER
|
36
|
+
when Types::BIGINT then [DBI::SQL_BIGINT, nil]
|
37
|
+
when Types::BINARY then [DBI::SQL_BINARY, nil]
|
38
|
+
when Types::BIT then [DBI::SQL_BIT, nil]
|
39
|
+
when Types::CHAR then [DBI::SQL_CHAR, nil]
|
40
|
+
when Types::DATE then [DBI::SQL_DATE, DBI::DBD::Jdbc::Type::Timestamp]
|
41
|
+
when Types::DECIMAL then [DBI::SQL_DECIMAL, nil]
|
42
|
+
when Types::DOUBLE then [DBI::SQL_DOUBLE, nil]
|
43
|
+
when Types::FLOAT then [DBI::SQL_FLOAT, nil]
|
44
|
+
when Types::INTEGER then [DBI::SQL_INTEGER, nil]
|
45
|
+
when Types::LONGVARBINARY then [DBI::SQL_LONGVARBINARY, nil]
|
46
|
+
when Types::LONGVARCHAR then [DBI::SQL_LONGVARCHAR, nil]
|
47
|
+
when Types::NUMERIC then [DBI::SQL_NUMERIC, nil]
|
48
|
+
when Types::REAL then [DBI::SQL_REAL, nil]
|
49
|
+
when Types::SMALLINT then [DBI::SQL_SMALLINT, nil]
|
50
|
+
when Types::TIME then [DBI::SQL_TIME, DBI::DBD::Jdbc::Type::Timestamp]
|
51
|
+
when Types::TIMESTAMP then [DBI::SQL_TIMESTAMP, DBI::DBD::Jdbc::Type::Timestamp]
|
52
|
+
when Types::TINYINT then [DBI::SQL_TINYINT, nil]
|
53
|
+
when Types::VARBINARY then [DBI::SQL_VARBINARY, nil]
|
54
|
+
when Types::VARCHAR then [DBI::SQL_VARCHAR, nil]
|
55
|
+
else
|
56
|
+
[DBI::SQL_OTHER, nil]
|
29
57
|
end
|
30
58
|
end
|
31
59
|
|
32
|
-
def
|
60
|
+
def date_to_jdbcdate(dbidate)
|
33
61
|
cal = Calendar.getInstance()
|
34
62
|
set_calendar_date_fields(dbidate, cal)
|
35
63
|
return java.sql.Date.new(cal.getTime().getTime())
|
36
|
-
end
|
64
|
+
end
|
37
65
|
|
38
|
-
def
|
66
|
+
def time_to_jdbctime(dbitime)
|
39
67
|
cal = Calendar.getInstance()
|
68
|
+
set_calendar_date_fields(dbitime, cal)
|
40
69
|
set_calendar_time_fields(dbitime, cal)
|
41
70
|
return java.sql.Time.new(cal.getTime().getTime())
|
42
|
-
end
|
71
|
+
end
|
43
72
|
|
44
|
-
def
|
73
|
+
def timestamp_to_jdbctimestamp(dbitimestamp)
|
45
74
|
cal = Calendar.getInstance()
|
46
75
|
set_calendar_date_fields(dbitimestamp, cal)
|
47
76
|
set_calendar_time_fields(dbitimestamp, cal)
|
48
77
|
return java.sql.Timestamp.new(cal.getTime().getTime())
|
49
78
|
end
|
50
79
|
|
51
|
-
def
|
80
|
+
def jdbcdate_to_date(jdbcdate)
|
52
81
|
return nil if jdbcdate.nil?
|
53
82
|
cal = get_calendar(jdbcdate)
|
54
|
-
return
|
83
|
+
return ::Date.new(cal.get(Calendar::YEAR), cal.get(Calendar::MONTH)+1, cal.get(Calendar::DAY_OF_MONTH))
|
55
84
|
end
|
56
85
|
|
57
|
-
def
|
86
|
+
def jdbctime_to_time(jdbctime)
|
58
87
|
return nil if jdbctime.nil?
|
59
|
-
cal = get_calendar(jdbctime)
|
60
|
-
return
|
88
|
+
cal = get_calendar(jdbctime)
|
89
|
+
return ::Time.mktime(cal.get(Calendar::YEAR), cal.get(Calendar::MONTH)+1, cal.get(Calendar::DAY_OF_MONTH), cal.get(Calendar::HOUR_OF_DAY), cal.get(Calendar::MINUTE), cal.get(Calendar::SECOND), cal.get(Calendar::MILLISECOND) * 1000)
|
61
90
|
end
|
62
91
|
|
63
|
-
def
|
92
|
+
def jdbctimestamp_to_timestamp(jdbctimestamp)
|
64
93
|
return nil if jdbctimestamp.nil?
|
65
94
|
cal = get_calendar(jdbctimestamp)
|
66
|
-
return
|
95
|
+
return ::DateTime.new(cal.get(Calendar::YEAR), cal.get(Calendar::MONTH)+1, cal.get(Calendar::DAY_OF_MONTH), cal.get(Calendar::HOUR_OF_DAY), cal.get(Calendar::MINUTE), cal.get(Calendar::SECOND))
|
67
96
|
end
|
68
97
|
|
69
98
|
private
|
70
99
|
|
71
100
|
def set_calendar_date_fields(dbidate, cal)
|
72
|
-
cal.set(Calendar::YEAR, dbidate.year)
|
73
|
-
cal.set(Calendar::MONTH, dbidate.month-1)
|
74
|
-
cal.set(Calendar::DAY_OF_MONTH, dbidate.day)
|
101
|
+
cal.set(Calendar::YEAR, dbidate.year) if dbidate.respond_to? :year
|
102
|
+
cal.set(Calendar::MONTH, dbidate.month-1) if dbidate.respond_to? :month
|
103
|
+
cal.set(Calendar::DAY_OF_MONTH, dbidate.day) if dbidate.respond_to? :day
|
75
104
|
end
|
76
105
|
|
77
106
|
def set_calendar_time_fields(dbitime, cal)
|
78
107
|
cal.set(Calendar::HOUR_OF_DAY, dbitime.hour)
|
79
|
-
cal.set(Calendar::MINUTE, dbitime.
|
80
|
-
cal.set(Calendar::SECOND, dbitime.
|
108
|
+
cal.set(Calendar::MINUTE, dbitime.min)
|
109
|
+
cal.set(Calendar::SECOND, dbitime.sec)
|
110
|
+
# According to the Ruby Docs usec is microsecond - java wants milli
|
111
|
+
cal.set(Calendar::MILLISECOND , (dbitime.usec / 1000) ) if dbitime.respond_to? :usec
|
81
112
|
end
|
82
113
|
|
83
114
|
def get_calendar(jdbctype)
|
data/lib/dbd/jdbc.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# (C) 2009 Chad Johnson <chad.j.johnson@gmail.com>.
|
2
|
+
# (C) 2008 Ola Bini <ola.bini@gmail.com>.
|
3
|
+
# (C) 2006 Kristopher Schmidt <krisschmidt@optonline.net>.
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions
|
9
|
+
# are met:
|
10
|
+
# 1. Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# 2. Redistributions in binary form must reproduce the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer in the
|
14
|
+
# documentation and/or other materials provided with the distribution.
|
15
|
+
# 3. The name of the author may not be used to endorse or promote products
|
16
|
+
# derived from this software without specific prior written permission.
|
17
|
+
#
|
18
|
+
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
+
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
+
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
21
|
+
# THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
22
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
23
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
24
|
+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
26
|
+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
27
|
+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
require File.dirname(__FILE__) + '/Jdbc'
|
metadata
CHANGED
@@ -1,57 +1,58 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
3
|
+
requirements:
|
4
|
+
- - '>='
|
5
|
+
- !ruby/object:Gem::Version
|
6
|
+
version: "0"
|
7
|
+
version:
|
8
|
+
email: chad.j.johnson@gmail.com
|
9
|
+
cert_chain: []
|
6
10
|
|
7
|
-
|
8
|
-
version: 0.0.1
|
11
|
+
summary: JDBC driver for DBI, originally by Kristopher Schmidt and Ola Bini
|
9
12
|
post_install_message:
|
10
|
-
|
11
|
-
files:
|
12
|
-
- lib/dbd/Jdbc.rb
|
13
|
-
- lib/dbd/JdbcTypeConversions.rb
|
14
|
-
- Rakefile
|
13
|
+
extra_rdoc_files:
|
15
14
|
- README.txt
|
16
|
-
|
15
|
+
homepage: http://kenai.com/projects/dbd-jdbc
|
16
|
+
signing_key:
|
17
|
+
name: dbd-jdbc
|
17
18
|
rdoc_options:
|
18
19
|
- --main
|
19
20
|
- README.txt
|
20
|
-
|
21
|
-
|
21
|
+
autorequire:
|
22
|
+
rubyforge_project: jruby-extras
|
23
|
+
executables: []
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
platform: ruby
|
26
|
-
summary: JDBC driver for DBI, originally by Kristopher Schmidt
|
25
|
+
description:
|
26
|
+
specification_version: 2
|
27
27
|
default_executable:
|
28
|
-
|
28
|
+
files:
|
29
|
+
- lib/dbd/JdbcTypeConversions.rb
|
30
|
+
- lib/dbd/Jdbc.rb
|
31
|
+
- lib/dbd/jdbc.rb
|
32
|
+
- Rakefile
|
33
|
+
- README.txt
|
29
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
30
|
-
version:
|
31
35
|
requirements:
|
32
36
|
- - '>='
|
33
37
|
- !ruby/object:Gem::Version
|
34
38
|
version: "0"
|
35
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
36
39
|
version:
|
37
|
-
|
38
|
-
- - '>='
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: "0"
|
41
|
-
require_paths:
|
42
|
-
- lib
|
43
|
-
specification_version: 2
|
44
|
-
test_files: []
|
40
|
+
extensions: []
|
45
41
|
|
46
|
-
|
42
|
+
rubygems_version: 1.3.1
|
43
|
+
requirements: []
|
47
44
|
|
48
|
-
description: ""
|
49
|
-
email: ola.bini@gmail.com
|
50
45
|
authors:
|
51
|
-
-
|
52
|
-
|
53
|
-
|
54
|
-
|
46
|
+
- Chad Johnson
|
47
|
+
date: 2009-03-08 00:00:00 Z
|
48
|
+
platform: ruby
|
49
|
+
test_files: []
|
55
50
|
|
56
|
-
|
57
|
-
|
51
|
+
version: !ruby/object:Gem::Version
|
52
|
+
version: 0.0.2
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
dependencies: []
|
56
|
+
|
57
|
+
bindir: bin
|
58
|
+
has_rdoc: true
|