Class CheckOperator
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a comparison operator used in CHECK constraints to validate column values.
public class CheckOperator : ICheckItem
- Inheritance
-
CheckOperator
- Implements
Remarks
CheckOperator is used within CHECK constraints to define validation rules for table columns. These operators compare column values against literal values to ensure data integrity. Supported comparison operators include:
- Equals (=)Value must equal the specified value
- NotEquals (!=, <>)Value must not equal the specified value
- GreaterThan (>)Value must be greater than the specified value
- LessThan (<)Value must be less than the specified value
- GreaterThanOrEquals (>=)Value must be greater than or equal to the specified value
- LessThanOrEquals (<=)Value must be less than or equal to the specified value
Examples
Simple CHECK constraint with greater than:
// Ensure age is at least 18
Table usersTable = schema.CreateTable("users");
Column ageColumn = usersTable.AddColumn("age", DataType.Integer);
CheckConstraint ageCheck = new CheckConstraint("age_minimum");
ageCheck.Add(ageColumn);
ageCheck.Add(new CheckOperator(CheckOperatorType.GreaterThanOrEquals));
ageCheck.Add(new Literal<int>(18));
usersTable.AddCheckConstraint(ageCheck);
// SQL: ALTER TABLE users ADD CONSTRAINT age_minimum CHECK (age >= 18)
Range validation with multiple operators:
// Ensure age is between 18 and 120
CheckConstraint ageRange = new CheckConstraint("age_range");
ageRange.Add(ageColumn);
ageRange.Add(new CheckOperator(CheckOperatorType.GreaterThanOrEquals));
ageRange.Add(new Literal<int>(18));
ageRange.Add(new BooleanItem(BooleanType.And));
ageRange.Add(ageColumn);
ageRange.Add(new CheckOperator(CheckOperatorType.LessThanOrEquals));
ageRange.Add(new Literal<int>(120));
usersTable.AddCheckConstraint(ageRange);
// SQL: CHECK (age >= 18 AND age <= 120)
Positive value validation:
// Ensure price is greater than zero
Table productsTable = schema.CreateTable("products");
Column priceColumn = productsTable.AddColumn("price", DataType.Decimal, precision: 10, scale: 2);
CheckConstraint positivePrice = new CheckConstraint("positive_price");
positivePrice.Add(priceColumn);
positivePrice.Add(new CheckOperator(CheckOperatorType.GreaterThan));
positivePrice.Add(new Literal<decimal>(0m));
productsTable.AddCheckConstraint(positivePrice);
// SQL: CHECK (price > 0)
Not equals validation:
// Ensure status is not 'deleted'
Column statusColumn = usersTable.AddColumn("status", DataType.VarChar, size: 50);
CheckConstraint notDeleted = new CheckConstraint("not_deleted");
notDeleted.Add(statusColumn);
notDeleted.Add(new CheckOperator(CheckOperatorType.NotEquals));
notDeleted.Add(new Literal<string>("deleted"));
usersTable.AddCheckConstraint(notDeleted);
// SQL: CHECK (status != 'deleted')
Constructors
- CheckOperator(CheckOperatorType)
Creates a new check operator for use in CHECK constraints.
Properties
- CheckConstraintType
Gets the check constraint type (always CheckConstraintType.Operator).
- CheckOperatorType
Gets or sets the type of comparison operator used in the CHECK constraint.
Methods
- IsEqual(ICheckItem)
Determines whether this check operator is equal to another check item.
- ToString()
Returns a string representation of the check operator.