Skip to content

Commit

Permalink
QueryPage
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1000000 committed Dec 23, 2021
1 parent 30a9cae commit d1dce4d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
- the DTO code-generator now has a parameter that allows generating C# records: ```--model-type ImmutableClass|Record``` or ```<SqModelGenType>ImmutableClass|Record</SqModelGenType>```;
- "CheckExistenceBy" in the Insert data builder what adds WHERE EXISTS(...) to a Insert source to avoid duplicates inserting;
- ExistsIn ...
- QueryPage extension for OffsetFetch queries.
### Bugfix
- When some column(s) in values constructor contains only nulls, sqexpress now adds an explicit type cast for the first cell e.g. ```CAST(NULL as int)```
- **MergeDataInto** now allows only keys mapping unless **WhenMatchThenUpdate** is defined (without additional updates)
- Derived tables now can have 0 declared columns

# 0.3.0.0
### New Features
Expand Down
26 changes: 26 additions & 0 deletions SqExpress/ExprExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System.Xml;
using SqExpress.DataAccess;
using SqExpress.QueryBuilders;
using SqExpress.QueryBuilders.Select;
using SqExpress.QueryBuilders.Update;
using SqExpress.Syntax;
using SqExpress.Syntax.Select;
using SqExpress.SyntaxTreeOperations;
using SqExpress.SyntaxTreeOperations.ExportImport;
using SqExpress.SyntaxTreeOperations.ExportImport.Internal;
Expand Down Expand Up @@ -67,6 +69,30 @@ public static Task<Dictionary<TKey, TValue>> QueryDictionary<TKey, TValue>(
where TKey : notnull
=> database.QueryDictionary(query.Done(), keyFactory, valueFactory, keyDuplicationHandler, predicate);

public static Task<DataPage<T>> QueryPage<T>(this ISelectOffsetFetchBuilderFinal builder, ISqDatabase database, Func<ISqDataRecordReader, T> reader)
=> builder.Done().QueryPage(database, reader);

public static async Task<DataPage<T>> QueryPage<T>(this ExprSelectOffsetFetch query, ISqDatabase database, Func<ISqDataRecordReader, T> reader)
{
var countColumn = CustomColumnFactory.Int32("$count$");

var selectQuery = (ExprQuerySpecification)query.SelectQuery;

query = query.WithSelectQuery(
selectQuery.WithSelectList(selectQuery.SelectList.Concat(SqQueryBuilder.CountOneOver().As(countColumn))));

var res = await query.Query(database,
new KeyValuePair<List<T>, int?>(new List<T>(), null),
(acc, r) =>
{
acc.Key.Add(reader(r));
var total = acc.Value ?? countColumn.Read(r);
return new KeyValuePair<List<T>, int?>(acc.Key, total);
});

return new DataPage<T>(res.Key, query.OrderBy.OffsetFetch.Offset.Value ?? 0, res.Value ?? 0);
}

public static Task<object> QueryScalar(this IExprQuery query, ISqDatabase database)
=> database.QueryScalar(query);

Expand Down
23 changes: 0 additions & 23 deletions SqExpress/ModelRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using SqExpress.DataAccess;
using SqExpress.ModelSelect;
using SqExpress.Syntax.Select;

namespace SqExpress
{
Expand All @@ -20,27 +19,5 @@ public static Task<TAcc> Query<TRes,TAcc>(this ModelRequestData<TRes> modelReque

public static Task<DataPage<TRes>> QueryPage<TRes>(this ModelRangeRequestData<TRes> modelRequestData, ISqDatabase database)
=> modelRequestData.Expr.QueryPage(database, modelRequestData.Mapper);

internal static async Task<DataPage<T>> QueryPage<T>(this ExprSelectOffsetFetch query, ISqDatabase database, Func<ISqDataRecordReader, T> reader)
{
var countColumn = CustomColumnFactory.Int32("$count");

var selectQuery = (ExprQuerySpecification)query.SelectQuery;

query = query.WithSelectQuery(
selectQuery.WithSelectList(selectQuery.SelectList.Concat(SqQueryBuilder.CountOneOver().As(countColumn))));


var res = await query.Query(database,
new KeyValuePair<List<T>, int?>(new List<T>(), null),
(acc, r) =>
{
acc.Key.Add(reader(r));
var total = acc.Value ?? countColumn.Read(r);
return new KeyValuePair<List<T>, int?>(acc.Key, total);
});

return new DataPage<T>(res.Key, query.OrderBy.OffsetFetch.Offset.Value ?? 0, res.Value ?? 0);
}
}
}
6 changes: 3 additions & 3 deletions SqExpress/SqExpress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Authors>Dmitry Tikhonov</Authors>
<Company />
<Version>0.3.0.2</Version>
<Version>0.3.1.0</Version>
<Summary>SqExpress is a sql query builder which allows creating SQL expression directly in C# code with strong typing and intellisense.</Summary>
<PackageReleaseNotes>https://github.com/0x1000000/SqExpress/blob/main/CHANGELOG.md</PackageReleaseNotes>
<Description>SqExpress is a sql query builder which allows creating SQL expression in C# code with strong typing and intellisense (without LINQ!).
Expand All @@ -29,8 +29,8 @@ This an article that explains the library principles: ["Syntax Tree and Alternat
<Copyright>Dmitry Tikhonov</Copyright>
<PackageProjectUrl>https://github.com/0x1000000/SqExpress</PackageProjectUrl>
<PackageTags>query postgresql tsql mysql builder mssql dataaccess</PackageTags>
<AssemblyVersion>0.3.0.2</AssemblyVersion>
<FileVersion>0.3.0.2</FileVersion>
<AssemblyVersion>0.3.1.0</AssemblyVersion>
<FileVersion>0.3.1.0</FileVersion>
<AssemblyOriginatorKeyFile>..\SqExpress.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
Expand Down
6 changes: 6 additions & 0 deletions SqExpress/SqQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public static ExprCast Cast(ExprValue expression, ExprType asType)
public static ExprCast Cast(IExprSelecting expression, ExprType asType)
=> new ExprCast(expression, asType);

public static ExprColumn Column(string columnName)
=> new ExprColumn(null, columnName);

public static ExprColumn Column(IExprColumnSource source, string columnName)
=> new ExprColumn(source, columnName);

public static ExprTableAlias TableAlias(Alias alias = default)
=> new ExprTableAlias(alias.BuildAliasExpression() ?? Alias.Auto.BuildAliasExpression()!);

Expand Down
3 changes: 1 addition & 2 deletions SqExpress/SqlExport/Internal/SqlBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,8 @@ public bool VisitExprDerivedTableQuery(ExprDerivedTableQuery exprDerivedTableQue
{
this.AcceptPar('(', exprDerivedTableQuery.Query, ')', exprDerivedTableQuery);
exprDerivedTableQuery.Alias.Accept(this, exprDerivedTableQuery);
if (exprDerivedTableQuery.Columns != null)
if (exprDerivedTableQuery.Columns != null && exprDerivedTableQuery.Columns.Count > 0)
{
exprDerivedTableQuery.Columns.AssertNotEmpty("List of columns in a derived table with values literals cannot be empty");
var selectedColumns = exprDerivedTableQuery.Query.GetOutputColumnNames();

if (selectedColumns.Count != exprDerivedTableQuery.Columns.Count)
Expand Down

0 comments on commit d1dce4d

Please sign in to comment.