class Cql::Schema
- Cql::Schema
- Reference
- Object
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.crConstant Summary
-
Log =
::Log.for(self)
Constructors
-
.new(name : Symbol, uri : String, adapter : Adapter = Adapter::Sqlite, version : String = "1.0")
Initializes a new schema.
Class Method Summary
-
.define(name : Symbol, uri : String, adapter : Adapter = Adapter::Sqlite, version : String = "1.0", &)
Builds a new schema.
Macro Summary
Instance Method Summary
-
#adapter : Adapter
- @return [Adapter] the database adapter (default:
Adapter::Sqlite
)
- @return [Adapter] the database adapter (default:
-
#alter(table_name : Symbol, &)
Alter a table in the schema.
-
#build
Builds the schema.
-
#db : DB::Connection
- @return [DB::Connection] the database connection
-
#delete
Creates a new delete query for the schema.
-
#exec(sql : String)
Executes a SQL statement.
-
#gen : Expression::Generator
- @return [Expression::Generator] the expression generator
-
#insert
Creates a new insert query for the schema.
-
#migrator
Creates a new migrator for the schema.
-
#name : Symbol
- @return [Symbol] the name of the schema
-
#query
Creates a new query for the schema.
-
#table(name : Symbol, as as_name = nil, &)
Creates a new table in the schema.
-
#tables : Hash(Symbol, Table)
- @return [Hash(Symbol, Table)] the tables in the schema
-
#update
Creates a new update query for the schema.
-
#uri : String
- @return [String] the URI of the database
-
#version : String
- @return [String] the version of the schema
Constructor Detail
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")
Class Method Detail
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
Macro Detail
Instance Method Detail
- @return [Adapter] the database adapter (default:
Adapter::Sqlite
)
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
Creates a new delete query for the schema.
- @return [Delete] the new delete query Example
delete = schema.delete
Executes a SQL statement.
- @param sql [String] the SQL statement to execute
Example
schema.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
Creates a new insert query for the schema.
- @return [Insert] the new insert query Example
insert = schema.insert
Creates a new migrator for the schema.
- @return [Migrator] the new migrator Example
migrator = schema.migrator
Creates a new query for the schema.
- @return [Query] the new query
Example
query = schema.query
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
- @return [Hash(Symbol, Table)] the tables in the schema
Creates a new update query for the schema.
- @return [Update] the new update query Example
update = schema.update