Skip to content

Commit fb38800

Browse files
committed
Add Sqlite On Conflict column option in create table statements
1 parent 0f1ec44 commit fb38800

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/SqlParser.Tests/Dialects/SqliteDialectTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,23 @@ public void Test_Dollar_Identifier_As_Placeholder()
285285
Assert.Equal(new Identifier("id"), expression.Left);
286286
Assert.Equal(new LiteralValue(new Value.Placeholder("$id")), expression.Right);
287287
}
288+
289+
[Fact]
290+
public void Parse_Create_Table_On_Conflict_Col()
291+
{
292+
foreach (var keyword in new[]{Keyword.ROLLBACK, Keyword.ABORT, Keyword.FAIL, Keyword.IGNORE, Keyword.REPLACE})
293+
{
294+
var sql = $"CREATE TABLE t1 (a INT, b INT ON CONFLICT {keyword})";
295+
296+
var create = VerifiedStatement<Statement.CreateTable>(sql);
297+
298+
Assert.Equal([new ColumnOptionDef(new ColumnOption.OnConflict(keyword))], create.Element.Columns[1].Options);
299+
}
300+
}
301+
302+
[Fact]
303+
public void Test_Parse_Create_Table_On_Conflict_Col_Err()
304+
{
305+
Assert.Throws<ParserException>(() => ParseSqlStatements("CREATE TABLE t1 (a INT, b INT ON CONFLICT BOH)"));
306+
}
288307
}

src/SqlParser/Ast/ColumnOption.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ public override void ToSql(SqlTextWriter writer)
237237
writer.WriteSql($"ON UPDATE {Expression}");
238238
}
239239
}
240+
241+
public record OnConflict(Keyword Keyword) : ColumnOption
242+
{
243+
public override void ToSql(SqlTextWriter writer)
244+
{
245+
writer.WriteSql($"ON CONFLICT {Keyword.ToString().ToUpperInvariant()}");
246+
}
247+
}
240248
/// <summary>
241249
/// Generated are modifiers that follow a column definition in a CREATE TABLE statement.
242250
/// </summary>

src/SqlParser/Parser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,11 @@ or Keyword.GENERATED
24042404
return new ColumnOption.Identity(property);
24052405
}
24062406

2407+
if(_dialect is SQLiteDialect or GenericDialect && ParseKeywordSequence(Keyword.ON, Keyword.CONFLICT))
2408+
{
2409+
return new ColumnOption.OnConflict(ExpectOneOfKeywords(Keyword.ROLLBACK, Keyword.ABORT, Keyword.FAIL, Keyword.IGNORE, Keyword.REPLACE));
2410+
}
2411+
24072412
return null;
24082413
}
24092414

0 commit comments

Comments
 (0)