tdbc_statement.n 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. '\"
  2. '\" tdbc_statement.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::statement" 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. '\" # CS - begin code excerpt
  47. .de CS
  48. .RS
  49. .nf
  50. .ta .25i .5i .75i 1i
  51. ..
  52. '\" # CE - end code excerpt
  53. .de CE
  54. .fi
  55. .RE
  56. ..
  57. '\" END IGNORE
  58. .BS
  59. .SH "NAME"
  60. tdbc::statement \- TDBC statement object
  61. .SH "SYNOPSIS"
  62. .nf
  63. package require \fBtdbc 1.0\fR
  64. package require \fBtdbc::\fR\fIdriver version\fR
  65. \fBtdbc::\fR\fIdriver\fR\fB::connection create \fR\fIdb\fR \fI?\-option value\fR...?
  66. \fBset\fR \fIstmt\fR \fB[\fR\fIdb\fR \fBprepare\fR \fIsql-code\fR\fB]\fR
  67. \fBset\fR \fIstmt\fR \fB[\fR\fIdb\fR \fBpreparecall\fR \fIcall\fR\fB]\fR
  68. \fI$stmt\fR \fBparams\fR
  69. \fI$stmt\fR \fBparamtype\fR ?\fIdirection\fR? \fItype\fR ?\fIprecision\fR? ?\fIscale\fR?
  70. \fI$stmt\fR \fBexecute\fR ?\fIdict\fR?
  71. \fI$stmt\fR \fBresultsets\fR
  72. .fi
  73. .ad l
  74. .in 14
  75. .ti 7
  76. \fI$stmt\fR \fBallrows\fR ?\fB-as lists|dicts\fR? ?\fB-columnsvariable\fR \fIname\fR? ?\fB--\fR? ?\fIdict\fR
  77. .br
  78. .ti 7
  79. \fI$stmt\fR \fBforeach\fR ?\fB-as lists|dicts\fR? ?\fB-columnsvariable\fR \fIname\fR? ?\fB--\fR? \fIvarName\fR ?\fIdict\fR? \fIscript\fR
  80. .br
  81. .ti 7
  82. \fI$stmt\fR \fBclose\fR
  83. .ad b
  84. .BE
  85. .SH "DESCRIPTION"
  86. .PP
  87. Every database driver for TDBC (Tcl DataBase Connectivity) implements
  88. a \fIstatement\fR object that represents a SQL statement in a
  89. database. Instances of this object are created by executing the
  90. \fBprepare\fR or \fBpreparecall\fR object command on a database
  91. connection.
  92. .PP
  93. The \fBprepare\fR object command against the connection
  94. accepts arbitrary SQL code to be
  95. executed against the database. The SQL code may contain \fIbound
  96. variables\fR, which are strings of alphanumeric characters or
  97. underscores (the first character of the string may not be numeric),
  98. prefixed with a colon (\fB:\fR). If a bound variable appears in the
  99. SQL statement, and is not in a string set off by single or double
  100. quotes, nor in a comment introduced by \fB--\fR, it becomes a value
  101. that is substituted when the statement is executed. A bound variable
  102. becomes a single value (string or numeric) in the resulting
  103. statement. \fIDrivers are responsible for ensuring that the mechanism
  104. for binding variables prevents SQL injection.\fR
  105. .PP
  106. The \fBpreparecall\fR object command against the connection accepts a
  107. stylized statement in the form:
  108. .PP
  109. .CS
  110. \fIprocname\fR \fB(\fR?\fB:\fR\fIvarname\fR? ?\fB,:\fR\fIvarname\fR...?\fB)\fR
  111. .CE
  112. .PP
  113. or
  114. .PP
  115. .CS
  116. \fIvarname\fR \fB=\fR \fIprocname\fR \fB(\fR?\fB:\fR\fIvarname\fR? ?\fB,:\fR\fIvarname\fR...?\fB)\fR
  117. .CE
  118. .PP
  119. This statement represents a call to a stored procedure \fIprocname\fR in the
  120. database. The variable name to the left of the equal sign (if
  121. present), and all variable names that are parameters inside
  122. parentheses, become bound variables.
  123. .PP
  124. The \fBparams\fR method against a statement object enumerates the
  125. bound variables that appear in the statement. The result returned from
  126. the \fBparams\fR method is a dictionary whose keys are the names of
  127. bound variables (listed in the order in which the variables first
  128. appear in the statement), and whose values are dictionaries. The
  129. subdictionaries include at least the following keys (database drivers
  130. may add additional keys that are not in this list).
  131. .IP \fBdirection\fR
  132. Contains one of the keywords, \fBin\fR, \fBout\fR or \fBinout\fR
  133. according to whether the variable is an input to or output from the
  134. statement. Only stored procedure calls will have \fBout\fR or
  135. \fBinout\fR parameters.
  136. .IP \fBtype\fR
  137. Contains the data type of the column, and will generally be chosen
  138. from the set,
  139. \fBbigint\fR, \fBbinary\fR, \fBbit\fR, \fBchar\fR, \fBdate\fR,
  140. \fBdecimal\fR, \fBdouble\fR, \fBfloat\fR, \fBinteger\fR,
  141. \fBlongvarbinary\fR, \fBlongvarchar\fR, \fBnumeric\fR, \fBreal\fR,
  142. \fBtime\fR, \fBtimestamp\fR, \fBsmallint\fR, \fBtinyint\fR,
  143. \fBvarbinary\fR, and \fBvarchar\fR. (If the variable has a type that
  144. cannot be represented as one of the above, \fBtype\fR will contain
  145. a driver-dependent description of the type.)
  146. .IP \fBprecision\fR
  147. Contains the precision of the column in bits, decimal digits, or the
  148. width in characters, according to the type.
  149. .IP \fBscale\fR
  150. Contains the scale of the column (the number of digits after the radix
  151. point), for types that support the concept.
  152. .IP \fBnullable\fR
  153. Contains 1 if the column can contain NULL values, and 0 otherwise.
  154. .PP
  155. The \fBparamtype\fR object command allows the script to specify the
  156. type and direction of parameter transmission of a variable in a
  157. statement. (Some databases provide no method to determine this
  158. information automatically and place the burden on the caller to do
  159. so.) The \fIdirection\fR, \fItype\fR, \fIprecision\fR, \fIscale\fR,
  160. and \fInullable\fR arguments have the same meaning as the
  161. corresponding dictionary values in the \fBparams\fR object command.
  162. .PP
  163. The \fBexecute\fR object command executes the statement. Prior to
  164. executing the statement, values are provided for the bound variables
  165. that appear in it. If the \fIdict\fR parameter is supplied, it is
  166. searched for a key whose name matches the name of the bound
  167. variable. If the key is present, its value becomes the substituted
  168. variable. If not, the value of the substituted variable becomes a SQL
  169. NULL. If the \fIdict\fR parameter is \fInot\fR supplied, the
  170. \fBexecute\fR object command searches for a variable in the caller's
  171. scope whose name matches the name of the bound variable. If one is
  172. found, its value becomes the bound variable's value. If none is found,
  173. the bound variable is assigned a SQL NULL as its value. Once
  174. substitution is finished, the resulting statement is executed. The
  175. return value is a result set object (see \fBtdbc::resultset\fR for
  176. details).
  177. .PP
  178. The \fBresultsets\fR method returns a list of all the result sets that
  179. have been returned by executing the statement and have not yet been
  180. closed.
  181. .PP
  182. The \fBallrows\fR object command executes the statement as with the
  183. \fBexecute\fR object command, accepting an
  184. optional \fIdict\fR parameter giving bind variables. After executing
  185. the statement,
  186. it uses the \fIallrows\fR object command on the result set (see
  187. \fBtdbc::resultset\fR) to construct a list of the results. Finally,
  188. the result set is closed. The return value is the list of
  189. results.
  190. .PP
  191. The \fBforeach\fR object command executes the statement as with the
  192. \fBexecute\fR object command, accepting an
  193. optional \fIdict\fR parameter giving bind variables. After executing
  194. the statement,
  195. it uses the \fIforeach\fR object command on the result set (see
  196. \fBtdbc::resultset\fR) to evaluate the given \fIscript\fR for each row of
  197. the results. Finally, the result set is closed, even
  198. if the given \fIscript\fR results in a \fBreturn\fR, an error, or
  199. an unusual return code.
  200. .PP
  201. The \fBclose\fR object command removes a statement and any result sets
  202. that it has created. All system resources associated with the objects
  203. are freed.
  204. .SH "EXAMPLES"
  205. The following code would look up a telephone number in a directory,
  206. assuming an appropriate SQL schema:
  207. .PP
  208. .CS
  209. package require tdbc::sqlite3
  210. tdbc::sqlite3::connection create db phonebook.sqlite3
  211. set statement [db prepare {
  212. select phone_num from directory
  213. where first_name = :firstname and last_name = :lastname
  214. }]
  215. set firstname Fred
  216. set lastname Flintstone
  217. $statement foreach row {
  218. puts [dict get $row phone_num]
  219. }
  220. $statement close
  221. db close
  222. .CE
  223. .SH "SEE ALSO"
  224. encoding(n), tdbc(n), tdbc::connection(n), tdbc::resultset(n), tdbc::tokenize(n)
  225. .SH "KEYWORDS"
  226. TDBC, SQL, database, connectivity, connection, resultset, statement,
  227. bound variable, stored procedure, call
  228. .SH "COPYRIGHT"
  229. Copyright (c) 2008 by Kevin B. Kenny.
  230. '\" Local Variables:
  231. '\" mode: nroff
  232. '\" End:
  233. '\"