database.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. require 'sqlite3/constants'
  2. require 'sqlite3/errors'
  3. require 'sqlite3/pragmas'
  4. require 'sqlite3/statement'
  5. require 'sqlite3/translator'
  6. require 'sqlite3/value'
  7. module SQLite3
  8. # The Database class encapsulates a single connection to a SQLite3 database.
  9. # Its usage is very straightforward:
  10. #
  11. # require 'sqlite3'
  12. #
  13. # SQLite3::Database.new( "data.db" ) do |db|
  14. # db.execute( "select * from table" ) do |row|
  15. # p row
  16. # end
  17. # end
  18. #
  19. # It wraps the lower-level methods provides by the selected driver, and
  20. # includes the Pragmas module for access to various pragma convenience
  21. # methods.
  22. #
  23. # The Database class provides type translation services as well, by which
  24. # the SQLite3 data types (which are all represented as strings) may be
  25. # converted into their corresponding types (as defined in the schemas
  26. # for their tables). This translation only occurs when querying data from
  27. # the database--insertions and updates are all still typeless.
  28. #
  29. # Furthermore, the Database class has been designed to work well with the
  30. # ArrayFields module from Ara Howard. If you require the ArrayFields
  31. # module before performing a query, and if you have not enabled results as
  32. # hashes, then the results will all be indexible by field name.
  33. class Database
  34. attr_reader :collations
  35. include Pragmas
  36. class << self
  37. alias :open :new
  38. # Quotes the given string, making it safe to use in an SQL statement.
  39. # It replaces all instances of the single-quote character with two
  40. # single-quote characters. The modified string is returned.
  41. def quote( string )
  42. string.gsub( /'/, "''" )
  43. end
  44. end
  45. # A boolean that indicates whether rows in result sets should be returned
  46. # as hashes or not. By default, rows are returned as arrays.
  47. attr_accessor :results_as_hash
  48. def type_translation= value # :nodoc:
  49. warn(<<-eowarn) if $VERBOSE
  50. #{caller[0]} is calling SQLite3::Database#type_translation=
  51. SQLite3::Database#type_translation= is deprecated and will be removed
  52. in version 2.0.0.
  53. eowarn
  54. @type_translation = value
  55. end
  56. attr_reader :type_translation # :nodoc:
  57. # Return the type translator employed by this database instance. Each
  58. # database instance has its own type translator; this allows for different
  59. # type handlers to be installed in each instance without affecting other
  60. # instances. Furthermore, the translators are instantiated lazily, so that
  61. # if a database does not use type translation, it will not be burdened by
  62. # the overhead of a useless type translator. (See the Translator class.)
  63. def translator
  64. @translator ||= Translator.new
  65. end
  66. # Installs (or removes) a block that will be invoked for every access
  67. # to the database. If the block returns 0 (or +nil+), the statement
  68. # is allowed to proceed. Returning 1 causes an authorization error to
  69. # occur, and returning 2 causes the access to be silently denied.
  70. def authorizer( &block )
  71. self.authorizer = block
  72. end
  73. # Returns a Statement object representing the given SQL. This does not
  74. # execute the statement; it merely prepares the statement for execution.
  75. #
  76. # The Statement can then be executed using Statement#execute.
  77. #
  78. def prepare sql
  79. stmt = SQLite3::Statement.new( self, sql )
  80. return stmt unless block_given?
  81. begin
  82. yield stmt
  83. ensure
  84. stmt.close
  85. end
  86. end
  87. # Executes the given SQL statement. If additional parameters are given,
  88. # they are treated as bind variables, and are bound to the placeholders in
  89. # the query.
  90. #
  91. # Note that if any of the values passed to this are hashes, then the
  92. # key/value pairs are each bound separately, with the key being used as
  93. # the name of the placeholder to bind the value to.
  94. #
  95. # The block is optional. If given, it will be invoked for each row returned
  96. # by the query. Otherwise, any results are accumulated into an array and
  97. # returned wholesale.
  98. #
  99. # See also #execute2, #query, and #execute_batch for additional ways of
  100. # executing statements.
  101. def execute sql, bind_vars = [], *args, &block
  102. # FIXME: This is a terrible hack and should be removed but is required
  103. # for older versions of rails
  104. hack = Object.const_defined?(:ActiveRecord) && sql =~ /^PRAGMA index_list/
  105. if bind_vars.nil? || !args.empty?
  106. if args.empty?
  107. bind_vars = []
  108. else
  109. bind_vars = [bind_vars] + args
  110. end
  111. warn(<<-eowarn) if $VERBOSE
  112. #{caller[0]} is calling SQLite3::Database#execute with nil or multiple bind params
  113. without using an array. Please switch to passing bind parameters as an array.
  114. Support for bind parameters as *args will be removed in 2.0.0.
  115. eowarn
  116. end
  117. prepare( sql ) do |stmt|
  118. stmt.bind_params(bind_vars)
  119. columns = stmt.columns
  120. stmt = ResultSet.new(self, stmt).to_a if type_translation
  121. if block_given?
  122. stmt.each do |row|
  123. if @results_as_hash
  124. yield type_translation ? row : ordered_map_for(columns, row)
  125. else
  126. yield row
  127. end
  128. end
  129. else
  130. if @results_as_hash
  131. stmt.map { |row|
  132. h = type_translation ? row : ordered_map_for(columns, row)
  133. # FIXME UGH TERRIBLE HACK!
  134. h['unique'] = h['unique'].to_s if hack
  135. h
  136. }
  137. else
  138. stmt.to_a
  139. end
  140. end
  141. end
  142. end
  143. # Executes the given SQL statement, exactly as with #execute. However, the
  144. # first row returned (either via the block, or in the returned array) is
  145. # always the names of the columns. Subsequent rows correspond to the data
  146. # from the result set.
  147. #
  148. # Thus, even if the query itself returns no rows, this method will always
  149. # return at least one row--the names of the columns.
  150. #
  151. # See also #execute, #query, and #execute_batch for additional ways of
  152. # executing statements.
  153. def execute2( sql, *bind_vars )
  154. prepare( sql ) do |stmt|
  155. result = stmt.execute( *bind_vars )
  156. if block_given?
  157. yield stmt.columns
  158. result.each { |row| yield row }
  159. else
  160. return result.inject( [ stmt.columns ] ) { |arr,row|
  161. arr << row; arr }
  162. end
  163. end
  164. end
  165. # Executes all SQL statements in the given string. By contrast, the other
  166. # means of executing queries will only execute the first statement in the
  167. # string, ignoring all subsequent statements. This will execute each one
  168. # in turn. The same bind parameters, if given, will be applied to each
  169. # statement.
  170. #
  171. # This always returns +nil+, making it unsuitable for queries that return
  172. # rows.
  173. def execute_batch( sql, bind_vars = [], *args )
  174. # FIXME: remove this stuff later
  175. unless [Array, Hash].include?(bind_vars.class)
  176. bind_vars = [bind_vars]
  177. warn(<<-eowarn) if $VERBOSE
  178. #{caller[0]} is calling SQLite3::Database#execute_batch with bind parameters
  179. that are not a list of a hash. Please switch to passing bind parameters as an
  180. array or hash. Support for this behavior will be removed in version 2.0.0.
  181. eowarn
  182. end
  183. # FIXME: remove this stuff later
  184. if bind_vars.nil? || !args.empty?
  185. if args.empty?
  186. bind_vars = []
  187. else
  188. bind_vars = [nil] + args
  189. end
  190. warn(<<-eowarn) if $VERBOSE
  191. #{caller[0]} is calling SQLite3::Database#execute_batch with nil or multiple bind params
  192. without using an array. Please switch to passing bind parameters as an array.
  193. Support for this behavior will be removed in version 2.0.0.
  194. eowarn
  195. end
  196. sql = sql.strip
  197. until sql.empty? do
  198. prepare( sql ) do |stmt|
  199. # FIXME: this should probably use sqlite3's api for batch execution
  200. # This implementation requires stepping over the results.
  201. if bind_vars.length == stmt.bind_parameter_count
  202. stmt.bind_params(bind_vars)
  203. end
  204. stmt.step
  205. sql = stmt.remainder.strip
  206. end
  207. end
  208. nil
  209. end
  210. # This is a convenience method for creating a statement, binding
  211. # paramters to it, and calling execute:
  212. #
  213. # result = db.query( "select * from foo where a=?", [5])
  214. # # is the same as
  215. # result = db.prepare( "select * from foo where a=?" ).execute( 5 )
  216. #
  217. # You must be sure to call +close+ on the ResultSet instance that is
  218. # returned, or you could have problems with locks on the table. If called
  219. # with a block, +close+ will be invoked implicitly when the block
  220. # terminates.
  221. def query( sql, bind_vars = [], *args )
  222. if bind_vars.nil? || !args.empty?
  223. if args.empty?
  224. bind_vars = []
  225. else
  226. bind_vars = [bind_vars] + args
  227. end
  228. warn(<<-eowarn) if $VERBOSE
  229. #{caller[0]} is calling SQLite3::Database#query with nil or multiple bind params
  230. without using an array. Please switch to passing bind parameters as an array.
  231. Support for this will be removed in version 2.0.0.
  232. eowarn
  233. end
  234. result = prepare( sql ).execute( bind_vars )
  235. if block_given?
  236. begin
  237. yield result
  238. ensure
  239. result.close
  240. end
  241. else
  242. return result
  243. end
  244. end
  245. # A convenience method for obtaining the first row of a result set, and
  246. # discarding all others. It is otherwise identical to #execute.
  247. #
  248. # See also #get_first_value.
  249. def get_first_row( sql, *bind_vars )
  250. execute( sql, *bind_vars ).first
  251. end
  252. # A convenience method for obtaining the first value of the first row of a
  253. # result set, and discarding all other values and rows. It is otherwise
  254. # identical to #execute.
  255. #
  256. # See also #get_first_row.
  257. def get_first_value( sql, *bind_vars )
  258. execute( sql, *bind_vars ) { |row| return row[0] }
  259. nil
  260. end
  261. alias :busy_timeout :busy_timeout=
  262. # Creates a new function for use in SQL statements. It will be added as
  263. # +name+, with the given +arity+. (For variable arity functions, use
  264. # -1 for the arity.)
  265. #
  266. # The block should accept at least one parameter--the FunctionProxy
  267. # instance that wraps this function invocation--and any other
  268. # arguments it needs (up to its arity).
  269. #
  270. # The block does not return a value directly. Instead, it will invoke
  271. # the FunctionProxy#result= method on the +func+ parameter and
  272. # indicate the return value that way.
  273. #
  274. # Example:
  275. #
  276. # db.create_function( "maim", 1 ) do |func, value|
  277. # if value.nil?
  278. # func.result = nil
  279. # else
  280. # func.result = value.split(//).sort.join
  281. # end
  282. # end
  283. #
  284. # puts db.get_first_value( "select maim(name) from table" )
  285. def create_function name, arity, text_rep=Constants::TextRep::ANY, &block
  286. define_function(name) do |*args|
  287. fp = FunctionProxy.new
  288. block.call(fp, *args)
  289. fp.result
  290. end
  291. self
  292. end
  293. # Creates a new aggregate function for use in SQL statements. Aggregate
  294. # functions are functions that apply over every row in the result set,
  295. # instead of over just a single row. (A very common aggregate function
  296. # is the "count" function, for determining the number of rows that match
  297. # a query.)
  298. #
  299. # The new function will be added as +name+, with the given +arity+. (For
  300. # variable arity functions, use -1 for the arity.)
  301. #
  302. # The +step+ parameter must be a proc object that accepts as its first
  303. # parameter a FunctionProxy instance (representing the function
  304. # invocation), with any subsequent parameters (up to the function's arity).
  305. # The +step+ callback will be invoked once for each row of the result set.
  306. #
  307. # The +finalize+ parameter must be a +proc+ object that accepts only a
  308. # single parameter, the FunctionProxy instance representing the current
  309. # function invocation. It should invoke FunctionProxy#result= to
  310. # store the result of the function.
  311. #
  312. # Example:
  313. #
  314. # db.create_aggregate( "lengths", 1 ) do
  315. # step do |func, value|
  316. # func[ :total ] ||= 0
  317. # func[ :total ] += ( value ? value.length : 0 )
  318. # end
  319. #
  320. # finalize do |func|
  321. # func.result = func[ :total ] || 0
  322. # end
  323. # end
  324. #
  325. # puts db.get_first_value( "select lengths(name) from table" )
  326. #
  327. # See also #create_aggregate_handler for a more object-oriented approach to
  328. # aggregate functions.
  329. def create_aggregate( name, arity, step=nil, finalize=nil,
  330. text_rep=Constants::TextRep::ANY, &block )
  331. factory = Class.new do
  332. def self.step( &block )
  333. define_method(:step, &block)
  334. end
  335. def self.finalize( &block )
  336. define_method(:finalize, &block)
  337. end
  338. end
  339. if block_given?
  340. factory.instance_eval(&block)
  341. else
  342. factory.class_eval do
  343. define_method(:step, step)
  344. define_method(:finalize, finalize)
  345. end
  346. end
  347. proxy = factory.new
  348. proxy.extend(Module.new {
  349. attr_accessor :ctx
  350. def step( *args )
  351. super(@ctx, *args)
  352. end
  353. def finalize
  354. super(@ctx)
  355. end
  356. })
  357. proxy.ctx = FunctionProxy.new
  358. define_aggregator(name, proxy)
  359. end
  360. # This is another approach to creating an aggregate function (see
  361. # #create_aggregate). Instead of explicitly specifying the name,
  362. # callbacks, arity, and type, you specify a factory object
  363. # (the "handler") that knows how to obtain all of that information. The
  364. # handler should respond to the following messages:
  365. #
  366. # +arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
  367. # message is optional, and if the handler does not respond to it,
  368. # the function will have an arity of -1.
  369. # +name+:: this is the name of the function. The handler _must_ implement
  370. # this message.
  371. # +new+:: this must be implemented by the handler. It should return a new
  372. # instance of the object that will handle a specific invocation of
  373. # the function.
  374. #
  375. # The handler instance (the object returned by the +new+ message, described
  376. # above), must respond to the following messages:
  377. #
  378. # +step+:: this is the method that will be called for each step of the
  379. # aggregate function's evaluation. It should implement the same
  380. # signature as the +step+ callback for #create_aggregate.
  381. # +finalize+:: this is the method that will be called to finalize the
  382. # aggregate function's evaluation. It should implement the
  383. # same signature as the +finalize+ callback for
  384. # #create_aggregate.
  385. #
  386. # Example:
  387. #
  388. # class LengthsAggregateHandler
  389. # def self.arity; 1; end
  390. #
  391. # def initialize
  392. # @total = 0
  393. # end
  394. #
  395. # def step( ctx, name )
  396. # @total += ( name ? name.length : 0 )
  397. # end
  398. #
  399. # def finalize( ctx )
  400. # ctx.result = @total
  401. # end
  402. # end
  403. #
  404. # db.create_aggregate_handler( LengthsAggregateHandler )
  405. # puts db.get_first_value( "select lengths(name) from A" )
  406. def create_aggregate_handler( handler )
  407. proxy = Class.new do
  408. def initialize handler
  409. @handler = handler
  410. @fp = FunctionProxy.new
  411. end
  412. def step( *args )
  413. @handler.step(@fp, *args)
  414. end
  415. def finalize
  416. @handler.finalize @fp
  417. @fp.result
  418. end
  419. end
  420. define_aggregator(handler.name, proxy.new(handler.new))
  421. self
  422. end
  423. # Begins a new transaction. Note that nested transactions are not allowed
  424. # by SQLite, so attempting to nest a transaction will result in a runtime
  425. # exception.
  426. #
  427. # The +mode+ parameter may be either <tt>:deferred</tt> (the default),
  428. # <tt>:immediate</tt>, or <tt>:exclusive</tt>.
  429. #
  430. # If a block is given, the database instance is yielded to it, and the
  431. # transaction is committed when the block terminates. If the block
  432. # raises an exception, a rollback will be performed instead. Note that if
  433. # a block is given, #commit and #rollback should never be called
  434. # explicitly or you'll get an error when the block terminates.
  435. #
  436. # If a block is not given, it is the caller's responsibility to end the
  437. # transaction explicitly, either by calling #commit, or by calling
  438. # #rollback.
  439. def transaction( mode = :deferred )
  440. execute "begin #{mode.to_s} transaction"
  441. if block_given?
  442. abort = false
  443. begin
  444. yield self
  445. rescue ::Object
  446. abort = true
  447. raise
  448. ensure
  449. abort and rollback or commit
  450. end
  451. end
  452. true
  453. end
  454. # Commits the current transaction. If there is no current transaction,
  455. # this will cause an error to be raised. This returns +true+, in order
  456. # to allow it to be used in idioms like
  457. # <tt>abort? and rollback or commit</tt>.
  458. def commit
  459. execute "commit transaction"
  460. true
  461. end
  462. # Rolls the current transaction back. If there is no current transaction,
  463. # this will cause an error to be raised. This returns +true+, in order
  464. # to allow it to be used in idioms like
  465. # <tt>abort? and rollback or commit</tt>.
  466. def rollback
  467. execute "rollback transaction"
  468. true
  469. end
  470. # Returns +true+ if the database has been open in readonly mode
  471. # A helper to check before performing any operation
  472. def readonly?
  473. @readonly
  474. end
  475. # A helper class for dealing with custom functions (see #create_function,
  476. # #create_aggregate, and #create_aggregate_handler). It encapsulates the
  477. # opaque function object that represents the current invocation. It also
  478. # provides more convenient access to the API functions that operate on
  479. # the function object.
  480. #
  481. # This class will almost _always_ be instantiated indirectly, by working
  482. # with the create methods mentioned above.
  483. class FunctionProxy
  484. attr_accessor :result
  485. # Create a new FunctionProxy that encapsulates the given +func+ object.
  486. # If context is non-nil, the functions context will be set to that. If
  487. # it is non-nil, it must quack like a Hash. If it is nil, then none of
  488. # the context functions will be available.
  489. def initialize
  490. @result = nil
  491. @context = {}
  492. end
  493. # Set the result of the function to the given error message.
  494. # The function will then return that error.
  495. def set_error( error )
  496. @driver.result_error( @func, error.to_s, -1 )
  497. end
  498. # (Only available to aggregate functions.) Returns the number of rows
  499. # that the aggregate has processed so far. This will include the current
  500. # row, and so will always return at least 1.
  501. def count
  502. @driver.aggregate_count( @func )
  503. end
  504. # Returns the value with the given key from the context. This is only
  505. # available to aggregate functions.
  506. def []( key )
  507. @context[ key ]
  508. end
  509. # Sets the value with the given key in the context. This is only
  510. # available to aggregate functions.
  511. def []=( key, value )
  512. @context[ key ] = value
  513. end
  514. end
  515. private
  516. def ordered_map_for columns, row
  517. h = Hash[*columns.zip(row).flatten]
  518. row.each_with_index { |r, i| h[i] = r }
  519. h
  520. end
  521. end
  522. end