Class: Browser::Database::SQL

Inherits:
Object
  • Object
show all
Includes:
Native
Defined in:
opal/browser/database/sql.rb

Defined Under Namespace

Classes: Error, Result, Row, Transaction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ SQL

Open a database with the given name and options.

Parameters:

  • name (String)

    the name for the database

  • options (Hash) (defaults to: {})

    options to open the database

Options Hash (options):

  • :description (String)

    the description for the database

  • :version (String) — default: ''

    the expected version of the database

  • :size (Integer) — default: 5 * 1024 * 1024

    the size constraint in bytes



49
50
51
52
53
54
55
56
# File 'opal/browser/database/sql.rb', line 49

def initialize(name, options = {})
  @name        = name
  @description = options[:description] || name
  @version     = options[:version]     || ''
  @size        = options[:size]        || 2 * 1024 * 1024

  super(`window.openDatabase(#{name}, #{@version}, #{@description}, #{@size})`)
end

Instance Attribute Details

#descriptionString (readonly)

Returns the description for the database.

Returns:

  • (String)

    the description for the database



36
37
38
# File 'opal/browser/database/sql.rb', line 36

def description
  @description
end

#nameString (readonly)

Returns the name of the database.

Returns:

  • (String)

    the name of the database



33
34
35
# File 'opal/browser/database/sql.rb', line 33

def name
  @name
end

#sizeInteger (readonly)

Returns the size constraint in bytes.

Returns:

  • (Integer)

    the size constraint in bytes



39
40
41
# File 'opal/browser/database/sql.rb', line 39

def size
  @size
end

Class Method Details

.supported?Boolean

Check if the browser supports WebSQL.

Returns:

  • (Boolean)


8
9
10
# File 'opal/browser/database/sql.rb', line 8

def self.supported?
  Browser.supports? 'WebSQL'
end

Instance Method Details

#transaction {|transaction| ... } ⇒ Object

Start a transaction on the database.

Yield Parameters:

  • transaction (Transaction)

    the transaction to work on

Raises:

  • (ArgumentError)


82
83
84
85
86
# File 'opal/browser/database/sql.rb', line 82

def transaction(&block)
  raise ArgumentError, 'no block given' unless block

  `#@native.transaction(#{-> t { block.call(Transaction.new(self, t)) }})`
end

#versionString #version(from, to) {|transaction| ... } ⇒ Object

Overloads:

  • #versionString

    Get the version of the database.

    Returns:

  • #version(from, to) {|transaction| ... } ⇒ Object

    Migrate the database to a new version.

    Parameters:

    • from (String)

      the version you're migrating from

    • to (String)

      the version you're migrating to

    Yield Parameters:

    • transaction (Transaction)

      the transaction to work with



72
73
74
75
76
77
# File 'opal/browser/database/sql.rb', line 72

def version(from = nil, to = nil, &block)
  return `#@native.version` unless block

  `#@native.changeVersion(#{from}, #{to},
    #{-> t { block.call(Transaction.new(self, t)) }})`
end