aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/tclsqlite.tcl
diff options
context:
space:
mode:
authordan miller2007-10-21 08:36:32 +0000
committerdan miller2007-10-21 08:36:32 +0000
commit2f8d7092bc2c9609fa98d6888106b96f38b22828 (patch)
treeda6c37579258cc965b52a75aee6135fe44237698 /libraries/sqlite/unix/sqlite-3.5.1/www/tclsqlite.tcl
parent* Committing new PolicyManager based on an ACL system. (diff)
downloadopensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.zip
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.gz
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.bz2
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.xz
libraries moved to opensim-libs, a new repository
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/tclsqlite.tcl')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/www/tclsqlite.tcl666
1 files changed, 0 insertions, 666 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/tclsqlite.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/tclsqlite.tcl
deleted file mode 100644
index 141cb5e..0000000
--- a/libraries/sqlite/unix/sqlite-3.5.1/www/tclsqlite.tcl
+++ /dev/null
@@ -1,666 +0,0 @@
1#
2# Run this Tcl script to generate the tclsqlite.html file.
3#
4set rcsid {$Id: tclsqlite.tcl,v 1.17 2007/06/19 17:48:57 drh Exp $}
5source common.tcl
6header {The Tcl interface to the SQLite library}
7proc METHOD {name text} {
8 puts "<a name=\"$name\">\n<h3>The \"$name\" method</h3>\n"
9 puts $text
10}
11puts {
12<h2>The Tcl interface to the SQLite library</h2>
13
14<p>The SQLite library is designed to be very easy to use from
15a Tcl or Tcl/Tk script. This document gives an overview of the Tcl
16programming interface.</p>
17
18<h3>The API</h3>
19
20<p>The interface to the SQLite library consists of single
21tcl command named <b>sqlite3</b>
22Because there is only this
23one command, the interface is not placed in a separate
24namespace.</p>
25
26<p>The <b>sqlite3</b> command is used as follows:</p>
27
28<blockquote>
29<b>sqlite3</b>&nbsp;&nbsp;<i>dbcmd&nbsp;&nbsp;database-name</i>
30</blockquote>
31
32<p>
33The <b>sqlite3</b> command opens the database named in the second
34argument. If the database does not already exist, it is
35automatically created.
36The <b>sqlite3</b> command also creates a new Tcl
37command to control the database. The name of the new Tcl command
38is given by the first argument. This approach is similar to the
39way widgets are created in Tk.
40</p>
41
42<p>
43The name of the database is just the name of a disk file in which
44the database is stored. If the name of the database is an empty
45string or the special name ":memory:" then a new database is created
46in memory.
47</p>
48
49<p>
50Once an SQLite database is open, it can be controlled using
51methods of the <i>dbcmd</i>. There are currently 22 methods
52defined.</p>
53
54<p>
55<ul>
56}
57foreach m [lsort {
58 authorizer
59 busy
60 cache
61 changes
62 close
63 collate
64 collation_needed
65 commit_hook
66 complete
67 copy
68 enable_load_extension
69 errorcode
70 eval
71 exists
72 function
73 last_insert_rowid
74 nullvalue
75 onecolumn
76 profile
77 progress
78 rollback_hook
79 timeout
80 total_changes
81 trace
82 transaction
83 update_hook
84 version
85}] {
86 puts "<li><a href=\"#$m\">$m</a></li>"
87}
88puts {
89</ul>
90</p>
91
92<p>The use of each of these methods will be explained in the sequel, though
93not in the order shown above.</p>
94
95}
96
97##############################################################################
98METHOD eval {
99<p>
100The most useful <i>dbcmd</i> method is "eval". The eval method is used
101to execute SQL on the database. The syntax of the eval method looks
102like this:</p>
103
104<blockquote>
105<i>dbcmd</i>&nbsp;&nbsp;<b>eval</b>&nbsp;&nbsp;<i>sql</i>
106&nbsp;&nbsp;&nbsp;&nbsp;?<i>array-name&nbsp;</i>?&nbsp;?<i>script</i>?
107</blockquote>
108
109<p>
110The job of the eval method is to execute the SQL statement or statements
111given in the second argument. For example, to create a new table in
112a database, you can do this:</p>
113
114<blockquote>
115<b>sqlite3 db1 ./testdb<br>
116db1 eval {CREATE TABLE t1(a int, b text)}</b>
117</blockquote>
118
119<p>The above code creates a new table named <b>t1</b> with columns
120<b>a</b> and <b>b</b>. What could be simpler?</p>
121
122<p>Query results are returned as a list of column values. If a
123query requests 2 columns and there are 3 rows matching the query,
124then the returned list will contain 6 elements. For example:</p>
125
126<blockquote>
127<b>db1 eval {INSERT INTO t1 VALUES(1,'hello')}<br>
128db1 eval {INSERT INTO t1 VALUES(2,'goodbye')}<br>
129db1 eval {INSERT INTO t1 VALUES(3,'howdy!')}<br>
130set x [db1 eval {SELECT * FROM t1 ORDER BY a}]</b>
131</blockquote>
132
133<p>The variable <b>$x</b> is set by the above code to</p>
134
135<blockquote>
136<b>1 hello 2 goodbye 3 howdy!</b>
137</blockquote>
138
139<p>You can also process the results of a query one row at a time
140by specifying the name of an array variable and a script following
141the SQL code. For each row of the query result, the values of all
142columns will be inserted into the array variable and the script will
143be executed. For instance:</p>
144
145<blockquote>
146<b>db1 eval {SELECT * FROM t1 ORDER BY a} values {<br>
147&nbsp;&nbsp;&nbsp;&nbsp;parray values<br>
148&nbsp;&nbsp;&nbsp;&nbsp;puts ""<br>
149}</b>
150</blockquote>
151
152<p>This last code will give the following output:</p>
153
154<blockquote><b>
155values(*) = a b<br>
156values(a) = 1<br>
157values(b) = hello<p>
158
159values(*) = a b<br>
160values(a) = 2<br>
161values(b) = goodbye<p>
162
163values(*) = a b<br>
164values(a) = 3<br>
165values(b) = howdy!</b>
166</blockquote>
167
168<p>
169For each column in a row of the result, the name of that column
170is used as an index in to array. The value of the column is stored
171in the corresponding array entry. The special array index * is
172used to store a list of column names in the order that they appear.
173</p>
174
175<p>
176If the array variable name is omitted or is the empty string, then the value of
177each column is stored in a variable with the same name as the column
178itself. For example:
179</p>
180
181<blockquote>
182<b>db1 eval {SELECT * FROM t1 ORDER BY a} {<br>
183&nbsp;&nbsp;&nbsp;&nbsp;puts "a=$a b=$b"<br>
184}</b>
185</blockquote>
186
187<p>
188From this we get the following output
189</p>
190
191<blockquote><b>
192a=1 b=hello<br>
193a=2 b=goodbye<br>
194a=3 b=howdy!</b>
195</blockquote>
196
197<p>
198Tcl variable names can appear in the SQL statement of the second argument
199in any position where it is legal to put a string or number literal. The
200value of the variable is substituted for the variable name. If the
201variable does not exist a NULL values is used. For example:
202</p>
203
204<blockquote><b>
205db1 eval {INSERT INTO t1 VALUES(5,$bigstring)}
206</b></blockquote>
207
208<p>
209Note that it is not necessary to quote the $bigstring value. That happens
210automatically. If $bigstring is a large string or binary object, this
211technique is not only easier to write, it is also much more efficient
212since it avoids making a copy of the content of $bigstring.
213</p>
214
215<p>
216If the $bigstring variable has both a string and a "bytearray" representation,
217then TCL inserts the value as a string. If it has only a "bytearray"
218representation, then the value is inserted as a BLOB. To force a
219value to be inserted as a BLOB even if it also has a text representation,
220us a "@" character to in place of the "$". Like this:
221</p>
222
223<blockquote><b>
224db1 eval {INSERT INTO t1 VALUES(5,@bigstring)}
225</b></blockquote>
226
227<p>
228If the variable does not have a bytearray representation, then "@" works
229just like "$".
230</p>
231
232}
233
234##############################################################################
235METHOD close {
236
237<p>
238As its name suggests, the "close" method to an SQLite database just
239closes the database. This has the side-effect of deleting the
240<i>dbcmd</i> Tcl command. Here is an example of opening and then
241immediately closing a database:
242</p>
243
244<blockquote>
245<b>sqlite3 db1 ./testdb<br>
246db1 close</b>
247</blockquote>
248
249<p>
250If you delete the <i>dbcmd</i> directly, that has the same effect
251as invoking the "close" method. So the following code is equivalent
252to the previous:</p>
253
254<blockquote>
255<b>sqlite3 db1 ./testdb<br>
256rename db1 {}</b>
257</blockquote>
258}
259
260##############################################################################
261METHOD transaction {
262
263<p>
264The "transaction" method is used to execute a TCL script inside an SQLite
265database transaction. The transaction is committed when the script completes,
266or it rolls back if the script fails. If the transaction occurs within
267another transaction (even one that is started manually using BEGIN) it
268is a no-op.
269</p>
270
271<p>
272The transaction command can be used to group together several SQLite
273commands in a safe way. You can always start transactions manually using
274BEGIN, of
275course. But if an error occurs so that the COMMIT or ROLLBACK are never
276run, then the database will remain locked indefinitely. Also, BEGIN
277does not nest, so you have to make sure no other transactions are active
278before starting a new one. The "transaction" method takes care of
279all of these details automatically.
280</p>
281
282<p>
283The syntax looks like this:
284</p>
285
286<blockquote>
287<i>dbcmd</i>&nbsp;&nbsp;<b>transaction</b>&nbsp;&nbsp;<i>?transaction-type?</i>
288&nbsp;&nbsp;<i>SCRIPT,</i>
289</blockquote>
290
291
292<p>
293The <i>transaction-type</i> can be one of <b>deferred</b>,
294<b>exclusive</b> or <b>immediate</b>. The default is deferred.
295</p>
296}
297
298##############################################################################
299METHOD cache {
300
301<p>
302The "eval" method described <a href="#eval">above</a> keeps a cache of
303<a href="capi3ref.html#sqlite3_prepare">prepared statements</a>
304for recently evaluated SQL commands.
305The "cache" method is used to control this cache.
306The first form of this command is:</p>
307
308<blockquote>
309<i>dbcmd</i>&nbsp;&nbsp;<b>cache size</b>&nbsp;&nbsp;<i>N</i>
310</blockquote>
311
312<p>This sets the maximum number of statements that can be cached.
313The upper limit is 100. The default is 10. If you set the cache size
314to 0, no caching is done.</p>
315
316<p>The second form of the command is this:</p>
317
318
319<blockquote>
320<i>dbcmd</i>&nbsp;&nbsp;<b>cache flush</b>
321</blockquote>
322
323<p>The cache-flush method
324<a href="capi3ref.html#sqlite3_finalize">finalizes</a>
325all prepared statements currently
326in the cache.</p>
327
328}
329
330##############################################################################
331METHOD complete {
332
333<p>
334The "complete" method takes a string of supposed SQL as its only argument.
335It returns TRUE if the string is a complete statement of SQL and FALSE if
336there is more to be entered.</p>
337
338<p>The "complete" method is useful when building interactive applications
339in order to know when the user has finished entering a line of SQL code.
340This is really just an interface to the
341<a href="capi3ref.html#sqlite3_complete"><b>sqlite3_complete()</b></a> C
342function.
343}
344
345##############################################################################
346METHOD copy {
347
348<p>
349The "copy" method copies data from a file into a table.
350It returns the number of rows processed successfully from the file.
351The syntax of the copy method looks like this:</p>
352
353<blockquote>
354<i>dbcmd</i>&nbsp;&nbsp;<b>copy</b>&nbsp;&nbsp;<i>conflict-algorithm</i>
355&nbsp;&nbsp;<i>table-name&nbsp;</i>&nbsp;&nbsp;<i>file-name&nbsp;</i>
356&nbsp;&nbsp;&nbsp;&nbsp;?<i>column-separator&nbsp;</i>?
357&nbsp;&nbsp;?<i>null-indicator</i>?
358</blockquote>
359
360<p>Conflict-alogrithm must be one of the SQLite conflict algorithms for
361the INSERT statement: <i>rollback</i>, <i>abort</i>,
362<i>fail</i>,<i>ignore</i>, or <i>replace</i>. See the SQLite Language
363section for <a href="lang.html#conflict">ON CONFLICT</a> for more information.
364The conflict-algorithm must be specified in lower case.
365</p>
366
367<p>Table-name must already exists as a table. File-name must exist, and
368each row must contain the same number of columns as defined in the table.
369If a line in the file contains more or less than the number of columns defined,
370the copy method rollbacks any inserts, and returns an error.</p>
371
372<p>Column-separator is an optional column separator string. The default is
373the ASCII tab character \t. </p>
374
375<p>Null-indicator is an optional string that indicates a column value is null.
376The default is an empty string. Note that column-separator and
377null-indicator are optional positional arguments; if null-indicator
378is specified, a column-separator argument must be specifed and
379precede the null-indicator argument.</p>
380
381<p>The copy method implements similar functionality to the <b>.import</b>
382SQLite shell command.
383The SQLite 2.x <a href="lang.html#copy"><b>COPY</b></a> statement
384(using the PostgreSQL COPY file format)
385can be implemented with this method as:</p>
386
387<blockquote>
388dbcmd&nbsp;&nbsp;copy&nbsp;&nbsp;$conflictalgo
389&nbsp;&nbsp;$tablename&nbsp;&nbsp;&nbsp;$filename&nbsp;
390&nbsp;&nbsp;&nbsp;&nbsp;\t&nbsp;
391&nbsp;&nbsp;\\N
392</blockquote>
393
394}
395
396##############################################################################
397METHOD timeout {
398
399<p>The "timeout" method is used to control how long the SQLite library
400will wait for locks to clear before giving up on a database transaction.
401The default timeout is 0 millisecond. (In other words, the default behavior
402is not to wait at all.)</p>
403
404<p>The SQLite database allows multiple simultaneous
405readers or a single writer but not both. If any process is writing to
406the database no other process is allows to read or write. If any process
407is reading the database other processes are allowed to read but not write.
408The entire database shared a single lock.</p>
409
410<p>When SQLite tries to open a database and finds that it is locked, it
411can optionally delay for a short while and try to open the file again.
412This process repeats until the query times out and SQLite returns a
413failure. The timeout is adjustable. It is set to 0 by default so that
414if the database is locked, the SQL statement fails immediately. But you
415can use the "timeout" method to change the timeout value to a positive
416number. For example:</p>
417
418<blockquote><b>db1 timeout 2000</b></blockquote>
419
420<p>The argument to the timeout method is the maximum number of milliseconds
421to wait for the lock to clear. So in the example above, the maximum delay
422would be 2 seconds.</p>
423}
424
425##############################################################################
426METHOD busy {
427
428<p>The "busy" method, like "timeout", only comes into play when the
429database is locked. But the "busy" method gives the programmer much more
430control over what action to take. The "busy" method specifies a callback
431Tcl procedure that is invoked whenever SQLite tries to open a locked
432database. This callback can do whatever is desired. Presumably, the
433callback will do some other useful work for a short while (such as service
434GUI events) then return
435so that the lock can be tried again. The callback procedure should
436return "0" if it wants SQLite to try again to open the database and
437should return "1" if it wants SQLite to abandon the current operation.
438}
439
440##############################################################################
441METHOD exists {
442
443<p>The "exists" method is similar to "onecolumn" and "eval" in that
444it executes SQL statements. The difference is that the "exists" method
445always returns a boolean value which is TRUE if a query in the SQL
446statement it executes returns one or more rows and FALSE if the SQL
447returns an empty set.</p>
448
449<p>The "exists" method is often used to test for the existance of
450rows in a table. For example:</p>
451
452<blockquote><b>
453if {[db exists {SELECT 1 FROM table1 WHERE user=$user}]} {<br>
454&nbsp;&nbsp;&nbsp;# Processing if $user exists<br>
455} else {<br>
456&nbsp;&nbsp;&nbsp;# Processing if $user does not exist<br>
457}
458</b></blockquote>
459}
460
461
462##############################################################################
463METHOD last_insert_rowid {
464
465<p>The "last_insert_rowid" method returns an integer which is the ROWID
466of the most recently inserted database row.</p>
467}
468
469##############################################################################
470METHOD function {
471
472<p>The "function" method registers new SQL functions with the SQLite engine.
473The arguments are the name of the new SQL function and a TCL command that
474implements that function. Arguments to the function are appended to the
475TCL command before it is invoked.</p>
476
477<p>
478The following example creates a new SQL function named "hex" that converts
479its numeric argument in to a hexadecimal encoded string:
480</p>
481
482<blockquote><b>
483db function hex {format 0x%X}
484</b></blockquote>
485
486}
487
488##############################################################################
489METHOD nullvalue {
490
491<p>
492The "nullvalue" method changes the representation for NULL returned
493as result of the "eval" method.</p>
494
495<blockquote><b>
496db1 nullvalue NULL
497</b></blockquote>
498
499<p>The "nullvalue" method is useful to differ between NULL and empty
500column values as Tcl lacks a NULL representation. The default
501representation for NULL values is an empty string.</p>
502}
503
504
505
506##############################################################################
507METHOD onecolumn {
508
509<p>The "onecolumn" method works like
510"<a href="#eval">eval</a>" in that it evaluates the
511SQL query statement given as its argument. The difference is that
512"onecolumn" returns a single element which is the first column of the
513first row of the query result.</p>
514
515<p>This is a convenience method. It saves the user from having to
516do a "<tt>[lindex&nbsp;...&nbsp;0]</tt>" on the results of an "eval"
517in order to extract a single column result.</p>
518}
519
520##############################################################################
521METHOD changes {
522
523<p>The "changes" method returns an integer which is the number of rows
524in the database that were inserted, deleted, and/or modified by the most
525recent "eval" method.</p>
526}
527
528##############################################################################
529METHOD total_changes {
530
531<p>The "total_changes" method returns an integer which is the number of rows
532in the database that were inserted, deleted, and/or modified since the
533current database connection was first opened.</p>
534}
535
536##############################################################################
537METHOD authorizer {
538
539<p>The "authorizer" method provides access to the
540<a href="capi3ref.html#sqlite3_set_authorizer">sqlite3_set_authorizer</a>
541C/C++ interface. The argument to authorizer is the name of a procedure that
542is called when SQL statements are being compiled in order to authorize
543certain operations. The callback procedure takes 5 arguments which describe
544the operation being coded. If the callback returns the text string
545"SQLITE_OK", then the operation is allowed. If it returns "SQLITE_IGNORE",
546then the operation is silently disabled. If the return is "SQLITE_DENY"
547then the compilation fails with an error.
548</p>
549
550<p>If the argument is an empty string then the authorizer is disabled.
551If the argument is omitted, then the current authorizer is returned.</p>
552}
553
554##############################################################################
555METHOD progress {
556
557<p>This method registers a callback that is invoked periodically during
558query processing. There are two arguments: the number of SQLite virtual
559machine opcodes between invocations, and the TCL command to invoke.
560Setting the progress callback to an empty string disables it.</p>
561
562<p>The progress callback can be used to display the status of a lengthy
563query or to process GUI events during a lengthy query.</p>
564}
565
566
567##############################################################################
568METHOD collate {
569
570<p>This method registers new text collating sequences. There are
571two arguments: the name of the collating sequence and the name of a
572TCL procedure that implements a comparison function for the collating
573sequence.
574</p>
575
576<p>For example, the following code implements a collating sequence called
577"NOCASE" that sorts in text order without regard to case:
578</p>
579
580<blockquote><b>
581proc nocase_compare {a b} {<br>
582&nbsp;&nbsp;&nbsp;&nbsp;return [string compare [string tolower $a] [string tolower $b]]<br>
583}<br>
584db collate NOCASE nocase_compare<br>
585</b></blockquote>
586}
587
588##############################################################################
589METHOD collation_needed {
590
591<p>This method registers a callback routine that is invoked when the SQLite
592engine needs a particular collating sequence but does not have that
593collating sequence registered. The callback can register the collating
594sequence. The callback is invoked with a single parameter which is the
595name of the needed collating sequence.</p>
596}
597
598##############################################################################
599METHOD commit_hook {
600
601<p>This method registers a callback routine that is invoked just before
602SQLite tries to commit changes to a database. If the callback throws
603an exception or returns a non-zero result, then the transaction rolls back
604rather than commit.</p>
605}
606
607##############################################################################
608METHOD rollback_hook {
609
610<p>This method registers a callback routine that is invoked just before
611SQLite tries to do a rollback. The script argument is run without change.</p>
612}
613
614##############################################################################
615METHOD update_hook {
616
617<p>This method registers a callback routine that is invoked just before
618each row is modified by an UPDATE, INSERT, or DELETE statement. Four
619arguments are appended to the callback before it is invoked:</p>
620
621<ul>
622<li>The keyword "INSERT", "UPDATE", or "DELETE", as appropriate</li>
623<li>The name of the database which is being changed</li>
624<li>The table that is being changed</li>
625<li>The rowid of the row in the table being changed</li>
626</ul>
627}
628
629##############################################################################
630METHOD incrblob {
631
632<p>This method opens a TCL channel that can be used to read or write
633into a preexisting BLOB in the database. The syntax is like this:</p>
634
635<blockquote>
636<i>dbcmd</i>&nbsp;&nbsp;<b>incrblob</b>&nbsp;&nbsp;<b>?-readonly??</b>
637&nbsp;&nbsp;<i>?DB?&nbsp;&nbsp;TABLE&nbsp;&nbsp;COLUMN&nbsp;&nbsp;ROWID</i>
638</blockquote>
639
640<p>
641The command returns a new TCL channel for reading or writing to the BLOB.
642The channel is opened using the underlying
643<a href="/capi3ref.html#sqlite3_blob_open">sqlite3_blob_open()</a> C-langauge
644interface. Close the channel using the <b>close</b> command of TCL.
645</p>
646}
647
648##############################################################################
649METHOD errorcode {
650
651<p>This method returns the numeric error code that resulted from the most
652recent SQLite operation.</p>
653}
654
655##############################################################################
656METHOD trace {
657
658<p>The "trace" method registers a callback that is invoked as each SQL
659statement is compiled. The text of the SQL is appended as a single string
660to the command before it is invoked. This can be used (for example) to
661keep a log of all SQL operations that an application performs.
662</p>
663}
664
665
666footer $rcsid