Module: Hanami::Model::Sql

Defined in:
lib/hanami/model/sql.rb,
lib/hanami/model/sql/types.rb,
lib/hanami/model/sql/console.rb,
lib/hanami/model/sql/entity/schema.rb,
lib/hanami/model/sql/consoles/mysql.rb,
lib/hanami/model/sql/consoles/sqlite.rb,
lib/hanami/model/sql/consoles/abstract.rb,
lib/hanami/model/sql/consoles/postgresql.rb,
lib/hanami/model/sql/types/schema/coercions.rb

Overview

SQL adapter

Since:

  • 0.7.0

Defined Under Namespace

Modules: Consoles, Entity, Types Classes: Console

Class Method Summary collapse

Class Method Details

.asc(column) ⇒ String

Returns SQL fragment for ascending order for the given column

Parameters:

  • column (Symbol)

    the column name

Returns:

  • (String)

    the SQL fragment

Since:

  • 0.7.0


127
128
129
# File 'lib/hanami/model/sql.rb', line 127

def self.asc(column)
  Sequel.asc(column)
end

.desc(column) ⇒ String

Returns SQL fragment for descending order for the given column

Parameters:

  • column (Symbol)

    the column name

Returns:

  • (String)

    the SQL fragment

Since:

  • 0.7.0


137
138
139
# File 'lib/hanami/model/sql.rb', line 137

def self.desc(column)
  Sequel.desc(column)
end

.function(name) ⇒ String

Returns a SQL fragment that references a database function by the given name This is useful for database migrations

Examples:

Hanami::Model.migration do
  up do
    execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'

    create_table :source_files do
      column :id, 'uuid', primary_key: true, default: Hanami::Model::Sql.function(:uuid_generate_v4)
      # ...
    end
  end

  down do
    drop_table :source_files
    execute 'DROP EXTENSION "uuid-ossp"'
  end
end

Parameters:

  • name (String, Symbol)

    the function name

Returns:

  • (String)

    the SQL fragment

Since:

  • 0.7.0


83
84
85
# File 'lib/hanami/model/sql.rb', line 83

def self.function(name)
  Sequel.function(name)
end

.literal(string) ⇒ String

Returns a literal SQL fragment for the given SQL fragment. This is useful for database migrations

Examples:

Hanami::Model.migration do
  up do
    execute %{
      CREATE TYPE inventory_item AS (
        name            text,
        supplier_id     integer,
        price           numeric
      );
    }

    create_table :items do
      column :item, 'inventory_item', default: Hanami::Model::Sql.literal("ROW('fuzzy dice', 42, 1.99)")
      # ...
    end
  end

  down do
    drop_table :items
    execute 'DROP TYPE inventory_item'
  end
end

Parameters:

  • string (String)

    the SQL fragment

Returns:

  • (String)

    the literal SQL fragment

Since:

  • 0.7.0


117
118
119
# File 'lib/hanami/model/sql.rb', line 117

def self.literal(string)
  Sequel.lit(string)
end