Skip to content

Commit 14f6311

Browse files
committed
Add test case for insertion operations
1 parent 480c1f5 commit 14f6311

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

SQLInsertEmitter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ Error SQLInsertEmitter::insertRowImpl(const Record * RowRecord) {
4747
Values.push_back({Name, StrVal->getAsString()});
4848
else if (const auto *DefVal = dyn_cast<DefInit>(Val)) {
4949
// Another row Record
50-
// TODO: Make sure the referred Record is a SQL row.
5150
const auto *DefRecord = DefVal->getDef();
51+
if (!DefRecord->isSubClassOf("Table"))
52+
return createTGStringError(DefRecord->getLoc()[0],
53+
"Reference to another non SQL row Record "
54+
"is not supported yet");
5255
// Insert the referred row if needed.
5356
if (auto E = insertRowImpl(DefRecord))
5457
return std::move(E);
5558
auto RowI = InsertedRows.find(DefRecord);
5659
if (RowI == InsertedRows.end())
57-
return createTGStringError(RV.getLoc(), "Cannot find record \"" +
60+
return createTGStringError(DefRecord->getLoc()[0],
61+
"Cannot find record \"" +
5862
DefRecord->getName() + "\"");
5963
Values.push_back({Name, std::to_string(RowI->second)});
6064
} else {

test/InsertRows.td

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: sqlgen %s | %FileCheck %s
2+
3+
include "CreateTables.td"
4+
5+
def john : Customer<"John Smith">;
6+
def mary : Customer<"Mary Blackburn">;
7+
def bob : Customer<"Bob Glass">;
8+
9+
// Customer rows will be created on-demand.
10+
11+
// CHECK: INSERT INTO Customer (ID, Name, Affiliation)
12+
// CHECK: VALUES (0, "John Smith", "");
13+
//
14+
// CHECK: INSERT INTO Orders (SeqNumber, ProductName, Person, Amount)
15+
// CHECK: VALUES (0, "water bottle", 0, 100);
16+
def : Orders<"water bottle", john, 100,
17+
2021, 10, 21>;
18+
19+
// CHECK: INSERT INTO Customer (ID, Name, Affiliation)
20+
// CHECK: VALUES (1, "Mary Blackburn", "");
21+
//
22+
// CHECK: INSERT INTO Orders (SeqNumber, ProductName, Person, Amount)
23+
// CHECK: VALUES (1, "F150", 1, 2);
24+
//
25+
// CHECK: INSERT INTO Orders (SeqNumber, ProductName, Person, Amount)
26+
// CHECK: VALUES (2, "Tide Pods", 1, 60);
27+
def : Orders<"F150", mary, 2,
28+
2021, 1, 4>;
29+
def : Orders<"Tide Pods", mary, 60,
30+
2021, 2, 1>;
31+
32+
// CHECK: INSERT INTO Customer (ID, Name, Affiliation)
33+
// CHECK: VALUES (2, "Bob Glass", "");
34+
//
35+
// CHECK: INSERT INTO Orders (SeqNumber, ProductName, Person, Amount)
36+
// CHECK: VALUES (3, "RTX 3090", 2, 1);
37+
def : Orders<"RTX 3090", bob, 1,
38+
2020, 5, 6>;

0 commit comments

Comments
 (0)