sqlite-ruby 2.1.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,34 @@
1
+ = SQLite/Ruby Interface
2
+
3
+ This module allows Ruby programs to interface with the SQLite
4
+ database engine (http://www.sqlite.org). You must have the
5
+ SQLite engine installed in order to build this module.
6
+
7
+ Note that this module is NOT compatible with SQLite 3.
8
+
9
+
10
+ == Compilation and Installation
11
+
12
+ Simply do the following, after installing SQLite:
13
+
14
+ ruby install.rb
15
+
16
+ Alternatively, you can download and install the RubyGem package for
17
+ SQLite/Ruby (you must have RubyGems and SQLite installed, first):
18
+
19
+ gem --remote --install sqlite
20
+
21
+
22
+ == Usage
23
+
24
+ For help figuring out the SQLite/Ruby interface, check out the
25
+ FAQ[http://sqlite-ruby.rubyforge.org/faq.html]. It includes examples of
26
+ usage. If you have any questions that you feel should be address in the
27
+ FAQ, please send them to jgb3@email.byu.edu.
28
+
29
+
30
+ == Contact Information
31
+
32
+ The project page is http://rubyforge.org/projects/sqlite-ruby. There, you can find
33
+ links to mailing lists and forums that you can use to discuss this library. Additionally,
34
+ there are trackers for submitting bugs and feature requests. Feel free to use them!
@@ -0,0 +1,390 @@
1
+ <html>
2
+ <head>
3
+ <title>SQLite/Ruby FAQ</title>
4
+ <style type="text/css">
5
+ a, a:visited, a:active {
6
+ color: #00F;
7
+ text-decoration: none;
8
+ }
9
+
10
+ a:hover {
11
+ text-decoration: underline;
12
+ }
13
+
14
+ .faq-list {
15
+ color: #000;
16
+ font-family: vera-sans, verdana, arial, sans-serif;
17
+ }
18
+
19
+ .faq-title {
20
+ background: #007;
21
+ color: #FFF;
22
+ font-family: vera-sans, verdana, arial, sans-serif;
23
+ padding-left: 1em;
24
+ padding-top: 0.5em;
25
+ padding-bottom: 0.5em;
26
+ font-weight: bold;
27
+ font-size: large;
28
+ border: 1px solid #000;
29
+ }
30
+
31
+ .faq-answer {
32
+ margin-left: 1em;
33
+ color: #000;
34
+ font-family: vera-sans, verdana, arial, sans-serif;
35
+ }
36
+
37
+ .faq-answer pre {
38
+ margin-left: 1em;
39
+ color: #000;
40
+ background: #FFE;
41
+ font-size: normal;
42
+ border: 1px dotted #CCC;
43
+ padding: 1em;
44
+ }
45
+
46
+ h1 {
47
+ background: #005;
48
+ color: #FFF;
49
+ font-family: vera-sans, verdana, arial, sans-serif;
50
+ padding-left: 1em;
51
+ padding-top: 1em;
52
+ padding-bottom: 1em;
53
+ font-weight: bold;
54
+ font-size: x-large;
55
+ border: 1px solid #00F;
56
+ }
57
+ </style>
58
+ </head>
59
+ <body>
60
+ <h1>SQLite/Ruby FAQ</h1>
61
+ <div class="faq-list">
62
+ <ul>
63
+ <li>How do I do a database query?
64
+ <ul>
65
+ <li><a href='#538732450'>I just want an array of the rows&#8230;</a></li>
66
+ <li><a href='#538732410'>I&#8217;d like to use a block to iterate through the rows&#8230;</a></li>
67
+ <li><a href='#538732370'>I need to get the column names as well as the rows&#8230;</a></li>
68
+ <li><a href='#538732330'>I need the result set object itself&#8230;</a></li>
69
+ <li><a href='#538732290'>I just want the first row of the result set&#8230;</a></li>
70
+ <li><a href='#538732250'>I just want the first value of the first row of the result set&#8230;</a></li>
71
+ </ul>
72
+ </li>
73
+ <li><a href='#538732180'>How do I prepare a statement for repeated execution?</a></li>
74
+ <li><a href='#538732140'>How do I use placeholders in an <span class="caps">SQL</span> statement?</a></li>
75
+ <li><a href='#538732100'>How do I discover metadata about a query?</a></li>
76
+ <li><a href='#538732060'>I&#8217;d like the rows to be indexible by column name.</a></li>
77
+ <li><a href='#538732020'>I&#8217;d like the values from a query to be the correct types, instead of String.</a></li>
78
+ <li><a href='#538731980'>How do I do a <span class="caps">DDL </span>(insert, update, delete) statement?</a></li>
79
+ <li><a href='#538731940'>How do I execute multiple statements in a single string?</a></li>
80
+ <li><a href='#538731900'>How do I begin/end a transaction?</a></li>
81
+ </ul>
82
+ </div>
83
+ <a name='538732450'></a>
84
+ <div class='faq-title'>How do I do a database query? I just want an array of the rows&#8230;</div>
85
+ <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method. If you don&#8217;t give it a block, it will return an array of all the rows:</p>
86
+
87
+ <pre>
88
+ require 'sqlite'
89
+
90
+ db = SQLite::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
91
+ rows = db.execute( "select * from test" )
92
+ </pre></div>
93
+ <a name='538732410'></a>
94
+ <div class='faq-title'>How do I do a database query? I&#8217;d like to use a block to iterate through the rows&#8230;</div>
95
+ <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method. If you give it a block, each row of the result will be yielded to the block:</p>
96
+
97
+ <pre>
98
+ require 'sqlite'
99
+
100
+ db = SQLite::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
101
+ db.execute( "select * from test" ) do |row|
102
+ ...
103
+ end
104
+ </pre></div>
105
+ <a name='538732370'></a>
106
+ <div class='faq-title'>How do I do a database query? I need to get the column names as well as the rows&#8230;</div>
107
+ <div class='faq-answer'><p>Use the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a> method. This works just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>; if you don&#8217;t give it a block, it returns an array of rows; otherwise, it will yield each row to the block. <em>However</em>, the first row returned is always an array of the column names from the query:</p>
108
+
109
+ <pre>
110
+ require 'sqlite'
111
+
112
+ db = SQLite::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
113
+ columns, *rows = db.execute( "select * from test" )
114
+
115
+ # or use a block:
116
+
117
+ columns = nil
118
+ db.execute( "select * from test" ) do |row|
119
+ if columns.nil?
120
+ columns = row
121
+ else
122
+ # process row
123
+ end
124
+ end
125
+ </pre></div>
126
+ <a name='538732330'></a>
127
+ <div class='faq-title'>How do I do a database query? I need the result set object itself&#8230;</div>
128
+ <div class='faq-answer'><p>Sometimes you don&#8217;t want all the rows at once, and yet you&#8217;d like to be able to iterate through the results. For instance, you may want to pass the results to some other function (or series of functions) and have them pull rows from the results on demand. This is more effecient for very large queries.</p>
129
+
130
+ <p>To do this, use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a>. If called without a block, it returns the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/ResultSet.html'>ResultSet</a> instance. If called <em>with</em> a block, it yields the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/ResultSet.html'>ResultSet</a> to the block (and then closes the set when the block terminates).</p>
131
+
132
+ <p>You can do both internal and external iteration with the result set this way:</p>
133
+
134
+ <pre>
135
+ require 'sqlite'
136
+
137
+ db = SQLite::<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a>.new( "test.db" )
138
+ result = db.query( "select * from test" )
139
+
140
+ if do_external_iteration
141
+
142
+ while ( row = result.next )
143
+ # process row
144
+ end
145
+
146
+ elsif do_internal_iteration
147
+
148
+ result.each do |row|
149
+ # process row
150
+ end
151
+
152
+ elsif get_result_metadata
153
+
154
+ column_names = result.columns
155
+ column_types = result.types
156
+
157
+ end
158
+
159
+ result.close
160
+ </pre>
161
+ <em>Note</em>: if you are using <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a> to execute statements with no result sets (ie, inserts, deletes, udpates, etc.), you <em>must</em> call <code>result.next</code> to actually perform the operation. Otherwise, the operation will never be executed:
162
+
163
+ <pre>
164
+ db.query( "insert into table values ( 'a', 'b' )" ) do |result|
165
+ result.next
166
+ end
167
+ </pre>
168
+
169
+ <p>In general, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a> is not a very good choice for such operations&#8230;</p></div>
170
+ <a name='538732290'></a>
171
+ <div class='faq-title'>How do I do a database query? I just want the first row of the result set&#8230;</div>
172
+ <div class='faq-answer'><p>Easy. Just call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#get_first_row</a>:</p>
173
+
174
+ <pre>
175
+ row = db.get_first_row( "select * from table" )
176
+ </pre>
177
+
178
+ <p>This also supports bind variables, just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> and friends.</p></div>
179
+ <a name='538732250'></a>
180
+ <div class='faq-title'>How do I do a database query? I just want the first value of the first row of the result set&#8230;</div>
181
+ <div class='faq-answer'><p>Also easy. Just call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#get_first_value</a>:</p>
182
+
183
+ <pre>
184
+ count = db.get_first_value( "select count(*) from table" )
185
+ </pre>
186
+
187
+ <p>This also supports bind variables, just like <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> and friends.</p></div>
188
+ <a name='538732180'></a>
189
+ <div class='faq-title'>How do I prepare a statement for repeated execution?</div>
190
+ <div class='faq-answer'><p>If the same statement is going to be executed repeatedly, you can speed things up a bit by <em>preparing</em> the statement. You do this via the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#prepare</a> method. It returns a <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a> object, and you can then invoke #execute on that to get the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/ResultSet.html'>ResultSet</a>:</p>
191
+
192
+ <pre>
193
+ stmt = db.prepare( "select * from person" )
194
+
195
+ 1000.times do
196
+ stmt.execute do |result|
197
+ ...
198
+ end
199
+ end
200
+ </pre>
201
+
202
+ <p>This is made more useful by the ability to bind variables to placeholders via the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_param</a> and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_params</a> methods. (See the next <span class="caps">FAQ</span> for details.)</p></div>
203
+ <a name='538732140'></a>
204
+ <div class='faq-title'>How do I use placeholders in an <span class="caps">SQL</span> statement?</div>
205
+ <div class='faq-answer'><p>Placeholders in an <span class="caps">SQL</span> statement take any of the following formats:</p>
206
+
207
+ <ul>
208
+ <li><code>?</code></li>
209
+ <li><code>?<em>n</em></code></li>
210
+ <li><code>:<em>word</em></code></li>
211
+ <li><code>:<em>word</em>:</code></li>
212
+ </ul>
213
+
214
+ <p>Where <em>n</em> is an integer, and <em>word</em> is an alpha-numeric identifier (or number). When the placeholder is associated with a number, that number identifies the index of the bind variable to replace it with. When it is an identifier, it identifies the name of the correponding bind variable. (In the instance of the first format&#8212;a single question mark&#8212;the placeholder is assigned a number one greater than the last index used, or 1 if it is the first.)</p>
215
+
216
+ <p>For example, here is a query using these placeholder formats:</p>
217
+
218
+ <pre>
219
+ select *
220
+ from table
221
+ where ( c = ?2 or c = ? )
222
+ and d = :name
223
+ and e = :spouse:
224
+ and f = :1
225
+ </pre>
226
+
227
+ <p>This defines 5 different placeholders: 1, 2, 3, &#8220;name&#8221;, and &#8220;spouse&#8221;.</p>
228
+
229
+ <p>You replace these placeholders by <em>binding</em> them to values. This can be accomplished in a variety of ways.</p>
230
+
231
+ <p>The <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a>, and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a> methods all accept additional arguments following the <span class="caps">SQL</span> statement. These arguments are assumed to be bind parameters, and they are bound (positionally) to their corresponding placeholders:</p>
232
+
233
+ <pre>
234
+ db.execute( "select * from table where a = ? and b = ?",
235
+ "hello",
236
+ "world" )
237
+ </pre>
238
+
239
+ <p>The above would replace the first question mark with &#8216;hello&#8217; and the second with &#8216;world&#8217;. If the placeholders have an explicit index given, they will be replaced with the bind parameter at that index (1-based).</p>
240
+
241
+ <p>If a Hash is given as a bind parameter, then its key/value pairs are bound to the placeholders. This is how you bind by name:</p>
242
+
243
+ <pre>
244
+ db.execute( "select * from table where a = :name and b = :value",
245
+ "name" =&gt; "bob",
246
+ "value" =&gt; "priceless" )
247
+ </pre>
248
+
249
+ <p>You can also bind explicitly using the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a> object itself. Just do a <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#prepare</a> to get the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a>, and then use either <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_param</a> or <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#bind_params</a>:</p>
250
+
251
+ <pre>
252
+ stmt = db.prepare( "select * from table where a = :name and b = ?" )
253
+
254
+ stmt.bind_param( "name", "bob" )
255
+ stmt.bind_param( 1, "value" )
256
+
257
+ # or
258
+
259
+ stmt.bind_params( "value", "name" =&gt; "bob" )
260
+ </pre></div>
261
+ <a name='538732100'></a>
262
+ <div class='faq-title'>How do I discover metadata about a query?</div>
263
+ <div class='faq-answer'><p>If you ever want to know the names or types of the columns in a result set, you can do it in several ways.</p>
264
+
265
+ <p>The first way is to ask the row object itself. Each row will have a property &#8220;fields&#8221; that returns an array of the column names. The row will also have a property &#8220;types&#8221; that returns an array of the column types:</p>
266
+
267
+ <pre>
268
+ rows = db.execute( "select * from table" )
269
+ p rows[0].fields
270
+ p rows[0].types
271
+ </pre>
272
+
273
+ <p>Obviously, this approach requires you to execute a statement that actually returns data. If you don&#8217;t know if the statement will return any rows, but you still need the metadata, you can use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a> and ask the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/ResultSet.html'>ResultSet</a> object itself:</p>
274
+
275
+ <pre>
276
+ db.query( "select * from table" ) do |result|
277
+ p result.columns
278
+ p result.types
279
+ ...
280
+ end
281
+ </pre>
282
+
283
+ <p>Lastly, you can use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#prepare</a> and ask the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement</a> object what the metadata are:</p>
284
+
285
+ <pre>
286
+ stmt = db.prepare( "select * from table" )
287
+ p stmt.columns
288
+ p stmt.types
289
+ </pre></div>
290
+ <a name='538732060'></a>
291
+ <div class='faq-title'>I&#8217;d like the rows to be indexible by column name.</div>
292
+ <div class='faq-answer'><p>By default, each row from a query is returned as an Array of values. This means that you can only obtain values by their index. Sometimes, however, you would like to obtain values by their column name.</p>
293
+
294
+ <p>The first way to do this is to set the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database</a> property &#8220;results_as_hash&#8221; to true. If you do this, then all rows will be returned as Hash objects, with the column names as the keys. (In this case, the &#8220;fields&#8221; property is unavailable on the row, although the &#8220;types&#8221; property remains.)</p>
295
+
296
+ <pre>
297
+ db.results_as_hash = true
298
+ db.execute( "select * from table" ) do |row|
299
+ p row['column1']
300
+ p row['column2']
301
+ end
302
+ </pre>
303
+
304
+ <p>The other way is to use Ara Howard&#8217;s <a href="http://rubyforge.org/projects/arrayfields">ArrayFields</a> module. Just require &#8220;arrayfields&#8221;, and all of your rows will be indexable by column name, even though they are still arrays!</p>
305
+
306
+ <pre>
307
+ require 'arrayfields'
308
+
309
+ ...
310
+ db.execute( "select * from table" ) do |row|
311
+ p row[0] == row['column1']
312
+ p row[1] == row['column2']
313
+ end
314
+ </pre></div>
315
+ <a name='538732020'></a>
316
+ <div class='faq-title'>I&#8217;d like the values from a query to be the correct types, instead of String.</div>
317
+ <div class='faq-answer'><p>You can turn on &#8220;type translation&#8221; by setting <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#type_translation</a> to true:</p>
318
+
319
+ <pre>
320
+ db.type_translation = true
321
+ db.execute( "select * from table" ) do |row|
322
+ p row
323
+ end
324
+ </pre>
325
+
326
+ <p>By doing this, each return value for each row will be translated to its correct type, based on its declared column type.</p>
327
+
328
+ <p>You can even declare your own translation routines, if (for example) you are using an <span class="caps">SQL</span> type that is not handled by default:</p>
329
+
330
+ <pre>
331
+ # assume "objects" table has the following schema:
332
+ # create table objects (
333
+ # name varchar2(20),
334
+ # thing object
335
+ # )
336
+
337
+ db.type_translation = true
338
+ db.translator.add_translator( "object" ) do |type, value|
339
+ db.decode( value )
340
+ end
341
+
342
+ h = { :one=&gt;:two, "three"=&gt;"four", 5=&gt;6 }
343
+ dump = db.encode( h )
344
+
345
+ db.execute( "insert into objects values ( ?, ? )", "bob", dump )
346
+
347
+ obj = db.get_first_value( "select thing from objects where name='bob'" )
348
+ p obj == h
349
+ </pre></div>
350
+ <a name='538731980'></a>
351
+ <div class='faq-title'>How do I do a <span class="caps">DDL </span>(insert, update, delete) statement?</div>
352
+ <div class='faq-answer'><p>You can actually do inserts, updates, and deletes in exactly the same way as selects, but in general the <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a> method will be most convenient:</p>
353
+
354
+ <pre>
355
+ db.execute( "insert into table values ( ?, ? )", *bind_vars )
356
+ </pre></div>
357
+ <a name='538731940'></a>
358
+ <div class='faq-title'>How do I execute multiple statements in a single string?</div>
359
+ <div class='faq-answer'><p>The standard query methods (<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute</a>, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute2</a>, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#query</a>, and <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Statement.html'>Statement#execute</a>) will only execute the first statement in the string that is given to them. Thus, if you have a string with multiple <span class="caps">SQL</span> statements, each separated by a string, you can&#8217;t use those methods to execute them all at once.</p>
360
+
361
+ <p>Instead, use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute_batch</a>:</p>
362
+
363
+ <pre>
364
+ sql = &lt;&lt;SQL
365
+ create table the_table (
366
+ a varchar2(30),
367
+ b varchar2(30)
368
+ );
369
+
370
+ insert into the_table values ( 'one', 'two' );
371
+ insert into the_table values ( 'three', 'four' );
372
+ insert into the_table values ( 'five', 'six' );
373
+ SQL
374
+
375
+ db.execute_batch( sql )
376
+ </pre>
377
+
378
+ <p>Unlike the other query methods, <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#execute_batch</a> accepts no block. It will also only ever return <ins>nil</ins>. Thus, it is really only suitable for batch processing of <span class="caps">DDL</span> statements.</p></div>
379
+ <a name='538731900'></a>
380
+ <div class='faq-title'>How do I begin/end a transaction?</div>
381
+ <div class='faq-answer'><p>Use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#transaction</a> to start a transaction. If you give it a block, the block will be automatically committed at the end of the block, unless an exception was raised, in which case the transaction will be rolled back. (Never explicitly call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#commit</a> or <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#rollback</a> inside of a transaction block&#8212;you&#8217;ll get errors when the block terminates!)</p>
382
+
383
+ <pre> database.transaction do |db| db.execute( "insert into table values ( 'a', 'b', 'c' )" ) ... end </pre>
384
+
385
+ <p>Alternatively, if you don&#8217;t give a block to <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#transaction</a>, the transaction remains open until you explicitly call <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#commit</a> or <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#rollback</a>.</p>
386
+
387
+ <pre> db.transaction db.execute( "insert into table values ( 'a', 'b', 'c' )" ) db.commit </pre>
388
+
389
+ <p>Note that SQLite does not allow nested transactions, so you&#8217;ll get errors if you try to open a new transaction while one is already active. Use <a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html'>Database#transaction_active</a>? to determine whether a transaction is active or not.</p></div>
390
+ </body></html>
@@ -0,0 +1,145 @@
1
+ require 'yaml'
2
+ require 'redcloth'
3
+
4
+ def process_faq_list( faqs )
5
+ puts "<ul>"
6
+ faqs.each do |faq|
7
+ process_faq_list_item faq
8
+ end
9
+ puts "</ul>"
10
+ end
11
+
12
+ def process_faq_list_item( faq )
13
+ question = faq.keys.first
14
+ answer = faq.values.first
15
+
16
+ print "<li>"
17
+
18
+ question_text = RedCloth.new(question).to_html.gsub( %r{</?p>},"" )
19
+ if answer.is_a?( Array )
20
+ puts question_text
21
+ process_faq_list answer
22
+ else
23
+ print "<a href='##{question.id}'>#{question_text}</a>"
24
+ end
25
+
26
+ puts "</li>"
27
+ end
28
+
29
+ def process_faq_descriptions( faqs, path=nil )
30
+ faqs.each do |faq|
31
+ process_faq_description faq, path
32
+ end
33
+ end
34
+
35
+ def process_faq_description( faq, path )
36
+ question = faq.keys.first
37
+ path = ( path ? path + " " : "" ) + question
38
+ answer = faq.values.first
39
+
40
+ if answer.is_a?( Array )
41
+ process_faq_descriptions( answer, path )
42
+ else
43
+ title = RedCloth.new( path ).to_html.gsub( %r{</?p>}, "" )
44
+ answer = RedCloth.new( answer || "" )
45
+
46
+ puts "<a name='#{question.id}'></a>"
47
+ puts "<div class='faq-title'>#{title}</div>"
48
+ puts "<div class='faq-answer'>#{add_api_links(answer.to_html)}</div>"
49
+ end
50
+ end
51
+
52
+ API_OBJECTS = [ "Database", "Statement", "ResultSet",
53
+ "ParsedStatement", "Pragmas", "Translator" ].inject( "(" ) { |acc,name|
54
+ acc << "|" if acc.length > 1
55
+ acc << name
56
+ acc
57
+ } + ")"
58
+
59
+ def add_api_links( text )
60
+ text.gsub( /#{API_OBJECTS}(#(\w+))?/ ) do
61
+ disp_obj = obj = $1
62
+
63
+ case obj
64
+ when "Pragmas": disp_obj = "Database"
65
+ end
66
+
67
+ method = $3
68
+ s = "<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/#{obj}.html'>#{disp_obj}"
69
+ s << "##{method}" if method
70
+ s << "</a>"
71
+ s
72
+ end
73
+ end
74
+
75
+ faqs = YAML.load( File.read( "faq.yml" ) )
76
+
77
+ puts <<-EOF
78
+ <html>
79
+ <head>
80
+ <title>SQLite/Ruby FAQ</title>
81
+ <style type="text/css">
82
+ a, a:visited, a:active {
83
+ color: #00F;
84
+ text-decoration: none;
85
+ }
86
+
87
+ a:hover {
88
+ text-decoration: underline;
89
+ }
90
+
91
+ .faq-list {
92
+ color: #000;
93
+ font-family: vera-sans, verdana, arial, sans-serif;
94
+ }
95
+
96
+ .faq-title {
97
+ background: #007;
98
+ color: #FFF;
99
+ font-family: vera-sans, verdana, arial, sans-serif;
100
+ padding-left: 1em;
101
+ padding-top: 0.5em;
102
+ padding-bottom: 0.5em;
103
+ font-weight: bold;
104
+ font-size: large;
105
+ border: 1px solid #000;
106
+ }
107
+
108
+ .faq-answer {
109
+ margin-left: 1em;
110
+ color: #000;
111
+ font-family: vera-sans, verdana, arial, sans-serif;
112
+ }
113
+
114
+ .faq-answer pre {
115
+ margin-left: 1em;
116
+ color: #000;
117
+ background: #FFE;
118
+ font-size: normal;
119
+ border: 1px dotted #CCC;
120
+ padding: 1em;
121
+ }
122
+
123
+ h1 {
124
+ background: #005;
125
+ color: #FFF;
126
+ font-family: vera-sans, verdana, arial, sans-serif;
127
+ padding-left: 1em;
128
+ padding-top: 1em;
129
+ padding-bottom: 1em;
130
+ font-weight: bold;
131
+ font-size: x-large;
132
+ border: 1px solid #00F;
133
+ }
134
+ </style>
135
+ </head>
136
+ <body>
137
+ <h1>SQLite/Ruby FAQ</h1>
138
+ <div class="faq-list">
139
+ EOF
140
+
141
+ process_faq_list( faqs )
142
+ puts "</div>"
143
+ process_faq_descriptions( faqs )
144
+
145
+ puts "</body></html>"