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, Int32, 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, version : String = "1.0")
Initializes a new schema.
Class Method Summary
-
.define(name : Symbol, uri : String, adapter : Adapter, 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.
-
#delete
Creates a new delete query for the schema.
- #dialect
-
#dump_structure(file = "db/structure.sql")
TODO For each adapter implement dumping the database structure tp a file called
structure.sql
. -
#exec(sql : String)
Executes a SQL statement.
- #exec_query(&)
-
#finalize
Ensures the database connection is closed when the schema is garbage collected
-
#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 : Symbol | Nil = nil, &)
Creates a new table in the schema.
- #table(name : Symbol, as as_name : String | Nil = nil, &)
-
#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, Int32, 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
TODO For each adapter implement dumping the database structure tp a file
called structure.sql
. This file should contain the SQL statements to create the
tables in the schema. The file should be saved in the ./src/db/ directory.
Example
schema.dump_structure
Example
schema.dump_structure("db/structure.sql")
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, Int32, 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