Table of Contents

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, ISnippet

Type Parameters

T

The C# type of the value.

Inheritance
Snippet<T>
Implements

Examples

Using Snippet in a CHECK constraint:

Table productsTable = schema.CreateTable("products");
productsTable.AddColumn("id", DataType.Integer);
productsTable.AddColumn("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.AddColumn("id", DataType.Integer);
ordersTable.AddColumn("status", DataType.VarChar, size: 50);
ordersTable.AddColumn("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 value
Query viewQuery = new Query();
viewQuery.AddFromItem(ordersTable);
viewQuery.AddSelectItem(ordersTable["id"]);
viewQuery.AddSelectItem(ordersTable["total"]);

// Add expression with literal discount rate
Expression discountedTotal = new Expression("discounted_total");
discountedTotal.Add(ordersTable["total"]);
discountedTotal.Add(new ArithmeticOperator(ArithmeticType.Multiply));
discountedTotal.Add(new Snippet<decimal>(0.9m));  // 10% discount

viewQuery.AddSelectItem(discountedTotal);

View discountView = new View("discounted_orders", viewQuery);
schema.AddView(discountView);

// 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.AddColumn("id", DataType.Integer);
employeesTable.AddColumn("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'))

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.

Constructors

Snippet(T)

Properties

CheckConstraintType

Gets the type of check constraint element.

Value

Methods

IsEqual(ICheckItem)

Determines whether this check item is equal to another check item.

ToString()

Returns a string that represents the current object.