Class Snippet<T>
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a raw value embedded directly in SQL without parameterization, used primarily in views and CHECK constraints.
public class Snippet<T> : IElement, ICheckItem
Type Parameters
TThe C# type of the value.
- Inheritance
-
Snippet<T>
- Implements
Remarks
Snippet is similar to Literal<T> but is used in contexts where parameterized queries are not possible, such as:
- View definitions: Views require literal values embedded in their SQL definition
- CHECK constraints: Constraint definitions cannot use parameters
- DEFAULT constraints: Default value expressions must be literals
While Literal<T> creates parameterized queries for safety, Snippet embeds values directly into SQL text. This is necessary for DDL operations where parameters cannot be used.
WARNING: Snippet values are embedded directly into SQL without parameterization. Use only with trusted values. Never use Snippet with user-provided input as it could enable SQL injection.
Examples
Using Snippet in a CHECK constraint:
Table productsTable = schema.CreateTable("products");
productsTable.CreateColumn("id", DataType.Integer);
productsTable.CreateColumn("price", DataType.Decimal, precision: 10, scale: 2);
// Create CHECK constraint: price must be positive
CheckConstraint priceCheck = new CheckConstraint("chk_positive_price");
priceCheck.AddColumn(productsTable["price"]);
priceCheck.Add(new CheckOperator(CheckOperatorType.GreaterThan));
priceCheck.Add(new Snippet<decimal>(0.0m)); // Literal 0 in SQL
productsTable.AddCheckConstraint(priceCheck);
// SQL: ALTER TABLE products ADD CONSTRAINT chk_positive_price CHECK (price > 0.0)
Using Snippet in a DEFAULT constraint:
Table ordersTable = schema.CreateTable("orders");
ordersTable.CreateColumn("id", DataType.Integer);
ordersTable.CreateColumn("status", DataType.VarChar, size: 50);
ordersTable.CreateColumn("is_paid", DataType.Boolean);
// Set default value for is_paid
DefaultConstraint paidDefault = new DefaultConstraint("df_is_paid");
paidDefault.Add(new Snippet<bool>(false)); // Default to false
ordersTable["is_paid"].AddDefaultConstraint(paidDefault);
// SQL: ALTER TABLE orders ADD CONSTRAINT df_is_paid DEFAULT 0 FOR is_paid
Using Snippet in a view definition:
// Create view with literal discount rate
Expression discountedTotal = new Expression("discounted_total",
ordersTable["total"],
new ArithmeticOperator(ArithmeticType.Multiply),
new Snippet<decimal>(0.9m) // 10% discount
);
Query viewQuery = new Query()
.Select([ ordersTable["id"], ordersTable["total"], discountedTotal ])
.From(ordersTable);
schema.CreateView("discounted_orders", ViewType.Code, viewQuery);
// SQL: CREATE VIEW discounted_orders AS
// SELECT id, total, total * 0.9 AS discounted_total FROM orders
CHECK constraint with range validation:
Table employeesTable = schema.CreateTable("employees");
employeesTable.CreateColumn("id", DataType.Integer);
employeesTable.CreateColumn("age", DataType.Integer);
// Create CHECK constraint: age between 18 and 65
CheckConstraint ageCheck = new CheckConstraint("chk_valid_age");
ageCheck.AddColumn(employeesTable["age"]);
ageCheck.Add(new CheckOperator(CheckOperatorType.GreaterThanOrEqualTo));
ageCheck.Add(new Snippet<int>(18));
ageCheck.Add(new BooleanItem(BooleanType.And));
ageCheck.AddColumn(employeesTable["age"]);
ageCheck.Add(new CheckOperator(CheckOperatorType.LessThanOrEqualTo));
ageCheck.Add(new Snippet<int>(65));
employeesTable.AddCheckConstraint(ageCheck);
// SQL: ALTER TABLE employees ADD CONSTRAINT chk_valid_age
// CHECK (age >= 18 AND age <= 65)
Snippet with string values:
// CHECK constraint with string comparison
CheckConstraint statusCheck = new CheckConstraint("chk_valid_status");
statusCheck.AddColumn(ordersTable["status"]);
statusCheck.Add(new CheckOperator(CheckOperatorType.In));
statusCheck.Add(new Snippet<string>("'pending', 'processing', 'shipped', 'delivered'"));
ordersTable.AddCheckConstraint(statusCheck);
// SQL: ALTER TABLE orders ADD CONSTRAINT chk_valid_status
// CHECK (status IN ('pending', 'processing', 'shipped', 'delivered'))
Constructors
- Snippet(T)
Creates a new Snippet<T> with the specified literal value.
Properties
- CheckConstraintType
Gets the type of check constraint element.
- Value
Gets the value that will be embedded directly into the SQL statement.
Methods
- IsEqual(ICheckItem)
Determines whether this snippet is equal to another check item by comparing their values.
- ToString()
Returns a string that represents the current object.