sqlpostgres 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +22 -0
  3. data/LICENSE.md +23 -0
  4. data/README.rdoc +59 -0
  5. data/Rakefile +32 -0
  6. data/VERSION +1 -0
  7. data/doc/BUGS +2 -0
  8. data/doc/examples/README +6 -0
  9. data/doc/examples/connection.rb +16 -0
  10. data/doc/examples/connection_auto.rb +22 -0
  11. data/doc/examples/connection_ctor.rb +18 -0
  12. data/doc/examples/connection_default.rb +15 -0
  13. data/doc/examples/connection_exec.rb +18 -0
  14. data/doc/examples/connection_manual.rb +12 -0
  15. data/doc/examples/connection_wrapped_new.rb +13 -0
  16. data/doc/examples/connection_wrapped_open.rb +13 -0
  17. data/doc/examples/cursor.rb +38 -0
  18. data/doc/examples/include_module.rb +9 -0
  19. data/doc/examples/include_module2.rb +12 -0
  20. data/doc/examples/insert.rb +30 -0
  21. data/doc/examples/insert2.rb +36 -0
  22. data/doc/examples/insert_bytea.rb +16 -0
  23. data/doc/examples/insert_bytea_array.rb +17 -0
  24. data/doc/examples/insert_default_values.rb +16 -0
  25. data/doc/examples/insert_insert.rb +16 -0
  26. data/doc/examples/insert_insert_default.rb +16 -0
  27. data/doc/examples/insert_insert_select.rb +20 -0
  28. data/doc/examples/insert_select.rb +20 -0
  29. data/doc/examples/interval.rb +17 -0
  30. data/doc/examples/savepoint.rb +38 -0
  31. data/doc/examples/select.rb +33 -0
  32. data/doc/examples/select2.rb +36 -0
  33. data/doc/examples/select_cross_join.rb +18 -0
  34. data/doc/examples/select_distinct.rb +18 -0
  35. data/doc/examples/select_distinct_on +19 -0
  36. data/doc/examples/select_for_update.rb +18 -0
  37. data/doc/examples/select_from.rb +17 -0
  38. data/doc/examples/select_from_subselect.rb +20 -0
  39. data/doc/examples/select_group_by.rb +19 -0
  40. data/doc/examples/select_having.rb +20 -0
  41. data/doc/examples/select_join_on.rb +18 -0
  42. data/doc/examples/select_join_using.rb +18 -0
  43. data/doc/examples/select_limit.rb +19 -0
  44. data/doc/examples/select_natural_join.rb +18 -0
  45. data/doc/examples/select_offset.rb +19 -0
  46. data/doc/examples/select_order_by.rb +20 -0
  47. data/doc/examples/select_select.rb +30 -0
  48. data/doc/examples/select_select_alias.rb +30 -0
  49. data/doc/examples/select_select_expression.rb +31 -0
  50. data/doc/examples/select_select_literal.rb +24 -0
  51. data/doc/examples/select_union.rb +21 -0
  52. data/doc/examples/select_where_array.rb +18 -0
  53. data/doc/examples/select_where_in.rb +18 -0
  54. data/doc/examples/select_where_string.rb +18 -0
  55. data/doc/examples/simple.rb +34 -0
  56. data/doc/examples/transaction.rb +30 -0
  57. data/doc/examples/transaction_abort.rb +30 -0
  58. data/doc/examples/transaction_commit.rb +34 -0
  59. data/doc/examples/translate_substitute_values.rb +17 -0
  60. data/doc/examples/update.rb +32 -0
  61. data/doc/examples/update2.rb +44 -0
  62. data/doc/examples/update_only.rb +17 -0
  63. data/doc/examples/update_set.rb +17 -0
  64. data/doc/examples/update_set_array.rb +16 -0
  65. data/doc/examples/update_set_bytea.rb +16 -0
  66. data/doc/examples/update_set_expression.rb +16 -0
  67. data/doc/examples/update_set_subselect.rb +20 -0
  68. data/doc/examples/update_where.rb +17 -0
  69. data/doc/examples/use_prefix.rb +8 -0
  70. data/doc/examples/use_prefix2.rb +11 -0
  71. data/doc/index.html +31 -0
  72. data/doc/insertexamples.rb +9 -0
  73. data/doc/makemanual +4 -0
  74. data/doc/makerdoc +5 -0
  75. data/doc/manual.dbk +622 -0
  76. data/lib/sqlpostgres/Connection.rb +198 -0
  77. data/lib/sqlpostgres/Cursor.rb +157 -0
  78. data/lib/sqlpostgres/Delete.rb +67 -0
  79. data/lib/sqlpostgres/Exceptions.rb +15 -0
  80. data/lib/sqlpostgres/Insert.rb +279 -0
  81. data/lib/sqlpostgres/NullConnection.rb +22 -0
  82. data/lib/sqlpostgres/PgBit.rb +73 -0
  83. data/lib/sqlpostgres/PgBox.rb +37 -0
  84. data/lib/sqlpostgres/PgCidr.rb +21 -0
  85. data/lib/sqlpostgres/PgCircle.rb +75 -0
  86. data/lib/sqlpostgres/PgInet.rb +21 -0
  87. data/lib/sqlpostgres/PgInterval.rb +208 -0
  88. data/lib/sqlpostgres/PgLineSegment.rb +37 -0
  89. data/lib/sqlpostgres/PgMacAddr.rb +21 -0
  90. data/lib/sqlpostgres/PgPath.rb +64 -0
  91. data/lib/sqlpostgres/PgPoint.rb +65 -0
  92. data/lib/sqlpostgres/PgPolygon.rb +56 -0
  93. data/lib/sqlpostgres/PgTime.rb +77 -0
  94. data/lib/sqlpostgres/PgTimeWithTimeZone.rb +98 -0
  95. data/lib/sqlpostgres/PgTimestamp.rb +93 -0
  96. data/lib/sqlpostgres/PgTwoPoints.rb +54 -0
  97. data/lib/sqlpostgres/PgType.rb +34 -0
  98. data/lib/sqlpostgres/PgWrapper.rb +41 -0
  99. data/lib/sqlpostgres/Savepoint.rb +98 -0
  100. data/lib/sqlpostgres/Select.rb +855 -0
  101. data/lib/sqlpostgres/Transaction.rb +120 -0
  102. data/lib/sqlpostgres/Translate.rb +436 -0
  103. data/lib/sqlpostgres/Update.rb +188 -0
  104. data/lib/sqlpostgres.rb +67 -0
  105. data/test/Assert.rb +72 -0
  106. data/test/Connection.test.rb +246 -0
  107. data/test/Cursor.test.rb +190 -0
  108. data/test/Delete.test.rb +68 -0
  109. data/test/Insert.test.rb +123 -0
  110. data/test/MockPGconn.rb +62 -0
  111. data/test/NullConnection.test.rb +32 -0
  112. data/test/PgBit.test.rb +98 -0
  113. data/test/PgBox.test.rb +108 -0
  114. data/test/PgCidr.test.rb +61 -0
  115. data/test/PgCircle.test.rb +107 -0
  116. data/test/PgInet.test.rb +61 -0
  117. data/test/PgInterval.test.rb +180 -0
  118. data/test/PgLineSegment.test.rb +108 -0
  119. data/test/PgMacAddr.test.rb +61 -0
  120. data/test/PgPath.test.rb +106 -0
  121. data/test/PgPoint.test.rb +100 -0
  122. data/test/PgPolygon.test.rb +95 -0
  123. data/test/PgTime.test.rb +120 -0
  124. data/test/PgTimeWithTimeZone.test.rb +117 -0
  125. data/test/PgTimestamp.test.rb +134 -0
  126. data/test/RandomThings.rb +25 -0
  127. data/test/Savepoint.test.rb +286 -0
  128. data/test/Select.test.rb +930 -0
  129. data/test/Test.rb +62 -0
  130. data/test/TestConfig.rb +21 -0
  131. data/test/TestSetup.rb +13 -0
  132. data/test/TestUtil.rb +92 -0
  133. data/test/Transaction.test.rb +275 -0
  134. data/test/Translate.test.rb +354 -0
  135. data/test/Update.test.rb +227 -0
  136. data/test/roundtrip.test.rb +565 -0
  137. data/test/test +34 -0
  138. data/tools/exampleinserter/ExampleInserter.rb +177 -0
  139. data/tools/rdoc/ChangeLog +796 -0
  140. data/tools/rdoc/EXAMPLE.rb +48 -0
  141. data/tools/rdoc/MANIFEST +58 -0
  142. data/tools/rdoc/Makefile +27 -0
  143. data/tools/rdoc/NEW_FEATURES +226 -0
  144. data/tools/rdoc/README +390 -0
  145. data/tools/rdoc/ToDo +6 -0
  146. data/tools/rdoc/contrib/Index +6 -0
  147. data/tools/rdoc/contrib/xslfo/ChangeLog +181 -0
  148. data/tools/rdoc/contrib/xslfo/README +106 -0
  149. data/tools/rdoc/contrib/xslfo/TODO +10 -0
  150. data/tools/rdoc/contrib/xslfo/convert.xsl +151 -0
  151. data/tools/rdoc/contrib/xslfo/demo/README +21 -0
  152. data/tools/rdoc/contrib/xslfo/demo/rdocfo +99 -0
  153. data/tools/rdoc/contrib/xslfo/fcm.xsl +54 -0
  154. data/tools/rdoc/contrib/xslfo/files.xsl +62 -0
  155. data/tools/rdoc/contrib/xslfo/labeled-lists.xsl +66 -0
  156. data/tools/rdoc/contrib/xslfo/lists.xsl +44 -0
  157. data/tools/rdoc/contrib/xslfo/modules.xsl +152 -0
  158. data/tools/rdoc/contrib/xslfo/rdoc.xsl +75 -0
  159. data/tools/rdoc/contrib/xslfo/source.xsl +66 -0
  160. data/tools/rdoc/contrib/xslfo/styles.xsl +69 -0
  161. data/tools/rdoc/contrib/xslfo/tables.xsl +67 -0
  162. data/tools/rdoc/contrib/xslfo/utils.xsl +21 -0
  163. data/tools/rdoc/debian/changelog +33 -0
  164. data/tools/rdoc/debian/compat +1 -0
  165. data/tools/rdoc/debian/control +20 -0
  166. data/tools/rdoc/debian/copyright +10 -0
  167. data/tools/rdoc/debian/dirs +2 -0
  168. data/tools/rdoc/debian/docs +2 -0
  169. data/tools/rdoc/debian/rdoc.1 +252 -0
  170. data/tools/rdoc/debian/rdoc.manpages +1 -0
  171. data/tools/rdoc/debian/rdoc.pod +149 -0
  172. data/tools/rdoc/debian/rules +9 -0
  173. data/tools/rdoc/dot/dot.rb +255 -0
  174. data/tools/rdoc/etc/rdoc.dtd +203 -0
  175. data/tools/rdoc/install.rb +137 -0
  176. data/tools/rdoc/markup/install.rb +43 -0
  177. data/tools/rdoc/markup/sample/sample.rb +42 -0
  178. data/tools/rdoc/markup/simple_markup/fragments.rb +323 -0
  179. data/tools/rdoc/markup/simple_markup/inline.rb +348 -0
  180. data/tools/rdoc/markup/simple_markup/lines.rb +147 -0
  181. data/tools/rdoc/markup/simple_markup/preprocess.rb +68 -0
  182. data/tools/rdoc/markup/simple_markup/to_html.rb +281 -0
  183. data/tools/rdoc/markup/simple_markup.rb +474 -0
  184. data/tools/rdoc/markup/test/AllTests.rb +2 -0
  185. data/tools/rdoc/markup/test/TestInline.rb +151 -0
  186. data/tools/rdoc/markup/test/TestParse.rb +411 -0
  187. data/tools/rdoc/rdoc/code_objects.rb +536 -0
  188. data/tools/rdoc/rdoc/diagram.rb +331 -0
  189. data/tools/rdoc/rdoc/generators/chm_generator.rb +112 -0
  190. data/tools/rdoc/rdoc/generators/html_generator.rb +1268 -0
  191. data/tools/rdoc/rdoc/generators/template/chm/chm.rb +86 -0
  192. data/tools/rdoc/rdoc/generators/template/html/html.rb +705 -0
  193. data/tools/rdoc/rdoc/generators/template/html/kilmer.rb +377 -0
  194. data/tools/rdoc/rdoc/generators/template/xml/rdf.rb +110 -0
  195. data/tools/rdoc/rdoc/generators/template/xml/xml.rb +110 -0
  196. data/tools/rdoc/rdoc/generators/xml_generator.rb +130 -0
  197. data/tools/rdoc/rdoc/options.rb +451 -0
  198. data/tools/rdoc/rdoc/parsers/parse_c.rb +287 -0
  199. data/tools/rdoc/rdoc/parsers/parse_f95.rb +118 -0
  200. data/tools/rdoc/rdoc/parsers/parse_rb.rb +2311 -0
  201. data/tools/rdoc/rdoc/parsers/parse_simple.rb +37 -0
  202. data/tools/rdoc/rdoc/parsers/parserfactory.rb +75 -0
  203. data/tools/rdoc/rdoc/rdoc.rb +219 -0
  204. data/tools/rdoc/rdoc/template.rb +234 -0
  205. data/tools/rdoc/rdoc/tokenstream.rb +25 -0
  206. data/tools/rdoc/rdoc.rb +9 -0
  207. metadata +291 -0
@@ -0,0 +1,354 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+
8
+ class TranslateTest < Test
9
+
10
+ include SqlPostgres
11
+ include TestUtil
12
+
13
+ def testEscapeSql
14
+ pi = 3.1415926535897932384626433832795028841971693993751058209749445923
15
+ testCases = [
16
+ [nil, 'null'],
17
+ ["", "E''"],
18
+ ["foo", %q"E'foo'"],
19
+ ["fred's", %q"E'fred\\047s'"],
20
+ ['\\', %q"E'\\134'"],
21
+ [Time.local(2000, 1, 2, 3, 4, 5, 6),
22
+ "timestamp '2000-01-02 03:04:05.000006'"],
23
+ [Time.local(1999, 12, 31, 23, 59, 59, 999999),
24
+ "timestamp '1999-12-31 23:59:59.999999'"],
25
+ [1, '1'],
26
+ [-1, '-1'],
27
+ [1.0, "1"],
28
+ [-1.0, "-1"],
29
+ [pi, "3.1415926535898"],
30
+ [-pi, "-3.1415926535898"],
31
+ [1e100, "1e+100"],
32
+ [-1e-100, "-1e-100"],
33
+ [true, "true"],
34
+ [false, "false"],
35
+ [:default, "default"],
36
+ [['1 + %s', 1], '1 + 1'],
37
+ [[:in, 1, 2], '(1, 2)'],
38
+ [[:in, 'foo', 'bar'], "(E'foo', E'bar')"],
39
+ [BigDecimal('0'), '0.0'],
40
+ [BigDecimal('0.'), '0.0'],
41
+ [BigDecimal('1234567890.0987654321'), '1234567890.0987654321'],
42
+ [BigDecimal('0.000000000000000000000000000001'), '0.000000000000000000000000000001'],
43
+ [PgTime.new(0, 0, 0), "time '00:00:00'"],
44
+ [PgTime.new(23, 59, 59), "time '23:59:59'"],
45
+ [
46
+ PgTimeWithTimeZone.new,
47
+ "time with time zone '00:00:00+00:00'"
48
+ ],
49
+ [
50
+ PgTimeWithTimeZone.new(12, 0, 0, -8),
51
+ "time with time zone '12:00:00-08:00'"
52
+ ],
53
+ [
54
+ PgTimeWithTimeZone.new(23, 59, 59, 8),
55
+ "time with time zone '23:59:59+08:00'"
56
+ ],
57
+ [
58
+ DateTime.civil(2001, 1, 1, 0, 0, 0, Rational(7, 24)),
59
+ "timestamp with time zone '2001-01-01 00:00:00+0700'",
60
+ ],
61
+ [Date.civil(2001, 1, 1), "date '2001-01-01'"],
62
+ [
63
+ PgTimestamp.new(2001, 1, 2, 12, 0, 1),
64
+ "timestamp '2001-01-02 12:00:01.00000'"
65
+ ],
66
+ [PgInterval.new('hours'=>1), "interval '01:00'"],
67
+ [PgInterval.new('days'=>1), "interval '1 day'"],
68
+ [PgPoint.new(1, 2), "point '(1, 2)'"],
69
+ [PgPoint.new(3, 4), "point '(3, 4)'"],
70
+ [
71
+ PgLineSegment.new(PgPoint.new(1, 2), PgPoint.new(3, 4)),
72
+ "lseg '((1, 2), (3, 4))'"
73
+ ],
74
+ [
75
+ PgBox.new(PgPoint.new(1, 2), PgPoint.new(3, 4)),
76
+ "box '((1, 2), (3, 4))'"
77
+ ],
78
+ [
79
+ PgPath.new(true, PgPoint.new(1, 2), PgPoint.new(3, 4)),
80
+ "path '((1, 2), (3, 4))'"
81
+ ],
82
+ [
83
+ PgPolygon.new(PgPoint.new(1, 2), PgPoint.new(3, 4)),
84
+ "polygon '((1, 2), (3, 4))'"
85
+ ],
86
+ [["%s %s", 1, 'Fred'], "1 E'Fred'"],
87
+ ]
88
+ for testCase in testCases
89
+ assertInfo("For test case #{testCase.inspect}") do
90
+ raw, escaped = *testCase
91
+ assertEquals(Translate.escape_sql(raw), escaped)
92
+ end
93
+ end
94
+ end
95
+
96
+ def test_escape_array
97
+ testCases = [
98
+ [nil, "null"],
99
+ [[], %q"'{}'"],
100
+ [[1], %q"ARRAY[1]"],
101
+ [[1, 2], %q"ARRAY[1, 2]"],
102
+ [['foo'], %q"ARRAY[E'foo']"],
103
+ [['\\'], %q"ARRAY[E'\\134']"],
104
+ [["a,b,c"], %q"ARRAY[E'a,b,c']"],
105
+ [["a", "b", "c"], %q"ARRAY[E'a', E'b', E'c']"],
106
+ [["\"Hello\""], %q"ARRAY[E'\"Hello\"']"],
107
+ [
108
+ [[0, 0], [0, 1], [1, 0], [1, 1]],
109
+ "ARRAY[ARRAY[0, 0], ARRAY[0, 1], ARRAY[1, 0], ARRAY[1, 1]]"
110
+ ],
111
+ ]
112
+ for array, escaped in testCases
113
+ assertInfo("For array #{array.inspect}") do
114
+ assertEquals(Translate.escape_array(array), escaped)
115
+ end
116
+ end
117
+ end
118
+
119
+ def test_escape_bytea_array
120
+ testCases = [
121
+ [[], "'{}'"],
122
+ [["", "foo"], "'{\"\",\"foo\"}'"],
123
+ ["\000\037 ", "'{\"\\\\\\\\000\037 \"}'"],
124
+ [["'\\"], "'{\"\\'\\\\\\\\\\\\\\\\\"}'"],
125
+ [["~\177\377"], "'{\"~\177\377\"}'"],
126
+ [["\""], "'{\"\\\\\"\"}'"],
127
+ [nil, "null"],
128
+ ]
129
+ for array, escaped in testCases
130
+ assertInfo("For array #{array.inspect}") do
131
+ assertEquals(Translate.escape_bytea_array(array), escaped)
132
+ end
133
+ end
134
+ end
135
+
136
+ def test_sql_to_array
137
+ goodTestCases = [
138
+ ["{}", []],
139
+ ["{foo}", ["foo"]],
140
+ ["{foo,bar,\"fool's gold\"}", ["foo", "bar", "fool's gold"]],
141
+ ["{\"\\\\\"}", ["\\"]],
142
+ ["{\"\\\\\",fool's}", ["\\", "fool's"]],
143
+ ["{\"a,b,c\"}", ["a,b,c"]],
144
+ ["{\"\\\"Hello!\\\"\"}", ["\"Hello!\""]],
145
+ ["{\"\001\n\037\"}", ["\001\n\037"]],
146
+ [
147
+ "{{f,f},{f,t},{t,f},{t,t}}",
148
+ [["f", "f"], ["f", "t"], ["t", "f"], ["t", "t"]],
149
+ ],
150
+ ]
151
+ for sql, array in goodTestCases
152
+ assertInfo("For sql #{sql.inspect}") do
153
+ assertEquals(Translate.sql_to_array(sql), array)
154
+ end
155
+ end
156
+ badTestCases = [
157
+ "",
158
+ "{",
159
+ "{foo",
160
+ "{foo}x",
161
+ ]
162
+ for sql in badTestCases
163
+ assertInfo("For sql #{sql.inspect}") do
164
+ assertException(ArgumentError, sql.inspect) do
165
+ Translate.sql_to_array(sql)
166
+ end
167
+ end
168
+ end
169
+ end
170
+
171
+ def test_escape_qchar
172
+ test_cases = [
173
+ [nil, 'null'],
174
+ ["\000", %q"E'\\000'"],
175
+ ["\001", %q"E'\\001'"],
176
+ ["\377", %q"E'\\377'"],
177
+ ["a", "E'\\141'"],
178
+ ]
179
+ for raw, escaped in test_cases
180
+ assertInfo("For raw=#{raw.inspect}") do
181
+ assertEquals(Translate.escape_qchar(raw), escaped)
182
+ end
183
+ end
184
+ end
185
+
186
+ def test_escape_bytea
187
+ testCases = [
188
+ [nil, 'null'],
189
+ [:default, "default"],
190
+ ["", "E''"],
191
+ ["foo", %q"E'foo'"],
192
+ ["\000\037 ", %q"E'\\\\000\\\\037 '"],
193
+ ["'\\", %q"E'''\\\\\\\\'"],
194
+ ["~\177\377", "E'~\\\\177\\\\377'"],
195
+ ]
196
+ for testCase in testCases
197
+ assertInfo("For test case #{testCase.inspect}") do
198
+ raw, escaped = *testCase
199
+ assertEquals(Translate.escape_bytea(raw), escaped)
200
+ end
201
+ end
202
+ end
203
+
204
+ def test_unescape_bytea
205
+ testCases = [
206
+ ["", ""],
207
+ ["abc", "abc"],
208
+ ["\\\\", "\\"],
209
+ ["\\001", "\001"],
210
+ ["\\037", "\037"],
211
+ ["\\177", "\177"],
212
+ ["\\200", "\200"],
213
+ ["\\377", "\377"],
214
+ ["\\477", "\\477"],
215
+ ["\\387", "\\387"],
216
+ ["\\378", "\\378"],
217
+ ["\\n", "\\n"],
218
+ ["abc\\", "abc\\"],
219
+ ]
220
+ for testCase in testCases
221
+ assertInfo("For test case #{testCase.inspect}") do
222
+ escaped, raw = *testCase
223
+ assertEquals(Translate.unescape_bytea(escaped), raw)
224
+ end
225
+ end
226
+ end
227
+
228
+ def test_unescape_qchar
229
+ testCases = [
230
+ ["", "\000"],
231
+ ["\001", "\001"],
232
+ ["\037", "\037"],
233
+ [" ", " "],
234
+ ["~", '~'],
235
+ ["\277", "\277"],
236
+ ["\300", "\300"],
237
+ ["\377", "\377"],
238
+ ]
239
+ for testCase in testCases
240
+ assertInfo("For test case #{testCase.inspect}") do
241
+ escaped, raw = *testCase
242
+ assertEquals(Translate.unescape_qchar(escaped), raw)
243
+ end
244
+ end
245
+ end
246
+
247
+ def testEscapeSql_Select
248
+ select = Object.new
249
+ def select.statement
250
+ "foo"
251
+ end
252
+ assertEquals(Translate.escape_sql(select), "(foo)")
253
+ end
254
+
255
+ def testEscapeSql_AllCharValues
256
+ tableName = testTableName("foo")
257
+ Connection.open(testDbArgs) do |connection|
258
+ connection.exec("begin transaction;")
259
+ connection.exec("create temporary table #{tableName} "\
260
+ "(i int, t text);")
261
+ range = (1..255)
262
+ for i in range
263
+ assertInfo("For i=#{i.inspect}") do
264
+ statement = ("insert into #{tableName} (i, t) "\
265
+ "values (#{i}, #{Translate.escape_sql(i.chr)});")
266
+ connection.query(statement)
267
+ end
268
+ end
269
+ connection.exec("end transaction;")
270
+ statement = "select i, t, length(t) from #{tableName} order by i;"
271
+ rows = connection.query(statement)
272
+ for i in range
273
+ assertInfo("for i = #{i}") do
274
+ row = rows.assoc(i.to_s)
275
+ char_number, text, length = *row
276
+ assertEquals(length.to_i, 1)
277
+ c = Translate.unescape_text(text)
278
+ assertEquals(c, i.chr)
279
+ end
280
+ end
281
+ end
282
+ end
283
+
284
+ def testSubstituteValues
285
+ assertEquals(Translate.substitute_values("foo"), "foo")
286
+ assertEquals(Translate.substitute_values(["bar %s", 1]), "bar 1")
287
+ assertEquals(Translate.substitute_values(["bar %s", "O'Malley"]),
288
+ "bar E'O\\047Malley'")
289
+ assertEquals(Translate.substitute_values(["%s %s", nil, 1.23]), "null 1.23")
290
+ end
291
+
292
+ def test_sql_to_datetime
293
+ testCases = [
294
+ [2003, 10, 18, 11, 30, 24, -7],
295
+ [2001, 1, 1, 0, 0, 0, 0],
296
+ [1970, 12, 31, 23, 59, 59, -11],
297
+ ]
298
+ for testCase in testCases
299
+ sql = "%04d-%02d-%02d %02d:%02d:%02d%+03d" % testCase
300
+ dateTime = DateTime.civil(*testCase[0..5] +
301
+ [Rational(testCase[6], 24)])
302
+ assertEquals(Translate.sql_to_datetime(sql), dateTime)
303
+ end
304
+ end
305
+
306
+ def test_datetime_to_sql
307
+ testCases = [
308
+ [2003, 10, 18, 11, 30, 24, -7],
309
+ [2001, 1, 1, 0, 0, 0, 0],
310
+ [1970, 12, 31, 23, 59, 59, -11],
311
+ ]
312
+ for testCase in testCases
313
+ sql = "%04d-%02d-%02d %02d:%02d:%02d%+03d00" % testCase
314
+ dateTime = DateTime.civil(*testCase[0..5] +
315
+ [Rational(testCase[6], 24)])
316
+ assertEquals(Translate.datetime_to_sql(dateTime), sql)
317
+ end
318
+ end
319
+
320
+ def test_sql_to_date
321
+ testCases = [
322
+ [2000, 1, 1],
323
+ [1899, 12, 31],
324
+ ]
325
+ for testCase in testCases
326
+ sql = "%04d-%02d-%02d" % testCase
327
+ date = Date.civil(*testCase)
328
+ assertEquals(Translate.sql_to_date(sql), date)
329
+ end
330
+ end
331
+
332
+ def test_deep_collect
333
+ testCases = [
334
+ ["1", 1],
335
+ [[], []],
336
+ [["1"], [1]],
337
+ [["1", "2"], [1, 2]],
338
+ [["1", ["2", "3"], []], [1, [2, 3], []]],
339
+ ]
340
+ for testCase in testCases
341
+ a, result = *testCase
342
+ assertEquals(Translate.deep_collect(a) do |e| e.to_i end, result)
343
+ end
344
+ end
345
+
346
+ end
347
+
348
+ TranslateTest.new.run if $0 == __FILE__
349
+
350
+ # Local Variables:
351
+ # tab-width: 2
352
+ # ruby-indent-level: 2
353
+ # indent-tabs-mode: nil
354
+ # End:
@@ -0,0 +1,227 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'TestSetup'
7
+
8
+ class UpdateTest < Test
9
+
10
+ include SqlPostgres
11
+ include TestUtil
12
+
13
+ def testBasicUpdate
14
+ makeTestConnection do |connection|
15
+ connection.exec("create temporary table #{table1} (i int)")
16
+ connection.exec("insert into #{table1} values (1)")
17
+ connection.exec("insert into #{table1} values (2)")
18
+ update = Update.new(table1, connection)
19
+ update.set('i', 0)
20
+ assertEquals(update.statement, "update #{table1} set i = 0")
21
+ update.exec
22
+ assertEquals(connection.query("select * from #{table1}"),
23
+ [["0"], ["0"]])
24
+ end
25
+ end
26
+
27
+ def testDefaultConnection
28
+ makeTestConnection do |connection|
29
+ setDefaultConnection(connection) do
30
+ connection.exec("create temporary table #{table1} (i int)")
31
+ connection.exec("insert into #{table1} values (1)")
32
+ connection.exec("insert into #{table1} values (2)")
33
+ update = Update.new(table1)
34
+ update.set('i', 0)
35
+ assertEquals(update.statement, "update #{table1} set i = 0")
36
+ update.exec
37
+ assertEquals(connection.query("select * from #{table1}"),
38
+ [["0"], ["0"]])
39
+ end
40
+ end
41
+ end
42
+
43
+ def testExec_Connection
44
+ makeTestConnection do |connection|
45
+ connection.exec("create temporary table #{table1} (i int)")
46
+ connection.exec("insert into #{table1} values (1)")
47
+ connection.exec("insert into #{table1} values (2)")
48
+ update = Update.new(table1)
49
+ update.set('i', 0)
50
+ assertEquals(update.statement, "update #{table1} set i = 0")
51
+ update.exec(connection)
52
+ assertEquals(connection.query("select * from #{table1}"),
53
+ [["0"], ["0"]])
54
+ end
55
+ end
56
+
57
+ def testWhere
58
+ makeTestConnection do |connection|
59
+ connection.exec("create temporary table #{table1} (i int)")
60
+ connection.exec("insert into #{table1} values (1)")
61
+ connection.exec("insert into #{table1} values (2)")
62
+ update = Update.new(table1, connection)
63
+ update.set('i', 3)
64
+ update.where('i = 2')
65
+ assertEquals(update.statement, "update #{table1} set i = 3 where i = 2")
66
+ update.exec
67
+ rows = connection.query("select * from #{table1} order by i")
68
+ assertEquals(rows, [["1"], ["3"]])
69
+ end
70
+ end
71
+
72
+ def testWhere_Substitution
73
+ makeTestConnection do |connection|
74
+ connection.exec("create temporary table #{table1} (t text)")
75
+ connection.exec("insert into #{table1} values ('Smith')")
76
+ connection.exec("insert into #{table1} values (E'O\\'Brien')")
77
+ update = Update.new(table1, connection)
78
+ update.set('t', 'Tam')
79
+ update.where(['t = %s', "O'Brien"])
80
+ assertEquals(update.statement, "update #{table1} set t = E'Tam' "\
81
+ "where t = E'O\\047Brien'")
82
+ update.exec
83
+ rows = connection.query("select * from #{table1} order by t")
84
+ assertEquals(rows, [["Smith"], ["Tam"]])
85
+ end
86
+ end
87
+
88
+ def testWhere_Multiple
89
+ makeTestConnection do |connection|
90
+ connection.exec("create temporary table #{table1} (i int)")
91
+ connection.exec("insert into #{table1} values (1)")
92
+ connection.exec("insert into #{table1} values (2)")
93
+ connection.exec("insert into #{table1} values (3)")
94
+ update = Update.new(table1, connection)
95
+ update.set('i', -2)
96
+ update.where('i > 1')
97
+ update.where('i < 3')
98
+ assertEquals(update.statement, "update #{table1} set i = -2 "\
99
+ "where i > 1 and i < 3")
100
+ update.exec
101
+ rows = connection.query("select * from #{table1} order by i")
102
+ assertEquals(rows, [["-2"], ["1"], ["3"]])
103
+ end
104
+ end
105
+
106
+ def testSet
107
+ time = Time.now
108
+ testCases = [
109
+ ["int", -1, "-1"],
110
+ ["int", 1, "1"],
111
+ ["int", nil, nil],
112
+ ["text", "foo", "foo"],
113
+ ["text", "It's", "It's"],
114
+ ["text", nil, nil],
115
+ ["real", 3.14, "3.14"],
116
+ ["real", nil, nil],
117
+ ["timestamp", time, Translate.timeToSql(time).chomp('0').chomp('0')],
118
+ ["timestamp", nil, nil],
119
+ ["boolean", false, "f"],
120
+ ["boolean", true, "t"],
121
+ ["boolean", nil, nil],
122
+ ]
123
+ makeTestConnection do |connection|
124
+ for columnType, value, string in testCases
125
+ connection.exec("create temporary table #{table1} (v #{columnType})")
126
+ connection.exec("insert into #{table1} (v) values (null)")
127
+ update = Update.new(table1, connection)
128
+ update.set('v', value)
129
+ update.exec
130
+ assertEquals(connection.query("select v from #{table1}"),
131
+ [[string]])
132
+ connection.exec("drop table #{table1}")
133
+ end
134
+ end
135
+ end
136
+
137
+ def testSet_Subquery
138
+ makeTestConnection do |connection|
139
+ connection.exec("create temporary table #{table1} (i int)")
140
+ connection.exec("create temporary table #{table2} (i int, j int)")
141
+ connection.exec("insert into #{table1} (i) values (1)")
142
+ connection.exec("insert into #{table1} (i) values (2)")
143
+ connection.exec("insert into #{table2} (i, j) values (1, -1)")
144
+ connection.exec("insert into #{table2} (i, j) values (2, -2)")
145
+ select = Select.new
146
+ select.select('j')
147
+ select.from(table2)
148
+ select.where(["i = #{table1}.i"])
149
+ update = Update.new(table1, connection)
150
+ update.set('i', select)
151
+ update.exec
152
+ assertEquals(update.statement, "update #{table1} "\
153
+ "set i = (select j from #{table2} where i = #{table1}.i)")
154
+ rows = connection.query("select i from #{table1} order by i")
155
+ assertEquals(rows, [["-2"], ["-1"]])
156
+ end
157
+ end
158
+
159
+ def testSetExpression
160
+ makeTestConnection do |connection|
161
+ connection.exec("create temporary table #{table1} (i int)")
162
+ connection.exec("create temporary table #{table2} (i int, j int)")
163
+ connection.exec("insert into #{table1} (i) values (1)")
164
+ connection.exec("insert into #{table1} (i) values (2)")
165
+ update = Update.new(table1, connection)
166
+ update.set('i', ['i + 1'])
167
+ update.exec
168
+ assertEquals(update.statement, "update #{table1} set i = i + 1")
169
+ rows = connection.query("select i from #{table1} order by i")
170
+ assertEquals(rows, [["2"], ["3"]])
171
+ end
172
+ end
173
+
174
+ def testSetArray
175
+ makeTestConnection do |connection|
176
+ connection.exec("create temporary table #{table1} (i int[])")
177
+ connection.exec("insert into #{table1} (i) values ('{}')")
178
+ update = Update.new(table1, connection)
179
+ update.set_array('i', [1, 2, 3])
180
+ update.exec
181
+ rows = connection.query("select i from #{table1} order by i")
182
+ assertEquals(rows, [["{1,2,3}"]])
183
+ end
184
+ end
185
+
186
+ def testOnly
187
+ makeTestConnection do |connection|
188
+ connection.exec("create temporary table #{table1} (i int)")
189
+ connection.exec("create temporary table #{table2} (j int) "\
190
+ "inherits(#{table1})")
191
+ connection.exec("insert into #{table1} (i) values (1)")
192
+ connection.exec("insert into #{table2} (i) values (2)")
193
+ update = Update.new(table1, connection)
194
+ update.only
195
+ update.set('i', 0)
196
+ assertEquals(update.statement, "update only #{table1} set i = 0")
197
+ update.exec
198
+ rows = connection.query("select i from #{table1} order by i")
199
+ assertEquals(rows, [["0"], ["2"]])
200
+ end
201
+ end
202
+
203
+ def testSetBytea
204
+ makeTestConnection do |connection|
205
+ expected = allCharacters
206
+ connection.exec("create temporary table #{table1} (b bytea)")
207
+ connection.exec("insert into #{table1} (b) values (NULL)")
208
+ update = Update.new(table1, connection)
209
+ update.set_bytea('b', expected)
210
+ update.exec
211
+ sql = Select.new(connection)
212
+ sql.select('b')
213
+ sql.from(table1)
214
+ actual = sql.exec.first['b']
215
+ assertEquals(actual, expected)
216
+ end
217
+ end
218
+
219
+ end
220
+
221
+ UpdateTest.new.run if $0 == __FILE__
222
+
223
+ # Local Variables:
224
+ # tab-width: 2
225
+ # ruby-indent-level: 2
226
+ # indent-tabs-mode: nil
227
+ # End: