class Cql::Table

Overview

Represents a table in the database. This class is responsible for handling table creation, modification, and deletion.

Usage

table = Table.new(:users, schema)
=> #<Table:0x00007f8e7a4e1e80>
table.column(:id, Int64, primary: true)
table.column(:name, String)
table.create_sql
=> "CREATE TABLE users (id BIGINT PRIMARY KEY, name TEXT);"
table = Table.new(:users, schema)
table.drop!
=> nil
table = Table.new(:users, schema)
table.truncate!
=> nil

table = Table.new(:users, schema)
table.column(:id, Int64, primary: true)
table.column(:name, String)
table.create!
=> nil

Defined in:

table.cr

Constant Summary

Log = ::Log.for(self)

Constructors

Macro Summary

Instance Method Summary

Constructor Detail

def self.new(table_name : Symbol, schema : Schema, as_name : String | Nil = nil) #

[View source]

Macro Detail

macro method_missing(call) #

[View source]

Instance Method Detail

def add_index(columns : Array(Symbol), unique : Bool = false, table : Table = self) #

Adds a new column to the table.

  • @param columns [Array(Symbol)] the columns to be indexed
  • @param unique [Bool] whether the index should be unique (default: false)
  • @param table [Table] the table to add the index to (default: self)
  • @return [Index] the new index

Example Adding a new index

add_index([:email], unique: true)
add_index([:email, :username], unique: true)
add_index([:email, :username], unique: true, table: users)

[View source]
def as_name : String | Nil #

[View source]
def bigint(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

bigint :age
bigint :age, as: "user_age", null: false, default: 18, unique: true, index: true

[View source]
def blob(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

blob :data
blob :data, as: "binary_data", null: false, default: nil, unique: true, index: true

[View source]
def boolean(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

boolean :active
boolean :active, as: "is_active", null: false, default: false, unique: true, index: true

[View source]
def column(name : Symbol, type : T.class, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Bool = false) forall T #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param type [T.class] the data type of the column
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param size [Int32, nil] the size of the column (default: nil)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

column :email, String

[View source]
def columns : Hash(Symbol, BaseColumn) #

[View source]
def create! #

Creates the table in the database.

  • @return [Nil]

Example

table = Table.new(:users, schema)
table.column(:id, Int64, primary: true)
table.column(:name, String)
table.create!
=> nil

[View source]
def create_sql #

Generates the SQL to create the table.

Example

table = Table.new(:users, schema)
table.column(:id, Int64, primary: true)
table.column(:name, String)
table.create_sql
=> "CREATE TABLE users (id BIGINT PRIMARY KEY, name TEXT);"

[View source]
def date(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

date :birthday
date :birthday, as: "date_of_birth", null: false, default: Time.local, unique: true, index: true

[View source]
def double(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

double :age
double :age, as: "user_age", null: false, default: 18.0, unique: true, index: true

[View source]
def drop! #

Drops the table from the database.

  • @return [Nil]

Example

table = Table.new(:users, schema)
table.drop!
=> nil

[View source]
def drop_sql #

Generates the SQL to drop the table.

  • @return [String] the SQL query

Example

table = Table.new(:users, schema)
table.drop_sql
=> "DROP TABLE users;"

[View source]
def expression #

Gets table expression for Sql query generation

  • @return [Expression::Table] the table expression

Example

table = Table.new(:users, schema)
table.expression
=> #<Expression::Table:0x00007f8e7a4e1e80>

[View source]
def float(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

float :age
float :age, as: "user_age", null: false, default: 18.0, unique: true, index: true

[View source]
def integer(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

integer :age
integer :age, as: "user_age", null: false, default: 18, unique: true, index: true

[View source]
def interval(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

interval :duration
interval :duration, as: "time_span", null: false, default: Time.local, unique: true, index: true

[View source]
def primary(name : Symbol = :id, type : T.class = Int64, auto_increment : Bool = true, as as_name = nil, unique : Bool = true) forall T #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param type [Any] the data type of the column
  • @param auto_increment [Bool] whether the column should auto increment (default: true)
  • @param as_name [String, nil] an optional alias for the column
  • @param unique [Bool] whether the column should have a unique constraint (default: true)

Example Adding a new primary key column

primary :id, Int64
primary :id, Int64, auto_increment: false

[View source]
def table_name : Symbol #

[View source]
def table_name=(table_name : Symbol) #

[View source]
def text(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

decimal :price
decimal :price, as: "product_price", null: false, default: 0.0, unique: true, index: true

[View source]
def timestamp(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false) #

Adds a new column to the table.

  • @param name [Symbol] the name of the column to be added
  • @param as_name [String, nil] an optional alias for the column
  • @param null [Bool] whether the column allows null values (default: false)
  • @param default [DB::Any, nil] the default value for the column (default: nil)
  • @param unique [Bool] whether the column should have a unique constraint (default: false)
  • @param index [Bool] whether the column should be indexed (default: false)
  • @return [Column] the new column

Example Adding a new column with default options

timestamp :created_at
timestamp :created_at, as: "created_at", null: false, default: Time.local, unique: true, index: true

[View source]
def timestamps #

Adds a new column to the table.

Example Adding timestamps to the table

timestamps

[View source]
def truncate! #

Truncates the table in the database.

  • @return [Nil]

Example

table = Table.new(:users, schema)
table.truncate!
=> nil

[View source]
def truncate_sql #

Generates the SQL to truncate the table.

  • @return [String] the SQL query

Example

table = Table.new(:users, schema)
table.truncate_sql
=> "TRUNCATE TABLE users;"

[View source]