Skip to content

Commit

Permalink
#274 CreateTableQuery class enhancements
Browse files Browse the repository at this point in the history
It is now possible to generate a query that selects the created table.
  • Loading branch information
mk3008 committed Nov 25, 2023
1 parent ef288e0 commit 3e4a1c4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Carbunql/Clauses/CreateTableClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public CreateTableClause(TableBase table)

public bool IsTemporary { get; set; } = true;

public TableBase? Table { get; set; }
public TableBase Table { get; set; } = null!;

public IEnumerable<CommonTable> GetCommonTables()
{
Expand Down
20 changes: 19 additions & 1 deletion src/Carbunql/CreateTableQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Carbunql.Clauses;
using Carbunql.Building;
using Carbunql.Clauses;
using Carbunql.Extensions;
using Carbunql.Tables;

Expand All @@ -13,6 +14,8 @@ public CreateTableQuery(CreateTableClause createTableClause)

public CreateTableClause CreateTableClause { get; init; }

public string TableFullName => CreateTableClause.Table.GetTableFullName();

public IReadQuery? Query { get; set; }

public IDictionary<string, object?>? Parameters { get; set; }
Expand Down Expand Up @@ -58,4 +61,19 @@ public IEnumerable<CommonTable> GetCommonTables()
if (Query == null) yield break;
foreach (var item in Query.GetCommonTables()) yield return item;
}

public SelectQuery ToSelectQuery()
{
if (Query == null) throw new NullReferenceException(nameof(Query));

var sq = new SelectQuery();
var (_, t) = sq.From(TableFullName).As("t");

foreach (var item in Query.GetColumnNames())
{
sq.Select(t, item);
}

return sq;
}
}
11 changes: 3 additions & 8 deletions test/Carbunql.Building.Test/CopyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@ public CopyTest(ITestOutputHelper output)
Output = output;
}

private string TruncateControlString(string text)
{
return text.Replace("\r", "").Replace("\n", "").Replace(" ", "").ToLower();
}

[Fact]
public void DeepCopy()
{
var sq = new SelectQuery("select a.column_1 as c1 from table_a as a");
var actual = sq.DeepCopy();

Assert.Equal(TruncateControlString(sq.ToText()), TruncateControlString(actual!.ToText()));
Assert.Equal(sq.ToText().ToValidateText(), actual!.ToText().ToValidateText());
Assert.NotEqual(sq, actual);
}

Expand All @@ -34,7 +29,7 @@ public void Serialize_SelectQuery()
var json = Serializer.Serialize(sq);
var actual = Serializer.Deserialize<SelectQuery>(json);

Assert.Equal(TruncateControlString(sq.ToText()), TruncateControlString(actual!.ToText()));
Assert.Equal(sq.ToText().ToValidateText(), actual!.ToText().ToValidateText());
Assert.NotEqual(sq, actual);
}

Expand All @@ -45,7 +40,7 @@ public void Serialize()
var json = Serializer.Serialize(sq);
var actual = Serializer.Deserialize(json);

Assert.Equal(TruncateControlString(sq.ToText()), TruncateControlString(actual!.ToText()));
Assert.Equal(sq.ToText().ToValidateText(), actual!.ToText().ToValidateText());
Assert.NotEqual(sq, actual);
}

Expand Down
32 changes: 28 additions & 4 deletions test/Carbunql.Building.Test/CreateTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Carbunql.Building.Test;

public class CreateTableTest
{
private readonly QueryCommandMonitor Monitor;
private readonly QueryCommandMonitor sq;

public CreateTableTest(ITestOutputHelper output)
{
Monitor = new QueryCommandMonitor(output);
sq = new QueryCommandMonitor(output);
}

[Fact]
Expand All @@ -22,7 +22,7 @@ public void CreateTable()
var ctq = q.ToCreateTableQuery("new_table");
ctq.CreateTableClause.IsTemporary = false;

Monitor.Log(ctq);
sq.Log(ctq);

var lst = ctq.GetTokens().ToList();

Expand All @@ -36,10 +36,34 @@ public void CreateTemporaryTable()
var q = QueryParser.Parse(sql);

var ctq = q.ToCreateTableQuery("new_table");
Monitor.Log(ctq);
sq.Log(ctq);

var lst = ctq.GetTokens().ToList();

Assert.Equal(15, lst.Count());
}

[Fact]
public void SelectQuery()
{
var sql = "select a.id, 'test' as value from table as a";
var q = QueryParser.Parse(sql);

var sq = q.ToCreateTableQuery("new_table").ToSelectQuery();
this.sq.Log(sq);

var lst = sq.GetTokens().ToList();

Assert.Equal(12, lst.Count());

var expect = @"
SELECT
t.id,
t.value
FROM
new_table AS t";

Assert.Equal(expect.ToValidateText(), sq.ToText().ToValidateText());

}
}
9 changes: 0 additions & 9 deletions test/Carbunql.Building.Test/Extension.cs

This file was deleted.

13 changes: 13 additions & 0 deletions test/Carbunql.Building.Test/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Carbunql.Building.Test;

public static class StringExtension
{
public static string ToValidateText(this string text)
{
return text.Replace("\r", "")
.Replace("\n", "")
.Replace(" ", "")
.Replace("\t", "")
.ToLower();
}
}

0 comments on commit 3e4a1c4

Please sign in to comment.