Method: SQLite3::Database#initialize

Defined in:
lib/sqlite3/database.rb

#initialize(file, options = {}, zvfs = nil) ⇒ Database

call-seq:

SQLite3::Database.new(file, options = {})

Create a new Database object that opens the given file.

Supported permissions options:

  • the default mode is READWRITE | CREATE

  • readonly: boolean (default false), true to set the mode to READONLY

  • readwrite: boolean (default false), true to set the mode to READWRITE

  • flags: set the mode to a combination of SQLite3::Constants::Open flags.

Supported encoding options:

  • utf16: boolish (default false), is the filename’s encoding UTF-16 (only needed if the filename encoding is not UTF_16LE or BE)

Other supported options:

  • strict: boolish (default false), disallow the use of double-quoted string literals (see www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted)

  • results_as_hash: boolish (default false), return rows as hashes instead of arrays

  • default_transaction_mode: one of :deferred (default), :immediate, or :exclusive. If a mode is not specified in a call to #transaction, this will be the default transaction mode.

  • extensions: Array[String | _ExtensionSpecifier] SQLite extensions to load into the database. See Database@SQLite+Extensions for more information.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/sqlite3/database.rb', line 142

def initialize file, options = {}, zvfs = nil
  mode = Constants::Open::READWRITE | Constants::Open::CREATE

  file = file.to_path if file.respond_to? :to_path
  if file.encoding == ::Encoding::UTF_16LE || file.encoding == ::Encoding::UTF_16BE || options[:utf16]
    open16 file
  else
    # The three primary flag values for sqlite3_open_v2 are:
    # SQLITE_OPEN_READONLY
    # SQLITE_OPEN_READWRITE
    # SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE -- always used for sqlite3_open and sqlite3_open16
    mode = Constants::Open::READONLY if options[:readonly]

    if options[:readwrite]
      raise "conflicting options: readonly and readwrite" if options[:readonly]
      mode = Constants::Open::READWRITE
    end

    if options[:flags]
      if options[:readonly] || options[:readwrite]
        raise "conflicting options: flags with readonly and/or readwrite"
      end
      mode = options[:flags]
    end

    open_v2 file.encode("utf-8"), mode, zvfs

    if options[:strict]
      disable_quirk_mode
    end
  end

  @tracefunc = nil
  @authorizer = nil
  @progress_handler = nil
  @collations = {}
  @functions = {}
  @results_as_hash = options[:results_as_hash]
  @readonly = mode & Constants::Open::READONLY != 0
  @default_transaction_mode = options[:default_transaction_mode] || :deferred

  initialize_extensions(options[:extensions])

  ForkSafety.track(self)

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end