tdbc_connection.n 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. '\"
  2. '\" tdbc::connection.n --
  3. '\"
  4. '\" Copyright (c) 2008 by Kevin B. Kenny.
  5. '\"
  6. '\" See the file "license.terms" for information on usage and redistribution of
  7. '\" this file, and for a DISCLAIMER OF ALL WARRANTIES.
  8. .TH "tdbc::connection" n 8.6 Tcl "Tcl Database Connectivity"
  9. '\" .so man.macros
  10. '\" IGNORE
  11. .if t .wh -1.3i ^B
  12. .nr ^l \n(.l
  13. .ad b
  14. '\" # BS - start boxed text
  15. '\" # ^y = starting y location
  16. '\" # ^b = 1
  17. .de BS
  18. .br
  19. .mk ^y
  20. .nr ^b 1u
  21. .if n .nf
  22. .if n .ti 0
  23. .if n \l'\\n(.lu\(ul'
  24. .if n .fi
  25. ..
  26. '\" # BE - end boxed text (draw box now)
  27. .de BE
  28. .nf
  29. .ti 0
  30. .mk ^t
  31. .ie n \l'\\n(^lu\(ul'
  32. .el \{\
  33. '\" Draw four-sided box normally, but don't draw top of
  34. '\" box if the box started on an earlier page.
  35. .ie !\\n(^b-1 \{\
  36. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  37. .\}
  38. .el \}\
  39. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  40. .\}
  41. .\}
  42. .fi
  43. .br
  44. .nr ^b 0
  45. ..
  46. '\" END IGNORE
  47. .BS
  48. .SH "NAME"
  49. tdbc::connection \- TDBC connection object
  50. .SH "SYNOPSIS"
  51. .nf
  52. package require \fBtdbc 1.0\fR
  53. package require \fBtdbc::\fIdriver version\fR
  54. \fBtdbc::\fIdriver\fB::connection create \fIdb\fR ?\fI\-option value\fR...?
  55. \fIdb \fBconfigure\fR ?\fI\-option value\fR...?
  56. \fIdb \fBclose\fR
  57. \fIdb \fBforeignkeys\fR ?\fI\-primary tableName\fR? ?\fI\-foreign tableName\fR?
  58. \fIdb \fBprepare\fR \fIsql-code\fR
  59. \fIdb \fBpreparecall\fR \fIcall\fR
  60. \fIdb \fBprimarykeys\fR \fItableName\fR
  61. \fIdb \fBstatements\fR
  62. \fIdb \fBresultsets\fR
  63. \fIdb \fBtables\fR ?\fIpattern\fR?
  64. \fIdb \fBcolumns\fR \fItable\fR ?\fIpattern\fR?
  65. \fIdb \fBbegintransaction\fR
  66. \fIdb \fBcommit\fR
  67. \fIdb \fBrollback\fR
  68. \fIdb \fBtransaction\fR \fIscript\fR
  69. .fi
  70. .ad l
  71. .in 14
  72. .ti 7
  73. \fIdb \fBallrows\fR ?\fB\-as lists\fR|\fBdicts\fR? ?\fB\-columnsvariable \fIname\fR? ?\fB\-\-\fR? \fIsql-code\fR ?\fIdictionary\fR?
  74. .br
  75. .ti 7
  76. \fIdb \fBforeach\fR ?\fB\-as lists\fR|\fBdicts\fR? ?\fB\-columnsvariable \fIname\fR? ?\-\-? \fIvarName sqlcode\fR ?\fIdictionary\fR? \fIscript\fR
  77. .ad b
  78. .BE
  79. .SH "DESCRIPTION"
  80. .PP
  81. Every database driver for TDBC (Tcl DataBase Connectivity) implements
  82. a \fIconnection\fR object that represents a connection to a database.
  83. By convention, this object is created by the command,
  84. \fBtdbc::\fIdriver\fB::connection create\fR.
  85. This command accepts the name of a Tcl command that will represent the
  86. connection and a possible set of options (see \fBCONFIGURATION
  87. OPTIONS\fR). It establishes a connection to the database and returns
  88. the name of the newly-created Tcl command.
  89. .PP
  90. The \fBconfigure\fR object command on a database connection, if
  91. presented with no arguments, returns a list of alternating keywords
  92. and values representing the connection's current configuration. If
  93. presented with a single argument \fI\-option\fR, it returns the
  94. configured value of the given option. Otherwise, it must be given an
  95. even number of arguments which are alternating options and values. The
  96. specified options receive the specified values, and nothing is
  97. returned.
  98. .PP
  99. The \fBclose\fR object command on a database connection closes the
  100. connection. All active statements and result sets on the connection
  101. are closed. Any uncommitted transaction is rolled back. The object
  102. command is deleted.
  103. .PP
  104. The \fBprepare\fR object command on a database connection prepares a SQL
  105. statement for execution. The \fIsql-code\fR argument must contain a
  106. single SQL statement to be executed. Bound variables may be
  107. included. The return value is a
  108. newly-created Tcl command that represents the statement. See
  109. \fBtdbc::statement\fR for more detailed discussion of the
  110. SQL accepted by the \fBprepare\fR object command and the
  111. interface accepted by a statement.
  112. .PP
  113. On a database connection where the underlying database and driver
  114. support stored procedures, the \fBpreparecall\fR
  115. object command prepares a call to a stored procedure for execution.
  116. The syntax of the stored procedure call is:
  117. .PP
  118. .CS
  119. ?\fIresultvar\fR =? \fIprocname\fR(?\fIarg\fR ?, \fIarg\fR...?)
  120. .CE
  121. .PP
  122. The return value is a
  123. newly-created Tcl command that represents the statement. See
  124. \fBtdbc::statement\fR for the interface accepted by a statement.
  125. .PP
  126. The \fBstatements\fR object command returns a list of statements
  127. that have been created by \fBprepare\fR and \fBpreparecall\fR
  128. statements against the given connection and have not yet been closed.
  129. .PP
  130. The \fBresultsets\fR object command returns a list of result sets
  131. that have been obtained by executing statements prepared using the
  132. given connection and not yet closed.
  133. .PP
  134. The \fBtables\fR object command allows the program to query the
  135. connection for the names of tables that exist in the database.
  136. The optional \fIpattern\fR parameter is a pattern to match the name of
  137. a table. It may contain the SQL wild-card characters '\fB%\fR' and
  138. '\fB_\fR'. The return value is a dictionary whose keys are table names
  139. and whose values are subdictionaries. See the documentation for the
  140. individual database driver for the interpretation of the values.
  141. .PP
  142. The \fBcolumns\fR object command allows the program to query the
  143. connection for the names of columns that exist in a given table.
  144. The optional \fBpattern\fR parameter is a pattern to match the name of
  145. a column. It may contain the SQL wild-card characters '\fB%\fR' and
  146. '\fB_\fR'. The return value is a dictionary whose keys are column names
  147. and whose values are dictionaries. Each of the subdictionaries will
  148. contain at least the following keys and values (and may contain others
  149. whose usage is determined by a specific database driver).
  150. .IP \fBtype\fR
  151. Contains the data type of the column, and will generally be chosen
  152. from the set,
  153. \fBbigint\fR, \fBbinary\fR, \fBbit\fR, \fBchar\fR, \fBdate\fR,
  154. \fBdecimal\fR, \fBdouble\fR, \fBfloat\fR, \fBinteger\fR,
  155. \fBlongvarbinary\fR, \fBlongvarchar\fR, \fBnumeric\fR, \fBreal\fR,
  156. \fBtime\fR, \fBtimestamp\fR, \fBsmallint\fR, \fBtinyint\fR,
  157. \fBvarbinary\fR, and \fBvarchar\fR. (If the column has a type that
  158. cannot be represented as one of the above, \fBtype\fR will contain
  159. a driver-dependent description of the type.)
  160. .IP \fBprecision\fR
  161. Contains the precision of the column in bits, decimal digits, or the
  162. width in characters, according to the type.
  163. .IP \fBscale\fR
  164. Contains the scale of the column (the number of digits after the radix
  165. point), for types that support the concept.
  166. .IP \fBnullable\fR
  167. Contains 1 if the column can contain NULL values, and 0 otherwise.
  168. .PP
  169. The \fBprimarykeys\fR object command allows the program to query the
  170. connection for the primary keys belonging to a given table. The
  171. \fItableName\fR parameter identifies the table being interrogated. The result
  172. is a list of dictionaries enumerating the keys (in a similar format to the
  173. list returned by \fI$connection\fR \fBallrows -as dicts\fR). The keys of the
  174. dictionary may include at least the following. Values that are NULL or
  175. meaningless in a given database are omitted.
  176. .IP \fBtableCatalog\fR
  177. Name of the catalog in which the table appears.
  178. .IP \fBtableSchema\fR
  179. Name of the schema in which the table appears.
  180. .IP \fBtableName\fR
  181. Name of the table owning the primary key.
  182. .IP \fBconstraintCatalog\fR
  183. Name of the catalog in which the primary key constraint appears. In some
  184. database systems, this may not be the same as the table's catalog.
  185. .IP \fBconstraintSchema\fR
  186. Name of the schema in which the primary key constraint appears. In some
  187. database systems, this may not be the same as the table's schema.
  188. .IP \fBconstraintName\fR
  189. Name of the primary key constraint,
  190. .IP \fBcolumnName\fR
  191. Name of a column that is a member of the primary key.
  192. .IP \fBordinalPosition\fR
  193. Ordinal position of the column within the primary key.
  194. .PP
  195. To these columns may be added additional ones that are specific to
  196. a particular database system.
  197. .PP
  198. The \fBforeignkeys\fR object command allows the program to query the
  199. connection for foreign key relationships that apply to a particular table.
  200. The relationships may be constrained to the keys that appear in a
  201. particular table (\fB-foreign\fR \fItableName\fR), the keys that
  202. refer to a particular table (\fB-primary\fR \fItableName\fR), or both.
  203. At least one of \fB-primary\fR and \fB-foreign\fR should be specified,
  204. although some drivers will enumerate all foreign keys in the current
  205. catalog if both options are omitted. The result of the \fBforeignkeys\fR
  206. object command is a list of dictionaries, with one list element per key
  207. (in a similar format to the
  208. list returned by \fI$connection\fR \fBallrows -as dicts\fR). The keys of the
  209. dictionary may include at least the following. Values that are NULL or
  210. meaningless in a given database are omitted.
  211. .IP \fBforeignConstraintCatalog\fR
  212. Catalog in which the foreign key constraint appears.
  213. .IP \fBforeignConstraintSchema\fR
  214. Schema in which the foreign key constraint appears.
  215. .IP \fBforeignConstraintName\fR
  216. Name of the foreign key constraint.
  217. .IP \fBprimaryConstraintCatalog\fR
  218. Catalog holding the primary key constraint (or unique key constraint) on the
  219. column to which the foreign key refers.
  220. .IP \fBprimaryConstraintSchema\fR
  221. Schema holding the primary key constraint (or unique key constraint) on the
  222. column to which the foreign key refers.
  223. .IP \fBprimaryConstraintName\fR
  224. Name of the primary key constraint (or unique key constraint) on the
  225. column to which the foreign key refers.
  226. .IP \fBupdateAction\fR
  227. Action to take when an UPDATE statement invalidates the constraint.
  228. The value will be \fBCASCADE\fR, \fBSET DEFAULT\fR, \fBSET NULL\fR,
  229. \fBRESTRICT\fR, or \fBNO ACTION\fR.
  230. .IP \fBdeleteAction\fR
  231. Action to take when a DELETE statement invalidates the constraint.
  232. The value will be \fBCASCADE\fR, \fBSET DEFAULT\fR, \fBSET NULL\fR,
  233. \fBRESTRICT\fR, or \fBNO ACTION\fR.
  234. .IP \fBprimaryCatalog\fR
  235. Catalog name in which the primary table (the one to which the foreign key
  236. refers) appears.
  237. .IP \fBprimarySchema\fR
  238. Schema name in which the primary table (the one to which the foreign key
  239. refers) appears.
  240. .PP
  241. .IP \fBprimaryTable\fR
  242. Table name of the primary table (the one to which the foreign key
  243. refers).
  244. .IP \fBprimaryColumn\fR
  245. Name of the column to which the foreign key refers.
  246. .IP \fBforeignCatalog\fR
  247. Name of the catalog in which the table containing the foreign key appears.
  248. .IP \fBforeignSchema\fR
  249. Name of the schema in which the table containing the foreign key appears.
  250. .IP \fBforeignTable\fR
  251. Name of the table containing the foreign key.
  252. .IP \fBforeignColumn\fR
  253. Name of the column appearing in the foreign key.
  254. .IP \fBordinalPosition\fR
  255. Position of the column in the foreign key, if the key is a compound key.
  256. .PP
  257. The \fBbegintransaction\fR object command on a database connection
  258. begins a transaction on the database. If the underlying database does
  259. not support atomic, consistent, isolated, durable transactions, the
  260. \fBbegintransaction\fR object command returns an error reporting the
  261. fact. Similarly, if multiple \fBbegintransaction\fR commands are executed
  262. withough an intervening \fBcommit\fR or \fBrollback\fR command, an
  263. error is returned unless the underlying database supports nested
  264. transactions.
  265. .PP
  266. The \fBcommit\fR object command on a database connection ends the most
  267. recent transaction started by \fBbegintransaction\fR and commits
  268. changes to the database.
  269. .PP
  270. The \fBrollback\fR object command on a database connection rolls back
  271. the most recent transaction started by \fBbegintransaction\fR. The
  272. state of the database is as if nothing happened during the
  273. transaction.
  274. .PP
  275. The \fBtransaction\fR object command on a database connection
  276. presents a simple way of bundling a database transaction. It begins a
  277. transaction, and evaluates the supplied \fIscript\fR argument as a Tcl
  278. script in the caller's scope. If \fIscript\fR terminates normally, or
  279. by \fBbreak\fR, \fBcontinue\fR, or \fBreturn\fR, the transaction is
  280. committed (and any action requested by \fBbreak\fR, \fBcontinue\fR, or
  281. \fBreturn\fR takes place). If the commit fails for any reason,
  282. the error in the commit is treated as an error in the \fIscript\fR.
  283. In the case of an error in \fIscript\fR or in the commit,
  284. the transaction is rolled back and the error is
  285. rethrown. Any nonstandard return code from the script
  286. causes the transaction to be rolled back and then is rethrown.
  287. .PP
  288. The \fBallrows\fR object command prepares a SQL statement (given by
  289. the \fIsql-code\fR parameter) to execute against the database.
  290. It then executes it (see \fBtdbc::statement\fR for details) with the
  291. optional \fIdictionary\fR parameter giving bind variables. Finally,
  292. it uses the \fIallrows\fR object command on the result set (see
  293. \fBtdbc::resultset\fR) to construct a list of the results. Finally, both
  294. result set and statement are closed. The return value is the list of
  295. results.
  296. .PP
  297. The \fBforeach\fR object command prepares a SQL statement (given by
  298. the \fIsql-code\fR parameter) to execute against the database.
  299. It then executes it (see \fBtdbc::statement\fR for details) with the
  300. optional \fIdictionary\fR parameter giving bind variables. Finally,
  301. it uses the \fIforeach\fR object command on the result set (see
  302. \fBtdbc::resultset\fR) to evaluate the given \fIscript\fR for each row of
  303. the results. Finally, both result set and statement are closed, even
  304. if the given \fIscript\fR results in a \fBreturn\fR, an error, or
  305. an unusual return code.
  306. .SH "CONFIGURATION OPTIONS"
  307. The configuration options accepted when the connection is created and
  308. on the connection's \fBconfigure\fR object command include the
  309. following, and may include others specific to a database driver.
  310. .IP "\fB\-encoding \fIname\fR"
  311. Specifies the encoding to be used in connecting to the database.
  312. The \fIname\fR should be one of the names accepted by the
  313. \fBencoding\fR command. This option is usually unnecessary; most
  314. database drivers can figure out the encoding in use by themselves.
  315. .IP "\fB\-isolation \fIlevel\fR"
  316. Specifies the transaction isolation level needed for transactions on
  317. the database. The acceptable values for \fIlevel\fR are shown under
  318. \fBTRANSACTION ISOLATION LEVELS\fR.
  319. .IP "\fB\-timeout \fIms\fR"
  320. Specifies the maximum time to wait for a an operation database engine before
  321. reporting an error to the caller. The \fIms\fR argument gives the
  322. maximum time in milliseconds. A value of zero (the default) specifies
  323. that the calling process is to wait indefinitely for database
  324. operations.
  325. .IP "\fB\-readonly \fIflag\fR"
  326. Specifies that the connection will not modify the database (if the
  327. Boolean parameter \fIflag\fR is true), or that it may modify the
  328. database (if \fIflag\fR is false). If \fIflag\fR is true, this option
  329. may have the effect of raising the transaction isolation level to
  330. \fIreadonly\fR.
  331. .SS "TRANSACTION ISOLATION LEVELS"
  332. The acceptable values for the \fB\-isolation\fR configuration option
  333. are as follows:
  334. .IP \fBreaduncommitted\fR
  335. Allows the transaction to read "dirty", that is, uncommitted
  336. data. This isolation level may compromise data integrity, does not
  337. guarantee that foreign keys or uniqueness constraints are satisfied,
  338. and in general does not guarantee data consistency.
  339. .IP \fBreadcommitted\fR
  340. Forbids the transaction from reading "dirty" data, but does not
  341. guarantee repeatable reads; if a transaction reads a row of a database
  342. at a given time, there is no guarantee that the same row will be
  343. available at a later time in the same transaction.
  344. .IP \fBrepeatableread\fR
  345. Guarantees that any row of the database, once read, will have the same
  346. values for the life of a transaction. Still permits "phantom reads"
  347. (that is, newly-added rows appearing if a table is queried a second
  348. time).
  349. .IP \fBserializable\fR
  350. The most restrictive (and most expensive) level of transaction isolation. Any query to the database, if repeated, will return precisely the same results for the life of the transaction, exactly as if the transaction is the only user of the database.
  351. .IP \fBreadonly\fR
  352. Behaves like \fBserializable\fR in that the only results visible to
  353. the transaction are those that were committed prior to the start of
  354. the transaction, but forbids the transaction from modifying the
  355. database.
  356. .PP
  357. A database that does not implement one of these isolation levels
  358. will instead use the next more restrictive isolation level. If the
  359. given level of isolation cannot be obtained, the database interface
  360. throws an error reporting the fact. The default isolation level
  361. is \fBreadcommitted\fR.
  362. .PP
  363. A script should not the isolation level when a transaction is in
  364. progress.
  365. .SH "SEE ALSO"
  366. encoding(n), tdbc(n), tdbc::resultset(n), tdbc::statement(n), tdbc::tokenize(n)
  367. .SH "KEYWORDS"
  368. TDBC, SQL, database, connectivity, connection, resultset, statement
  369. .SH "COPYRIGHT"
  370. Copyright (c) 2008 by Kevin B. Kenny.
  371. '\" Local Variables:
  372. '\" mode: nroff
  373. '\" End:
  374. '\"