Skip to content

Commit

Permalink
Refactor: Mark Exists and NotExists methods as obsolete and add updat…
Browse files Browse the repository at this point in the history
…ed versions

- Marked `Exists(this SelectQuery query, FluentTable validationTable, IEnumerable<string> keyColumnNames)` as obsolete.
- Marked `NotExists(this SelectQuery query, FluentTable validationTable, IEnumerable<string> keyColumnNames)` as obsolete.
- Added updated versions of the `Exists` and `NotExists` methods that take `IEnumerable<string> keyColumnNames` and `FluentTable validationTable`.
- Implemented improved query logic with support for checking key columns and validation tables.
  • Loading branch information
mk3008 committed Oct 13, 2024
1 parent c6fc74b commit 76319d6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 11 deletions.
2 changes: 1 addition & 1 deletion demo/DatasourceCompare/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static CreateTableQuery GenerateDifferentCreateTableQuery(string tableName,
var request = FluentTable.Create("select sale_id from request", "req", "r");

var query = reverseQuery.UnionAll(currentQuery)
.Exists(request, ["sale_id"])
.Exists(["sale_id"], request)
.ToCreateTableQuery(tableName, true)
.Comment(nameof(GenerateDifferentCreateTableQuery));

Expand Down
106 changes: 100 additions & 6 deletions src/Carbunql/Fluent/SelectQueryWhereExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,24 @@ public static SelectQuery BetweenInclusive(this SelectQuery query, string tableN
return query;
}

public static SelectQuery Exists(this SelectQuery query, FluentTable table, IEnumerable<string> keyColumnNames)
[Obsolete("use 'Exists(this SelectQuery query, IEnumerable<string> keyColumnNames, FluentTable validationTable)'")]
public static SelectQuery Exists(this SelectQuery query, FluentTable validationTable, IEnumerable<string> keyColumnNames)
{
if (table.IsCommonTable)
return query.Exists(keyColumnNames, validationTable);
}

/// <summary>
/// Applies an Exists query to the specified validation table based on the key column names.
/// </summary>
/// <param name="query">The SelectQuery object to operate on.</param>
/// <param name="keyColumnNames">A list of key column names used in the query.</param>
/// <param name="validationTable">The FluentTable object used for validation.</param>
/// <returns>Returns the updated SelectQuery object.</returns>
public static SelectQuery Exists(this SelectQuery query, IEnumerable<string> keyColumnNames, FluentTable validationTable)
{
if (validationTable.IsCommonTable)
{
query.With(table);
query.With(validationTable);
}

query.GetQuerySources()
Expand All @@ -491,8 +504,42 @@ public static SelectQuery Exists(this SelectQuery query, FluentTable table, IEnu
{
qs.Query.Where(() =>
{
var sq = new SelectQuery().From(table);
keyColumnNames.ForEach(keyColumn => sq.Where($"{table.Alias}.{keyColumn} = {qs.Alias}.{keyColumn}"));
var sq = new SelectQuery().From(validationTable);
keyColumnNames.ForEach(keyColumn => sq.Where($"{validationTable.Alias}.{keyColumn} = {qs.Alias}.{keyColumn}"));
return sq.ToExists();
});
});

return query;
}

/// <summary>
/// Applies an Exists query to the specified validation table based on the given table and key column names.
/// </summary>
/// <param name="query">The SelectQuery object to operate on.</param>
/// <param name="table">The FluentTable object to operate on.</param>
/// <param name="keyColumnNames">A list of key column names used in the query.</param>
/// <param name="validationTable">The FluentTable object used for validation.</param>
/// <returns>Returns the updated SelectQuery object.</returns>
public static SelectQuery Exists(this SelectQuery query, FluentTable table, IEnumerable<string> keyColumnNames, FluentTable validationTable)
{
if (validationTable.IsCommonTable)
{
query.With(validationTable);
}

query.GetQuerySources()
.Where(x => x.HasTable(table.Alias, true))
.EnsureAny($"table:{table.Alias}")
.Where(x => keyColumnNames.All(keyColumn => x.ColumnNames.Contains(keyColumn)))
.EnsureAny($"columns:{string.Join(",", keyColumnNames)}")
.GetRootsByQuery()
.ForEach(qs =>
{
qs.Query.Where(() =>
{
var sq = new SelectQuery().From(validationTable);
keyColumnNames.ForEach(keyColumn => sq.Where($"{validationTable.Alias}.{keyColumn} = {qs.Alias}.{keyColumn}"));
return sq.ToExists();
});
});
Expand All @@ -512,7 +559,20 @@ public static SelectQuery Exists(this SelectQuery query, string tableName, IEnum
return query;
}

public static SelectQuery NotExists(this SelectQuery query, FluentTable table, IEnumerable<string> keyColumnNames)
[Obsolete("use 'NotExists(this SelectQuery query, IEnumerable<string> keyColumnNames, FluentTable validationTable)'")]
public static SelectQuery NotExists(this SelectQuery query, FluentTable validationTable, IEnumerable<string> keyColumnNames)
{
return query.NotExists(keyColumnNames, validationTable);
}

/// <summary>
/// Applies a NotExists query to the specified validation table based on the key column names.
/// </summary>
/// <param name="query">The SelectQuery object to operate on.</param>
/// <param name="keyColumnNames">A list of key column names used in the query.</param>
/// <param name="validationTable">The FluentTable object used for validation.</param>
/// <returns>Returns the updated SelectQuery object.</returns>
public static SelectQuery NotExists(this SelectQuery query, IEnumerable<string> keyColumnNames, FluentTable validationTable)
{
if (table.IsCommonTable)

Check failure on line 577 in src/Carbunql/Fluent/SelectQueryWhereExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

The name 'table' does not exist in the current context
{
Expand All @@ -536,6 +596,40 @@ public static SelectQuery NotExists(this SelectQuery query, FluentTable table, I
return query;
}

/// <summary>
/// Applies a NotExists query to the specified validation table based on the given table and key column names.
/// </summary>
/// <param name="query">The SelectQuery object to operate on.</param>
/// <param name="table">The FluentTable object to operate on.</param>
/// <param name="keyColumnNames">A list of key column names used in the query.</param>
/// <param name="validationTable">The FluentTable object used for validation.</param>
/// <returns>Returns the updated SelectQuery object.</returns>
public static SelectQuery NotExists(this SelectQuery query, FluentTable table, IEnumerable<string> keyColumnNames, FluentTable validationTable)
{
if (validationTable.IsCommonTable)
{
query.With(validationTable);
}

query.GetQuerySources()
.Where(x => x.HasTable(table.Alias, true))
.EnsureAny($"table:{table.Alias}")
.Where(x => keyColumnNames.All(keyColumn => x.ColumnNames.Contains(keyColumn)))
.EnsureAny($"columns:{string.Join(",", keyColumnNames)}")
.GetRootsByQuery()
.ForEach(qs =>
{
qs.Query.Where(() =>
{
var sq = new SelectQuery().From(validationTable);
keyColumnNames.ForEach(keyColumn => sq.Where($"{validationTable.Alias}.{keyColumn} = {qs.Alias}.{keyColumn}"));
return sq.ToNotExists();
});
});

return query;
}

public static SelectQuery NotExists(this SelectQuery query, IEnumerable<string> keyColumnNames, string validationTableName)
{
query.AddNotExists(keyColumnNames, validationTableName);
Expand Down
12 changes: 12 additions & 0 deletions src/Carbunql/IQuerySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,17 @@ public static HashSet<int> GetReferenceIndexes(this IQuerySource source)
var lst = q.ToHashSet();
return lst;
}

public static bool HasTable(this IQuerySource source, string tableName, bool isAliasIncluded)
{
if (isAliasIncluded && source.Alias.IsEqualNoCase(tableName))

Check failure on line 131 in src/Carbunql/IQuerySource.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

'string' does not contain a definition for 'IsEqualNoCase' and no accessible extension method 'IsEqualNoCase' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
{
return true;
}
else
{
return source.GetTableFullName().IsEqualNoCase(tableName);

Check failure on line 137 in src/Carbunql/IQuerySource.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

'string' does not contain a definition for 'IsEqualNoCase' and no accessible extension method 'IsEqualNoCase' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
}
}
}

8 changes: 4 additions & 4 deletions src/Carbunql/SelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ public SelectQuery AddWhere(Func<string> adder)
public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySource, string> adder, bool isAliasIncluded = false)
{
GetQuerySources()
.Where(x => HasTable(x, tableName, true))
.Where(x => x.HasTable(tableName, true))
.EnsureAny($"table:{tableName}")
.Where(x => HasColumn(x, columnName, isAliasIncluded))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{tableName}, column:{columnName}")
Expand All @@ -1164,7 +1164,7 @@ public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySour
public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySource, string, string> adder, bool isAliasIncluded = false)
{
GetQuerySources()
.Where(x => HasTable(x, tableName, true))
.Where(x => x.HasTable(tableName, true))
.EnsureAny($"table:{tableName}")
.Where(x => HasColumn(x, columnName, isAliasIncluded))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{tableName}, column:{columnName}")
Expand Down Expand Up @@ -1215,7 +1215,7 @@ public SelectQuery AddExists(IEnumerable<string> keyColumnNames, string validati
public SelectQuery AddExists(string sourceTableName, IEnumerable<string> keyColumnNames, string validationTableName, Action<IQuerySource>? action = null)
{
GetQuerySources()
.Where(x => HasTable(x, sourceTableName, true))
.Where(x => x.HasTable(sourceTableName, true))
.EnsureAny($"table:{sourceTableName}")
.Where(x => keyColumnNames.All(keyColumn => x.ColumnNames.Contains(keyColumn)))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{sourceTableName}, columns:{string.Join(",", keyColumnNames)}")
Expand Down Expand Up @@ -1273,7 +1273,7 @@ public SelectQuery AddNotExists(IEnumerable<string> keyColumnNames, string valid
public SelectQuery AddNotExists(string sourceTableName, IEnumerable<string> keyColumnNames, string validationTableName, Action<IQuerySource>? action = null)
{
GetQuerySources()
.Where(x => HasTable(x, sourceTableName, true))
.Where(x => x.HasTable(sourceTableName, true))
.EnsureAny($"table:{sourceTableName}")
.Where(x => keyColumnNames.All(keyColumn => x.ColumnNames.Contains(keyColumn)))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{sourceTableName}, columns:{string.Join(",", keyColumnNames)}")
Expand Down

0 comments on commit 76319d6

Please sign in to comment.