ruby-informix 0.6.2-i386-mswin32 → 0.7.0-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,169 @@
1
+ # $Id: seqcursor.rb,v 1.3 2008/03/29 07:35:03 santana Exp $
2
+ #
3
+ # Copyright (c) 2008, Gerardo Santana Gomez Garrido <gerardo.santana@gmail.com>
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions
8
+ # are met:
9
+ #
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 BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
+ # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22
+ # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
+ # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
+ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
+ # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ # POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'informixc'
31
+
32
+ module Informix
33
+ class SequentialCursor < CursorBase
34
+ include Enumerable
35
+
36
+ # Fetches the next record.
37
+ #
38
+ # Returns the record fetched as an array, or nil if there are no
39
+ # records left.
40
+ #
41
+ # cursor.fetch => array or nil
42
+ def fetch
43
+ fetch0(Array, false)
44
+ end
45
+
46
+ # Fetches the next record, storing it in the same Array object every time
47
+ # it is called.
48
+ #
49
+ # Returns the record fetched as an array, or nil if there are no
50
+ # records left.
51
+ #
52
+ # cursor.fetch! => array or nil
53
+ def fetch!
54
+ fetch0(Array, true)
55
+ end
56
+
57
+ # Fetches the next record.
58
+ #
59
+ # Returns the record fetched as a hash, or nil if there are no
60
+ # records left.
61
+ #
62
+ # cursor.fetch_hash => hash or nil
63
+ def fetch_hash
64
+ fetch0(Hash, false)
65
+ end
66
+
67
+ # Fetches the next record, storing it in the same Hash object every time
68
+ # it is called.
69
+ #
70
+ # Returns the record fetched as a hash, or nil if there are no
71
+ # records left.
72
+ #
73
+ # cursor.fetch_hash! => hash or nil
74
+ def fetch_hash!
75
+ fetch0(Hash, true)
76
+ end
77
+
78
+ # Reads at most <i>n</i> records.
79
+ #
80
+ # Returns the records read as an array of arrays
81
+ #
82
+ # cursor.fetch_many(n) => array
83
+ def fetch_many(n)
84
+ fetch_many0(n, Array)
85
+ end
86
+
87
+ # Reads at most <i>n</i> records.
88
+ # Returns the records read as an array of hashes.
89
+ #
90
+ # cursor.fetch_hash_many(n) => array
91
+ def fetch_hash_many(n)
92
+ fetch_many0(n, Hash)
93
+ end
94
+
95
+ # Returns all the records left as an array of arrays
96
+ #
97
+ # cursor.fetch_all => array
98
+ def fetch_all
99
+ fetch_many0(nil, Array)
100
+ end
101
+
102
+ # Returns all the records left as an array of hashes
103
+ #
104
+ # cursor.fetch_hash_all => array
105
+ def fetch_hash_all
106
+ fetch_many0(nil, Hash)
107
+ end
108
+
109
+ # Iterates over the remaining records, passing each <i>record</i> to the
110
+ # <i>block</i> as an array.
111
+ #
112
+ # Returns __self__.
113
+ #
114
+ # cursor.each {|record| block } => cursor
115
+ def each(&block)
116
+ each0(Array, false, &block)
117
+ end
118
+
119
+ # Iterates over the remaining records, passing each <i>record</i> to the
120
+ # <i>block</i> as an array. No new Array objects are created for each
121
+ # record. The same Array object is reused in each call.
122
+ #
123
+ # Returns __self__.
124
+ #
125
+ # cursor.each! {|record| block } => cursor
126
+ def each!(&block)
127
+ each0(Array, true, &block)
128
+ end
129
+
130
+ # Iterates over the remaining records, passing each <i>record</i> to the
131
+ # <i>block</i> as a hash.
132
+ #
133
+ # cursor.each_hash {|record| block } => cursor
134
+ def each_hash(&block)
135
+ each0(Hash, false, &block)
136
+ end
137
+
138
+ # Iterates over the remaining records, passing each <i>record</i> to the
139
+ # <i>block</i> as a hash. No new Hash objects are created for each record.
140
+ # The same Hash object is reused in each call.
141
+ #
142
+ # Returns __self__.
143
+ #
144
+ # cursor.each_hash! {|record| block } => cursor
145
+ def each_hash!(&block)
146
+ each0(Hash, true, &block)
147
+ end
148
+
149
+ # Iterates over the remaining records, passing at most <i>n</i>
150
+ # <i>records</i> to the <i>block</i> as arrays.
151
+ #
152
+ # Returns __self__.
153
+ #
154
+ # cursor.each_by(n) {|records| block } => cursor
155
+ def each_by(n, &block)
156
+ each_by0(n, Array, &block)
157
+ end
158
+
159
+ # Iterates over the remaining records, passing at most <i>n</i>
160
+ # <i>records</i> to the <i>block</i> as hashes.
161
+ #
162
+ # Returns __self__.
163
+ #
164
+ # cursor.each_hash_by(n) {|records| block } => cursor
165
+ def each_hash_by(n, &block)
166
+ each_by0(n, Hash, &block)
167
+ end
168
+ end # class SequentialCursor
169
+ end # module Informix
data/lib/informixc.so ADDED
Binary file
data/test/ifx_all.rb ADDED
@@ -0,0 +1,31 @@
1
+ testdir = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift File.join(testdir, "..")
3
+
4
+ require 'informix'
5
+ require 'test/unit'
6
+
7
+ class IfxAll
8
+ def IfxAll.suite
9
+ suite = Test::Unit::TestSuite.new "Ruby Informix Test Suite"
10
+ Object.constants.grep(/^IfxTest/).sort.each do |const_name|
11
+ if (c = Object.const_get(const_name)).kind_of?(Class) && c.respond_to?(:suite)
12
+ puts "Adding #{const_name}"
13
+ suite << c.suite
14
+ end
15
+ end
16
+ suite
17
+ end
18
+ end
19
+
20
+ if __FILE__ == $0
21
+ if ARGV.size == 0
22
+ STDERR.puts "Usage:
23
+ ruby #{$0} database [username password]"
24
+ exit 1
25
+ end
26
+ require 'test/unit/ui/console/testrunner'
27
+ Dir.glob(File.join(testdir, "ifx_test*.rb")).each do |testcase|
28
+ require "#{testcase}"
29
+ end
30
+ Test::Unit::UI::Console::TestRunner.run(IfxAll)
31
+ end
@@ -0,0 +1,26 @@
1
+ testdir = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift testdir
3
+ $LOAD_PATH.unshift File.join(testdir, "..")
4
+
5
+ require 'informix'
6
+ require 'test/unit'
7
+ require 'testcase'
8
+
9
+ class IfxTestCreate < Informix::TestCase
10
+ def setup
11
+ connect_string, username, password = ARGV[0..2]
12
+ super connect_string, username, password
13
+ end
14
+
15
+ def test_create_table
16
+ drop_test_table
17
+
18
+ assert_nothing_raised(Informix::Error, "Creating table #{TEST_TABLE_NAME}") do
19
+ begin
20
+ create_test_table
21
+ ensure
22
+ drop_test_table
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ testdir = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift testdir
3
+ $LOAD_PATH.unshift File.join(testdir, "..")
4
+
5
+ require 'informix'
6
+ require 'test/unit'
7
+ require 'testcase'
8
+
9
+ # Informix::ExcInfo is just a struct, not much testing needed
10
+ class IfxTestExcInfo < Informix::TestCase
11
+ # Don't need an Informix database for these tests
12
+ def setup; end
13
+
14
+ def test_initialize
15
+ sql_code, sql_state, class_origin, subclass_origin, message, server_name, connection_name =
16
+ -987, "IX111", "IX", "000", "Some message", "server", "conn"
17
+
18
+ info = Informix::ExcInfo.new(sql_code,
19
+ sql_state,
20
+ class_origin,
21
+ subclass_origin,
22
+ message,
23
+ server_name,
24
+ connection_name)
25
+
26
+ assert_equal sql_code, info.sql_code
27
+ assert_equal sql_state, info.sql_state
28
+ assert_equal class_origin, info.class_origin
29
+ assert_equal subclass_origin, info.subclass_origin
30
+ assert_equal message, info.message
31
+ assert_equal server_name, info.server_name
32
+ assert_equal connection_name, info.connection_name
33
+ end
34
+
35
+ end
36
+
37
+
@@ -0,0 +1,71 @@
1
+ testdir = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift testdir
3
+ $LOAD_PATH.unshift File.join(testdir, "..")
4
+
5
+ require 'informix'
6
+ require 'test/unit'
7
+ require 'testcase'
8
+
9
+ class IfxTestExceptions < Informix::TestCase
10
+ def info_params
11
+ [
12
+ [ -100, "IX111", "IX", "000", "Some message", "server", "conn" ],
13
+ [ -200, "IX111", "IX", "000", "Some message", "server", "conn" ],
14
+ [ -300, "IX111", "IX", "000", "Some message", "server", "conn" ]
15
+ ]
16
+ end
17
+
18
+ # Don't need an Informix database for these tests
19
+ def setup
20
+ @info_arr = info_params.map { |arr| Informix::ExcInfo.new(*arr) }
21
+ @test_exc = Informix::Error.new @info_arr
22
+ end
23
+
24
+ # Also tests Informix::Error#each via each_with_index
25
+ def test_add_info
26
+ params = self.info_params
27
+ exc = Informix::Error.new
28
+ params.each { |arr| exc.add_info(*arr) }
29
+ exc.each_with_index { |info, i| assert_equal params[i], info.to_a }
30
+ end
31
+
32
+ def test_each_for_empty_exception
33
+ exc = Informix::Error.new
34
+ assert_nothing_raised { exc.each { |x| } }
35
+ end
36
+
37
+ def test_size
38
+ assert_equal info_params.size, @test_exc.size
39
+ end
40
+
41
+ def test_length
42
+ assert_equal info_params.length, @test_exc.length
43
+ end
44
+
45
+ def test_initialize_type_error
46
+ assert_raise(TypeError) { Informix::Error.new [1, 2, 3] }
47
+ assert_raise(TypeError) { Informix::Error.new 0 }
48
+ end
49
+
50
+ def test_initialize_with_string
51
+ err = nil
52
+ expected_msg = "This is a message"
53
+ assert_nothing_raised { err = Informix::Error.new expected_msg }
54
+ assert_equal expected_msg, err.message
55
+ end
56
+
57
+ def test_to_s
58
+ assert_nothing_raised { @test_exc.to_s }
59
+ end
60
+
61
+ def test_at
62
+ @test_exc.size.times { |i| assert_equal @info_arr[i], @test_exc[i] }
63
+ end
64
+
65
+ def test_sqlcode
66
+ assert_equal -100, @test_exc.sql_code
67
+ assert_equal 0, Informix::Error.new.sql_code
68
+ end
69
+
70
+ end
71
+
@@ -0,0 +1,64 @@
1
+ testdir = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift testdir
3
+ $LOAD_PATH.unshift File.join(testdir, "..")
4
+
5
+ require 'informix'
6
+ require 'test/unit'
7
+ require 'testcase'
8
+ require 'stringio'
9
+ require 'date'
10
+
11
+ class IfxTestXFetchNEach < Informix::TestCase
12
+ def setup
13
+ connect_string, username, password = ARGV[0..2]
14
+ super connect_string, username, password
15
+ drop_test_table
16
+ create_test_table
17
+ populate_test_table
18
+
19
+ assert_nothing_raised(Informix::Error, "Selecting records") do
20
+ @c = @db.cursor("select * from #{TEST_TABLE_NAME}").open
21
+ end
22
+ end
23
+
24
+ def teardown
25
+ @c.drop
26
+ end
27
+
28
+ def test_fetch_each_each_by
29
+ fetch = []
30
+ while r = @c.fetch; fetch << r; end
31
+ @c.close
32
+
33
+ each = []
34
+ @c.open.each {|r| each << r }.close
35
+
36
+ each_by = []
37
+ @c.open.each_by(2) {|a, b|
38
+ each_by << a if a
39
+ each_by << b if b
40
+ }
41
+ assert_equal(fetch.flatten, each.flatten, "fetch & each")
42
+ assert_equal(fetch.flatten, each_by.flatten, "fetch & each_by")
43
+ end
44
+
45
+ def test_fetch_each_each_by_hash
46
+ fetch_hash = []
47
+ @c.open
48
+ while r = @c.fetch_hash; fetch_hash << r; end
49
+ @c.close
50
+
51
+ each_hash = []
52
+ @c.open.each_hash {|r| each_hash << r }.close
53
+
54
+ each_hash_by = []
55
+ @c.open.each_hash_by(2) {|a, b|
56
+ each_hash_by << a if a
57
+ each_hash_by << b if b
58
+ }
59
+ assert_equal(fetch_hash.flatten, each_hash.flatten,
60
+ "fetch_hash & each_hash")
61
+ assert_equal(fetch_hash.flatten, each_hash_by.flatten,
62
+ "fetch_hash & each_hash_by")
63
+ end
64
+ end
@@ -0,0 +1,27 @@
1
+ testdir = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift testdir
3
+ $LOAD_PATH.unshift File.join(testdir, "..")
4
+
5
+ require 'informix'
6
+ require 'test/unit'
7
+ require 'testcase'
8
+ require 'stringio'
9
+ require 'date'
10
+
11
+ class IfxTestInsert < Informix::TestCase
12
+ def setup
13
+ connect_string, username, password = ARGV[0..2]
14
+ super connect_string, username, password
15
+ drop_test_table
16
+ create_test_table
17
+ end
18
+
19
+ def test_insert
20
+ begin
21
+ populate_test_table
22
+ ensure
23
+ drop_test_table
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,87 @@
1
+ testdir = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift testdir
3
+ $LOAD_PATH.unshift File.join(testdir, "..")
4
+
5
+ require 'informix'
6
+ require 'test/unit'
7
+ require 'testcase'
8
+ require 'stringio'
9
+ require 'date'
10
+
11
+ class IfxTestSelect < Informix::TestCase
12
+ def setup
13
+ connect_string, username, password = ARGV[0..2]
14
+ super connect_string, username, password
15
+ drop_test_table
16
+ create_test_table
17
+ populate_test_table
18
+ end
19
+
20
+ def test_select
21
+ rows = nil
22
+
23
+ assert_nothing_raised(Informix::Error, "Selecting records") do
24
+ rows = db.cursor('select * from test').open.fetch_all
25
+ end
26
+
27
+ assert_equal(2, rows.size, "# of records retrieved")
28
+
29
+ exp_rows = @rows
30
+
31
+ 2.times do |i|
32
+ assert_equal(exp_rows[i].shift, rows[i].shift, "serial")
33
+ assert_equal(exp_rows[i].shift, rows[i].shift, "char")
34
+ assert_equal(exp_rows[i].shift, rows[i].shift, "varchar")
35
+ assert_equal(exp_rows[i].shift, rows[i].shift, "smallint")
36
+ assert_equal(exp_rows[i].shift, rows[i].shift, "integer")
37
+
38
+ assert_in_delta(exp_rows[i].shift, rows[i].shift, 1e-6, "smallfloat")
39
+ assert_in_delta(exp_rows[i].shift, rows[i].shift, 1e-14, "float")
40
+
41
+ obj = rows[i].shift
42
+ obj = obj.strftime("%m/%d/%Y") if obj.respond_to? :strftime
43
+ exp = exp_rows[i].shift
44
+ exp = exp.strftime("%m/%d/%Y") if exp.respond_to? :strftime
45
+
46
+ assert_equal(exp, obj, "date")
47
+
48
+ obj = rows[i].shift
49
+ obj = obj.strftime("%Y-%m-%d %H:%M:%S.000") if obj.respond_to? :strftime
50
+ exp = exp_rows[i].shift
51
+ exp = exp.strftime("%Y-%m-%d %H:%M:%S.000") if exp.respond_to? :strftime
52
+
53
+ assert_equal(exp, obj, "datetime")
54
+
55
+ assert_equal(exp_rows[i].shift, rows[i].shift, "interval year to month")
56
+ assert_equal(exp_rows[i].shift, rows[i].shift, "interval day to second")
57
+
58
+ assert_equal(exp_rows[i].shift, rows[i].shift, "decimal")
59
+
60
+ obj = rows[i].shift
61
+ exp = exp_rows[i].shift
62
+
63
+ if obj.class == StringIO
64
+ obj.rewind
65
+ obj = obj.read # Convert to string
66
+ end
67
+
68
+ if exp.class == StringIO
69
+ exp.rewind
70
+ exp = exp.read # Convert to string
71
+ end
72
+
73
+ assert_equal(exp, obj, "text")
74
+
75
+ if @supported["boolean"]
76
+ assert_equal(exp_rows[i].shift, rows[i].shift ? 't': 'f', "boolean")
77
+ end
78
+
79
+ if @supported["int8"]
80
+ assert_equal(exp_rows[i].shift, rows[i].shift, "int8")
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+
87
+