Skip to content

Commit

Permalink
Added sample to class reserved select queries
Browse files Browse the repository at this point in the history
By creating a class with a pre-written selection query and defining rows, you can build queries using IntelliSense.
  • Loading branch information
mk3008 committed Apr 21, 2024
1 parent fba791e commit fcf04a8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/Carbunql.Postgres/SelectQuery.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carbunql.Postgres;
namespace Carbunql.Postgres;

public class SelectQuery<T> : SelectQuery
{
public SelectQuery() : base()
{
}

public SelectQuery(string query) : base(query)
{
}
}
2 changes: 1 addition & 1 deletion test/Carbunql.Postgres.Test/Carbunql.Postgres.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
31 changes: 31 additions & 0 deletions test/Carbunql.Postgres.Test/SampleQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Carbunql.Postgres.Test;

public static class SampleQuery
{
public static string CommandText =>
"""
SELECT
a.a_id,
a.text,
b.value
FROM
table_a AS a
INNER JOIN table_b AS b ON a.a_id = b.a_id
""";

public static SelectQuery<Row> Query => GetQuery();

private static SelectQuery<Row>? QueryCache;

private static SelectQuery<Row> GetQuery()
{
if (QueryCache == null) QueryCache = new SelectQuery<Row>(CommandText);
return QueryCache;
}

public readonly record struct Row(
int a_id,
string text,
int value
);
}
33 changes: 31 additions & 2 deletions test/Carbunql.Postgres.Test/SubQueryText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,43 @@ INNER JOIN RecordB AS b ON (a.a_id = b.a_id AND b.text = 'test')
return sq;
}


[Fact]
public void QueryClassTest()
{
var sq = new SelectQuery();
var (from, a) = sq.FromAs(SampleQuery.Query, "a");

sq.Select(() => a.a_id);
sq.Select(() => a.value * 2).As("value");

Monitor.Log(sq);

Assert.Equal(45, sq.GetTokens().ToList().Count);

var sql = @"SELECT
a.a_id,
a.value * 2 AS value
FROM
(
SELECT
a.a_id,
a.text,
b.value
FROM
table_a AS a
INNER JOIN table_b AS b ON a.a_id = b.a_id
) AS a";

Assert.Equal(sql, sq.ToText(), true, true, true);
}

public record struct RecordA(int a_id, string text, int value, bool is_enabled, double rate, DateTime timestamp, Gender gender);

public record struct RecordB(int a_id, int b_id, string text, int value);

public record struct SubQueryRow(int a_id, string text, int value);

public class Myclass { public int MyProperty { get; set; } }

public enum Gender
{
Male,
Expand Down

0 comments on commit fcf04a8

Please sign in to comment.