dbi 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/ChangeLog +3694 -0
  2. data/LICENSE +25 -0
  3. data/README +271 -0
  4. data/bin/dbi +518 -0
  5. data/build/Rakefile.dbi.rb +57 -0
  6. data/examples/test1.pl +39 -0
  7. data/examples/test1.rb +20 -0
  8. data/examples/xmltest.rb +8 -0
  9. data/lib/dbi.rb +323 -0
  10. data/lib/dbi/base_classes.rb +12 -0
  11. data/lib/dbi/base_classes/database.rb +135 -0
  12. data/lib/dbi/base_classes/driver.rb +47 -0
  13. data/lib/dbi/base_classes/statement.rb +167 -0
  14. data/lib/dbi/binary.rb +25 -0
  15. data/lib/dbi/columninfo.rb +106 -0
  16. data/lib/dbi/exceptions.rb +65 -0
  17. data/lib/dbi/handles.rb +49 -0
  18. data/lib/dbi/handles/database.rb +211 -0
  19. data/lib/dbi/handles/driver.rb +60 -0
  20. data/lib/dbi/handles/statement.rb +375 -0
  21. data/lib/dbi/row.rb +249 -0
  22. data/lib/dbi/sql.rb +23 -0
  23. data/lib/dbi/sql/preparedstatement.rb +115 -0
  24. data/lib/dbi/sql_type_constants.rb +75 -0
  25. data/lib/dbi/trace.rb +91 -0
  26. data/lib/dbi/types.rb +158 -0
  27. data/lib/dbi/typeutil.rb +108 -0
  28. data/lib/dbi/utils.rb +60 -0
  29. data/lib/dbi/utils/date.rb +59 -0
  30. data/lib/dbi/utils/tableformatter.rb +112 -0
  31. data/lib/dbi/utils/time.rb +52 -0
  32. data/lib/dbi/utils/timestamp.rb +96 -0
  33. data/lib/dbi/utils/xmlformatter.rb +73 -0
  34. data/test/dbi/tc_columninfo.rb +94 -0
  35. data/test/dbi/tc_date.rb +88 -0
  36. data/test/dbi/tc_dbi.rb +178 -0
  37. data/test/dbi/tc_row.rb +256 -0
  38. data/test/dbi/tc_sqlbind.rb +168 -0
  39. data/test/dbi/tc_statementhandle.rb +16 -0
  40. data/test/dbi/tc_time.rb +77 -0
  41. data/test/dbi/tc_timestamp.rb +142 -0
  42. data/test/dbi/tc_types.rb +220 -0
  43. data/test/dbi/trace.rb +26 -0
  44. data/test/ts_dbi.rb +15 -0
  45. metadata +108 -0
@@ -0,0 +1,16 @@
1
+ $: << 'lib'
2
+ require 'test/unit'
3
+ require 'dbi'
4
+
5
+ class TC_DBI_StatementHandle < Test::Unit::TestCase
6
+ def test_fetch
7
+ mock_handle = 'any_object'
8
+ def mock_handle.cancel; end
9
+ def mock_handle.column_info; {}; end
10
+ def mock_handle.fetch; nil; end
11
+ sth = DBI::StatementHandle.new( mock_handle, true, false )
12
+ 10.times do
13
+ assert_nil sth.fetch
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,77 @@
1
+ ##############################################################################
2
+ # tc_time.rb
3
+ #
4
+ # Test case for the DBI::Time class (currently) located in the utils.rb file.
5
+ ##############################################################################
6
+ $LOAD_PATH.unshift(Dir.pwd)
7
+ $LOAD_PATH.unshift(File.dirname(Dir.pwd))
8
+ $LOAD_PATH.unshift("../../lib")
9
+ $LOAD_PATH.unshift("../../lib/dbi")
10
+ $LOAD_PATH.unshift("lib")
11
+
12
+ require 'dbi'
13
+ require 'test/unit'
14
+
15
+ Deprecate.set_action(proc { })
16
+
17
+ class TC_DBI_Time < Test::Unit::TestCase
18
+ def setup
19
+ @time = Time.new
20
+ @dbi_time = DBI::Time.new
21
+ end
22
+
23
+ def test_constructor
24
+ assert_nothing_raised{ DBI::Time.new(9) }
25
+ assert_nothing_raised{ DBI::Time.new(9, 41) }
26
+ assert_nothing_raised{ DBI::Time.new(9, 41, 20) }
27
+ assert_nothing_raised{ DBI::Time.new(Date.new) }
28
+ assert_nothing_raised{ DBI::Time.new(Time.now) }
29
+ end
30
+
31
+ def test_hour
32
+ assert_respond_to(@dbi_time, :hour)
33
+ assert_respond_to(@dbi_time, :hour=)
34
+ assert_equal(0, @dbi_time.hour)
35
+ end
36
+
37
+ def test_minute
38
+ assert_respond_to(@dbi_time, :minute)
39
+ assert_respond_to(@dbi_time, :minute=)
40
+ assert_equal(0, @dbi_time.minute)
41
+ end
42
+
43
+ # Alias for minute
44
+ def test_min
45
+ assert_respond_to(@dbi_time, :min)
46
+ assert_respond_to(@dbi_time, :min=)
47
+ assert_equal(0, @dbi_time.min)
48
+ end
49
+
50
+ def test_second
51
+ assert_respond_to(@dbi_time, :second)
52
+ assert_respond_to(@dbi_time, :second=)
53
+ assert_equal(0, @dbi_time.second)
54
+ end
55
+
56
+ def test_sec
57
+ assert_respond_to(@dbi_time, :sec)
58
+ assert_respond_to(@dbi_time, :sec=)
59
+ assert_equal(0, @dbi_time.sec)
60
+ end
61
+
62
+ def test_to_time
63
+ assert_respond_to(@dbi_time, :to_time)
64
+ assert_equal(@time, DBI::Time.new(@time).to_time)
65
+ #assert_equal(@time.object_id, DBI::Time.new(@time).object_id) # Fails ??
66
+ end
67
+
68
+ def test_to_s
69
+ assert_respond_to(@dbi_time, :to_s)
70
+ assert_equal("00:00:00", @dbi_time.to_s)
71
+ end
72
+
73
+ def teardown
74
+ @time = nil
75
+ @dbi_time = nil
76
+ end
77
+ end
@@ -0,0 +1,142 @@
1
+ ##############################################################################
2
+ # tc_timestamp.rb
3
+ #
4
+ # Test case for the DBI::Timestamp class (currently) located in the
5
+ # utils.rb file.
6
+ ##############################################################################
7
+ $LOAD_PATH.unshift(Dir.pwd)
8
+ $LOAD_PATH.unshift(File.dirname(Dir.pwd))
9
+ $LOAD_PATH.unshift("../../lib")
10
+ $LOAD_PATH.unshift("../../lib/dbi")
11
+ $LOAD_PATH.unshift("lib")
12
+
13
+ require 'date'
14
+ require 'dbi'
15
+ require 'test/unit'
16
+
17
+ Deprecate.set_action(proc { })
18
+
19
+ class TC_DBI_Date < Test::Unit::TestCase
20
+ def setup
21
+ @date = Date.new
22
+ @time = Time.now
23
+ @dbi_ts = DBI::Timestamp.new(2006, 1, 31, 10, 23, 22, 45)
24
+ end
25
+
26
+ def test_constructor
27
+ assert_nothing_raised{ DBI::Timestamp.new }
28
+ assert_nothing_raised{ DBI::Timestamp.new(2006) }
29
+ assert_nothing_raised{ DBI::Timestamp.new(2006, 1) }
30
+ assert_nothing_raised{ DBI::Timestamp.new(2006, 1, 31) }
31
+ assert_nothing_raised{ DBI::Timestamp.new(2006, 1, 31, 10) }
32
+ assert_nothing_raised{ DBI::Timestamp.new(2006, 1, 31, 10, 23) }
33
+ assert_nothing_raised{ DBI::Timestamp.new(2006, 1, 31, 10, 23, 22) }
34
+ assert_nothing_raised{ DBI::Timestamp.new(2006, 1, 31, 10, 23, 22, 45) }
35
+ end
36
+
37
+ def test_to_date
38
+ assert_respond_to(@dbi_ts, :to_date)
39
+ assert_kind_of(Date, @dbi_ts.to_date)
40
+ end
41
+
42
+ def test_to_time
43
+ assert_respond_to(@dbi_ts, :to_time)
44
+ assert_kind_of(Time, @dbi_ts.to_time)
45
+ end
46
+
47
+ def test_to_s
48
+ assert_respond_to(@dbi_ts, :to_s)
49
+ assert_equal("2006-01-31 10:23:22.000000045", @dbi_ts.to_s)
50
+ assert_equal("2008-03-08 10:39:01.0045",
51
+ DBI::Timestamp.new(2008, 3, 8, 10, 39, 1, 4500000).to_s)
52
+ assert_equal("2008-03-08 10:39:01.0",
53
+ DBI::Timestamp.new(2008, 3, 8, 10, 39, 1, 0).to_s)
54
+ assert_equal("2008-03-08 10:39:01",
55
+ DBI::Timestamp.new(2008, 3, 8, 10, 39, 1, nil).to_s)
56
+ assert_equal("0000-00-00 00:00:00", DBI::Timestamp.new.to_s)
57
+ end
58
+
59
+ def test_equality
60
+ assert_equal(true, @dbi_ts == DBI::Timestamp.new(2006,1,31,10,23,22,45))
61
+ assert_equal(false, @dbi_ts == DBI::Timestamp.new(2006,1,31,10,23,22,46))
62
+ assert_equal(false, @dbi_ts == nil)
63
+ assert_equal(false, @dbi_ts == 1)
64
+ assert_equal(false, @dbi_ts == "hello")
65
+ end
66
+
67
+ def test_fraction
68
+ assert_respond_to(@dbi_ts, :fraction)
69
+ assert_respond_to(@dbi_ts, :fraction=)
70
+ assert_equal(45, @dbi_ts.fraction)
71
+ end
72
+
73
+ def test_second
74
+ assert_respond_to(@dbi_ts, :second)
75
+ assert_respond_to(@dbi_ts, :second=)
76
+ assert_equal(22, @dbi_ts.second)
77
+ end
78
+
79
+ # Alias for second
80
+ def test_sec
81
+ assert_respond_to(@dbi_ts, :sec)
82
+ assert_respond_to(@dbi_ts, :sec=)
83
+ assert_equal(22, @dbi_ts.sec)
84
+ end
85
+
86
+ def test_minute
87
+ assert_respond_to(@dbi_ts, :minute)
88
+ assert_respond_to(@dbi_ts, :minute=)
89
+ assert_equal(23, @dbi_ts.minute)
90
+ end
91
+
92
+ # Alias for minute
93
+ def test_min
94
+ assert_respond_to(@dbi_ts, :min)
95
+ assert_respond_to(@dbi_ts, :min=)
96
+ assert_equal(23, @dbi_ts.min)
97
+ end
98
+
99
+ def test_hour
100
+ assert_respond_to(@dbi_ts, :hour)
101
+ assert_respond_to(@dbi_ts, :hour=)
102
+ assert_equal(10, @dbi_ts.hour)
103
+ end
104
+
105
+ def test_day
106
+ assert_respond_to(@dbi_ts, :day)
107
+ assert_respond_to(@dbi_ts, :day=)
108
+ assert_equal(31, @dbi_ts.day)
109
+ end
110
+
111
+ # Alias for day
112
+ def test_mday
113
+ assert_respond_to(@dbi_ts, :mday)
114
+ assert_respond_to(@dbi_ts, :mday=)
115
+ assert_equal(31, @dbi_ts.mday)
116
+ end
117
+
118
+ def test_month
119
+ assert_respond_to(@dbi_ts, :month)
120
+ assert_respond_to(@dbi_ts, :month=)
121
+ assert_equal(1, @dbi_ts.month)
122
+ end
123
+
124
+ # Alias for month
125
+ def test_mon
126
+ assert_respond_to(@dbi_ts, :mon)
127
+ assert_respond_to(@dbi_ts, :mon=)
128
+ assert_equal(1, @dbi_ts.mon)
129
+ end
130
+
131
+ def test_year
132
+ assert_respond_to(@dbi_ts, :year)
133
+ assert_respond_to(@dbi_ts, :year=)
134
+ assert_equal(2006, @dbi_ts.year)
135
+ end
136
+
137
+ def teardown
138
+ @date = nil
139
+ @time = nil
140
+ @dbi_ts = nil
141
+ end
142
+ end
@@ -0,0 +1,220 @@
1
+ $LOAD_PATH.unshift(Dir.pwd)
2
+ $LOAD_PATH.unshift(File.dirname(Dir.pwd))
3
+ $LOAD_PATH.unshift("../../lib")
4
+ $LOAD_PATH.unshift("../../lib/dbi")
5
+ $LOAD_PATH.unshift("lib")
6
+
7
+ require "dbi"
8
+ require "test/unit"
9
+
10
+ class MyType
11
+ def initialize(obj)
12
+ @obj = obj
13
+ end
14
+
15
+ def to_s
16
+ @obj.to_s
17
+ end
18
+ end
19
+
20
+ class TC_DBI_Type < Test::Unit::TestCase
21
+ def test_null
22
+ # all types except Varchar need to appropriately handle NULL
23
+ [
24
+ DBI::Type::Null,
25
+ DBI::Type::Integer,
26
+ DBI::Type::Float,
27
+ DBI::Type::Timestamp,
28
+ DBI::Type::Boolean
29
+ ].each do |klass|
30
+ assert_equal(nil, klass.parse("NULL"))
31
+ assert_equal(nil, klass.parse("null"))
32
+ assert_equal(nil, klass.parse("Null"))
33
+ end
34
+ end
35
+
36
+ def test_boolean
37
+ klass = DBI::Type::Boolean
38
+ assert_kind_of(NilClass, klass.parse(nil))
39
+ assert_kind_of(NilClass, klass.parse("NULL"))
40
+ assert_kind_of(TrueClass, klass.parse('t'))
41
+ assert_kind_of(TrueClass, klass.parse(1))
42
+ assert_kind_of(TrueClass, klass.parse('1'))
43
+ assert_kind_of(FalseClass, klass.parse('f'))
44
+ assert_kind_of(FalseClass, klass.parse(0))
45
+ assert_kind_of(FalseClass, klass.parse('0'))
46
+ end
47
+
48
+ def test_varchar
49
+ klass = DBI::Type::Varchar
50
+ assert_kind_of(String, klass.parse("hello"))
51
+ assert_kind_of(String, klass.parse("1"))
52
+ assert_kind_of(String, klass.parse("1.23"))
53
+
54
+ assert_equal("NULL", klass.parse("NULL"))
55
+ assert_equal("1", klass.parse("1"))
56
+ assert_equal("hello", klass.parse("hello"))
57
+ assert_equal("1.23", klass.parse("1.23"))
58
+ end
59
+
60
+ def test_integer
61
+ klass = DBI::Type::Integer
62
+ assert_kind_of(Integer, klass.parse("1.23"))
63
+ assert_kind_of(Integer, klass.parse("-1.23"))
64
+ assert_kind_of(Integer, klass.parse("1.0"))
65
+ assert_kind_of(Integer, klass.parse("1"))
66
+ assert_kind_of(Integer, klass.parse("-1"))
67
+ assert_kind_of(Integer, klass.parse("0"))
68
+
69
+ assert_equal(nil, klass.parse("NULL"))
70
+ assert_equal(1, klass.parse("1.23"))
71
+ assert_equal(-1, klass.parse("-1.23"))
72
+ assert_equal(1, klass.parse("1.0"))
73
+ assert_equal(1, klass.parse("1"))
74
+ assert_equal(-1, klass.parse("-1"))
75
+ assert_equal(0, klass.parse("0"))
76
+ end
77
+
78
+ def test_float
79
+ klass = DBI::Type::Float
80
+ assert_kind_of(Float, klass.parse("1.23"))
81
+ assert_kind_of(Float, klass.parse("-1.23"))
82
+ assert_kind_of(Float, klass.parse("1.0"))
83
+ assert_kind_of(Float, klass.parse("1"))
84
+ assert_kind_of(Float, klass.parse("-1"))
85
+ assert_kind_of(Float, klass.parse("0"))
86
+
87
+ assert_equal(nil, klass.parse("NULL"))
88
+ assert_equal(1.23, klass.parse("1.23"))
89
+ assert_equal(-1.23, klass.parse("-1.23"))
90
+ assert_equal(1, klass.parse("1.0"))
91
+ assert_equal(1, klass.parse("1"))
92
+ assert_equal(-1, klass.parse("-1"))
93
+ assert_equal(0, klass.parse("0"))
94
+ end
95
+
96
+ def test_timestamp
97
+ klass = DBI::Type::Timestamp
98
+ assert_kind_of(DateTime, klass.parse(Time.now))
99
+ assert_kind_of(DateTime, klass.parse(Date.today))
100
+ assert_kind_of(DateTime, klass.parse(DateTime.now))
101
+ assert_kind_of(DateTime, klass.parse(Time.now.to_s))
102
+ assert_kind_of(DateTime, klass.parse(Date.today.to_s))
103
+ assert_kind_of(DateTime, klass.parse(DateTime.now.to_s))
104
+
105
+ assert_equal(nil, klass.parse("NULL"))
106
+
107
+ # string coercion
108
+ dt = DateTime.now
109
+ assert_equal(dt.to_s, klass.parse(dt).to_s)
110
+
111
+ t = Time.now
112
+ assert_equal(DateTime.parse(t.to_s), klass.parse(t))
113
+
114
+ d = Date.today
115
+ assert_equal(DateTime.parse(d.to_s), klass.parse(d))
116
+
117
+ # be sure we're actually getting the right data back
118
+ assert_equal(
119
+ "2008-10-11",
120
+ klass.parse(Date.parse("10/11/2008")).strftime("%Y-%m-%d")
121
+ )
122
+
123
+ assert_equal(
124
+ "10:01:02",
125
+ klass.parse(Time.parse("10:01:02")).strftime("%H:%M:%S")
126
+ )
127
+
128
+ assert_equal(
129
+ "10/11/2008 10:01:02",
130
+ klass.parse(DateTime.parse("10/11/2008 10:01:02")).strftime("%m/%d/%Y %H:%M:%S")
131
+ )
132
+ end
133
+ end
134
+
135
+ class TC_DBI_TypeUtil < Test::Unit::TestCase
136
+ def cast(obj)
137
+ DBI::TypeUtil.convert(nil, obj)
138
+ end
139
+
140
+ def datecast(obj)
141
+ "'#{::DateTime.parse(obj.to_s).strftime("%m/%d/%Y %H:%M:%S")}'"
142
+ end
143
+
144
+ def test_default_unknown_cast
145
+ assert_kind_of(String, cast(MyType.new("foo")))
146
+ assert_equal("'foo'", cast(MyType.new("foo")))
147
+ end
148
+
149
+ def test_default_numeric_cast
150
+ assert_kind_of(String, cast(1))
151
+ assert_equal("1", cast(1))
152
+ end
153
+
154
+ def test_default_string_cast
155
+ assert_kind_of(String, cast("foo"))
156
+ assert_equal("'foo'", cast("foo"))
157
+ assert_equal("'foo''bar'", cast("foo'bar"))
158
+ end
159
+
160
+ def test_default_time_casts
161
+ assert_kind_of(String, cast(Time.now))
162
+ assert_kind_of(String, cast(Date.today))
163
+ assert_kind_of(String, cast(DateTime.now))
164
+
165
+ obj = Time.now
166
+ assert_equal(datecast(obj), cast(obj))
167
+ obj = Date.today
168
+ assert_equal(datecast(obj), cast(obj))
169
+ obj = DateTime.now
170
+ assert_equal(datecast(obj), cast(obj))
171
+ end
172
+
173
+ def test_default_boolean_casts
174
+ assert_kind_of(String, cast(false))
175
+ assert_kind_of(String, cast(true))
176
+ assert_kind_of(NilClass, cast(nil))
177
+
178
+ assert_equal("'1'", cast(true))
179
+ assert_equal("'0'", cast(false))
180
+ assert_equal(nil, cast(nil))
181
+ end
182
+
183
+ def test_default_binary_casts
184
+ assert_kind_of(DBI::Binary, cast(DBI::Binary.new("poop")))
185
+ obj = DBI::Binary.new("poop")
186
+ assert_equal(obj.object_id, cast(obj).object_id)
187
+ end
188
+ end
189
+
190
+ DBI::TypeUtil.register_conversion("test") do |obj|
191
+ case obj
192
+ when ::NilClass
193
+ ["Custom Nil", false]
194
+ when ::TrueClass
195
+ ["Custom True", false]
196
+ when ::FalseClass
197
+ ["Custom False", false]
198
+ else
199
+ [obj, true]
200
+ end
201
+ end
202
+
203
+ class TC_DBI_TypeUtil_Custom < Test::Unit::TestCase
204
+ def cast(obj)
205
+ DBI::TypeUtil.convert("test", obj)
206
+ end
207
+
208
+ def test_custom_casts
209
+ assert_equal("Custom Nil", cast(nil))
210
+ assert_equal("Custom True", cast(true))
211
+ assert_equal("Custom False", cast(false))
212
+ end
213
+
214
+ def test_custom_fallthrough
215
+ assert_equal("'foo'", cast("foo"))
216
+ assert_equal("'foo''bar'", cast("foo'bar"))
217
+ assert_equal("1", cast(1))
218
+ assert_equal("'foo'", cast(MyType.new("foo")))
219
+ end
220
+ end