class Cql::Schema

Overview

The Schema class represents a database schema.

This class provides methods to build and manage a database schema, including creating tables, executing SQL statements, and generating queries.

Example Creating a new schema

schema = Cql::Schema.define(:northwind, "sqlite3://db.sqlite3") do
  table :users do
    primary :id, Int64, auto_increment: true
    column :name, String
    column :email, String
  end
end

Example Executing a SQL statement

schema.exec("CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT)")

Example Creating a new query

query = schema.query

The Schema class represents a database schema.

Defined in:

schema.cr

Constant Summary

Log = ::Log.for(self)

Constructors

Class Method Summary

Macro Summary

Instance Method Summary

Constructor Detail

def self.new(name : Symbol, uri : String, adapter : Adapter = Adapter::Sqlite, version : String = "1.0") #

Initializes a new schema.

  • @param name [Symbol] the name of the schema
  • @param uri [String] the URI of the database
  • @param adapter [Adapter] the database adapter (default: Adapter::Sqlite)
  • @param version [String] the version of the schema (default: "1.0")

Example Initializing a new schema

schema = Cql::Schema.new(:northwind, "sqlite3://db.sqlite3")

[View source]

Class Method Detail

def self.define(name : Symbol, uri : String, adapter : Adapter = Adapter::Sqlite, version : String = "1.0", &) #

Builds a new schema.

  • @param name [Symbol] the name of the schema
  • @param uri [String] the URI of the database
  • @param adapter [Adapter] the database adapter (default: Adapter::Sqlite)
  • @param version [String] the version of the schema (default: "1.0")
  • @yield [Schema] the schema being built
  • @return [Schema] the built schema

Example

schema = Cql::Schema.define(:northwind, "sqlite3://db.sqlite3") do |s|
  s.create_table :users do
    primary :id, Int64, auto_increment: true
    column :name, String
    column :email, String
  end
end

[View source]

Macro Detail

macro method_missing(call) #

[View source]

Instance Method Detail

def adapter : Adapter #

[View source]
def alter(table_name : Symbol, &) #

Alter a table in the schema.

  • @param table_name [Symbol] the name of the table
  • @yield [AlterTable] the table being altered Example
schema.alter(:users) do |t|
  t.add_column :age, Int32
end

Example

schema.alter(:users) do |t|
  t.drop_column :age
end

[View source]
def build #

Builds the schema. This method creates the tables in the schema.

Example

Schema.define

[View source]
def db : DB::Connection #
  • @return [DB::Connection] the database connection

[View source]
def delete #

Creates a new delete query for the schema.

  • @return [Delete] the new delete query Example
delete = schema.delete

[View source]
def exec(sql : String) #

Executes a SQL statement.

  • @param sql [String] the SQL statement to execute

Example

schema.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")

[View source]
def gen : Expression::Generator #
  • @return [Expression::Generator] the expression generator

[View source]
def insert #

Creates a new insert query for the schema.

  • @return [Insert] the new insert query Example
insert = schema.insert

[View source]
def migrator #

Creates a new migrator for the schema.

  • @return [Migrator] the new migrator Example
migrator = schema.migrator

[View source]
def name : Symbol #
  • @return [Symbol] the name of the schema

[View source]
def query #

Creates a new query for the schema.

  • @return [Query] the new query

Example

query = schema.query

[View source]
def table(name : Symbol, as as_name = nil, &) #

Creates a new table in the schema.

  • @param name [Symbol] the name of the table
  • @param as_name [Symbol] the alias of the table
  • @yield [Table] the table being created
  • @return [Table] the created table Example
schema.create_table :users do
  primary :id, Int64, auto_increment: true
  column :name, String
  column :email, String
end

[View source]
def tables : Hash(Symbol, Table) #
  • @return [Hash(Symbol, Table)] the tables in the schema

[View source]
def update #

Creates a new update query for the schema.

  • @return [Update] the new update query Example
update = schema.update

[View source]
def uri : String #
  • @return [String] the URI of the database

[View source]
def version : String #
  • @return [String] the version of the schema

[View source]