public static void AddCheck(this ITransaction transaction, ObjectName tableName, SqlExpression expression, ConstraintDeferrability deferrability, string constraintName) { var tn = SystemSchema.CheckInfoTableName; var t = transaction.GetMutableTable(tn); int colCount = t.TableInfo.ColumnCount; try { byte[] binExp; using (var stream = new MemoryStream()) { using (var writer = new BinaryWriter(stream, Encoding.Unicode)) { SqlExpression.Serialize(expression, writer); writer.Flush(); binExp = stream.ToArray(); } } // Insert check constraint data. var uniqueId = transaction.NextTableId(tn); constraintName = MakeUniqueConstraintName(constraintName, uniqueId); var rd = t.NewRow(); rd.SetValue(0, uniqueId); rd.SetValue(1, constraintName); rd.SetValue(2, tableName.ParentName); rd.SetValue(3, tableName.Name); rd.SetValue(4, expression.ToString()); rd.SetValue(5, (short)deferrability); if (colCount > 6) { rd.SetValue(6, DataObject.Binary(new SqlBinary(binExp))); } t.AddRow(rd); } catch (ConstraintViolationException e) { // Constraint violation when inserting the data. Check the type and // wrap around an appropriate error message. if (e.ErrorCode == SqlModelErrorCodes.UniqueViolation) { // This means we gave a constraint name that's already being used. throw new InvalidOperationException("Check constraint name '" + constraintName + "' is already being used."); } throw; } }
public static void AddCheck(this ITransaction transaction, ObjectName tableName, SqlExpression expression, ConstraintDeferrability deferrability, string constraintName) { var tn = SystemSchema.CheckInfoTableName; var t = transaction.GetMutableTable(tn); int colCount = t.TableInfo.ColumnCount; try { byte[] binExp; using (var stream = new MemoryStream()) { using (var writer = new BinaryWriter(stream, Encoding.Unicode)) { SqlExpression.Serialize(expression, writer); writer.Flush(); binExp = stream.ToArray(); } } // Insert check constraint data. var uniqueId = transaction.NextTableId(tn); constraintName = MakeUniqueConstraintName(constraintName, uniqueId); var rd = t.NewRow(); rd.SetValue(0, uniqueId); rd.SetValue(1, constraintName); rd.SetValue(2, tableName.ParentName); rd.SetValue(3, tableName.Name); rd.SetValue(4, expression.ToString()); rd.SetValue(5, (short) deferrability); if (colCount > 6) { rd.SetValue(6, Field.Binary(new SqlBinary(binExp))); } t.AddRow(rd); } catch (UniqueKeyViolationException) { throw new InvalidOperationException("Check constraint name '" + constraintName + "' is already being used."); } }