Skip to content

Commit

Permalink
Merge pull request #478 from mk3008/474-simplify-the-description-of-e…
Browse files Browse the repository at this point in the history
…xtraction-conditions

Added AddWhere method
  • Loading branch information
mk3008 authored Jul 19, 2024
2 parents 172d174 + 0ca321c commit 402f590
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 62 deletions.
66 changes: 16 additions & 50 deletions demo/QuerySource/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,38 @@
Console.WriteLine(query.ToText());
Console.WriteLine(";");


Console.WriteLine("--sub----------");
query = new SelectQuery(sql).ToSubQuery("q");
Console.WriteLine(query.ToText());
Console.WriteLine(";");

try
{
Console.WriteLine("--snapshot----------");
var crateTablequery = new SelectQuery(sql)
.ToCTEQuery("datasource", "ds")
.OverrideColumn(dateColumn, (source, item) =>
.OverrideSelect(dateColumn, (source, value) =>
{
source.Query.AddComment($"Inject a lower limit for {dateColumn}");
return $"greatest({item.Value.ToOneLineText()}, '{lowerLimit}'::date)";
return $"greatest({value}, {source.Query.AddParameter(":lower_limit", lowerLimit)})";
})
.InjectNotJournaledFilter(keyTable, keyColumn)
.AddColumn("nextval('sale_journal_id_seq')", "sale_journal_id")
.AddSelect("nextval('sale_journal_id_seq')", "sale_journal_id")
.ToCTEQuery("final", "f")
.ToCreateTableQuery("tmp", isTemporary: true);
Console.WriteLine(crateTablequery.ToText());
Console.WriteLine(";");

Console.WriteLine("--validate----------");
var insertQuery = new SelectQuery(sql)
.ToDiffQuery(journalTable, keyColumn, valueColumn, ignoreColumn)
.InjectUnderLimitProcessing(dateColumn, lowerLimit)
.InjectFilter(keyColumn, 1)
.ToCTEQuery("final", "f")
.ToInsertQuery("sale_journals");
.ToDiffQuery(journalTable, keyColumn, valueColumn, ignoreColumn)
.OverrideSelect(dateColumn, (source, value) =>
{
source.Query.AddComment($"Inject a lower limit for {dateColumn}");
return $"greatest({value}, {source.Query.AddParameter(":lower_limit", lowerLimit)})";
})
.AddWhere(keyColumn, source =>
{
source.Query.AddComment($"Inject filter. {keyColumn}");
return $"{source.Alias}.{keyColumn} = {source.Query.AddParameter(":sale_id", 1)}";
})
.ToCTEQuery("final", "f")
.ToInsertQuery("sale_journals");
Console.WriteLine(insertQuery.ToText());
Console.WriteLine(";");
}
Expand All @@ -63,26 +65,6 @@

public static class SelectQueryExtension
{
public static SelectQuery InjectUnderLimitProcessing(this SelectQuery query, string dateColumnName, DateTime lowerLimitDate)
{
var lowerLimit = lowerLimitDate.ToString("yyyy-MM-dd");

query.GetQuerySources()
.Where(x => x.Query.GetSelectableItems().Where(x => x.Alias == dateColumnName).Any())
.GetRootsBySource()
.EnsureAny()
.ForEach(x =>
{
x.Query.AddComment($"Inject a lower limit for {dateColumnName}");
var si = x.Query.GetSelectableItems().Where(x => x.Alias == dateColumnName).First();

//override
si.Value = ValueParser.Parse($"greatest({si.Value.ToOneLineText()}, '{lowerLimit}'::date)");
});

return query;
}

public static SelectQuery InjectNotJournaledFilter(this SelectQuery query, string keyTable, string keyColumn)
{
query.GetQuerySources()
Expand Down Expand Up @@ -178,20 +160,4 @@ SelectQuery selectRevisedQuery()

return diffquer;
}

public static SelectQuery InjectFilter(this SelectQuery query, string keyColumn, int keyValue)
{
query.GetQuerySources()
.Where(x => x.ColumnNames.Contains(keyColumn))
.GetRootsBySource()
.EnsureAny()
.ForEach(x =>
{
//filter
x.Query.AddComment($"Inject a filter {keyColumn}");
x.Query.Where(ValueParser.Parse($"{x.Alias}.{keyColumn} = {keyValue}"));
});

return query;
}
}
2 changes: 1 addition & 1 deletion src/Carbunql/IQueryCommandable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static string GetParameterText(this QueryCommand source)
{
sb.AppendLine($" {item.Key} is NULL");
}
else if (item.Value.GetType() == typeof(string))
else if (item.Value.GetType() == typeof(string) || item.Value.GetType() == typeof(DateTime))
{
sb.AppendLine($" {item.Key} = '{item.Value}'");
}
Expand Down
35 changes: 32 additions & 3 deletions src/Carbunql/SelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -815,13 +815,13 @@ public SelectQuery ToSubQuery(string alias)
return q;
}

public SelectQuery AddColumn(string column, string alias)
public SelectQuery AddSelect(string column, string alias)
{
this.Select(column).As(alias);
return this;
}

public SelectQuery OverrideColumn(string columnName, Func<IQuerySource, SelectableItem, string> overrider)
public SelectQuery OverrideSelect(string columnName, Func<IQuerySource, string, string> overrider)
{
GetQuerySources()
.Where(x => x.Query.GetSelectableItems().Where(x => x.Alias.IsEqualNoCase(columnName)).Any())
Expand All @@ -831,7 +831,7 @@ public SelectQuery OverrideColumn(string columnName, Func<IQuerySource, Selectab
{
var si = x.Query.GetSelectableItems().Where(x => x.Alias.IsEqualNoCase(columnName)).First();
//override
si.Value = ValueParser.Parse(overrider(x, si));
si.Value = ValueParser.Parse(overrider(x, si.Value.ToOneLineText()));
});

return this;
Expand All @@ -853,4 +853,33 @@ public SelectQuery OverrideColumn(string tableName, string columnName, Func<IQue

return this;
}

public SelectQuery AddWhere(string columnName, Func<IQuerySource, string> adder)
{
GetQuerySources()
.Where(x => x.Query.GetSelectableItems().Where(x => x.Alias.IsEqualNoCase(columnName)).Any())
.GetRootsBySource()
.EnsureAny()
.ForEach(x =>
{
x.Query.Where(adder(x));
});

return this;
}

public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySource, string> adder)
{
GetQuerySources()
.Where(x => x.GetTableFullName().IsEqualNoCase(tableName))
.Where(x => x.Query.GetSelectableItems().Where(x => x.Alias.IsEqualNoCase(columnName)).Any())
.GetRootsBySource()
.EnsureAny()
.ForEach(x =>
{
x.Query.Where(adder(x));
});

return this;
}
}
8 changes: 4 additions & 4 deletions test/Carbunql.Building.Test/IEnumerableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public void ToValuesQuery()
r0c0 = 1
r0c1 = 'a'
r0c2 = 10
r0c3 = 2000/01/01 0:00:00
r0c3 = '2000/01/01 0:00:00'
r0c4 = True
r1c0 = 2
r1c1 = 'b'
r1c2 = 20
r1c3 = 2010/10/10 0:00:00
r1c3 = '2010/10/10 0:00:00'
r1c4 = False
r2c0 = 3
r2c1 is NULL
Expand Down Expand Up @@ -73,12 +73,12 @@ public void ToSelectQuery()
r0c0 = 1
r0c1 = 'a'
r0c2 = 10
r0c3 = 2000/01/01 0:00:00
r0c3 = '2000/01/01 0:00:00'
r0c4 = True
r1c0 = 2
r1c1 = 'b'
r1c2 = 20
r1c3 = 2010/10/10 0:00:00
r1c3 = '2010/10/10 0:00:00'
r1c4 = False
r2c0 = 3
r2c1 is NULL
Expand Down
8 changes: 4 additions & 4 deletions test/Carbunql.TypeSafe.Test/SingleTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void LiteralTest()

var expect = @"/*
:const_0 = 'test'
:const_1 = 2000/01/01 10:10:00
:const_1 = '2000/01/01 10:10:00'
*/
SELECT
1 AS id,
Expand Down Expand Up @@ -170,7 +170,7 @@ public void VariableTest()
:rate = 0.1
:tf_value = True
:remarks = 'test'
:created_at = 2000/01/01 10:10:00
:created_at = '2000/01/01 10:10:00'
*/
SELECT
:id AS id,
Expand Down Expand Up @@ -269,7 +269,7 @@ public void DatetimeTest_SqlCommand()
Output.WriteLine(actual);

var expect = @"/*
:d = 2000/10/20 0:00:00
:d = '2000/10/20 0:00:00'
*/
SELECT
DATE_TRUNC('year', :d) AS v_trunc_year,
Expand Down Expand Up @@ -311,7 +311,7 @@ public void DatetimeTest_SqlExtension()
Output.WriteLine(actual);

var expect = @"/*
:d = 2000/10/20 0:00:00
:d = '2000/10/20 0:00:00'
*/
SELECT
DATE_TRUNC('year', :d) AS v_trunc_year,
Expand Down

0 comments on commit 402f590

Please sign in to comment.