Skip to content

Commit

Permalink
syntax fixes, auto generated sql constraint name function changed
Browse files Browse the repository at this point in the history
  • Loading branch information
bberka committed Aug 18, 2023
1 parent b332395 commit 2cb1d55
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 72 deletions.
16 changes: 8 additions & 8 deletions EfCore.ConstraintsBuilder/ByteConstraintsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ internal ByteConstraintsBuilder(
_columnName = _builder.Metadata.GetProperty(propertyInfo.Name).GetColumnName();
}

public ByteConstraintsBuilder<TEntity> NumberInBetween(byte min, byte max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween"), min, max);
public ByteConstraintsBuilder<TEntity> NumberInBetween(byte min, byte max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween", min + "_" + max), min, max);
public ByteConstraintsBuilder<TEntity> NumberInBetween(string uniqueConstraintName, byte min, byte max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} AND \"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} AND [{_columnName}] <= {max}"));
return this;
}
public ByteConstraintsBuilder<TEntity> NumberMin(byte min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin"), min);
public ByteConstraintsBuilder<TEntity> NumberMin(byte min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin", min), min);
public ByteConstraintsBuilder<TEntity> NumberMin(string uniqueConstraintName, byte min) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} "));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} "));
return this;
}


public ByteConstraintsBuilder<TEntity> NumberMax(byte max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax"), max);
public ByteConstraintsBuilder<TEntity> NumberMax(byte max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax", max), max);
public ByteConstraintsBuilder<TEntity> NumberMax(string uniqueConstraintName, byte max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= {max}"));
return this;
}

public ByteConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<byte> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf"), acceptedValues);
public ByteConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<byte> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf", acceptedValues), acceptedValues);
public ByteConstraintsBuilder<TEntity> EqualOneOf(string uniqueConstraintName, IEnumerable<byte> acceptedValues) {
var values = string.Join(',', acceptedValues);
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" IN ({values})"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] IN ({values})"));
return this;
}
}
2 changes: 1 addition & 1 deletion EfCore.ConstraintsBuilder/EfCore.ConstraintsBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.1.3</Version>
<Version>0.1.4</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>bberka</Authors>
<Company>bberka</Company>
Expand Down
16 changes: 8 additions & 8 deletions EfCore.ConstraintsBuilder/IntConstraintsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ internal IntConstraintsBuilder(
_tableName = _builder.Metadata.GetTableName() ?? typeof(TEntity).Name;
_columnName = _builder.Metadata.GetProperty(propertyInfo.Name).GetColumnName();
}
public IntConstraintsBuilder<TEntity> NumberInBetween(int min, int max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween"), min, max);
public IntConstraintsBuilder<TEntity> NumberInBetween(int min, int max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween", min + "_" + max), min, max);
public IntConstraintsBuilder<TEntity> NumberInBetween(string uniqueConstraintName, int min, int max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} AND \"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} AND [{_columnName}] <= {max}"));
return this;
}
public IntConstraintsBuilder<TEntity> NumberMin(int min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin"), min);
public IntConstraintsBuilder<TEntity> NumberMin(int min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin", min), min);
public IntConstraintsBuilder<TEntity> NumberMin(string uniqueConstraintName, int min) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} "));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} "));
return this;
}


public IntConstraintsBuilder<TEntity> NumberMax(int max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax"), max);
public IntConstraintsBuilder<TEntity> NumberMax(int max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax", max), max);
public IntConstraintsBuilder<TEntity> NumberMax(string uniqueConstraintName, int max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= {max}"));
return this;
}

public IntConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<int> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf"), acceptedValues);
public IntConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<int> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf", acceptedValues), acceptedValues);
public IntConstraintsBuilder<TEntity> EqualOneOf(string uniqueConstraintName, IEnumerable<int> acceptedValues) {
var values = string.Join(',', acceptedValues);
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" IN ({values})"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] IN ({values})"));
return this;
}
}
6 changes: 3 additions & 3 deletions EfCore.ConstraintsBuilder/InternalTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public static class InternalTool
public const string CreditCardRegex = @"^(\d{4}[- ]){3}\d{4}|\d{16}$";


public static string CreateUniqueConstraintName(string tableName, string columnName, string suffix, bool addRandomSuffix = true) {
public static string CreateUniqueConstraintName(string tableName, string columnName, string suffix, object? valueForCheck = null) {
var sb = new StringBuilder();
sb.Append("CK_");
sb.Append(tableName);
sb.Append('_');
sb.Append(columnName);
sb.Append('_');
sb.Append(suffix);
if (!addRandomSuffix) return sb.ToString();
sb.Append('_');
sb.Append(Guid.NewGuid().ToString().Replace("-", ""));
var num = valueForCheck?.GetHashCode() ?? 0;
sb.Append(num);
return sb.ToString();
}

Expand Down
16 changes: 8 additions & 8 deletions EfCore.ConstraintsBuilder/LongConstraintsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ internal LongConstraintsBuilder(
_columnName = _builder.Metadata.GetProperty(propertyInfo.Name).GetColumnName();
}

public LongConstraintsBuilder<TEntity> NumberInBetween(long min, long max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween"), min, max);
public LongConstraintsBuilder<TEntity> NumberInBetween(long min, long max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween", min + "_" + max), min, max);
public LongConstraintsBuilder<TEntity> NumberInBetween(string uniqueConstraintName, long min, long max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} AND \"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} AND [{_columnName}] <= {max}"));
return this;
}
public LongConstraintsBuilder<TEntity> NumberMin(long min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin"), min);
public LongConstraintsBuilder<TEntity> NumberMin(long min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin", min), min);
public LongConstraintsBuilder<TEntity> NumberMin(string uniqueConstraintName, long min) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} "));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} "));
return this;
}


public LongConstraintsBuilder<TEntity> NumberMax(long max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax"), max);
public LongConstraintsBuilder<TEntity> NumberMax(long max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax", max), max);
public LongConstraintsBuilder<TEntity> NumberMax(string uniqueConstraintName, long max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= {max}"));
return this;
}

public LongConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<long> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf"), acceptedValues);
public LongConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<long> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf", acceptedValues), acceptedValues);
public LongConstraintsBuilder<TEntity> EqualOneOf(string uniqueConstraintName, IEnumerable<long> acceptedValues) {
var values = string.Join(',', acceptedValues);
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" IN ({values})"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] IN ({values})"));
return this;
}
}
16 changes: 8 additions & 8 deletions EfCore.ConstraintsBuilder/ShortConstraintsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ internal ShortConstraintsBuilder(
_columnName = _builder.Metadata.GetProperty(propertyInfo.Name).GetColumnName();
}

public ShortConstraintsBuilder<TEntity> NumberInBetween(short min, short max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween"), min, max);
public ShortConstraintsBuilder<TEntity> NumberInBetween(short min, short max) => NumberInBetween(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberInBetween", min + "_" + max), min, max);
public ShortConstraintsBuilder<TEntity> NumberInBetween(string uniqueConstraintName, short min, short max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} AND \"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} AND [{_columnName}] <= {max}"));
return this;
}
public ShortConstraintsBuilder<TEntity> NumberMin(short min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin"), min);
public ShortConstraintsBuilder<TEntity> NumberMin(short min) => NumberMin(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMin" ,min), min);
public ShortConstraintsBuilder<TEntity> NumberMin(string uniqueConstraintName, short min) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" >= {min} "));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= {min} "));
return this;
}


public ShortConstraintsBuilder<TEntity> NumberMax(short max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax"), max);
public ShortConstraintsBuilder<TEntity> NumberMax(short max) => NumberMax(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "NumberMax", max), max);
public ShortConstraintsBuilder<TEntity> NumberMax(string uniqueConstraintName, short max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" <= {max}"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= {max}"));
return this;
}

public ShortConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<short> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf"), acceptedValues);
public ShortConstraintsBuilder<TEntity> EqualOneOf(IEnumerable<short> acceptedValues) => EqualOneOf(InternalTool.CreateUniqueConstraintName(_tableName,_columnName, "EqualOneOf", acceptedValues), acceptedValues);
public ShortConstraintsBuilder<TEntity> EqualOneOf(string uniqueConstraintName, IEnumerable<short> acceptedValues) {
var values = string.Join(',', acceptedValues);
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"\"{_columnName}\" IN ({values})"));
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] IN ({values})"));
return this;
}
}
Loading

0 comments on commit 2cb1d55

Please sign in to comment.