Permalink
Browse files

adding sqlite to framework, updating gitignore

  • Loading branch information...
1 parent f265b86 commit bebf1207426fff7a116acefd01916ce2cbef2d96 @larsburgess larsburgess committed Mar 1, 2009
Showing with 2,484 additions and 351 deletions.
  1. +62 −0 .gitignore
  2. +13 −12 rhodes/rhodes-framework/Manifest.txt
  3. +0 −8 rhodes/rhodes-framework/bin/rhogen
  4. +0 −136 rhodes/rhodes-framework/generators/rhogen.rb
  5. +0 −4 rhodes/rhodes-framework/generators/templates/application/application.rb
  6. +0 −11 rhodes/rhodes-framework/generators/templates/application/index.erb
  7. +0 −17 rhodes/rhodes-framework/generators/templates/application/layout.erb
  8. +0 −3 rhodes/rhodes-framework/generators/templates/model/config.rb
  9. +0 −49 rhodes/rhodes-framework/generators/templates/model/controller.rb
  10. +0 −21 rhodes/rhodes-framework/generators/templates/model/edit.erb
  11. +0 −10 rhodes/rhodes-framework/generators/templates/model/index.erb
  12. +0 −16 rhodes/rhodes-framework/generators/templates/model/new.erb
  13. +0 −6 rhodes/rhodes-framework/generators/templates/model/show.erb
  14. +0 −56 rhodes/rhodes-framework/generators/templates/source/source_adapter.rb
  15. +49 −0 rhodes/rhodes-framework/res/sqlite3/constants.rb
  16. +715 −0 rhodes/rhodes-framework/res/sqlite3/database.rb
  17. +154 −0 rhodes/rhodes-framework/res/sqlite3/driver/dl/api.rb
  18. +307 −0 rhodes/rhodes-framework/res/sqlite3/driver/dl/driver.rb
  19. +257 −0 rhodes/rhodes-framework/res/sqlite3/driver/native/driver.rb
  20. +68 −0 rhodes/rhodes-framework/res/sqlite3/errors.rb
  21. +271 −0 rhodes/rhodes-framework/res/sqlite3/pragmas.rb
  22. +176 −0 rhodes/rhodes-framework/res/sqlite3/resultset.rb
  23. 0 rhodes/rhodes-framework/res/sqlite3/sqlite3_api.rb
  24. +230 −0 rhodes/rhodes-framework/res/sqlite3/statement.rb
  25. +109 −0 rhodes/rhodes-framework/res/sqlite3/translator.rb
  26. +57 −0 rhodes/rhodes-framework/res/sqlite3/value.rb
  27. +14 −0 rhodes/rhodes-framework/res/sqlite3/version.rb
  28. +2 −2 rhodes/rhodes-generator/lib/version.rb
View
62 .gitignore
@@ -0,0 +1,62 @@
+*~
+.DS_Store
+shared/framework/coverage
+shared/framework/pkg
+iphone/build/*
+iphone/rhorubylib/build/*
+iphone/rhosynclib/build/*
+iphone/bin/*
+mac/bin/*
+iphone/*/*.link
+wm/bin/*
+win32/bin/*
+wm/target/*
+wm/build/cleanup.js
+iphone/*/rholog.txt
+
+bb/.metadata/*
+bb/bin/*
+bb/target/*
+bb/build/LogFile.txt
+bb/*/.settings
+bb/*/.project
+bb/*/.classpath
+*.cso
+*.csl
+*.err
+bb/.BB.jdw/*.files
+*.jad
+bb/build/rholog.txt
+
+symbian/.metadata/*
+symbian/bin/*
+symbian/target/*
+symbian/*/.project
+abld.bat
+ABLD.BAT
+#*.sis
+#*.sisx
+.cproject
+#*.hlp
+*.hrh
+
+#*.jar
+*.cod
+*.debug
+*.rapc
+*.log
+*.obj
+*.old
+*.suo
+*.user
+*.pdb
+*.idb
+*.lib
+
+*.pch
+*.mode1v3
+*.pbxuser
+*.CAB
+*.cab
+*.ncb
+*.perspectivev3
View
25 rhodes/rhodes-framework/Manifest.txt
@@ -3,18 +3,6 @@ History.txt
Manifest.txt
README.rdoc
Rakefile
-bin/rhogen
-generators/rhogen.rb
-generators/templates/application/application.rb
-generators/templates/application/index.erb
-generators/templates/application/layout.erb
-generators/templates/model/config.rb
-generators/templates/model/controller.rb
-generators/templates/model/edit.erb
-generators/templates/model/index.erb
-generators/templates/model/new.erb
-generators/templates/model/show.erb
-generators/templates/source/source_adapter.rb
lib/ServeME.rb
lib/TestServe.rb
lib/bsearch.rb
@@ -49,6 +37,19 @@ lib/rhom/rhom_source.rb
lib/singleton.rb
lib/time.rb
lib/version.rb
+res/sqlite3/constants.rb
+res/sqlite3/database.rb
+res/sqlite3/driver/dl/api.rb
+res/sqlite3/driver/dl/driver.rb
+res/sqlite3/driver/native/driver.rb
+res/sqlite3/errors.rb
+res/sqlite3/pragmas.rb
+res/sqlite3/resultset.rb
+res/sqlite3/sqlite3_api.rb
+res/sqlite3/statement.rb
+res/sqlite3/translator.rb
+res/sqlite3/value.rb
+res/sqlite3/version.rb
rhodes-framework.gemspec
rhodes.gemspec
spec/app_generator_spec.rb
View
8 rhodes/rhodes-framework/bin/rhogen
@@ -1,8 +0,0 @@
-#!/usr/bin/env ruby
-
-require 'rubygems'
-require 'rhodes'
-require 'templater'
-require File.join(File.dirname(__FILE__), '..', 'generators', 'rhogen')
-
-Rhogen.run_cli(Dir.pwd, 'rhogen', Rhodes::VERSION, ARGV)
View
136 rhodes/rhodes-framework/generators/rhogen.rb
@@ -1,136 +0,0 @@
-require 'rubygems'
-require 'templater'
-
-module Rhogen
- extend Templater::Manifold
-
- desc <<-DESC
- Generate rhodes application
- DESC
-
- class BaseGenerator < Templater::Generator
- def class_name
- name.gsub('-', '_').camel_case
- end
-
- alias_method :module_name, :class_name
- end
-
- class AppGenerator < BaseGenerator
-
- def self.source_root
- File.join(File.dirname(__FILE__), 'templates', 'application')
- end
-
- desc <<-DESC
- Generates a new rhodes application. This will create a new directory with two files:
- application.rb and index.html
- DESC
-
- #option :testing_framework, :desc => 'Specify which testing framework to use (spec, test_unit)'
-
- first_argument :name, :required => true, :desc => "application name"
-
- template :application do |template|
- template.source = 'application.rb'
- template.destination = "#{name.camel_case}/application.rb"
- end
-
- template :index do |template|
- template.source = 'index.erb'
- template.destination = "#{name.camel_case}/index.erb"
- end
-
- template :layout do |template|
- template.source = 'layout.erb'
- template.destination = "#{name.camel_case}/layout.erb"
- end
-
- end
-
- class ModelGenerator < BaseGenerator
-
- def self.source_root
- File.join(File.dirname(__FILE__), 'templates', 'model')
- end
-
- desc <<-DESC
- Generates a new model for a given source. You must specify name, source_url, and source_id.
- You can also specify an optional attribute list in the form: 'attribute1', 'attribute2', 'attribute3'...
- DESC
-
- #option :testing_framework, :desc => 'Specify which testing framework to use (spec, test_unit)'
-
- first_argument :name, :required => true, :desc => "model name"
- second_argument :source_url, :required => true, :desc => "source url"
- third_argument :source_id, :required => true, :desc => "source id"
- fourth_argument :attributes, :as => :array, :default => [], :required => false, :desc => "array of attributes (only string suppported right now)"
-
- template :config do |template|
- template.source = 'config.rb'
- template.destination = "#{name.camel_case}/config.rb"
- end
-
- template :index do |template|
- template.source = 'index.erb'
- template.destination = "#{name.camel_case}/index.erb"
- end
-
- template :edit do |template|
- template.source = 'edit.erb'
- template.destination = "#{name.camel_case}/edit.erb"
- end
-
- template :new do |template|
- template.source = 'new.erb'
- template.destination = "#{name.camel_case}/new.erb"
- end
-
- template :new do |template|
- template.source = 'show.erb'
- template.destination = "#{name.camel_case}/show.erb"
- end
-
- template :controller do |template|
- template.source = 'controller.rb'
- template.destination = "#{name.camel_case}/controller.rb"
- end
-
- def attributes?
- self.attributes && !self.attributes.empty?
- end
-
- # taken from activesuppport
- def humanize(lower_case_and_underscored_word)
- result = lower_case_and_underscored_word.to_s.dup
- inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
- result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
- end
-
- end
-
- class SourceGenerator < BaseGenerator
- def self.source_root
- File.join(File.dirname(__FILE__), 'templates', 'source')
- end
-
- desc <<-DESC
- Generates a new source adapter with the given name and attributes. You must specify name.
- You can also specify an optional attribute list in the form: 'attribute1', 'attribute2', 'attribute3'...
- DESC
-
- first_argument :name, :required => true, :desc => "model name"
- second_argument :attributes, :as => :array, :default => [], :required => false, :desc => "array of attributes (only string suppported right now)"
-
- template :config do |template|
- template.source = 'source_adapter.rb'
- template.destination = "lib/#{name.snake_case}.rb"
- end
-
- end
-
- add :app, AppGenerator
- add :model, ModelGenerator
- add :source, SourceGenerator
-
-end
View
4 rhodes/rhodes-framework/generators/templates/application/application.rb
@@ -1,4 +0,0 @@
-require 'rho/rhoapplication'
-
-class <%= class_name %>Application < Rho::RhoApplication
-end
View
11 rhodes/rhodes-framework/generators/templates/application/index.erb
@@ -1,11 +0,0 @@
-<div class="toolbar">
- <h1 id="pageTitle">
- <script language="JavaScript">
- document.write(document.title)
- </script>
- </h1>
- <a id="backButton" class="button" href="./"></a>
-</div>
-<ul id="home" selected="true" title="<%= class_name %>">
- <li>Something interesting here...</li>
-</ul>
View
17 rhodes/rhodes-framework/generators/templates/application/layout.erb
@@ -1,17 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title><%= class_name %></title>
- <%% if System::get_property('platform') == 'APPLE' %>
- <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
- <style type="text/css" media="screen">@import "/shared/js/iui/iui.css"; @import "/shared/css/rho.css";</style>
- <script type="application/x-javascript" src="/shared/js/iui/iui.js"></script>
- <script src="/shared/js/jquery-1.2.6.min.js"></script>
- <script src="/shared/js/rho.js"></script>
- <%% end %>
- </head>
- <body>
- <%%= @content %>
- </body>
-</html>
View
3 rhodes/rhodes-framework/generators/templates/model/config.rb
@@ -1,3 +0,0 @@
-require 'rho'
-
-Rho::RhoConfig::add_source("<%= class_name %>", {"url"=>"<%= source_url %>", "source_id"=><%= source_id %>})
View
49 rhodes/rhodes-framework/generators/templates/model/controller.rb
@@ -1,49 +0,0 @@
-require 'rho/rhocontroller'
-
-class <%= class_name %>Controller < Rho::RhoController
-
- #GET /<%= class_name %>
- def index
- @<%= name.pluralize %> = <%= class_name %>.find(:all)
- render
- end
-
- # GET /<%= class_name %>/1
- def show
- @<%= name %> = <%= class_name %>.find(@params['id'])
- render :action => :show
- end
-
- # GET /<%= class_name %>/new
- def new
- @<%= name %> = <%= class_name %>.new
- render :action => :new
- end
-
- # GET /<%= class_name %>/1/edit
- def edit
- @<%= name %> = <%= class_name %>.find(@params['id'])
- render :action => :edit
- end
-
- # POST /<%= class_name %>/create
- def create
- @<%= name %> = <%= class_name %>.new(@params['<%= name %>'])
- @<%= name %>.save
- redirect :action => :index
- end
-
- # POST /<%= class_name %>/1/update
- def update
- @<%= name %> = <%= class_name %>.find(@params['id'])
- @<%= name %>.update_attributes(@params['<%= name %>'])
- redirect :action => :index
- end
-
- # POST /<%= class_name %>/1/delete
- def delete
- @<%= name %> = <%= class_name %>.find(@params['id'])
- @<%= name %>.destroy
- redirect :action => :index
- end
-end
View
21 rhodes/rhodes-framework/generators/templates/model/edit.erb
@@ -1,21 +0,0 @@
-<form title="Edit <%= class_name %>"
- class="panel"
- id="<%= name %>_edit_form"
- method="POST"
- action="<%%=url_for(:action => :update)%>">
- <fieldset>
- <input type="hidden" name="id" value="<%%=@<%= name %>.object%>"/>
- <% attributes.each do |attribute| %>
- <div class="row">
- <label><%=humanize(attribute)%>: </label>
- <input type="text" name="<%= name %>[<%= attribute %>]" value="<%%=@<%=name%>.<%=attribute%>%>"/>
- </div>
- <% end %>
- </fieldset>
- <input type="submit" value="Update"/>
- <p align="center">
- <a href="<%%=url_for(:action => :delete, :id => @<%= name %>.object)%>">
- <font color="#990000">Delete</font>
- </a>
- </p>
-</form>
View
10 rhodes/rhodes-framework/generators/templates/model/index.erb
@@ -1,10 +0,0 @@
-<ul id="<%= name.pluralize %>" title="<%= class_name.pluralize %>">
-<%%@<%=name.pluralize%>.each do |x|%>
-<%if self.attributes?%>
-<li><%%=link_to "#{x.<%=attributes[0]%>}", :action => :show, :id => x.object%></li>
-<%else%>
-<li>Some list entry...</li>
-<%end%>
-<%%end%>
-<li><font color="blue"><%%=link_to "New <%= class_name %>", :action => :new%></font></li>
-</ul>
View
16 rhodes/rhodes-framework/generators/templates/model/new.erb
@@ -1,16 +0,0 @@
-<form title="New <%= class_name %>"
- class="panel"
- id="<%= name %>_new_form"
- method="POST"
- action="<%%=url_for(:action => :create)%>">
- <fieldset>
- <input type="hidden" name="id" value="<%%=@<%= name %>.object%>"/>
- <% attributes.each do |attribute| %>
- <div class="row">
- <label><%=humanize(attribute)%>: </label>
- <input type="text" name="<%= name %>[<%= attribute %>]"/>
- </div>
- <% end %>
- </fieldset>
- <input type="submit" value="Create"/>
-</form>
View
6 rhodes/rhodes-framework/generators/templates/model/show.erb
@@ -1,6 +0,0 @@
-<ul id="<%= name %>" title="Show <%= class_name %>">
- <% attributes.each do |attribute| %>
- <li><%%=@<%=name%>.<%=attribute%>%></li>
- <% end %>
- <li><%%=link_to "Edit #{@<%=name%>.<%=attributes[0]%>}", :action => :edit, :id => @<%=name%>.object%></li>
-</ul>
View
56 rhodes/rhodes-framework/generators/templates/source/source_adapter.rb
@@ -1,56 +0,0 @@
-class <%=name%> < SourceAdapter
-
- def initialize(source)
- super(source)
- end
-
- def login
- #TODO: Write some code here
- # use the variable @source.login and @source.password
- raise "Please provide some code to perform an authenticated login to the backend application"
- end
-
- def query
- # TODO: write some code here
- raise "Please provide some code to read records from the backend application"
- end
-
- def sync
- # usually this generic code does the job
- @result.entry_list.each do |x|
- x.name_value_list.each do |y|
- o=ObjectValue.new
- o.source_id=@source.id
- o.object=x['id']
- o.attrib=y.name
- o.value=y.value
- o.user_id=user_id if user_id
- o.save
- end
- end
- end
-
- def create(name_value_list)
- #TODO: write some code here
- # the backend application will provide the object hash key and corresponding value
- raise "Please provide some code to create a single object in the backend application using the hash values in name_value_list"
- end
-
- def update(name_value_list)
- #TODO: write some code here
- # be sure to have a hash key and value for "object"
- raise "Please provide some code to update a single object in the backend application using the hash values in name_value_list"
- end
-
- def delete(name_value_list)
- #TODO: write some code here if applicable
- # be sure to have a hash key and value for "object"
- # for now, we'll say that its OK to not have a delete operation
- # raise "Please provide some code to delete a single object in the backend application using the hash values in name_value_list"
- end
-
- def logoff
- #TODO: write some code here if applicable
- # no need to do a raise here
- end
-end
View
49 rhodes/rhodes-framework/res/sqlite3/constants.rb
@@ -0,0 +1,49 @@
+module SQLite3 ; module Constants
+
+ module TextRep
+ UTF8 = 1
+ UTF16LE = 2
+ UTF16BE = 3
+ UTF16 = 4
+ ANY = 5
+ end
+
+ module ColumnType
+ INTEGER = 1
+ FLOAT = 2
+ TEXT = 3
+ BLOB = 4
+ NULL = 5
+ end
+
+ module ErrorCode
+ OK = 0 # Successful result
+ ERROR = 1 # SQL error or missing database
+ INTERNAL = 2 # An internal logic error in SQLite
+ PERM = 3 # Access permission denied
+ ABORT = 4 # Callback routine requested an abort
+ BUSY = 5 # The database file is locked
+ LOCKED = 6 # A table in the database is locked
+ NOMEM = 7 # A malloc() failed
+ READONLY = 8 # Attempt to write a readonly database
+ INTERRUPT = 9 # Operation terminated by sqlite_interrupt()
+ IOERR = 10 # Some kind of disk I/O error occurred
+ CORRUPT = 11 # The database disk image is malformed
+ NOTFOUND = 12 # (Internal Only) Table or record not found
+ FULL = 13 # Insertion failed because database is full
+ CANTOPEN = 14 # Unable to open the database file
+ PROTOCOL = 15 # Database lock protocol error
+ EMPTY = 16 # (Internal Only) Database table is empty
+ SCHEMA = 17 # The database schema changed
+ TOOBIG = 18 # Too much data for one row of a table
+ CONSTRAINT = 19 # Abort due to contraint violation
+ MISMATCH = 20 # Data type mismatch
+ MISUSE = 21 # Library used incorrectly
+ NOLFS = 22 # Uses OS features not supported on host
+ AUTH = 23 # Authorization denied
+
+ ROW = 100 # sqlite_step() has another row ready
+ DONE = 101 # sqlite_step() has finished executing
+ end
+
+end ; end
View
715 rhodes/rhodes-framework/res/sqlite3/database.rb
@@ -0,0 +1,715 @@
+require 'sqlite3/constants'
+require 'sqlite3/errors'
+require 'sqlite3/pragmas'
+require 'sqlite3/statement'
+require 'sqlite3/translator'
+require 'sqlite3/value'
+
+module SQLite3
+
+ # The Database class encapsulates a single connection to a SQLite3 database.
+ # Its usage is very straightforward:
+ #
+ # require 'sqlite3'
+ #
+ # db = SQLite3::Database.new( "data.db" )
+ #
+ # db.execute( "select * from table" ) do |row|
+ # p row
+ # end
+ #
+ # db.close
+ #
+ # It wraps the lower-level methods provides by the selected driver, and
+ # includes the Pragmas module for access to various pragma convenience
+ # methods.
+ #
+ # The Database class provides type translation services as well, by which
+ # the SQLite3 data types (which are all represented as strings) may be
+ # converted into their corresponding types (as defined in the schemas
+ # for their tables). This translation only occurs when querying data from
+ # the database--insertions and updates are all still typeless.
+ #
+ # Furthermore, the Database class has been designed to work well with the
+ # ArrayFields module from Ara Howard. If you require the ArrayFields
+ # module before performing a query, and if you have not enabled results as
+ # hashes, then the results will all be indexible by field name.
+ class Database
+ include Pragmas
+
+ class <<self
+
+ alias :open :new
+
+ # Quotes the given string, making it safe to use in an SQL statement.
+ # It replaces all instances of the single-quote character with two
+ # single-quote characters. The modified string is returned.
+ def quote( string )
+ string.gsub( /'/, "''" )
+ end
+
+ end
+
+ # The low-level opaque database handle that this object wraps.
+ attr_reader :handle
+
+ # A reference to the underlying SQLite3 driver used by this database.
+ attr_reader :driver
+
+ # A boolean that indicates whether rows in result sets should be returned
+ # as hashes or not. By default, rows are returned as arrays.
+ attr_accessor :results_as_hash
+
+ # A boolean indicating whether or not type translation is enabled for this
+ # database.
+ attr_accessor :type_translation
+
+ # Create a new Database object that opens the given file. If utf16
+ # is +true+, the filename is interpreted as a UTF-16 encoded string.
+ #
+ # By default, the new database will return result rows as arrays
+ # (#results_as_hash) and has type translation disabled (#type_translation=).
+ def initialize( file_name, options={} )
+ utf16 = options.fetch(:utf16, false)
+ load_driver( options[:driver] )
+
+ @statement_factory = options[:statement_factory] || Statement
+
+ result, @handle = @driver.open( file_name, utf16 )
+ Error.check( result, self, "could not open database" )
+
+ @closed = false
+ @results_as_hash = options.fetch(:results_as_hash,false)
+ @type_translation = options.fetch(:type_translation,false)
+ @translator = nil
+ @transaction_active = false
+ end
+
+ # Return +true+ if the string is a valid (ie, parsable) SQL statement, and
+ # +false+ otherwise. If +utf16+ is +true+, then the string is a UTF-16
+ # character string.
+ def complete?( string, utf16=false )
+ @driver.complete?( string, utf16 )
+ end
+
+ # Return a string describing the last error to have occurred with this
+ # database.
+ def errmsg( utf16=false )
+ @driver.errmsg( @handle, utf16 )
+ end
+
+ # Return an integer representing the last error to have occurred with this
+ # database.
+ def errcode
+ @driver.errcode( @handle )
+ end
+
+ # Return the type translator employed by this database instance. Each
+ # database instance has its own type translator; this allows for different
+ # type handlers to be installed in each instance without affecting other
+ # instances. Furthermore, the translators are instantiated lazily, so that
+ # if a database does not use type translation, it will not be burdened by
+ # the overhead of a useless type translator. (See the Translator class.)
+ def translator
+ @translator ||= Translator.new
+ end
+
+ # Closes this database.
+ def close
+ unless @closed
+ result = @driver.close( @handle )
+ Error.check( result, self )
+ end
+ @closed = true
+ end
+
+ # Returns +true+ if this database instance has been closed (see #close).
+ def closed?
+ @closed
+ end
+
+ # Installs (or removes) a block that will be invoked for every SQL
+ # statement executed. The block receives a two parameters: the +data+
+ # argument, and the SQL statement executed. If the block is +nil+,
+ # any existing tracer will be uninstalled.
+ def trace( data=nil, &block )
+ @driver.trace( @handle, data, &block )
+ end
+
+ # Installs (or removes) a block that will be invoked for every access
+ # to the database. If the block returns 0 (or +nil+), the statement
+ # is allowed to proceed. Returning 1 causes an authorization error to
+ # occur, and returning 2 causes the access to be silently denied.
+ def authorizer( data=nil, &block )
+ result = @driver.set_authorizer( @handle, data, &block )
+ Error.check( result, self )
+ end
+
+ # Returns a Statement object representing the given SQL. This does not
+ # execute the statement; it merely prepares the statement for execution.
+ #
+ # The Statement can then be executed using Statement#execute.
+ #
+ def prepare( sql )
+ stmt = @statement_factory.new( self, sql )
+ if block_given?
+ begin
+ yield stmt
+ ensure
+ stmt.close
+ end
+ else
+ return stmt
+ end
+ end
+
+ # Executes the given SQL statement. If additional parameters are given,
+ # they are treated as bind variables, and are bound to the placeholders in
+ # the query.
+ #
+ # Note that if any of the values passed to this are hashes, then the
+ # key/value pairs are each bound separately, with the key being used as
+ # the name of the placeholder to bind the value to.
+ #
+ # The block is optional. If given, it will be invoked for each row returned
+ # by the query. Otherwise, any results are accumulated into an array and
+ # returned wholesale.
+ #
+ # See also #execute2, #query, and #execute_batch for additional ways of
+ # executing statements.
+ def execute( sql, *bind_vars )
+ prepare( sql ) do |stmt|
+ result = stmt.execute( *bind_vars )
+ if block_given?
+ result.each { |row| yield row }
+ else
+ return result.inject( [] ) { |arr,row| arr << row; arr }
+ end
+ end
+ end
+
+ # Executes the given SQL statement, exactly as with #execute. However, the
+ # first row returned (either via the block, or in the returned array) is
+ # always the names of the columns. Subsequent rows correspond to the data
+ # from the result set.
+ #
+ # Thus, even if the query itself returns no rows, this method will always
+ # return at least one row--the names of the columns.
+ #
+ # See also #execute, #query, and #execute_batch for additional ways of
+ # executing statements.
+ def execute2( sql, *bind_vars )
+ prepare( sql ) do |stmt|
+ result = stmt.execute( *bind_vars )
+ if block_given?
+ yield result.columns
+ result.each { |row| yield row }
+ else
+ return result.inject( [ result.columns ] ) { |arr,row|
+ arr << row; arr }
+ end
+ end
+ end
+
+ # Executes all SQL statements in the given string. By contrast, the other
+ # means of executing queries will only execute the first statement in the
+ # string, ignoring all subsequent statements. This will execute each one
+ # in turn. The same bind parameters, if given, will be applied to each
+ # statement.
+ #
+ # This always returns +nil+, making it unsuitable for queries that return
+ # rows.
+ def execute_batch( sql, *bind_vars )
+ sql = sql.strip
+ until sql.empty? do
+ prepare( sql ) do |stmt|
+ stmt.execute( *bind_vars )
+ sql = stmt.remainder.strip
+ end
+ end
+ nil
+ end
+
+ # This is a convenience method for creating a statement, binding
+ # paramters to it, and calling execute:
+ #
+ # result = db.query( "select * from foo where a=?", 5 )
+ # # is the same as
+ # result = db.prepare( "select * from foo where a=?" ).execute( 5 )
+ #
+ # You must be sure to call +close+ on the ResultSet instance that is
+ # returned, or you could have problems with locks on the table. If called
+ # with a block, +close+ will be invoked implicitly when the block
+ # terminates.
+ def query( sql, *bind_vars )
+ result = prepare( sql ).execute( *bind_vars )
+ if block_given?
+ begin
+ yield result
+ ensure
+ result.close
+ end
+ else
+ return result
+ end
+ end
+
+ # A convenience method for obtaining the first row of a result set, and
+ # discarding all others. It is otherwise identical to #execute.
+ #
+ # See also #get_first_value.
+ def get_first_row( sql, *bind_vars )
+ execute( sql, *bind_vars ) { |row| return row }
+ nil
+ end
+
+ # A convenience method for obtaining the first value of the first row of a
+ # result set, and discarding all other values and rows. It is otherwise
+ # identical to #execute.
+ #
+ # See also #get_first_row.
+ def get_first_value( sql, *bind_vars )
+ execute( sql, *bind_vars ) { |row| return row[0] }
+ nil
+ end
+
+ # Obtains the unique row ID of the last row to be inserted by this Database
+ # instance.
+ def last_insert_row_id
+ @driver.last_insert_rowid( @handle )
+ end
+
+ # Returns the number of changes made to this database instance by the last
+ # operation performed. Note that a "delete from table" without a where
+ # clause will not affect this value.
+ def changes
+ @driver.changes( @handle )
+ end
+
+ # Returns the total number of changes made to this database instance
+ # since it was opened.
+ def total_changes
+ @driver.total_changes( @handle )
+ end
+
+ # Interrupts the currently executing operation, causing it to abort.
+ def interrupt
+ @driver.interrupt( @handle )
+ end
+
+ # Register a busy handler with this database instance. When a requested
+ # resource is busy, this handler will be invoked. If the handler returns
+ # +false+, the operation will be aborted; otherwise, the resource will
+ # be requested again.
+ #
+ # The handler will be invoked with the name of the resource that was
+ # busy, and the number of times it has been retried.
+ #
+ # See also the mutually exclusive #busy_timeout.
+ def busy_handler( data=nil, &block ) # :yields: data, retries
+ result = @driver.busy_handler( @handle, data, &block )
+ Error.check( result, self )
+ end
+
+ # Indicates that if a request for a resource terminates because that
+ # resource is busy, SQLite should sleep and retry for up to the indicated
+ # number of milliseconds. By default, SQLite does not retry
+ # busy resources. To restore the default behavior, send 0 as the
+ # +ms+ parameter.
+ #
+ # See also the mutually exclusive #busy_handler.
+ def busy_timeout( ms )
+ result = @driver.busy_timeout( @handle, ms )
+ Error.check( result, self )
+ end
+
+ # Creates a new function for use in SQL statements. It will be added as
+ # +name+, with the given +arity+. (For variable arity functions, use
+ # -1 for the arity.)
+ #
+ # The block should accept at least one parameter--the FunctionProxy
+ # instance that wraps this function invocation--and any other
+ # arguments it needs (up to its arity).
+ #
+ # The block does not return a value directly. Instead, it will invoke
+ # the FunctionProxy#set_result method on the +func+ parameter and
+ # indicate the return value that way.
+ #
+ # Example:
+ #
+ # db.create_function( "maim", 1 ) do |func, value|
+ # if value.nil?
+ # func.result = nil
+ # else
+ # func.result = value.split(//).sort.join
+ # end
+ # end
+ #
+ # puts db.get_first_value( "select maim(name) from table" )
+ def create_function( name, arity, text_rep=Constants::TextRep::ANY,
+ &block ) # :yields: func, *args
+ # begin
+ callback = proc do |func,*args|
+ begin
+ block.call( FunctionProxy.new( @driver, func ),
+ *args.map{|v| Value.new(self,v)} )
+ rescue StandardError, Exception => e
+ @driver.result_error( func,
+ "#{e.message} (#{e.class})", -1 )
+ end
+ end
+
+ result = @driver.create_function( @handle, name, arity, text_rep, nil,
+ callback, nil, nil )
+ Error.check( result, self )
+
+ self
+ end
+
+ # Creates a new aggregate function for use in SQL statements. Aggregate
+ # functions are functions that apply over every row in the result set,
+ # instead of over just a single row. (A very common aggregate function
+ # is the "count" function, for determining the number of rows that match
+ # a query.)
+ #
+ # The new function will be added as +name+, with the given +arity+. (For
+ # variable arity functions, use -1 for the arity.)
+ #
+ # The +step+ parameter must be a proc object that accepts as its first
+ # parameter a FunctionProxy instance (representing the function
+ # invocation), with any subsequent parameters (up to the function's arity).
+ # The +step+ callback will be invoked once for each row of the result set.
+ #
+ # The +finalize+ parameter must be a +proc+ object that accepts only a
+ # single parameter, the FunctionProxy instance representing the current
+ # function invocation. It should invoke FunctionProxy#set_result to
+ # store the result of the function.
+ #
+ # Example:
+ #
+ # db.create_aggregate( "lengths", 1 ) do
+ # step do |func, value|
+ # func[ :total ] ||= 0
+ # func[ :total ] += ( value ? value.length : 0 )
+ # end
+ #
+ # finalize do |func|
+ # func.set_result( func[ :total ] || 0 )
+ # end
+ # end
+ #
+ # puts db.get_first_value( "select lengths(name) from table" )
+ #
+ # See also #create_aggregate_handler for a more object-oriented approach to
+ # aggregate functions.
+ def create_aggregate( name, arity, step=nil, finalize=nil,
+ text_rep=Constants::TextRep::ANY, &block )
+ # begin
+ if block
+ proxy = AggregateDefinitionProxy.new
+ proxy.instance_eval(&block)
+ step ||= proxy.step_callback
+ finalize ||= proxy.finalize_callback
+ end
+
+ step_callback = proc do |func,*args|
+ ctx = @driver.aggregate_context( func )
+ unless ctx[:__error]
+ begin
+ step.call( FunctionProxy.new( @driver, func, ctx ),
+ *args.map{|v| Value.new(self,v)} )
+ rescue Exception => e
+ ctx[:__error] = e
+ end
+ end
+ end
+
+ finalize_callback = proc do |func|
+ ctx = @driver.aggregate_context( func )
+ unless ctx[:__error]
+ begin
+ finalize.call( FunctionProxy.new( @driver, func, ctx ) )
+ rescue Exception => e
+ @driver.result_error( func,
+ "#{e.message} (#{e.class})", -1 )
+ end
+ else
+ e = ctx[:__error]
+ @driver.result_error( func,
+ "#{e.message} (#{e.class})", -1 )
+ end
+ end
+
+ result = @driver.create_function( @handle, name, arity, text_rep, nil,
+ nil, step_callback, finalize_callback )
+ Error.check( result, self )
+
+ self
+ end
+
+ # This is another approach to creating an aggregate function (see
+ # #create_aggregate). Instead of explicitly specifying the name,
+ # callbacks, arity, and type, you specify a factory object
+ # (the "handler") that knows how to obtain all of that information. The
+ # handler should respond to the following messages:
+ #
+ # +arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
+ # message is optional, and if the handler does not respond to it,
+ # the function will have an arity of -1.
+ # +name+:: this is the name of the function. The handler _must_ implement
+ # this message.
+ # +new+:: this must be implemented by the handler. It should return a new
+ # instance of the object that will handle a specific invocation of
+ # the function.
+ #
+ # The handler instance (the object returned by the +new+ message, described
+ # above), must respond to the following messages:
+ #
+ # +step+:: this is the method that will be called for each step of the
+ # aggregate function's evaluation. It should implement the same
+ # signature as the +step+ callback for #create_aggregate.
+ # +finalize+:: this is the method that will be called to finalize the
+ # aggregate function's evaluation. It should implement the
+ # same signature as the +finalize+ callback for
+ # #create_aggregate.
+ #
+ # Example:
+ #
+ # class LengthsAggregateHandler
+ # def self.arity; 1; end
+ #
+ # def initialize
+ # @total = 0
+ # end
+ #
+ # def step( ctx, name )
+ # @total += ( name ? name.length : 0 )
+ # end
+ #
+ # def finalize( ctx )
+ # ctx.set_result( @total )
+ # end
+ # end
+ #
+ # db.create_aggregate_handler( LengthsAggregateHandler )
+ # puts db.get_first_value( "select lengths(name) from A" )
+ def create_aggregate_handler( handler )
+ arity = -1
+ text_rep = Constants::TextRep::ANY
+
+ arity = handler.arity if handler.respond_to?(:arity)
+ text_rep = handler.text_rep if handler.respond_to?(:text_rep)
+ name = handler.name
+
+ step = proc do |func,*args|
+ ctx = @driver.aggregate_context( func )
+ unless ctx[ :__error ]
+ ctx[ :handler ] ||= handler.new
+ begin
+ ctx[ :handler ].step( FunctionProxy.new( @driver, func, ctx ),
+ *args.map{|v| Value.new(self,v)} )
+ rescue Exception, StandardError => e
+ ctx[ :__error ] = e
+ end
+ end
+ end
+
+ finalize = proc do |func|
+ ctx = @driver.aggregate_context( func )
+ unless ctx[ :__error ]
+ ctx[ :handler ] ||= handler.new
+ begin
+ ctx[ :handler ].finalize( FunctionProxy.new( @driver, func, ctx ) )
+ rescue Exception => e
+ ctx[ :__error ] = e
+ end
+ end
+
+ if ctx[ :__error ]
+ e = ctx[ :__error ]
+ @driver.sqlite3_result_error( func, "#{e.message} (#{e.class})", -1 )
+ end
+ end
+
+ result = @driver.create_function( @handle, name, arity, text_rep, nil,
+ nil, step, finalize )
+ Error.check( result, self )
+
+ self
+ end
+
+ # Begins a new transaction. Note that nested transactions are not allowed
+ # by SQLite, so attempting to nest a transaction will result in a runtime
+ # exception.
+ #
+ # The +mode+ parameter may be either <tt>:deferred</tt> (the default),
+ # <tt>:immediate</tt>, or <tt>:exclusive</tt>.
+ #
+ # If a block is given, the database instance is yielded to it, and the
+ # transaction is committed when the block terminates. If the block
+ # raises an exception, a rollback will be performed instead. Note that if
+ # a block is given, #commit and #rollback should never be called
+ # explicitly or you'll get an error when the block terminates.
+ #
+ # If a block is not given, it is the caller's responsibility to end the
+ # transaction explicitly, either by calling #commit, or by calling
+ # #rollback.
+ def transaction( mode = :deferred )
+ execute "begin #{mode.to_s} transaction"
+ @transaction_active = true
+
+ if block_given?
+ abort = false
+ begin
+ yield self
+ rescue ::Object
+ abort = true
+ raise
+ ensure
+ abort and rollback or commit
+ end
+ end
+
+ true
+ end
+
+ # Commits the current transaction. If there is no current transaction,
+ # this will cause an error to be raised. This returns +true+, in order
+ # to allow it to be used in idioms like
+ # <tt>abort? and rollback or commit</tt>.
+ def commit
+ execute "commit transaction"
+ @transaction_active = false
+ true
+ end
+
+ # Rolls the current transaction back. If there is no current transaction,
+ # this will cause an error to be raised. This returns +true+, in order
+ # to allow it to be used in idioms like
+ # <tt>abort? and rollback or commit</tt>.
+ def rollback
+ execute "rollback transaction"
+ @transaction_active = false
+ true
+ end
+
+ # Returns +true+ if there is a transaction active, and +false+ otherwise.
+ def transaction_active?
+ @transaction_active
+ end
+
+ # Loads the corresponding driver, or if it is nil, attempts to locate a
+ # suitable driver.
+ def load_driver( driver )
+ case driver
+ when Class
+ # do nothing--use what was given
+ when Symbol, String
+ require "sqlite3/driver/#{driver.to_s.downcase}/driver"
+ driver = SQLite3::Driver.const_get( driver )::Driver
+ else
+ [ "Native", "DL" ].each do |d|
+ begin
+ require "sqlite3/driver/#{d.downcase}/driver"
+ driver = SQLite3::Driver.const_get( d )::Driver
+ break
+ rescue SyntaxError
+ raise
+ rescue ScriptError, Exception, NameError
+ end
+ end
+ raise "no driver for sqlite3 found" unless driver
+ end
+
+ @driver = driver.new
+ end
+ private :load_driver
+
+ # A helper class for dealing with custom functions (see #create_function,
+ # #create_aggregate, and #create_aggregate_handler). It encapsulates the
+ # opaque function object that represents the current invocation. It also
+ # provides more convenient access to the API functions that operate on
+ # the function object.
+ #
+ # This class will almost _always_ be instantiated indirectly, by working
+ # with the create methods mentioned above.
+ class FunctionProxy
+
+ # Create a new FunctionProxy that encapsulates the given +func+ object.
+ # If context is non-nil, the functions context will be set to that. If
+ # it is non-nil, it must quack like a Hash. If it is nil, then none of
+ # the context functions will be available.
+ def initialize( driver, func, context=nil )
+ @driver = driver
+ @func = func
+ @context = context
+ end
+
+ # Calls #set_result to set the result of this function.
+ def result=( result )
+ set_result( result )
+ end
+
+ # Set the result of the function to the given value. The function will
+ # then return this value.
+ def set_result( result, utf16=false )
+ @driver.result_text( @func, result, utf16 )
+ end
+
+ # Set the result of the function to the given error message.
+ # The function will then return that error.
+ def set_error( error )
+ @driver.result_error( @func, error.to_s, -1 )
+ end
+
+ # (Only available to aggregate functions.) Returns the number of rows
+ # that the aggregate has processed so far. This will include the current
+ # row, and so will always return at least 1.
+ def count
+ ensure_aggregate!
+ @driver.aggregate_count( @func )
+ end
+
+ # Returns the value with the given key from the context. This is only
+ # available to aggregate functions.
+ def []( key )
+ ensure_aggregate!
+ @context[ key ]
+ end
+
+ # Sets the value with the given key in the context. This is only
+ # available to aggregate functions.
+ def []=( key, value )
+ ensure_aggregate!
+ @context[ key ] = value
+ end
+
+ # A function for performing a sanity check, to ensure that the function
+ # being invoked is an aggregate function. This is implied by the
+ # existence of the context variable.
+ def ensure_aggregate!
+ unless @context
+ raise MisuseException, "function is not an aggregate"
+ end
+ end
+ private :ensure_aggregate!
+
+ end
+
+ # A proxy used for defining the callbacks to an aggregate function.
+ class AggregateDefinitionProxy # :nodoc:
+ attr_reader :step_callback, :finalize_callback
+
+ def step( &block )
+ @step_callback = block
+ end
+
+ def finalize( &block )
+ @finalize_callback = block
+ end
+ end
+
+ end
+
+end
+
View
154 rhodes/rhodes-framework/res/sqlite3/driver/dl/api.rb
@@ -0,0 +1,154 @@
+require 'dl/import'
+
+module SQLite3 ; module Driver; module DL;
+
+ module API
+ extend ::DL::Importable
+
+ puts "RUBY_PLATFORM.downcase = " + RUBY_PLATFORM.downcase
+
+ library_name = case RUBY_PLATFORM.downcase
+ when /darwin/
+ "libsqlite3.dylib"
+ when /linux/, /freebsd|netbsd|openbsd|dragonfly/, /solaris/
+ "libsqlite3.so"
+ when /win32/
+ "sqlite3.dll"
+ else
+ abort <<-EOF
+== * UNSUPPORTED PLATFORM ======================================================
+The platform '#{RUBY_PLATFORM}' is unsupported. Please help the author by
+editing the following file to allow your sqlite3 library to be found, and
+submitting a patch to jamis_buck@byu.edu. Thanks!
+
+#{__FILE__}
+=========================================================================== * ==
+ EOF
+ end
+
+ if defined? SQLITE3_LIB_PATH
+ library_name = File.join( SQLITE3_LIB_PATH, library_name )
+ end
+
+ dlload library_name
+
+ typealias "db", "void*"
+ typealias "stmt", "void*"
+ typealias "value", "void*"
+ typealias "context", "void*"
+
+ # until Ruby/DL supports 64-bit ints, we'll just treat them as 32-bit ints
+ typealias "int64", "unsigned long"
+
+ extern "const char *sqlite3_libversion()"
+
+ extern "int sqlite3_open(const char*,db*)"
+ extern "int sqlite3_open16(const void*,db*)"
+ extern "int sqlite3_close(db)"
+ extern "const char* sqlite3_errmsg(db)"
+ extern "void* sqlite3_errmsg16(db)"
+ extern "int sqlite3_errcode(db)"
+
+ extern "int sqlite3_prepare(db,const char*,int,stmt*,const char**)"
+ extern "int sqlite3_prepare16(db,const void*,int,stmt*,const void**)"
+ extern "int sqlite3_finalize(stmt)"
+ extern "int sqlite3_reset(stmt)"
+ extern "int sqlite3_step(stmt)"
+
+ extern "int64 sqlite3_last_insert_rowid(db)"
+ extern "int sqlite3_changes(db)"
+ extern "int sqlite3_total_changes(db)"
+ extern "void sqlite3_interrupt(db)"
+ extern "ibool sqlite3_complete(const char*)"
+ extern "ibool sqlite3_complete16(const void*)"
+
+ extern "int sqlite3_busy_handler(db,void*,void*)"
+ extern "int sqlite3_busy_timeout(db,int)"
+
+ extern "int sqlite3_set_authorizer(db,void*,void*)"
+ extern "void* sqlite3_trace(db,void*,void*)"
+
+ extern "int sqlite3_bind_blob(stmt,int,const void*,int,void*)"
+ extern "int sqlite3_bind_double(stmt,int,double)"
+ extern "int sqlite3_bind_int(stmt,int,int)"
+ extern "int sqlite3_bind_int64(stmt,int,int64)"
+ extern "int sqlite3_bind_null(stmt,int)"
+ extern "int sqlite3_bind_text(stmt,int,const char*,int,void*)"
+ extern "int sqlite3_bind_text16(stmt,int,const void*,int,void*)"
+ #extern "int sqlite3_bind_value(stmt,int,value)"
+
+ extern "int sqlite3_bind_parameter_count(stmt)"
+ extern "const char* sqlite3_bind_parameter_name(stmt,int)"
+ extern "int sqlite3_bind_parameter_index(stmt,const char*)"
+
+ extern "int sqlite3_column_count(stmt)"
+ extern "int sqlite3_data_count(stmt)"
+
+ extern "const void *sqlite3_column_blob(stmt,int)"
+ extern "int sqlite3_column_bytes(stmt,int)"
+ extern "int sqlite3_column_bytes16(stmt,int)"
+ extern "const char *sqlite3_column_decltype(stmt,int)"
+ extern "void *sqlite3_column_decltype16(stmt,int)"
+ extern "double sqlite3_column_double(stmt,int)"
+ extern "int sqlite3_column_int(stmt,int)"
+ extern "int64 sqlite3_column_int64(stmt,int)"
+ extern "const char *sqlite3_column_name(stmt,int)"
+ extern "const void *sqlite3_column_name16(stmt,int)"
+ extern "const char *sqlite3_column_text(stmt,int)"
+ extern "const void *sqlite3_column_text16(stmt,int)"
+ extern "int sqlite3_column_type(stmt,int)"
+
+ extern "int sqlite3_create_function(db,const char*,int,int,void*,void*,void*,void*)"
+ extern "int sqlite3_create_function16(db,const void*,int,int,void*,void*,void*,void*)"
+ extern "int sqlite3_aggregate_count(context)"
+
+ extern "const void *sqlite3_value_blob(value)"
+ extern "int sqlite3_value_bytes(value)"
+ extern "int sqlite3_value_bytes16(value)"
+ extern "double sqlite3_value_double(value)"
+ extern "int sqlite3_value_int(value)"
+ extern "int64 sqlite3_value_int64(value)"
+ extern "const char* sqlite3_value_text(value)"
+ extern "const void* sqlite3_value_text16(value)"
+ extern "const void* sqlite3_value_text16le(value)"
+ extern "const void* sqlite3_value_text16be(value)"
+ extern "int sqlite3_value_type(value)"
+
+ extern "void *sqlite3_aggregate_context(context,int)"
+ extern "void *sqlite3_user_data(context)"
+ extern "void *sqlite3_get_auxdata(context,int)"
+ extern "void sqlite3_set_auxdata(context,int,void*,void*)"
+
+ extern "void sqlite3_result_blob(context,const void*,int,void*)"
+ extern "void sqlite3_result_double(context,double)"
+ extern "void sqlite3_result_error(context,const char*,int)"
+ extern "void sqlite3_result_error16(context,const void*,int)"
+ extern "void sqlite3_result_int(context,int)"
+ extern "void sqlite3_result_int64(context,int64)"
+ extern "void sqlite3_result_null(context)"
+ extern "void sqlite3_result_text(context,const char*,int,void*)"
+ extern "void sqlite3_result_text16(context,const void*,int,void*)"
+ extern "void sqlite3_result_text16le(context,const void*,int,void*)"
+ extern "void sqlite3_result_text16be(context,const void*,int,void*)"
+ extern "void sqlite3_result_value(context,value)"
+
+ extern "int sqlite3_create_collation(db,const char*,int,void*,void*)"
+ extern "int sqlite3_create_collation16(db,const char*,int,void*,void*)"
+ extern "int sqlite3_collation_needed(db,void*,void*)"
+ extern "int sqlite3_collation_needed16(db,void*,void*)"
+
+ # ==== CRYPTO (NOT IN PUBLIC RELEASE) ====
+ if defined?( CRYPTO_API ) && CRYPTO_API
+ extern "int sqlite3_key(db,void*,int)"
+ extern "int sqlite3_rekey(db,void*,int)"
+ end
+
+ # ==== EXPERIMENTAL ====
+ if defined?( EXPERIMENTAL_API ) && EXPERIMENTAL_API
+ extern "int sqlite3_progress_handler(db,int,void*,void*)"
+ extern "int sqlite3_commit_hook(db,void*,void*)"
+ end
+
+ end
+
+end ; end ; end
View
307 rhodes/rhodes-framework/res/sqlite3/driver/dl/driver.rb
@@ -0,0 +1,307 @@
+require 'sqlite3/driver/dl/api'
+
+warn "The DL driver for sqlite3-ruby is deprecated and will be removed"
+warn "in a future release. Please update your installation to use the"
+warn "Native driver."
+
+module Kernel
+ # Allows arbitrary objects to be passed as a pointer to functions.
+ # (Probably not very GC safe, but by encapsulating it like this we
+ # can change the implementation later.)
+ def to_ptr
+ ptr = DL.malloc(DL.sizeof("L"))
+ ptr.set_object self
+ ptr
+ end
+end
+
+class DL::PtrData
+ # The inverse of the Kernel#to_ptr operation.
+ def to_object
+ n = to_s(4).unpack("L").first
+ return nil if n < 1
+ ObjectSpace._id2ref(n) rescue self.to_s
+ end
+
+ def set_object(obj)
+ self[0] = [obj.object_id].pack("L")
+ end
+end
+
+module SQLite3 ; module Driver ; module DL
+
+ class Driver
+ STATIC = ::DL::PtrData.new(0)
+ TRANSIENT = ::DL::PtrData.new(-1)
+
+ def open( filename, utf16=false )
+ handle = ::DL::PtrData.new(0)
+ result = API.send( ( utf16 ? :sqlite3_open16 : :sqlite3_open ),
+ filename+"\0", handle.ref )
+ [ result, handle ]
+ end
+
+ def errmsg( db, utf16=false )
+ if utf16
+ msg = API.sqlite3_errmsg16( db )
+ msg.free = nil
+ msg.to_s(utf16_length(msg))
+ else
+ API.sqlite3_errmsg( db )
+ end
+ end
+
+ def prepare( db, sql, utf16=false )
+ handle = ::DL::PtrData.new(0)
+ remainder = ::DL::PtrData.new(0)
+
+ result = API.send( ( utf16 ? :sqlite3_prepare16 : :sqlite3_prepare ),
+ db, sql+"\0", sql.length, handle.ref, remainder.ref )
+
+ args = utf16 ? [ utf16_length(remainder) ] : []
+ remainder = remainder.to_s( *args )
+
+ [ result, handle, remainder ]
+ end
+
+ def complete?( sql, utf16=false )
+ API.send( utf16 ? :sqlite3_complete16 : :sqlite3_complete, sql+"\0" )
+ end
+
+ def value_blob( value )
+ blob = API.sqlite3_value_blob( value )
+ blob.free = nil
+ blob.to_s( API.sqlite3_value_bytes( value ) )
+ end
+
+ def value_text( value, utf16=false )
+ method = case utf16
+ when nil, false then :sqlite3_value_text
+ when :le then :sqlite3_value_text16le
+ when :be then :sqlite3_value_text16be
+ else :sqlite3_value_text16
+ end
+
+ result = API.send( method, value )
+ if utf16
+ result.free = nil
+ size = API.sqlite3_value_bytes( value )
+ result = result.to_s( size )
+ end
+
+ result
+ end
+
+ def column_blob( stmt, column )
+ blob = API.sqlite3_column_blob( stmt, column )
+ blob.free = nil
+ blob.to_s( API.sqlite3_column_bytes( stmt, column ) )
+ end
+
+ def result_text( func, text, utf16=false )
+ method = case utf16
+ when false, nil then :sqlite3_result_text
+ when :le then :sqlite3_result_text16le
+ when :be then :sqlite3_result_text16be
+ else :sqlite3_result_text16
+ end
+
+ s = text.to_s
+ API.send( method, func, s, s.length, TRANSIENT )
+ end
+
+ def busy_handler( db, data=nil, &block )
+ @busy_handler = block
+
+ unless @busy_handler_callback
+ @busy_handler_callback = ::DL.callback( "IPI" ) do |cookie, timeout|
+ @busy_handler.call( cookie, timeout ) || 0
+ end
+ end
+
+ API.sqlite3_busy_handler( db, block&&@busy_handler_callback, data )
+ end
+
+ def set_authorizer( db, data=nil, &block )
+ @authorizer_handler = block
+
+ unless @authorizer_handler_callback
+ @authorizer_handler_callback = ::DL.callback( "IPIPPPP"
+ ) do |cookie,mode,a,b,c,d|
+ @authorizer_handler.call( cookie, mode,
+ a&&a.to_s, b&&b.to_s, c&&c.to_s, d&&d.to_s ) || 0
+ end
+ end
+
+ API.sqlite3_set_authorizer( db, block&&@authorizer_handler_callback,
+ data )
+ end
+
+ def trace( db, data=nil, &block )
+ @trace_handler = block
+
+ unless @trace_handler_callback
+ @trace_handler_callback = ::DL.callback( "IPS" ) do |cookie,sql|
+ @trace_handler.call( cookie ? cookie.to_object : nil, sql ) || 0
+ end
+ end
+
+ API.sqlite3_trace( db, block&&@trace_handler_callback, data )
+ end
+
+ def create_function( db, name, args, text, cookie,
+ func, step, final )
+ # begin
+ if @func_handler_callback.nil? && func
+ @func_handler_callback = ::DL.callback( "0PIP" ) do |context,nargs,args|
+ args = args.to_s(nargs*4).unpack("L*").map {|i| ::DL::PtrData.new(i)}
+ data = API.sqlite3_user_data( context ).to_object
+ data[:func].call( context, *args )
+ end
+ end
+
+ if @step_handler_callback.nil? && step
+ @step_handler_callback = ::DL.callback( "0PIP" ) do |context,nargs,args|
+ args = args.to_s(nargs*4).unpack("L*").map {|i| ::DL::PtrData.new(i)}
+ data = API.sqlite3_user_data( context ).to_object
+ data[:step].call( context, *args )
+ end
+ end
+
+ if @final_handler_callback.nil? && final
+ @final_handler_callback = ::DL.callback( "0P" ) do |context|
+ data = API.sqlite3_user_data( context ).to_object
+ data[:final].call( context )
+ end
+ end
+
+ data = { :cookie => cookie,
+ :name => name,
+ :func => func,
+ :step => step,
+ :final => final }
+
+ API.sqlite3_create_function( db, name, args, text, data,
+ ( func ? @func_handler_callback : nil ),
+ ( step ? @step_handler_callback : nil ),
+ ( final ? @final_handler_callback : nil ) )
+ end
+
+ def aggregate_context( context )
+ ptr = API.sqlite3_aggregate_context( context, 4 )
+ ptr.free = nil
+ obj = ( ptr ? ptr.to_object : nil )
+ if obj.nil?
+ obj = Hash.new
+ ptr.set_object obj
+ end
+ obj
+ end
+
+ def bind_blob( stmt, index, value )
+ s = value.to_s
+ API.sqlite3_bind_blob( stmt, index, s, s.length, TRANSIENT )
+ end
+
+ def bind_text( stmt, index, value, utf16=false )
+ s = value.to_s
+ method = ( utf16 ? :sqlite3_bind_text16 : :sqlite3_bind_text )
+ API.send( method, stmt, index, s, s.length, TRANSIENT )
+ end
+
+ def column_text( stmt, column )
+ result = API.sqlite3_column_text( stmt, column )
+ result ? result.to_s : nil
+ end
+
+ def column_name( stmt, column )
+ result = API.sqlite3_column_name( stmt, column )
+ result ? result.to_s : nil
+ end
+
+ def column_decltype( stmt, column )
+ result = API.sqlite3_column_decltype( stmt, column )
+ result ? result.to_s : nil
+ end
+
+ def self.api_delegate( name )
+ define_method( name ) { |*args| API.send( "sqlite3_#{name}", *args ) }
+ end
+
+ api_delegate :aggregate_count
+ api_delegate :bind_double
+ api_delegate :bind_int
+ api_delegate :bind_int64
+ api_delegate :bind_null
+ api_delegate :bind_parameter_index
+ api_delegate :bind_parameter_name
+ api_delegate :busy_timeout
+ api_delegate :changes
+ api_delegate :close
+ api_delegate :column_bytes
+ api_delegate :column_bytes16
+ api_delegate :column_count
+ api_delegate :column_double
+ api_delegate :column_int
+ api_delegate :column_int64
+ api_delegate :column_type
+ api_delegate :data_count
+ api_delegate :errcode
+ api_delegate :finalize
+ api_delegate :interrupt
+ api_delegate :last_insert_rowid
+ api_delegate :libversion
+ api_delegate :reset
+ api_delegate :result_error
+ api_delegate :step
+ api_delegate :total_changes
+ api_delegate :value_bytes
+ api_delegate :value_bytes16
+ api_delegate :value_double
+ api_delegate :value_int
+ api_delegate :value_int64
+ api_delegate :value_type
+
+ # ==== EXPERIMENTAL ====
+ if defined?( EXPERIMENTAL_API ) && EXPERIMENTAL_API
+ def progress_handler( db, n, data=nil, &block )
+ @progress_handler = block
+
+ unless @progress_handler_callback
+ @progress_handler_callback = ::DL.callback( "IP" ) do |cookie|
+ @progress_handler.call( cookie )
+ end
+ end
+
+ API.sqlite3_progress_handler( db, n, block&&@progress_handler_callback,
+ data )
+ end
+
+ def commit_hook( db, data=nil, &block )
+ @commit_hook_handler = block
+
+ unless @commit_hook_handler_callback
+ @commit_hook_handler_callback = ::DL.callback( "IP" ) do |cookie|
+ @commit_hook_handler.call( cookie )
+ end
+ end
+
+ API.sqlite3_commit_hook( db, block&&@commit_hook_handler_callback,
+ data )
+ end
+ end
+
+ private
+
+ def utf16_length(ptr)
+ len = 0
+ loop do
+ break if ptr[len,1] == "\0"
+ len += 2
+ end
+ len
+ end
+
+ end
+
+end ; end ; end
View
257 rhodes/rhodes-framework/res/sqlite3/driver/native/driver.rb
@@ -0,0 +1,257 @@
+require 'sqlite3/sqlite3_api'
+
+module SQLite3 ; module Driver ; module Native
+
+ class Driver
+
+ def initialize
+ @callback_data = Hash.new
+ @authorizer = Hash.new
+ @busy_handler = Hash.new
+ @trace = Hash.new
+ end
+
+ def complete?( sql, utf16=false )
+ API.send( utf16 ? :sqlite3_complete16 : :sqlite3_complete, sql ) != 0
+ end
+
+ def busy_handler( db, data=nil, &block )
+ if block
+ cb = API::CallbackData.new
+ cb.proc = block
+ cb.data = data
+ result = API.sqlite3_busy_handler( db, API::Sqlite3_ruby_busy_handler, cb )
+ # Reference the Callback object so that
+ # it is not deleted by the GC
+ @busy_handler[db] = cb
+ else
+ # Unreference the callback *after* having removed it
+ # from sqlite
+ result = API.sqlite3_busy_handler( db, nil, nil )
+ @busy_handler.delete(db)
+ end
+
+ result
+ end
+
+ def set_authorizer( db, data=nil, &block )
+ if block
+ cb = API::CallbackData.new
+ cb.proc = block
+ cb.data = data
+ result = API.sqlite3_set_authorizer( db, API::Sqlite3_ruby_authorizer, cb )
+ @authorizer[db] = cb # see comments in busy_handler
+ else
+ result = API.sqlite3_set_authorizer( db, nil, nil )
+ @authorizer.delete(db) # see comments in busy_handler
+ end
+
+ result
+ end
+
+ def trace( db, data=nil, &block )
+ if block
+ cb = API::CallbackData.new
+ cb.proc = block
+ cb.data = data
+ result = API.sqlite3_trace( db, API::Sqlite3_ruby_trace, cb )
+ @trace[db] = cb # see comments in busy_handler
+ else
+ result = API.sqlite3_trace( db, nil, nil )
+ @trace.delete(db) # see comments in busy_handler
+ end
+
+ result
+ end
+
+ def open( filename, utf16=false )
+ API.send( utf16 ? :sqlite3_open16 : :sqlite3_open, filename )
+ end
+
+ def errmsg( db, utf16=false )
+ API.send( utf16 ? :sqlite3_errmsg16 : :sqlite3_errmsg, db )
+ end
+
+ def prepare( db, sql, utf16=false )
+ API.send( ( utf16 ? :sqlite3_prepare16 : :sqlite3_prepare ),
+ db, sql )
+ end
+
+ def bind_text( stmt, index, value, utf16=false )
+ API.send( ( utf16 ? :sqlite3_bind_text16 : :sqlite3_bind_text ),
+ stmt, index, value.to_s )
+ end
+
+ def column_name( stmt, index, utf16=false )
+ API.send( ( utf16 ? :sqlite3_column_name16 : :sqlite3_column_name ),
+ stmt, index )
+ end
+
+ def column_decltype( stmt, index, utf16=false )
+ API.send(
+ ( utf16 ? :sqlite3_column_decltype16 : :sqlite3_column_decltype ),
+ stmt, index )
+ end
+
+ def column_text( stmt, index, utf16=false )
+ API.send( ( utf16 ? :sqlite3_column_text16 : :sqlite3_column_text ),
+ stmt, index )
+ end
+
+ def create_function( db, name, args, text, cookie, func, step, final )
+ if func || ( step && final )
+ cb = API::CallbackData.new
+ cb.proc = cb.proc2 = nil
+ cb.data = cookie
+ end
+
+ if func
+ cb.proc = func
+
+ func = API::Sqlite3_ruby_function_step
+ step = final = nil
+ elsif step && final
+ cb.proc = step
+ cb.proc2 = final
+
+ func = nil
+ step = API::Sqlite3_ruby_function_step
+ final = API::Sqlite3_ruby_function_final
+ end
+
+ result = API.sqlite3_create_function( db, name, args, text, cb, func, step, final )
+
+ # see comments in busy_handler
+ if cb
+ @callback_data[ name ] = cb
+ else
+ @callback_data.delete( name )
+ end
+
+ return result
+ end
+
+ def value_text( value, utf16=false )
+ method = case utf16
+ when nil, false then :sqlite3_value_text
+ when :le then :sqlite3_value_text16le
+ when :be then :sqlite3_value_text16be
+ else :sqlite3_value_text16
+ end
+
+ API.send( method, value )
+ end
+
+ def result_text( context, result, utf16=false )
+ method = case utf16
+ when nil, false then :sqlite3_result_text
+ when :le then :sqlite3_result_text16le
+ when :be then :sqlite3_result_text16be
+ else :sqlite3_result_text16
+ end
+
+ API.send( method, context, result.to_s )
+ end
+
+ def result_error( context, value, utf16=false )
+ API.send( ( utf16 ? :sqlite3_result_error16 : :sqlite3_result_error ),
+ context, value )
+ end
+
+ def libversion (*args) API.sqlite3_libversion( *args ) ; end
+ def close (*args) API.sqlite3_close( *args ) ; end
+ def last_insert_rowid (*args) API.sqlite3_last_insert_rowid( *args ) ; end
+ def changes (*args) API.sqlite3_changes( *args ) ; end
+ def total_changes (*args) API.sqlite3_total_changes( *args ) ; end
+ def interrupt (*args) API.sqlite3_interrupt( *args ) ; end
+ def busy_timeout (*args) API.sqlite3_busy_timeout( *args ) ; end
+ def errcode (*args) API.sqlite3_errcode( *args ) ; end
+ def bind_blob (*args) API.sqlite3_bind_blob( *args ) ; end
+ def bind_double (*args) API.sqlite3_bind_double( *args ) ; end
+ def bind_int (*args) API.sqlite3_bind_int( *args ) ; end
+ def bind_int64 (*args) API.sqlite3_bind_int64( *args ) ; end
+ def bind_null (*args) API.sqlite3_bind_null( *args ) ; end
+ def bind_parameter_count (*args) API.sqlite3_bind_parameter_count( *args ) ; end
+ def bind_parameter_name (*args) API.sqlite3_bind_parameter_name( *args ) ; end
+ def bind_parameter_index (*args) API.sqlite3_bind_parameter_index( *args ) ; end
+ def column_count (*args) API.sqlite3_column_count( *args ) ; end
+ def step (*args) API.sqlite3_step( *args ) ; end
+ def data_count (*args) API.sqlite3_data_count( *args ) ; end
+ def column_blob (*args) API.sqlite3_column_blob( *args ) ; end
+ def column_bytes (*args) API.sqlite3_column_bytes( *args ) ; end
+ def column_bytes16 (*args) API.sqlite3_column_bytes16( *args ) ; end
+ def column_double (*args) API.sqlite3_column_double( *args ) ; end
+ def column_int (*args) API.sqlite3_column_int( *args ) ; end
+ def column_int64 (*args) API.sqlite3_column_int64( *args ) ; end
+ def column_type (*args) API.sqlite3_column_type( *args ) ; end
+ def finalize (*args) API.sqlite3_finalize( *args ) ; end
+ def reset (*args) API.sqlite3_reset( *args ) ; end
+ def aggregate_count (*args) API.sqlite3_aggregate_count( *args ) ; end
+ def value_blob (*args) API.sqlite3_value_blob( *args ) ; end
+ def value_bytes (*args) API.sqlite3_value_bytes( *args ) ; end
+ def value_bytes16 (*args) API.sqlite3_value_bytes16( *args ) ; end
+ def value_double (*args) API.sqlite3_value_double( *args ) ; end
+ def value_int (*args) API.sqlite3_value_int( *args ) ; end
+ def value_int64 (*args) API.sqlite3_value_int64( *args ) ; end
+ def value_type (*args) API.sqlite3_value_type( *args ) ; end
+ def result_blob (*args) API.sqlite3_result_blob( *args ) ; end
+ def result_double (*args) API.sqlite3_result_double( *args ) ; end
+ def result_int (*args) API.sqlite3_result_int( *args ) ; end
+ def result_int64 (*args) API.sqlite3_result_int64( *args ) ; end
+ def result_null (*args) API.sqlite3_result_null( *args ) ; end
+ def result_value (*args) API.sqlite3_result_value( *args ) ; end
+ def aggregate_context (*args) API.sqlite3_aggregate_context( *args ) ; end
+
+=begin
+ def self.api_delegate( name )
+ eval "def #{name} (*args) API.sqlite3_#{name}( *args ) ; end"
+ end
+
+ api_delegate :libversion
+ api_delegate :close
+ api_delegate :last_insert_rowid
+ api_delegate :changes
+ api_delegate :total_changes
+ api_delegate :interrupt
+ api_delegate :busy_timeout
+ api_delegate :errcode
+ api_delegate :bind_blob
+ api_delegate :bind_double
+ api_delegate :bind_int
+ api_delegate :bind_int64
+ api_delegate :bind_null
+ api_delegate :bind_parameter_count
+ api_delegate :bind_parameter_name
+ api_delegate :bind_parameter_index
+ api_delegate :column_count
+ api_delegate :step
+ api_delegate :data_count
+ api_delegate :column_blob
+ api_delegate :column_bytes
+ api_delegate :column_bytes16
+ api_delegate :column_double
+ api_delegate :column_int
+ api_delegate :column_int64
+ api_delegate :column_type
+ api_delegate :finalize
+ api_delegate :reset
+ api_delegate :aggregate_count
+ api_delegate :value_blob
+ api_delegate :value_bytes
+ api_delegate :value_bytes16
+ api_delegate :value_double
+ api_delegate :value_int
+ api_delegate :value_int64
+ api_delegate :value_type
+ api_delegate :result_blob
+ api_delegate :result_double
+ api_delegate :result_int
+ api_delegate :result_int64
+ api_delegate :result_null
+ api_delegate :result_value
+ api_delegate :aggregate_context
+=end
+
+ end
+
+end ; end ; end
View
68 rhodes/rhodes-framework/res/sqlite3/errors.rb
@@ -0,0 +1,68 @@
+require 'sqlite3/constants'
+
+module SQLite3
+
+ class Exception < ::StandardError
+ @code = 0
+
+ # The numeric error code that this exception represents.
+ def self.code
+ @code
+ end
+
+ # A convenience for accessing the error code for this exception.
+ def code
+ self.class.code
+ end
+ end
+
+ class SQLException < Exception; end
+ class InternalException < Exception; end
+ class PermissionException < Exception; end
+ class AbortException < Exception; end
+ class BusyException < Exception; end
+ class LockedException < Exception; end
+ class MemoryException < Exception; end
+ class ReadOnlyException < Exception; end
+ class InterruptException < Exception; end
+ class IOException < Exception; end
+ class CorruptException < Exception; end
+ class NotFoundException < Exception; end
+ class FullException < Exception; end
+ class CantOpenException < Exception; end
+ class ProtocolException < Exception; end
+ class EmptyException < Exception; end
+ class SchemaChangedException < Exception; end
+ class TooBigException < Exception; end
+ class ConstraintException < Exception; end
+ class MismatchException < Exception; end
+ class MisuseException < Exception; end
+ class UnsupportedException < Exception; end
+ class AuthorizationException < Exception; end
+ class FormatException < Exception; end
+ class RangeException < Exception; end
+ class NotADatabaseException < Exception; end
+
+ EXCEPTIONS = [
+ nil,
+ SQLException, InternalException, PermissionException,
+ AbortException, BusyException, LockedException, MemoryException,
+ ReadOnlyException, InterruptException, IOException, CorruptException,
+ NotFoundException, FullException, CantOpenException, ProtocolException,
+ EmptyException, SchemaChangedException, TooBigException,
+ ConstraintException, MismatchException, MisuseException,
+ UnsupportedException, AuthorizationException, FormatException,
+ RangeException, NotADatabaseException
+ ].each_with_index { |e,i| e.instance_variable_set( :@code, i ) if e }
+
+ module Error
+ def check( result, db=nil, msg=nil )
+ unless result == Constants::ErrorCode::OK
+ msg = ( msg ? msg + ": " : "" ) + db.errmsg if db
+ raise(( EXCEPTIONS[result] || SQLite3::Exception ), msg)
+ end
+ end
+ module_function :check
+ end
+
+end
View
271 rhodes/rhodes-framework/res/sqlite3/pragmas.rb
@@ -0,0 +1,271 @@
+require 'sqlite3/errors'
+
+module SQLite3
+
+ # This module is intended for inclusion solely by the Database class. It
+ # defines convenience methods for the various pragmas supported by SQLite3.
+ #
+ # For a detailed description of these pragmas, see the SQLite3 documentation
+ # at http://sqlite.org/pragma.html.
+ module Pragmas
+
+ # Returns +true+ or +false+ depending on the value of the named pragma.
+ def get_boolean_pragma( name )
+ get_first_value( "PRAGMA #{name}" ) != "0"
+ end
+ private :get_boolean_pragma
+
+ # Sets the given pragma to the given boolean value. The value itself
+ # may be +true+ or +false+, or any other commonly used string or
+ # integer that represents truth.
+ def set_boolean_pragma( name, mode )
+ case mode
+ when String
+ case mode.downcase
+ when "on", "yes", "true", "y", "t"; mode = "'ON'"
+ when "off", "no", "false", "n", "f"; mode = "'OFF'"
+ else
+ raise Exception,
+ "unrecognized pragma parameter #{mode.inspect}"
+ end
+ when true, 1
+ mode = "ON"
+ when false, 0, nil
+ mode = "OFF"
+ else
+ raise Exception,
+ "unrecognized pragma parameter #{mode.inspect}"
+ end
+
+ execute( "PRAGMA #{name}=#{mode}" )
+ end
+ private :set_boolean_pragma
+
+ # Requests the given pragma (and parameters), and if the block is given,
+ # each row of the result set will be yielded to it. Otherwise, the results
+ # are returned as an array.
+ def get_query_pragma( name, *parms, &block ) # :yields: row
+ if parms.empty?
+ execute( "PRAGMA #{name}", &block )
+ else
+ args = "'" + parms.join("','") + "'"
+ execute( "PRAGMA #{name}( #{args} )", &block )
+ end
+ end
+ private :get_query_pragma
+
+ # Return the value of the given pragma.
+ def get_enum_pragma( name )
+ get_first_value( "PRAGMA #{name}" )
+ end
+ private :get_enum_pragma
+
+ # Set the value of the given pragma to +mode+. The +mode+ parameter must
+ # conform to one of the values in the given +enum+ array. Each entry in
+ # the array is another array comprised of elements in the enumeration that
+ # have duplicate values. See #synchronous, #default_synchronous,
+ # #temp_store, and #default_temp_store for usage examples.
+ def set_enum_pragma( name, mode, enums )
+ match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
+ raise Exception,
+ "unrecognized #{name} #{mode.inspect}" unless match
+ execute( "PRAGMA #{name}='#{match.first.upcase}'" )
+ end
+ private :set_enum_pragma
+
+ # Returns the value of the given pragma as an integer.
+ def get_int_pragma( name )
+ get_first_value( "PRAGMA #{name}" ).to_i
+ end
+ private :get_int_pragma
+
+ # Set the value of the given pragma to the integer value of the +value+
+ # parameter.
+ def set_int_pragma( name, value )
+ execute( "PRAGMA #{name}=#{value.to_i}" )
+ end
+ private :set_int_pragma
+
+ # The enumeration of valid synchronous modes.
+ SYNCHRONOUS_MODES = [ [ 'full', 2 ], [ 'normal', 1 ], [ 'off', 0 ] ]
+
+ # The enumeration of valid temp store modes.
+ TEMP_STORE_MODES = [ [ 'default', 0 ], [ 'file', 1 ], [ 'memory', 2 ] ]
+
+ # Does an integrity check on the database. If the check fails, a
+ # SQLite3::Exception will be raised. Otherwise it
+ # returns silently.
+ def integrity_check
+ execute( "PRAGMA integrity_check" ) do |row|
+ raise Exception, row[0] if row[0] != "ok"
+ end
+ end
+
+ def auto_vacuum
+ get_boolean_pragma "auto_vacuum"
+ end
+
+ def auto_vacuum=( mode )
+ set_boolean_pragma "auto_vacuum", mode
+ end
+
+ def schema_cookie
+ get_int_pragma "schema_cookie"
+ end
+
+ def schema_cookie=( cookie )
+ set_int_pragma "schema_cookie", cookie
+ end
+
+ def user_cookie
+ get_int_pragma "user_cookie"
+ end
+
+ def user_cookie=( cookie )
+ set_int_pragma "user_cookie", cookie
+ end
+
+ def cache_size
+ get_int_pragma "cache_size"
+ end
+
+ def cache_size=( size )
+ set_int_pragma "cache_size", size
+ end
+
+ def default_cache_size
+ get_int_pragma "default_cache_size"
+ end
+
+ def default_cache_size=( size )
+ set_int_pragma "default_cache_size", size
+ end
+
+ def default_synchronous
+ get_enum_pragma "default_synchronous"
+ end
+
+ def default_synchronous=( mode )
+ set_enum_pragma "default_synchronous", mode, SYNCHRONOUS_MODES
+ end
+
+ def synchronous
+ get_enum_pragma "synchronous"
+ end
+
+ def synchronous=( mode )
+ set_enum_pragma "synchronous", mode, SYNCHRONOUS_MODES
+ end
+
+ def default_temp_store
+ get_enum_pragma "default_temp_store"
+ end
+
+ def default_temp_store=( mode )
+ set_enum_pragma "default_temp_store", mode, TEMP_STORE_MODES
+ end
+
+ def temp_store
+ get_enum_pragma "temp_store"
+ end
+
+ def temp_store=( mode )
+ set_enum_pragma "temp_store", mode, TEMP_STORE_MODES
+ end
+
+ def full_column_names
+ get_boolean_pragma "full_column_names"
+ end
+
+ def full_column_names=( mode )
+ set_boolean_pragma "full_column_names", mode
+ end
+
+ def parser_trace
+ get_boolean_pragma "parser_trace"
+ end
+
+ def parser_trace=( mode )
+ set_boolean_pragma "parser_trace", mode
+ end
+
+ def vdbe_trace
+ get_boolean_pragma "vdbe_trace"
+ end
+
+ def vdbe_trace=( mode )
+ set_boolean_pragma "vdbe_trace", mode
+ end
+
+ def database_list( &block ) # :yields: row
+ get_query_pragma "database_list", &block
+ end
+
+ def foreign_key_list( table, &block ) # :yields: row
+ get_query_pragma "foreign_key_list", table, &block
+ end
+
+ def index_info( index, &block ) # :yields: row
+ get_query_pragma "index_info", index, &block
+ end
+
+ def index_list( table, &block ) # :yields: row
+ get_query_pragma "index_list", table, &block
+ end
+
+ def table_info( table, &block ) # :yields: row
+ columns, *rows = execute2("PRAGMA table_info(#{table})")
+
+ needs_tweak_default = version_compare(driver.libversion, "3.3.7") > 0
+
+ result = [] unless block_given?
+ rows.each do |row|
+ new_row = {}
+ columns.each_with_index do |name, index|
+ new_row[name] = row[index]
+ end
+
+ tweak_default(new_row) if needs_tweak_default
+
+ if block_given?
+ yield new_row
+ else
+ result << new_row
+ end
+ end
+
+ result
+ end
+
+ private
+
+ # Compares two version strings
+ def version_compare(v1, v2)
+ v1 = v1.split(".").map { |i| i.to_i }
+ v2 = v2.split(".").map { |i| i.to_i }
+ parts = [v1.length, v2.length].max
+ v1.push 0 while v1.length < parts
+ v2.push 0 while v2.length < parts
+ v1.zip(v2).each do |a,b|
+ return -1 if a < b
+ return 1 if a > b
+ end
+ return 0
+ end
+
+ # Since SQLite 3.3.8, the table_info pragma has returned the default
+ # value of the row as a quoted SQL value. This method essentially
+ # unquotes those values.
+ def tweak_default(hash)
+ case hash["dflt_value"]
+ when /^null$/i
+ hash["dflt_value"] = nil
+ when /^'(.*)'$/
+ hash["dflt_value"] = $1.gsub(/''/, "'")
+ when /^"(.*)"$/
+ hash["dflt_value"] = $1.gsub(/""/, '"')
+ end
+ end
+ end
+
+end
View
176 rhodes/rhodes-framework/res/sqlite3/resultset.rb