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
data/doc/manual.dbk ADDED
@@ -0,0 +1,622 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1" ?>
2
+ <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3
+ "/usr/share/sgml/docbook/dtd/xml/4.2/docbookx.dtd">
4
+ <book>
5
+ <bookinfo>
6
+ <title>sqlpostgres</title>
7
+ <author>
8
+ <firstname>Wayne</firstname>
9
+ <surname>Conrad</surname>
10
+ </author>
11
+ <copyright>
12
+ <year>2003</year>
13
+ <holder>Wayne Conrad</holder>
14
+ </copyright>
15
+ <abstract>
16
+
17
+ <para><application>sqlpostgres</application> is a Ruby library that
18
+ builds and executes insert, update, and select statements for
19
+ Postgresql. <application>sqlpostgres</application> statements are
20
+ easier to read and maintain than raw SQL. Ruby data types are
21
+ automatically converted to and from SQL data types, and results are
22
+ returned as an array of hashes rather than an array of arrays.</para>
23
+
24
+ </abstract>
25
+ </bookinfo>
26
+
27
+ <chapter>
28
+ <title>Overview</title>
29
+
30
+ <para><application>Sqlpostgres</application> is a Ruby library that
31
+ builds and executes insert, update and select statements for
32
+ Postgresql.</para>
33
+
34
+ <para><application>Features</application>:
35
+
36
+ <itemizedlist>
37
+
38
+ <listitem><para> uses the venerable <ulink
39
+ url="http://www.postgresql.jp/interfaces/ruby/">
40
+ <application>postgresql</application></ulink> library for its basic
41
+ access to the database. That means it will run anywhere the
42
+ postgresql runs and benefits from the proven reliability of the
43
+ <application>postgresql</application> library. </para></listitem>
44
+
45
+ <listitem><para> is fully documented with plenty of tested
46
+ examples.</para></listitem>
47
+
48
+ <listitem><para> is fully tested with a comprehensive suite of unit
49
+ tests.</para></listitem>
50
+
51
+ <listitem><para> automatically escapes <literal>'</literal>,
52
+ <literal>\</literal>, and non-printable characters in
53
+ strings.</para></listitem>
54
+
55
+ <listitem><para> automatically converts data read from the database
56
+ into Ruby types. If you read an integer column, you get a Ruby
57
+ <classname>Integer</classname>. If you read a text column, you get a
58
+ Ruby <classname>String</classname>. If you read a timestamp, you get
59
+ a Ruby <classname>Time</classname>. </para></listitem>
60
+
61
+ <listitem><para> returns each row as a hash keyed by column name (or
62
+ alias), not as an array. No more counting columns.</para></listitem>
63
+
64
+ <listitem><para> creates all common and most uncommon types of select,
65
+ insert or update statement with ease, including
66
+ <application>Postgres</application> extensions.</para></listitem>
67
+
68
+ <listitem><para> has an escape mechanism allowing you to execute any
69
+ SQL statement, for those things that this library doesn't
70
+ do.</para></listitem>
71
+
72
+ </itemizedlist>
73
+ </para>
74
+
75
+ <section>
76
+ <title>Connecting</title>
77
+
78
+ <para>
79
+ Here a simple way to make a connection to database 'foo':
80
+
81
+ <programlisting id='connection'>
82
+ Connection.open do |connection|
83
+ # Use the connection
84
+ end
85
+ </programlisting>
86
+
87
+ The connection is automatically closed at the end of the block.
88
+ </para>
89
+
90
+ <para> See <xref linkend="Connecting"/> for more about making and
91
+ using database connections. </para>
92
+
93
+ </section>
94
+
95
+ <section>
96
+ <title>Insert</title>
97
+
98
+ <para>Here is a simple insert statement:
99
+ <programlisting id="insert2">
100
+ insert = Insert.new('person', connection)
101
+ insert.insert('name', "O'Reilly")
102
+ insert.insert('date_of_birth', Date.civil(1972, 1, 1))
103
+ p insert.statement # "insert into person (name, date_of_birth) values
104
+ # (E'O\\047Reilly', date '1972-01-01')"
105
+ insert.exec
106
+ </programlisting>
107
+ </para>
108
+
109
+ <para>For documentation and examples, see the <ulink
110
+ url="rdoc/classes/SqlPostgres/Insert.html">class
111
+ documentation</ulink>. </para>
112
+
113
+ <sidebar>
114
+ <title>
115
+ Why do so many of the examples call <methodname>statement</methodname>?
116
+ </title>
117
+
118
+ <para>Many of the examples call <methodname>statement</methodname> and
119
+ print out the result. Do you have to? No. It's only done in the
120
+ examples because showing the what SQL the library generated makes it
121
+ easier to understand the example. In your own code, you will probably
122
+ only call <methodname>statement</methodname> when debugging.</para>
123
+
124
+ </sidebar>
125
+
126
+ </section>
127
+
128
+ <section>
129
+ <title>Update</title>
130
+
131
+ <para>Here is a simple update statement:
132
+ <programlisting id="update2">
133
+ update = Update.new('person', connection)
134
+ update.set('married', true)
135
+ update.where(['name = %s', 'Smith'])
136
+ p update.statement # "update person set married = true where
137
+ # name = E'Smith'"
138
+ update.exec
139
+ </programlisting>
140
+ </para>
141
+
142
+ <para>For documentation and examples, see the <ulink
143
+ url="rdoc/classes/SqlPostgres/Update.html">class
144
+ documentation</ulink>.</para>
145
+
146
+ </section>
147
+
148
+ <section>
149
+ <title>Select</title>
150
+
151
+ <para>Here is a simple select statement:
152
+ <programlisting id="select2">
153
+ select = Select.new(connection)
154
+ select.select('name')
155
+ select.select('married')
156
+ select.from('person')
157
+ select.where(['married = %s', false])
158
+ p select.statement # "select name, married from person where married =
159
+ # false"
160
+ p select.exec # [{"name"=>"Fred", "married"=>false}, {"name"=>"Mary",
161
+ # "married"=>false}]
162
+ </programlisting>
163
+ </para>
164
+
165
+ <para>For documentation and examples, see the <ulink
166
+ url="rdoc/classes/SqlPostgres/Select.html">class
167
+ documentation</ulink>. </para>
168
+
169
+ </section>
170
+
171
+ <section>
172
+ <title>Transactions</title>
173
+
174
+ <para>Transactions are simple. See the example in the <ulink
175
+ url="rdoc/classes/SqlPostgres/Transaction.html">class
176
+ documentation</ulink>.</para>
177
+
178
+ </section>
179
+
180
+ <section>
181
+ <title>Namespace</title>
182
+
183
+ <para> All classes and functions in the library are in module
184
+ <classname>SqlPostgres</classname>. You may either include
185
+ <classname>SqlPostgres</classname>:
186
+
187
+ <programlisting id='include_module2'>
188
+ require 'sqlpostgres'
189
+ include SqlPostgres
190
+
191
+ Connection.open do |connection|
192
+ #...
193
+ end
194
+ </programlisting>
195
+
196
+ or add the <classname>SqlPostgres</classname> prefix to the class
197
+ names you use:
198
+
199
+ <programlisting id='use_prefix2'>
200
+ require 'sqlpostgres'
201
+
202
+ SqlPostgres::Connection.open do |connection|
203
+ #...
204
+ end
205
+ </programlisting>
206
+ </para>
207
+
208
+ <para>The examples in this manual assume you've included
209
+ <classname>SqlPostgres</classname>.</para>
210
+
211
+ </section>
212
+
213
+ </chapter>
214
+
215
+ <chapter id="Connecting">
216
+ <title>Connecting</title>
217
+
218
+ <section>
219
+ <title>Getting a connection</title>
220
+
221
+ <section>
222
+ <title>Automatically closed connection</title>
223
+
224
+ <para>Here's the simplest way to make a database connection:
225
+
226
+ <programlisting id='connection_auto'>
227
+ Connection.open('db_name'=>'sqlpostgres_test') do |connection|
228
+ # Use the connection
229
+ end
230
+ </programlisting>
231
+ </para>
232
+
233
+ <para>The connection is automatically closed at the end of the block,
234
+ even if an exception occurs. You can create connections that you have
235
+ to close yourself, but you should prefer the automatic way when
236
+ possible.</para>
237
+
238
+ <para><methodname>Connection.open</methodname> takes many arguments to
239
+ allow you to select the database name, host name, user name, password,
240
+ and so on:
241
+
242
+ <table>
243
+ <title><methodname>Connection.open</methodname> arguments</title>
244
+ <tgroup cols='3'>
245
+ <thead>
246
+ <row>
247
+ <entry>name</entry>
248
+ <entry>type</entry>
249
+ <entry>description</entry>
250
+ <entry>default</entry>
251
+ </row>
252
+ </thead>
253
+ <tbody>
254
+
255
+ <row>
256
+
257
+ <entry><literal>"host_name"</literal></entry>
258
+
259
+ <entry>String</entry>
260
+
261
+ <entry> The name of the host to connect to. This can be a DNS name,
262
+ or an IP (dotted-quad)</entry>
263
+
264
+ <entry><literal>"localhost"</literal></entry>
265
+
266
+ </row>
267
+
268
+ <row>
269
+
270
+ <entry><literal>"port"</literal> </entry>
271
+
272
+ <entry>Integer</entry>
273
+
274
+ <entry>The port number to connect to</entry>
275
+
276
+ <entry><literal>5432</literal></entry>
277
+
278
+ </row>
279
+
280
+ <row>
281
+
282
+ <entry><literal>"options"</literal></entry>
283
+
284
+ <entry>String</entry>
285
+
286
+ <entry>Trace/debug options to be sent to the server</entry>
287
+
288
+ <entry><literal>""</literal></entry>
289
+
290
+ </row>
291
+
292
+ <row>
293
+
294
+ <entry><literal>"tty"</literal></entry>
295
+
296
+ <entry>String</entry>
297
+
298
+ <entry>Name of tty for back end messages</entry>
299
+
300
+ <entry><literal>""</literal></entry>
301
+
302
+ </row>
303
+
304
+ <row>
305
+
306
+ <entry><literal>"db_name"</literal></entry>
307
+
308
+ <entry>String</entry>
309
+
310
+ <entry>Database name. <literal>""</literal> means to use the user
311
+ name as the database name.</entry>
312
+
313
+ <entry><literal>""</literal></entry>
314
+
315
+ </row>
316
+
317
+ <row>
318
+
319
+ <entry><literal>"login"</literal></entry>
320
+
321
+ <entry>String</entry>
322
+
323
+ <entry>Login name. nil means to use the user name as the login
324
+ name.</entry>
325
+
326
+ <entry><literal>nil</literal></entry>
327
+
328
+ </row>
329
+
330
+ <row>
331
+
332
+ <entry><literal>"password"</literal></entry>
333
+
334
+ <entry>String</entry>
335
+
336
+ <entry>Password. nil means no password.</entry>
337
+
338
+ <entry><literal>nil</literal></entry>
339
+
340
+ </row>
341
+
342
+ </tbody>
343
+ </tgroup>
344
+ </table>
345
+
346
+ </para>
347
+
348
+ </section>
349
+
350
+ <section>
351
+ <title>Manually closed connection</title>
352
+
353
+ <para>If you need to take control over closing the database
354
+ connection, you can do it:
355
+
356
+ <programlisting id="connection_manual">
357
+ connection = Connection.new
358
+ # use the connection
359
+ connection.close
360
+ </programlisting>
361
+ </para>
362
+
363
+ <para> When doing the close yourself, only two things close the
364
+ connection: Your program ending, or you executing the close method.
365
+ Nothing else will close the connection (no, they're not closed when
366
+ garbage collected).</para>
367
+
368
+ </section>
369
+
370
+ <section>
371
+ <title>Wrapping an existing connection</title>
372
+
373
+ <para> If you have a connection you have already obtained from another
374
+ source, and wish to use with this library, then pass it to the
375
+ constructor of Connection like so:
376
+
377
+ <programlisting id="connection_wrapped_new">
378
+ pgconn = PGconn.connect('localhost', 5432, '', '', ENV['USER'])
379
+ connection = Connection.new('connection'=>pgconn)
380
+ # use the connection
381
+ connection.close # or, if you prefer, pgconn.close
382
+ </programlisting>
383
+ </para>
384
+
385
+ <para> When wrapping a connection this way, the library will not
386
+ automatically close it for you. You may close it by calling either
387
+ <methodname>Connection.close</methodname>, or
388
+ <methodname>PGconn.close</methodname> </para>
389
+
390
+ <para> You can wrap a connection and have it closed for you by passing
391
+ it to <methodname>Connection.open</methodname>:
392
+
393
+ <programlisting id="connection_wrapped_open">
394
+ pgconn = PGconn.connect('localhost', 5432, '', '', ENV['USER'])
395
+ connection = Connection.open('connection'=>pgconn) do |connection|
396
+ # use the connection
397
+ end
398
+ </programlisting>
399
+
400
+ </para>
401
+
402
+ </section>
403
+
404
+ </section>
405
+
406
+ <section>
407
+ <title>Using the connection</title>
408
+
409
+ <para>Once you have a connection, you have three ways to use it:
410
+
411
+ <orderedlist>
412
+
413
+ <listitem><para>Make it the default connection.</para></listitem>
414
+
415
+ <listitem><para>Pass it to the constructor of Insert, Update, or Select</para></listitem>
416
+
417
+ <listitem><para>Pass it to the exec method of Insert, Update, or Select.</para></listitem>
418
+
419
+ </orderedlist>
420
+
421
+ </para>
422
+
423
+ <section>
424
+ <title>Default Connection</title>
425
+
426
+ <para>You may set a default connection. After that, any
427
+ <classname>Insert</classname>, <classname>Update</classname> or
428
+ <classname>Select</classname> statements you make will use that
429
+ connection (unless a different connection is passed to
430
+ <methodname>new</methodname> or <methodname>exec</methodname>).
431
+
432
+ <programlisting id="connection_default">
433
+ Connection.open do |connection|
434
+ Connection.default = connection
435
+ select = Select.new
436
+ select.select_literal(1, 'i') # [{"i"=>1}]
437
+ p select.exec
438
+ end
439
+ </programlisting>
440
+ </para>
441
+
442
+ </section>
443
+
444
+ <section>
445
+ <title>Pass a connection to the constructor</title>
446
+
447
+ <para>You may pass a connection to the constructor of any
448
+ <classname>Select</classname>, <classname>Insert</classname> or
449
+ <classname>Update</classname>.
450
+
451
+ <programlisting id="connection_ctor">
452
+ insert = Insert.new('person', connection)
453
+ insert.insert('name', 'Fred')
454
+ insert.exec
455
+ </programlisting>
456
+ </para>
457
+
458
+ <para>A connection passed to the constructor overrides any default
459
+ connection.</para>
460
+
461
+ </section>
462
+
463
+ <section>
464
+ <title>Pass a connection to the exec method</title>
465
+
466
+ <para>You may pass a connection to the exec method of any
467
+ <classname>Select</classname>, <classname>Insert</classname> or
468
+ <classname>Update</classname>:
469
+
470
+ <programlisting id="connection_exec">
471
+ insert = Insert.new('person')
472
+ insert.insert('name', 'Fred')
473
+ insert.exec(connection)
474
+ </programlisting>
475
+ </para>
476
+
477
+ <para>A connection passed to the <methodname>exec</methodname> method
478
+ overrides the default connection or a connection passed to the
479
+ constructor.</para>
480
+
481
+ </section>
482
+
483
+ </section>
484
+
485
+ </chapter>
486
+
487
+ <chapter id="Types">
488
+ <title>Types</title>
489
+
490
+ <table>
491
+ <title>Types</title>
492
+ <tgroup cols='3'>
493
+ <thead>
494
+ <row>
495
+ <entry>Postgres type</entry>
496
+ <entry>Ruby type</entry>
497
+ <entry>Insert method (scalar)</entry>
498
+ <entry>Insert method(array)</entry>
499
+ </row>
500
+ </thead>
501
+ <tbody>
502
+ <row>
503
+ <entry>text, character varying(n), varchar(n)</entry>
504
+ <entry>String</entry>
505
+ <entry>insert</entry>
506
+ <entry>insert_array</entry>
507
+ </row>
508
+ <row>
509
+ <entry>character(n), char(n)</entry>
510
+ <entry>String</entry>
511
+ <entry>insert</entry>
512
+ <entry>insert_array</entry>
513
+ </row>
514
+ <row>
515
+ <entry>character, char</entry>
516
+ <entry>String</entry>
517
+ <entry>insert</entry>
518
+ <entry>insert_array</entry>
519
+ </row>
520
+ <row>
521
+ <entry>"char"</entry>
522
+ <entry>String</entry>
523
+ <entry>insert_char</entry>
524
+ <entry></entry>
525
+ </row>
526
+ <row>
527
+ <entry>name</entry>
528
+ <entry>String</entry>
529
+ <entry>insert</entry>
530
+ <entry>insert_char</entry>
531
+ </row>
532
+ <row>
533
+ <entry>integer, int, int4</entry>
534
+ <entry>Integer</entry>
535
+ <entry>insert</entry>
536
+ <entry>insert_array</entry>
537
+ </row>
538
+ <row>
539
+ <entry>smallint, int2</entry>
540
+ <entry>Integer</entry>
541
+ <entry>insert</entry>
542
+ <entry>insert_array</entry>
543
+ </row>
544
+ <row>
545
+ <entry>serial, serial4</entry>
546
+ <entry>Integer</entry>
547
+ <entry>insert</entry>
548
+ <entry>insert_array</entry>
549
+ </row>
550
+ <row>
551
+ <entry>bigserial, serial8</entry>
552
+ <entry>Integer</entry>
553
+ <entry>insert</entry>
554
+ <entry>insert_array</entry>
555
+ </row>
556
+ <row>
557
+ <entry>real, float4</entry>
558
+ <entry>Float</entry>
559
+ <entry>insert</entry>
560
+ <entry>insert_array</entry>
561
+ </row>
562
+ <row>
563
+ <entry>double precision, float8</entry>
564
+ <entry>Float</entry>
565
+ <entry>insert</entry>
566
+ <entry>insert_array</entry>
567
+ </row>
568
+ <row>
569
+ <entry>decimal, numeric</entry>
570
+ <entry>BigDecimal</entry>
571
+ <entry>insert</entry>
572
+ <entry>insert_array</entry>
573
+ </row>
574
+ </tbody>
575
+ </tgroup>
576
+ </table>
577
+
578
+ </chapter>
579
+
580
+ <chapter id="Development">
581
+ <title>Development</title>
582
+
583
+ <section>
584
+ <title>Building Debian Packages</title>
585
+
586
+ <para>This is just how I do it. Experts will doubtless do it better.
587
+ Each of these steps is done from the top level directory of the
588
+ package source (that is, from the parent directory of the
589
+ <filename>debian</filename> directory.</para>
590
+
591
+ <para>This is a debian native package, so there's no fooling around
592
+ with upstream versions, and package versions never have a "-1", "-2",
593
+ etc. on the end.</para>
594
+
595
+ <para>First, edit <filename>debian/changelog</filename> to increment
596
+ the version number and explain the changes. I like using
597
+ <application>debchange</application> to do it:</para>
598
+
599
+ <para><userinput><prompt>$ </prompt>dch -i -p 'Fixed a bad
600
+ bug'</userinput></para>
601
+
602
+ <para>The <userinput>-p</userinput> switch tells
603
+ <application>debchange</application> not to change the name of the
604
+ directory. That's just my preference.</para>
605
+
606
+ <para>Now build the package:</para>
607
+
608
+ <para><userinput><prompt>$ </prompt>dpkg-buildpackage -rfakeroot -us -uc
609
+ </userinput></para>
610
+
611
+ <para>I use <userinput>-us</userinput> and <userinput>-uc</userinput>
612
+ to avoid having to sign the packages. Better package maintainers will
613
+ no doubt do the right thing.</para>
614
+
615
+ <para>A whole bunch of stuff will happen. When it's done, the package
616
+ files will be in the parent directory.</para>
617
+
618
+ </section>
619
+
620
+ </chapter>
621
+
622
+ </book>