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
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.AddColumn("user_id", DataType.Long, autoGenerate: true, notNull: true);
usersTable.AddColumn("username", DataType.VarChar, size: 100, notNull: true);
usersTable.AddColumn("email", DataType.VarChar, size: 200, notNull: true);
usersTable.AddColumn("created_at", DataType.DateTime);
Table ordersTable = schema.CreateTable("orders");
ordersTable.AddColumn("order_id", DataType.Long, autoGenerate: true, notNull: true);
ordersTable.AddColumn("user_id", DataType.Long, notNull: true);
ordersTable.AddColumn("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
manager.LoadSchema("my_application");
Schema schema = manager.GetSchema("my_application");
// Access tables
Table usersTable = schema["users"];
Table ordersTable = schema["orders"];
// Query data
Query query = new Query();
query.AddFromItem(usersTable);
query.AddSelectItem(usersTable["username"]);
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");
// 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.WriteSchemaToFile(schema, "schema.xml", SchemaFormat.Xml);
// Or save to JSON
manager.WriteSchemaToFile(schema, "schema.json", SchemaFormat.Json);
// Or save to YAML
manager.WriteSchemaToFile(schema, "schema.yaml", SchemaFormat.Yaml);
}
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 Manager.ReadSchemaFromFile
- Reverse EngineeringLoaded from existing database using Manager.LoadSchema
The Origin property indicates where the schema originated from (Memory, File, or Database).
Properties
- DatabaseName
Gets the name of the database that contains this schema.
- Name
Gets the name of the schema.
- Origin
Gets the origin type of this schema (Memory, File, or Database).
Methods
- CompareTo(Schema)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
- 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)
Indicates whether the current object is equal to another object of the same type.
- NewSchema(string)
Creates a new in-memory schema with the specified name.
- ToString()
Returns a string that represents the current object.