tdbcpostgres.tcl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # tdbcpostgres.tcl --
  2. #
  3. # Class definitions and Tcl-level methods for the tdbc::postgres bridge.
  4. #
  5. # Copyright (c) 2009 by Slawomir Cygan
  6. #
  7. # See the file "license.terms" for information on usage and redistribution
  8. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9. #
  10. #------------------------------------------------------------------------------
  11. package require tdbc
  12. ::namespace eval ::tdbc::mypostgres {
  13. namespace export connection datasources drivers
  14. }
  15. #------------------------------------------------------------------------------
  16. #
  17. # tdbc::postgres::connection --
  18. #
  19. # Class representing a connection to a Postgres database.
  20. #
  21. #-------------------------------------------------------------------------------
  22. ::oo::class create ::tdbc::postgres::connection {
  23. superclass ::tdbc::connection
  24. # The constructor is written in C. It takes alternating keywords
  25. # and values pairs as its arguments. (See the manual page for the
  26. # available options.)
  27. # The 'statementCreate' method delegates to the constructor of the
  28. # statement class
  29. forward statementCreate ::tdbc::postgres::statement create
  30. # The 'prepareCall' method gives a portable interface to prepare
  31. # calls to stored procedures. It delegates to 'prepare' to do the
  32. # actual work.
  33. method preparecall {call} {
  34. regexp {^[[:space:]]*(?:([A-Za-z_][A-Za-z_0-9]*)[[:space:]]*=)?(.*)} \
  35. $call -> varName rest
  36. if {$varName eq {}} {
  37. my prepare \\{$rest\\}
  38. } else {
  39. my prepare \\{:$varName=$rest\\}
  40. }
  41. }
  42. # The 'init', 'begintransaction', 'commit, 'rollback', 'tables'
  43. # and 'columns' methods are implemented in C.
  44. }
  45. #------------------------------------------------------------------------------
  46. #
  47. # tdbc::postgres::statement --
  48. #
  49. # The class 'tdbc::postgres::statement' models one statement against a
  50. # database accessed through a Postgres connection
  51. #
  52. #------------------------------------------------------------------------------
  53. ::oo::class create ::tdbc::postgres::statement {
  54. superclass ::tdbc::statement
  55. # The 'resultSetCreate' method forwards to the constructor of the
  56. # result set.
  57. forward resultSetCreate ::tdbc::postgres::resultset create
  58. # Methods implemented in C:
  59. #
  60. # constructor connection SQLCode
  61. # The constructor accepts the handle to the connection and the SQL code
  62. # for the statement to prepare. It creates a subordinate namespace to
  63. # hold the statement's active result sets, and then delegates to the
  64. # 'init' method, written in C, to do the actual work of preparing the
  65. # statement.
  66. # params
  67. # Returns descriptions of the parameters of a statement.
  68. # paramtype paramname ?direction? type ?precision ?scale??
  69. # Declares the type of a parameter in the statement
  70. }
  71. #------------------------------------------------------------------------------
  72. #
  73. # tdbc::postgres::resultset --
  74. #
  75. # The class 'tdbc::postgres::resultset' models the result set that is
  76. # produced by executing a statement against a Postgres database.
  77. #
  78. #------------------------------------------------------------------------------
  79. ::oo::class create ::tdbc::postgres::resultset {
  80. superclass ::tdbc::resultset
  81. # The 'nextresults' method is stubbed out; tdbcpostgres does not
  82. # allow a single call to return multiple results.
  83. method nextresults {} {
  84. while {[my nextdict rubbish]} {}
  85. return 0
  86. }
  87. # Methods implemented in C include:
  88. # constructor statement ?dictionary?
  89. # -- Executes the statement against the database, optionally providing
  90. # a dictionary of substituted parameters (default is to get params
  91. # from variables in the caller's scope).
  92. # columns
  93. # -- Returns a list of the names of the columns in the result.
  94. # nextdict
  95. # -- Stores the next row of the result set in the given variable in
  96. # the caller's scope as a dictionary whose keys are
  97. # column names and whose values are column values, or else
  98. # as a list of cells.
  99. # nextlist
  100. # -- Stores the next row of the result set in the given variable in
  101. # the caller's scope as a list of cells.
  102. # rowcount
  103. # -- Returns a count of rows affected by the statement, or -1
  104. # if the count of rows has not been determined.
  105. }