Class: Sequel::Extralite::Database

Inherits:
Database
  • Object
show all
Includes:
SQLite::DatabaseMethods
Defined in:
lib/sequel/adapters/extralite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = OPTS) ⇒ Database

Returns a new instance of Database.



103
104
105
106
# File 'lib/sequel/adapters/extralite.rb', line 103

def initialize(opts = OPTS)
  super
  @allow_regexp = typecast_value_boolean(opts[:setup_regexp_function])
end

Instance Attribute Details

#conversion_procsObject (readonly)

The conversion procs to use for this database



101
102
103
# File 'lib/sequel/adapters/extralite.rb', line 101

def conversion_procs
  @conversion_procs
end

Instance Method Details

#allow_regexp?Boolean

Whether this Database instance is setup to allow regexp matching. True if the :setup_regexp_function option was passed when creating the Database.

Returns:

  • (Boolean)


143
144
145
# File 'lib/sequel/adapters/extralite.rb', line 143

def allow_regexp?
  @allow_regexp
end

#connect(server) ⇒ Object

Connect to the database. Since Extralite is a file based database, available options are limited:

:database :: database name (filename or ':memory:' or file: URI) :readonly :: open database in read-only mode; useful for reading static data that you do not want to modify :timeout :: how long to wait for the database to be available if it is locked, given in milliseconds (default is 5000)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sequel/adapters/extralite.rb', line 116

def connect(server)
  opts = server_opts(server)
  opts[:database] = ':memory:' if blank_object?(opts[:database])
  # db opts may be used in a future version of Extralite
  db_opts = {}
  db_opts[:readonly] = typecast_value_boolean(opts[:readonly]) if opts.has_key?(:readonly)
  db = ::Extralite::Database.new(opts[:database].to_s)
  # db.busy_timeout(typecast_value_integer(opts.fetch(:timeout, 5000)))

  connection_pragmas.each{|s| log_connection_yield(s, db){db.query(s)}}

  if typecast_value_boolean(opts[:setup_regexp_function])
    db.create_function("regexp", 2) do |func, regexp_str, string|
      func.result = Regexp.new(regexp_str).match(string) ? 1 : 0
    end
  end
  
  class << db
    attr_reader :prepared_statements
  end
  db.instance_variable_set(:@prepared_statements, {})
  
  db
end

#disconnect_connection(c) ⇒ Object

Disconnect given connections from the database.



148
149
150
# File 'lib/sequel/adapters/extralite.rb', line 148

def disconnect_connection(c)
  c.close
end

#execute(sql, opts = OPTS, &block) ⇒ Object

Run the given SQL with the given arguments and yield each row.



153
154
155
# File 'lib/sequel/adapters/extralite.rb', line 153

def execute(sql, opts=OPTS, &block)
  _execute(:select, sql, opts, &block)
end

#execute_ddl(sql, opts = OPTS) ⇒ Object

Drop any prepared statements on the connection when executing DDL. This is because prepared statements lock the table in such a way that you can't drop or alter the table while a prepared statement that references it still exists.



165
166
167
168
169
170
171
# File 'lib/sequel/adapters/extralite.rb', line 165

def execute_ddl(sql, opts=OPTS)
  synchronize(opts[:server]) do |conn|
    # conn.prepared_statements.values.each{|cps, s| cps.close}
    # conn.prepared_statements.clear
    super
  end
end

#execute_dui(sql, opts = OPTS) ⇒ Object

Run the given SQL with the given arguments and return the number of changed rows.



158
159
160
# File 'lib/sequel/adapters/extralite.rb', line 158

def execute_dui(sql, opts=OPTS)
  _execute(:update, sql, opts)
end

#execute_insert(sql, opts = OPTS) ⇒ Object



173
174
175
# File 'lib/sequel/adapters/extralite.rb', line 173

def execute_insert(sql, opts=OPTS)
  _execute(:insert, sql, opts)
end

#freezeObject



177
178
179
180
# File 'lib/sequel/adapters/extralite.rb', line 177

def freeze
  @conversion_procs.freeze
  super
end

#to_application_timestamp(s) ⇒ Object

Handle Integer and Float arguments, since Extralite can store timestamps as integers and floats.



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/sequel/adapters/extralite.rb', line 183

def to_application_timestamp(s)
  case s
  when String
    super
  when Integer
    super(Time.at(s).to_s)
  when Float
    super(DateTime.jd(s).to_s)
  else
    raise Sequel::Error, "unhandled type when converting to : #{s.inspect} (#{s.class.inspect})"
  end
end