statement.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. require 'sqlite3/errors'
  2. require 'sqlite3/resultset'
  3. class String
  4. def to_blob
  5. SQLite3::Blob.new( self )
  6. end
  7. end
  8. module SQLite3
  9. # A statement represents a prepared-but-unexecuted SQL query. It will rarely
  10. # (if ever) be instantiated directly by a client, and is most often obtained
  11. # via the Database#prepare method.
  12. class Statement
  13. include Enumerable
  14. # This is any text that followed the first valid SQL statement in the text
  15. # with which the statement was initialized. If there was no trailing text,
  16. # this will be the empty string.
  17. attr_reader :remainder
  18. # Binds the given variables to the corresponding placeholders in the SQL
  19. # text.
  20. #
  21. # See Database#execute for a description of the valid placeholder
  22. # syntaxes.
  23. #
  24. # Example:
  25. #
  26. # stmt = db.prepare( "select * from table where a=? and b=?" )
  27. # stmt.bind_params( 15, "hello" )
  28. #
  29. # See also #execute, #bind_param, Statement#bind_param, and
  30. # Statement#bind_params.
  31. def bind_params( *bind_vars )
  32. index = 1
  33. bind_vars.flatten.each do |var|
  34. if Hash === var
  35. var.each { |key, val| bind_param key, val }
  36. else
  37. bind_param index, var
  38. index += 1
  39. end
  40. end
  41. end
  42. # Execute the statement. This creates a new ResultSet object for the
  43. # statement's virtual machine. If a block was given, the new ResultSet will
  44. # be yielded to it; otherwise, the ResultSet will be returned.
  45. #
  46. # Any parameters will be bound to the statement using #bind_params.
  47. #
  48. # Example:
  49. #
  50. # stmt = db.prepare( "select * from table" )
  51. # stmt.execute do |result|
  52. # ...
  53. # end
  54. #
  55. # See also #bind_params, #execute!.
  56. def execute( *bind_vars )
  57. reset! if active? || done?
  58. bind_params(*bind_vars) unless bind_vars.empty?
  59. @results = ResultSet.new(@connection, self)
  60. step if 0 == column_count
  61. yield @results if block_given?
  62. @results
  63. end
  64. # Execute the statement. If no block was given, this returns an array of
  65. # rows returned by executing the statement. Otherwise, each row will be
  66. # yielded to the block.
  67. #
  68. # Any parameters will be bound to the statement using #bind_params.
  69. #
  70. # Example:
  71. #
  72. # stmt = db.prepare( "select * from table" )
  73. # stmt.execute! do |row|
  74. # ...
  75. # end
  76. #
  77. # See also #bind_params, #execute.
  78. def execute!( *bind_vars, &block )
  79. execute(*bind_vars)
  80. block_given? ? each(&block) : to_a
  81. end
  82. # Returns true if the statement is currently active, meaning it has an
  83. # open result set.
  84. def active?
  85. !done?
  86. end
  87. # Return an array of the column names for this statement. Note that this
  88. # may execute the statement in order to obtain the metadata; this makes it
  89. # a (potentially) expensive operation.
  90. def columns
  91. get_metadata unless @columns
  92. return @columns
  93. end
  94. def each
  95. loop do
  96. val = step
  97. break self if done?
  98. yield val
  99. end
  100. end
  101. # Return an array of the data types for each column in this statement. Note
  102. # that this may execute the statement in order to obtain the metadata; this
  103. # makes it a (potentially) expensive operation.
  104. def types
  105. must_be_open!
  106. get_metadata unless @types
  107. @types
  108. end
  109. # Performs a sanity check to ensure that the statement is not
  110. # closed. If it is, an exception is raised.
  111. def must_be_open! # :nodoc:
  112. if closed?
  113. raise SQLite3::Exception, "cannot use a closed statement"
  114. end
  115. end
  116. private
  117. # A convenience method for obtaining the metadata about the query. Note
  118. # that this will actually execute the SQL, which means it can be a
  119. # (potentially) expensive operation.
  120. def get_metadata
  121. @columns = Array.new(column_count) do |column|
  122. column_name column
  123. end
  124. @types = Array.new(column_count) do |column|
  125. column_decltype column
  126. end
  127. end
  128. end
  129. end