Skip to content

Commit

Permalink
Merge pull request #393 from mk3008/392-tryconverttoinsertselect-does…
Browse files Browse the repository at this point in the history
…-not-work-correctly-when-using-the-function

Fixed a bug where TryToValuesQuery did not work correctly.
  • Loading branch information
mk3008 authored Apr 17, 2024
2 parents 383dee8 + dedaa17 commit 307608c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Carbunql/SelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public bool TryToValuesQuery([MaybeNullWhen(false)] out ValuesQuery query)
query = default;
if (SelectClause is null) return false;

var row = new ValueCollection(SelectClause.Select(x => x.Value).OfType<LiteralValue>().ToList<ValueBase>());
var row = new ValueCollection(SelectClause.Select(x => x.Value).ToList<ValueBase>());
var q = new ValuesQuery();
q.Rows.Add(row);

Expand Down
64 changes: 64 additions & 0 deletions test/Carbunql.Building.Test/InsertQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,68 @@ UNION ALL
Assert.Equal(36, lst.Count);
Assert.Equal(expect, iq.ToText(), true, true, true);
}

[Fact]
public void InsertSelectToInsertValues_DiverseValues()
{
var text = @"INSERT INTO
sale(col1, col2, col3, col4)
SELECT
now() AS col1,
:price AS col2,
current_timestamp AS col3,
null::int as col4";

var expect = @"INSERT INTO
sale(col1, col2, col3, col4)
VALUES
(NOW(), :price, current_timestamp, null::int)";

var iq = InsertQueryParser.Parse(text);
if (iq == null) throw new Exception();

if (iq.TryConvertToInsertValues(out var x))
{
iq = x;
}

Monitor.Log(iq);

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

Assert.Equal(25, lst.Count);
Assert.Equal(expect, iq.ToText(), true, true, true);
}

[Fact]
public void InsertValuesToInsertSelect_DiverseValues()
{
var text = @"INSERT INTO
sale(col1, col2, col3, col4)
VALUES
(NOW(), :price, current_timestamp, null::int)";

var expect = @"INSERT INTO
sale(col1, col2, col3, col4)
SELECT
NOW() AS col1,
:price AS col2,
current_timestamp AS col3,
null::int as col4";

var iq = InsertQueryParser.Parse(text);
if (iq == null) throw new Exception();

if (iq.TryConvertToInsertSelect(out var x))
{
iq = x;
}

Monitor.Log(iq);

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

Assert.Equal(31, lst.Count);
Assert.Equal(expect, iq.ToText(), true, true, true);
}
}

0 comments on commit 307608c

Please sign in to comment.