Class Schema
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a database schema containing tables and views.
public class Schema : IEquatable<Schema>, IComparable<Schema>
- Inheritance
-
Schema
- Implements
Remarks
A schema acts as a container for database objects such as tables and views. Schemas provide logical grouping of related database objects and namespace isolation.
Schemas can be created in three ways:
- ProgrammaticCreated in memory using NewSchema(string)
- DeclarativeLoaded from XML/JSON/YAML files using ReadSchemaFromFile(string, ConfigType)
- Reverse EngineeringLoaded from existing database using LoadSchema(string)
The Origin property indicates where the schema originated from (Memory, File, or Database).
Examples
Creating a schema programmatically:
// Create a new in-memory schema
Schema schema = Schema.NewSchema("my_application");
// Add tables to the schema
Table usersTable = schema.CreateTable("users");
usersTable.CreateColumn("user_id", DataType.Long, autoGenerate: true, notNull: true);
usersTable.CreateColumn("username", DataType.VarChar, size: 100, notNull: true);
usersTable.CreateColumn("email", DataType.VarChar, size: 200, notNull: true);
usersTable.CreateColumn("created_at", DataType.DateTime);
Table ordersTable = schema.CreateTable("orders");
ordersTable.CreateColumn("order_id", DataType.Long, autoGenerate: true, notNull: true);
ordersTable.CreateColumn("user_id", DataType.Long, notNull: true);
ordersTable.CreateColumn("total", DataType.Decimal, precision: 10, scale: 2);
// Add foreign key relationship
ordersTable.AddForeignKey("fk_orders_users", ordersTable["user_id"], usersTable["user_id"]);
// Deploy the schema to a database
using (Manager manager = new Manager(connection))
{
manager.BuildSchema(schema);
}
Loading a schema from an existing database:
using (Manager manager = new Manager(connection))
{
// Load schema from database — LoadSchema returns the Schema directly
Schema schema = manager.LoadSchema("my_application");
// Access tables
Table usersTable = schema["users"];
Table ordersTable = schema["orders"];
// Query data
var query = new Query()
.Select(usersTable["username"])
.From(usersTable);
using (ResultSet results = manager.Retrieve(query))
{
foreach (Result row in results)
{
Console.WriteLine(row.GetFieldString("username"));
}
}
}
Loading a schema from a file:
using (Manager manager = new Manager(connection))
{
// Load schema from XML/JSON/YAML file
Schema schema = manager.ReadSchemaFromFile("schema.xml", ConfigType.Xml);
// Deploy to database
manager.BuildSchema(schema);
}
Saving a schema to a file:
// Create schema programmatically
Schema schema = Schema.NewSchema("my_application");
// ... add tables and columns ...
using (Manager manager = new Manager())
{
// Save to XML file
manager.WriteSchema(schema, "schema.xml", ConfigType.Xml);
// Or save to JSON
manager.WriteSchema(schema, "schema.json", ConfigType.Json);
// Or save to YAML
manager.WriteSchema(schema, "schema.yaml", ConfigType.Yaml);
}
Properties
- DatabaseName
Gets the name of the database that contains this schema.
- this[string]
Gets the table with the specified name from this schema.
- Name
Gets the name of the schema.
- Origin
Gets the origin type of this schema (Memory, File, or Database).
- TableNames
Gets the ordered collection of table names in this schema.
- Tables
Gets the ordered collection of tables in this schema.
- ViewNames
Gets the ordered collection of view names in this schema.
- Views
Gets the ordered collection of views in this schema.
Methods
- CompareTo(Schema)
Compares this schema to another by name for ordering purposes.
- CreateTable(string)
Creates a new table in this schema.
- CreateView(string, ViewType)
Creates a new view in this schema.
- CreateView(string, ViewType, string)
Creates a new view in this schema with a SQL definition.
- CreateView(string, ViewType, Query)
Creates a new view in this schema with a Query definition.
- Equals(Schema)
Performs a deep structural comparison of two schemas, comparing all tables and views by name and definition.
- GetTable(string)
Returns the table with the specified name from this schema.
- GetView(string)
Returns the view with the specified name from this schema.
- HasColumn(string)
Determines whether a table with the specified name exists in this schema.
- NewSchema(string)
Creates a new in-memory schema with the specified name.
- ToString()
Returns a string that represents the current object.