sqlite-ruby 2.1.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,160 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
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 are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * 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
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ $:.unshift "lib"
34
+
35
+ require 'sqlite/parsed_statement'
36
+ require 'test/unit'
37
+
38
+ class TC_ParsedStatement < Test::Unit::TestCase
39
+
40
+ def test_trailing
41
+ sql = %q{first; and second}
42
+ stmt = SQLite::ParsedStatement.new( sql )
43
+ assert_equal "first", stmt.sql
44
+ assert_equal " and second", stmt.trailing
45
+
46
+ sql = %q{first}
47
+ stmt = SQLite::ParsedStatement.new( sql )
48
+ assert_equal "first", stmt.sql
49
+ assert_equal "", stmt.trailing
50
+ end
51
+
52
+ def test_text_only_statement
53
+ sql = %q{select * from some.table t where ( t.b = 'a string' )}
54
+
55
+ stmt = SQLite::ParsedStatement.new( sql )
56
+ expected = "select * from some.table t where ( t.b = 'a string' )"
57
+
58
+ assert_equal expected, stmt.sql
59
+ assert_equal expected, stmt.to_s
60
+ assert_equal expected, stmt.to_str
61
+
62
+ stmt.bind_params
63
+ assert_equal expected, stmt.to_s
64
+ end
65
+
66
+ def test_bind_single_positional_param
67
+ sql = %q{select * from some.table t where ( t.b = ? )}
68
+
69
+ expected = "select * from some.table t where ( t.b = NULL )"
70
+ stmt = SQLite::ParsedStatement.new( sql )
71
+ assert_equal expected, stmt.to_s
72
+
73
+ expected = "select * from some.table t where ( t.b = 'a string' )"
74
+
75
+ stmt = SQLite::ParsedStatement.new( sql )
76
+ stmt.bind_params( "a string" )
77
+ assert_equal expected, stmt.to_s
78
+
79
+ stmt = SQLite::ParsedStatement.new( sql )
80
+ stmt.bind_param( 1, "a string" )
81
+ assert_equal expected, stmt.to_s
82
+ end
83
+
84
+ def test_bind_multiple_positional_params
85
+ sql = %q{? and ? and ?}
86
+ expected = "'one' and NULL and 'O''Reilly'"
87
+
88
+ stmt = SQLite::ParsedStatement.new( sql )
89
+ stmt.bind_param( 1, "one" )
90
+ stmt.bind_param( 3, "O'Reilly" )
91
+ stmt.bind_param( 4, "ignored" )
92
+ assert_equal expected, stmt.to_s
93
+
94
+ stmt = SQLite::ParsedStatement.new( sql )
95
+ stmt.bind_params( "one", nil, "O'Reilly", "ignored" )
96
+ assert_equal expected, stmt.to_s
97
+ end
98
+
99
+ def test_syntax_bind_positional_params
100
+ sql = %q{? and ? and ?1}
101
+ expected = "'one' and NULL and 'one'"
102
+
103
+ stmt = SQLite::ParsedStatement.new( sql )
104
+ stmt.bind_param( 1, "one" )
105
+ stmt.bind_param( 3, "O'Reilly" )
106
+ stmt.bind_param( 4, "ignored" )
107
+ assert_equal expected, stmt.to_s
108
+
109
+ stmt = SQLite::ParsedStatement.new( sql )
110
+ stmt.bind_params( "one", nil, "O'Reilly", "ignored" )
111
+ assert_equal expected, stmt.to_s
112
+
113
+ sql = %q{:2 and ? and ?1 and :4:}
114
+ expected = "NULL and 'O''Reilly' and 'one' and 'ignored'"
115
+ stmt = SQLite::ParsedStatement.new( sql )
116
+ stmt.bind_params( "one", nil, "O'Reilly", "ignored" )
117
+ assert_equal expected, stmt.to_s
118
+ end
119
+
120
+ def test_bind_named_params
121
+ sql = %q{:name and :spouse:}
122
+ expected = "'joe' and NULL"
123
+
124
+ stmt = SQLite::ParsedStatement.new( sql )
125
+ stmt.bind_param( "name", "joe" )
126
+ assert_equal expected, stmt.to_s
127
+
128
+ stmt = SQLite::ParsedStatement.new( sql )
129
+ stmt.bind_params( "name"=>"joe", "spouse"=>nil )
130
+ assert_equal expected, stmt.to_s
131
+
132
+ stmt = SQLite::ParsedStatement.new( sql )
133
+ stmt.bind_params( "name"=>"joe", "spouse"=>"jane" )
134
+ assert_equal "'joe' and 'jane'", stmt.to_s
135
+ end
136
+
137
+ def test_mixed_params
138
+ sql = %q{:name and :spouse: and ?2 and ? and :1 and :2:}
139
+ stmt = SQLite::ParsedStatement.new( sql )
140
+ stmt.bind_params( "one", 2, "three",
141
+ "name"=>"joe", "spouse"=>"jane" )
142
+
143
+ assert_equal "'joe' and 'jane' and 2 and 'three' and 'one' and 2", stmt.to_s
144
+ end
145
+
146
+ def test_sql
147
+ sql = %q{:name and :spouse: and ?2 and ? and :1 and :2:}
148
+ stmt = SQLite::ParsedStatement.new( sql )
149
+ assert_equal ":name and :spouse and :2 and :3 and :1 and :2", stmt.sql
150
+ end
151
+
152
+ def test_placeholders
153
+ sql = %q{:name and :spouse: and ? and ?5 and :12 and :15:}
154
+
155
+ stmt = SQLite::ParsedStatement.new( sql )
156
+ assert_equal 6, stmt.placeholders.length
157
+ assert( ( [ "name", "spouse", 1, 5, 12, 15 ] - stmt.placeholders ).empty? )
158
+ end
159
+
160
+ end
@@ -0,0 +1,207 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
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 are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * 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
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ $:.unshift "lib"
34
+
35
+ require 'sqlite'
36
+ require 'test/unit'
37
+
38
+ class TC_Pragmas < Test::Unit::TestCase
39
+
40
+ def setup
41
+ @db = SQLite::Database.open( "db/fixtures.db" )
42
+ end
43
+
44
+ def teardown
45
+ @db.close
46
+ end
47
+
48
+ def test_integrity_check
49
+ assert_nothing_raised do
50
+ @db.integrity_check
51
+ end
52
+ end
53
+
54
+ def test_cache_size
55
+ size = @db.cache_size
56
+ assert_instance_of Fixnum, size
57
+ @db.cache_size = size + 100
58
+ new_size = @db.cache_size
59
+ assert_equal size+100, new_size
60
+ end
61
+
62
+ def test_default_cache_size
63
+ size = @db.default_cache_size
64
+ assert_instance_of Fixnum, size
65
+ @db.default_cache_size = size + 100
66
+ new_size = @db.default_cache_size
67
+ assert_equal size+100, new_size
68
+ end
69
+
70
+ def test_synchronous
71
+ assert_raise( SQLite::Exceptions::DatabaseException ) do
72
+ @db.synchronous = "bogus"
73
+ end
74
+
75
+ assert_nothing_raised do
76
+ @db.synchronous = "full"
77
+ @db.synchronous = 2
78
+ @db.synchronous = "normal"
79
+ @db.synchronous = 1
80
+ @db.synchronous = "off"
81
+ @db.synchronous = 0
82
+ end
83
+
84
+ assert_equal "0", @db.synchronous
85
+ end
86
+
87
+ def test_default_synchronous
88
+ assert_raise( SQLite::Exceptions::DatabaseException ) do
89
+ @db.default_synchronous = "bogus"
90
+ end
91
+
92
+ assert_nothing_raised do
93
+ @db.default_synchronous = "full"
94
+ @db.default_synchronous = 2
95
+ @db.default_synchronous = "normal"
96
+ @db.default_synchronous = 1
97
+ @db.default_synchronous = "off"
98
+ @db.default_synchronous = 0
99
+ end
100
+
101
+ assert_equal "0", @db.default_synchronous
102
+ end
103
+
104
+ def test_temp_store
105
+ assert_raise( SQLite::Exceptions::DatabaseException ) do
106
+ @db.temp_store = "bogus"
107
+ end
108
+
109
+ assert_nothing_raised do
110
+ @db.temp_store = "memory"
111
+ @db.temp_store = 2
112
+ @db.temp_store = "file"
113
+ @db.temp_store = 1
114
+ @db.temp_store = "default"
115
+ @db.temp_store = 0
116
+ end
117
+
118
+ assert_equal "0", @db.temp_store
119
+ end
120
+
121
+ def test_default_temp_store
122
+ assert_raise( SQLite::Exceptions::DatabaseException ) do
123
+ @db.default_temp_store = "bogus"
124
+ end
125
+
126
+ assert_nothing_raised do
127
+ @db.default_temp_store = "memory"
128
+ @db.default_temp_store = 2
129
+ @db.default_temp_store = "file"
130
+ @db.default_temp_store = 1
131
+ @db.default_temp_store = "default"
132
+ @db.default_temp_store = 0
133
+ end
134
+
135
+ assert_equal "0", @db.default_temp_store
136
+ end
137
+
138
+ def test_full_column_names
139
+ assert_raise( SQLite::Exceptions::DatabaseException ) do
140
+ @db.full_column_names = "sure"
141
+ end
142
+
143
+ assert_raise( SQLite::Exceptions::DatabaseException ) do
144
+ @db.full_column_names = :yes
145
+ end
146
+
147
+ assert_nothing_raised do
148
+ @db.full_column_names = "yes"
149
+ @db.full_column_names = "no"
150
+ @db.full_column_names = 1
151
+ @db.full_column_names = 0
152
+ @db.full_column_names = true
153
+ @db.full_column_names = false
154
+ @db.full_column_names = nil
155
+ @db.full_column_names = "y"
156
+ @db.full_column_names = "n"
157
+ @db.full_column_names = "t"
158
+ @db.full_column_names = "f"
159
+ end
160
+
161
+ assert !@db.full_column_names
162
+ end
163
+
164
+ def test_parser_trace
165
+ # apparently, the parser_trace pragma always returns true...?
166
+ assert @db.parser_trace
167
+ #@db.parser_trace = false
168
+ #assert !@db.parser_trace
169
+ end
170
+
171
+ def test_vdbe_trace
172
+ @db.vdbe_trace = true
173
+ assert @db.vdbe_trace
174
+ @db.vdbe_trace = false
175
+ assert !@db.vdbe_trace
176
+ end
177
+
178
+ def test_database_list
179
+ assert_equal ["main","temp"], @db.database_list.map { |i| i[1] }
180
+ end
181
+
182
+ def test_foreign_key_list
183
+ list = @db.foreign_key_list( "D" )
184
+ assert_equal 1, list.size
185
+ assert_equal "B", list.first[2]
186
+ end
187
+
188
+ def test_index_info
189
+ info = @db.index_info( "B_idx" )
190
+ assert_equal 1, info.size
191
+ assert_equal "name", info.first[2]
192
+ end
193
+
194
+ def test_index_list
195
+ list = @db.index_list( "B" )
196
+ assert_equal 1, list.size
197
+ assert_equal "B_idx", list.first[1]
198
+ end
199
+
200
+ def test_table_info
201
+ info = @db.table_info( "A" )
202
+ assert_equal 2, info.size
203
+ assert_equal "name", info[0][1]
204
+ assert_equal "age", info[1][1]
205
+ end
206
+
207
+ end
@@ -0,0 +1,115 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
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 are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * 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
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ $:.unshift "lib"
34
+
35
+ require 'base64'
36
+ require 'sqlite/translator'
37
+ require 'test/unit'
38
+
39
+ class TC_Translator < Test::Unit::TestCase
40
+
41
+ def setup
42
+ @translator = SQLite::Translator.new
43
+ end
44
+
45
+ def test_date_time
46
+ value = "2004-09-08 14:09:39"
47
+ expect = Time.mktime( 2004, 9, 8, 14, 9, 39 )
48
+
49
+ assert_equal expect, @translator.translate( "date", value )
50
+ assert_equal expect, @translator.translate( "datetime", value )
51
+ assert_equal expect, @translator.translate( "time", value )
52
+ end
53
+
54
+ def test_real
55
+ value = "3.1415"
56
+ expect = 3.1415
57
+
58
+ assert_equal expect, @translator.translate( "decimal", value )
59
+ assert_equal expect, @translator.translate( "float", value )
60
+ assert_equal expect, @translator.translate( "numeric", value )
61
+ assert_equal expect, @translator.translate( "double", value )
62
+ assert_equal expect, @translator.translate( "real", value )
63
+ assert_equal expect, @translator.translate( "dec", value )
64
+ assert_equal expect, @translator.translate( "fixed", value )
65
+ end
66
+
67
+ def test_integer
68
+ value = "128"
69
+ expect = 128
70
+
71
+ assert_equal expect, @translator.translate( "integer", value )
72
+ assert_equal expect, @translator.translate( "smallint", value )
73
+ assert_equal expect, @translator.translate( "mediumint", value )
74
+ assert_equal expect, @translator.translate( "int", value )
75
+ assert_equal expect, @translator.translate( "bigint", value )
76
+ end
77
+
78
+ def test_boolean
79
+ assert_equal true, @translator.translate( "bit", "1" )
80
+ assert_equal false, @translator.translate( "bit", "0" )
81
+ assert_equal false, @translator.translate( "bool", "0" )
82
+ assert_equal false, @translator.translate( "bool", "false" )
83
+ assert_equal false, @translator.translate( "bool", "f" )
84
+ assert_equal false, @translator.translate( "bool", "no" )
85
+ assert_equal false, @translator.translate( "bool", "n" )
86
+ assert_equal false, @translator.translate( "boolean", "0" )
87
+ assert_equal false, @translator.translate( "boolean", "false" )
88
+ assert_equal false, @translator.translate( "boolean", "f" )
89
+ assert_equal false, @translator.translate( "boolean", "no" )
90
+ assert_equal false, @translator.translate( "boolean", "n" )
91
+ assert_equal true, @translator.translate( "bool", "heck ya!" )
92
+ assert_equal true, @translator.translate( "boolean", "heck ya!" )
93
+ end
94
+
95
+ def test_timestamp
96
+ time = Time.mktime( 2004, 9, 8, 14, 9, 39 )
97
+ assert_equal time, @translator.translate( "timestamp", time.to_i.to_s )
98
+ end
99
+
100
+ def test_tinyint
101
+ assert_equal true, @translator.translate( "tinyint(1)", "1" )
102
+ assert_equal false, @translator.translate( "tinyint(1)", "0" )
103
+ assert_equal 16, @translator.translate( "tinyint", "16" )
104
+ end
105
+
106
+ def test_custom
107
+ @translator.add_translator( "object" ) { |t,v| Marshal.load( Base64.decode64( v ) ) }
108
+
109
+ value = { :one => "hello", :four => "blah" }
110
+ dump = Base64.encode64( Marshal.dump( value ) ).strip
111
+
112
+ assert_equal value, @translator.translate( "object", dump )
113
+ end
114
+
115
+ end