Module: Hanami::Model::Sql

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

Overview

SQL adapter

Since:

  • 0.7.0

Defined Under Namespace

Modules: Consoles, Entity, Types

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

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

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

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

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