Skip to content

Commit

Permalink
Merge pull request #336 from mk3008/332-supports-parsing-of-create-in…
Browse files Browse the repository at this point in the history
…dex-statement

Supports using command
  • Loading branch information
mk3008 authored Feb 18, 2024
2 parents 73b5f09 + 1de0c73 commit 6e86b92
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Carbunql/Analysis/CreateIndexQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ private static IndexOnClause ParseAsOnClause(ITokenReader r)
var table = ParseAsTableName(r);
var clause = new IndexOnClause(table.schema, table.name);

if (r.Peek().IsEqualNoCase("using"))
{
r.Read();
clause.Using = r.Read();
}

r.Read("(");
do
{
Expand Down
8 changes: 8 additions & 0 deletions src/Carbunql/Clauses/IndexOnClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public IndexOnClause(string schema, string table)

public string TableFullName => (string.IsNullOrEmpty(Schema)) ? Table : Schema + "." + Table;

public string? Using { get; set; } = null;

public override IEnumerable<Token> GetTokens(Token? parent)
{
if (!Items.Any()) yield break;
Expand All @@ -29,6 +31,12 @@ public override IEnumerable<Token> GetTokens(Token? parent)
yield return clause;
yield return new Token(this, parent, TableFullName);

if (!string.IsNullOrEmpty(Using))
{
yield return new Token(this, parent, "using", isReserved: true);
yield return new Token(this, parent, Using);
}

yield return Token.ReservedBracketStart(this, parent);
foreach (var item in base.GetTokens(clause)) yield return item;
yield return Token.ReservedBracketEnd(this, parent);
Expand Down
15 changes: 14 additions & 1 deletion test/Carbunql.Analysis.Test/CreateIndexParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public void UniqueKey()
Assert.Equal(text, v.ToCommand().CommandText, true, true, true);
}


[Fact]
public void Nameless()
{
Expand All @@ -84,4 +83,18 @@ public void Nameless()
Assert.Equal(6, lst.Count);
Assert.Equal(text, v.ToCommand().CommandText, true, true, true);
}

[Fact]
public void Using()
{
var text = @"CREATE UNIQUE INDEX i1_customer__key_m_corporate_customer ON interlink.customer__key_m_corporate_customer USING btree (
customer_id
)";
var v = CreateIndexQueryParser.Parse(text);
Monitor.Log(v);

var lst = v.GetTokens().ToList();
Assert.Equal(9, lst.Count);
Assert.Equal(text, v.ToCommand().CommandText, true, true, true);
}
}

0 comments on commit 6e86b92

Please sign in to comment.