tdbc_resultset.n 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. '\"
  2. '\" tdbc_resultset.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. '\"
  9. .TH "tdbc::resultset" n 8.6 Tcl "Tcl Database Connectivity"
  10. '\" .so man.macros
  11. '\" IGNORE
  12. .if t .wh -1.3i ^B
  13. .nr ^l \n(.l
  14. .ad b
  15. '\" # BS - start boxed text
  16. '\" # ^y = starting y location
  17. '\" # ^b = 1
  18. .de BS
  19. .br
  20. .mk ^y
  21. .nr ^b 1u
  22. .if n .nf
  23. .if n .ti 0
  24. .if n \l'\\n(.lu\(ul'
  25. .if n .fi
  26. ..
  27. '\" # BE - end boxed text (draw box now)
  28. .de BE
  29. .nf
  30. .ti 0
  31. .mk ^t
  32. .ie n \l'\\n(^lu\(ul'
  33. .el \{\
  34. '\" Draw four-sided box normally, but don't draw top of
  35. '\" box if the box started on an earlier page.
  36. .ie !\\n(^b-1 \{\
  37. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  38. .\}
  39. .el \}\
  40. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  41. .\}
  42. .\}
  43. .fi
  44. .br
  45. .nr ^b 0
  46. ..
  47. '\" # CS - begin code excerpt
  48. .de CS
  49. .RS
  50. .nf
  51. .ta .25i .5i .75i 1i
  52. ..
  53. '\" # CE - end code excerpt
  54. .de CE
  55. .fi
  56. .RE
  57. ..
  58. '\" END IGNORE
  59. .BS
  60. .SH "NAME"
  61. tdbc::resultset \- TDBC result set object
  62. .SH "SYNOPSIS"
  63. .nf
  64. package require \fBtdbc 1.0\fR
  65. package require \fBtdbc::\fR\fIdriver version\fR
  66. \fBtdbc::\fR\fIdriver\fR\fB::connection create \fR\fIdb\fR \fI?\-option value\fR...?
  67. \fBset\fR \fIstmt\fR \fB[\fR\fIdb\fR \fBprepare\fR \fIsql-code\fR\fB]\fR
  68. \fBset\fR \fIresultset\fR \fB[\fR\fI$stmt\fR \fBexecute\fR ?\fIargs...\fR?\fB]\fR
  69. \fI$resultset\fR \fBcolumns\fR
  70. \fI$resultset\fR \fBrowcount\fR
  71. \fI$resultset\fR \fBnextrow\fR ?\fB-as\fR \fBlists\fR|\fBdicts\fR? ?\fB--\fR? \fIvarname\fR
  72. \fI$resultset\fR \fBnextlist\fR \fIvarname\fR
  73. \fI$resultset\fR \fBnextdict\fR \fIvarname\fR
  74. \fI$resultset\fR \fBnextresults\fR
  75. .fi
  76. .ad l
  77. .in 14
  78. .ti 7
  79. \fI$resultset\fR \fBallrows\fR ?\fB-as lists|dicts\fR? ?\fB-columnsvariable\fR \fIname\fR? ?\fB--\fR?
  80. .br
  81. .ti 7
  82. \fI$resultset\fR \fBforeach\fR ?\fB-as lists|dicts\fR? ?\fB-columnsvariable\fR \fIname\fR? ?\fB--\fR? \fIvarname\fR \fIscript\fR
  83. .br
  84. .ti 7
  85. \fI$resultset\fR \fBclose\fR
  86. .ad b
  87. .BE
  88. .SH "DESCRIPTION"
  89. .PP
  90. Every database driver for TDBC (Tcl DataBase Connectivity) implements
  91. a \fIresult set\fR object that represents a the results returned from
  92. executing SQL statement in a database. Instances of this object are created
  93. by executing the \fBexecute\fR object command on a statement object.
  94. .PP
  95. The \fBcolumns\fR object command returns a list of the names of the columns
  96. in the result set. The columns will appear in the same order as they appeared
  97. in the SQL statement that performed the database query. If the SQL statement
  98. does not return a set of columns (for instance, if it is an INSERT,
  99. UPDATE, or DELETE statement), the \fBcolumns\fR command will return an empty
  100. list.
  101. .PP
  102. The \fBrowcount\fR object command returns the number of rows in the database
  103. that were affected by the execution of an INSERT, UPDATE or DELETE statement.
  104. For a SELECT statement, the row count is unspecified.
  105. .PP
  106. The \fBnextlist\fR object command sets the variable given by \fIvarname\fR
  107. in the caller's scope to the next row of the results, expressed as a list
  108. of column values. NULL values are replaced by empty strings. The
  109. columns of the result row appear in the same order in which they
  110. appeared on the SELECT statement. The
  111. return of \fBnextlist\fR is \fB1\fR if the operation succeeded, and
  112. \fB0\fR if the end of the result set was reached.
  113. .PP
  114. The \fBnextdict\fR object command sets the variable given by \fIvarname\fR
  115. in the caller's scope to the next row of the results, expressed as a
  116. dictionary. The dictionary's keys are column names, and the values are
  117. the values of those columns in the row. If a column's value in the row
  118. is NULL, its key is omitted from the dictionary.
  119. The keys appear in the dictionary in the same order in which the
  120. columns appeared on the SELECT statement. The
  121. return of \fBnextdict\fR is \fB1\fR if the operation succeeded, and
  122. \fB0\fR if the end of the result set was reached.
  123. .PP
  124. The \fBnextrow\fR object command is precisely equivalent to the
  125. \fBnextdict\fR or \fBnextlist\fR object command, depending on whether
  126. \fB-as dicts\fR (the default) or \fB-as lists\fR is specified.
  127. .PP
  128. Some databases support the idea of a single statement that returns multiple
  129. sets of results. The \fBnextresults\fR object command is executed, typically
  130. after the \fBnextlist\fR of \fBnextdict\fR object command has returned
  131. \fB0\fR, to advance to the next result set. It returns \fB1\fR if there
  132. is another result set to process, and \fB0\fR if the result set just
  133. processed was the last. After calling \fBnextresults\fR and getting
  134. the return value of \fB1\fR, the caller may once again call \fBcolumns\fR
  135. to get the column descriptions of the next result set, and then return to
  136. calling \fBnextdict\fR or \fBnextlist\fR to process the rows of the
  137. next result set. It is an error to call \fBcolumns\fR, \fBnextdict\fR,
  138. \fBnextlist\fR or \fBnextrow\fR after \fBnextresults\fR has returned \fB0\fR.
  139. .PP
  140. The \fBallrows\fR object command sets the variable designated by the
  141. \fB-columnsvariable\fR option (if present) to the result of the \fBcolumns\fR
  142. object command. It then executes the \fBnextrow\fR object command
  143. repeatedly until the end of the result set is reached. If \fBnextresults\fR
  144. returns a nonzero value, it executes the above two steps (\fBcolumns\fR
  145. followed by iterated \fBnextrow\fR calls) as long as further results are
  146. available. The rows returned by \fBnextrow\fR
  147. are assembled into a Tcl list and become the return value of the
  148. \fBallrows\fR command; the last value returned from \fBcolumns\fR is what
  149. the application will see in \fB-columnsvariable\fR.
  150. .PP
  151. The \fBforeach\fR object command sets the variable designated by the
  152. \fB-columnsvariable\fR option (if present) to the result of the \fBcolumns\fR
  153. object command. It then executes the \fBnextrow\fR object command
  154. repeatedly until the end of the result set is reached, storing the
  155. successive rows in the variable designated by \fIvarName\fR. For each
  156. row, it executes the given \fIscript\fR. If the script terminates with
  157. an error, the error is reported by the \fBforeach\fR command, and
  158. iteration stops. If the script performs a \fBbreak\fR operation, the
  159. iteration terminates prematurely. If the script performs a
  160. \fBcontinue\fR operation, the iteration recommences with the next row.
  161. If the script performs a \fBreturn\fR, results are the same as if a
  162. script outside the control of \fBforeach\fR had returned. Any other
  163. unusual return code terminates the iteration and is reported from the
  164. \fBforeach\fR.
  165. .PP
  166. Once \fBnextrow\fR returns \fB0\fR, the \fBforeach\fR object command
  167. tries to advance to the next result set using \fBnextresults\fR. If
  168. \fBnextresults\fR returns \fB1\fR, the above steps (\fBcolumns\fR and
  169. \fBnextrow\fR, with script invocation) are repeated as long as more
  170. result sets remain. The \fIscript\fR will always see the correct description
  171. of the columns of the current result set in the variable designated
  172. byt \fB-columnsvariable\fR. At the end of the call, the variable
  173. designated by \fB-columnsvariable\fR will have the description of the
  174. columns of the last result set.
  175. .PP
  176. The \fBclose\fR object command deletes the result set and frees any
  177. associated system resources.
  178. .SH "SEE ALSO"
  179. encoding(n), tdbc(n), tdbc::connection(n), tdbc::statement(n), tdbc::tokenize(n)
  180. .SH "KEYWORDS"
  181. TDBC, SQL, database, connectivity, connection, resultset, statement,
  182. bound variable, stored procedure, call
  183. .SH "COPYRIGHT"
  184. Copyright (c) 2008 by Kevin B. Kenny.
  185. '\" Local Variables:
  186. '\" mode: nroff
  187. '\" End:
  188. '\"