Class: Sequel::Sqljs::Database

Inherits:
Database
  • Object
show all
Includes:
DatabaseMethods
Defined in:
lib/bormashino_sequel_sqljs_adapter/sqljs.rb

Constant Summary

Constants included from DatabaseMethods

Sequel::Sqljs::DatabaseMethods::AUTO_VACUUM, Sequel::Sqljs::DatabaseMethods::SYNCHRONOUS, Sequel::Sqljs::DatabaseMethods::TEMP_STORE, Sequel::Sqljs::DatabaseMethods::TRANSACTION_MODE

Instance Attribute Summary collapse

Attributes included from DatabaseMethods

#current_timestamp_utc, #integer_booleans, #transaction_mode, #use_timestamp_timezones

Instance Method Summary collapse

Methods included from DatabaseMethods

#database_type, #foreign_key_list, #indexes, #set_integer_booleans, #sqlite_version, #supports_create_table_if_not_exists?, #supports_deferrable_foreign_key_constraints?, #supports_partial_indexes?, #supports_savepoints?, #tables, #use_timestamp_timezones?, #values, #views

Constructor Details

#initialize(opts = OPTS) ⇒ Database

Returns a new instance of Database.



127
128
129
130
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 127

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



125
126
127
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 125

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)


167
168
169
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 167

def allow_regexp?
  @allow_regexp
end

#connect(server) ⇒ Object

Connect to the database. Since SQLite 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)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 140

def connect(server)
  opts = server_opts(server)
  # db = ::SQLite3::Database.new(opts[:database].to_s, sqlite3_opts)
  db = Wrapper.new(JS.global[opts[:database].to_sym])

  # db.busy_timeout(typecast_value_integer(opts.fetch(:timeout, 5000)))

  # db.extended_result_codes = true

  connection_pragmas.each { |s| log_connection_yield(s, db) { db.exec(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.



172
173
174
175
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 172

def disconnect_connection(c)
  c.prepared_statements.each_value { |v| v.first.close }
  c.close
end

#execute(sql, opts = OPTS) ⇒ Object

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



178
179
180
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 178

def execute(sql, opts = OPTS, &)
  _execute(:select, sql, opts, &)
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.



190
191
192
193
194
195
196
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 190

def execute_ddl(sql, opts = OPTS)
  synchronize(opts[:server]) do |conn|
    conn.prepared_statements.each_value { |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.



183
184
185
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 183

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

#execute_insert(sql, opts = OPTS) ⇒ Object



198
199
200
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 198

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

#freezeObject



202
203
204
205
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 202

def freeze
  @conversion_procs.freeze
  super
end

#to_application_timestamp(s) ⇒ Object

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



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/bormashino_sequel_sqljs_adapter/sqljs.rb', line 208

def to_application_timestamp(s)
  case s
  when String
    super
  when Integer
    super(Time.zone.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