Skip to content

Commit 2bbb39f

Browse files
committed
Dont require default columns
1 parent 07d1aec commit 2bbb39f

4 files changed

Lines changed: 54 additions & 21 deletions

File tree

Sources/Compiler/Columns.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ extension Columns {
2323
public struct Column: Equatable, Sendable {
2424
public let type: Type
2525
public let isGenerated: Bool
26+
public let hasDefault: Bool
2627

27-
public init(type: Type, isGenerated: Bool = false) {
28+
public init(
29+
type: Type,
30+
hasDefault: Bool = false,
31+
isGenerated: Bool = false
32+
) {
2833
self.type = type
34+
self.hasDefault = hasDefault
2935
self.isGenerated = isGenerated
3036
}
3137

3238
public var isRequired: Bool {
33-
guard !isGenerated else { return false }
34-
return !type.isOptional
39+
return !isGenerated && !hasDefault && !type.isOptional
3540
}
3641

3742
public func mapType(_ transform: (Type) -> Type) -> Column {

Sources/Compiler/Sema/StmtTypeChecker.swift

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,13 +1165,7 @@ extension StmtTypeChecker {
11651165
case let .columns(columnsDefs, constraints, options):
11661166
var columns: Columns = [:]
11671167
for (name, def) in columnsDefs {
1168-
let type = typeFor(
1169-
column: def,
1170-
tableColumns: columns,
1171-
tableName: createTable.name.value
1172-
)
1173-
let isGenerated = def.constraints.contains { $0.isGenerated }
1174-
let column = Column(type: type, isGenerated: isGenerated)
1168+
let column = column(for: def, columns: columns, tableName: createTable.name.value)
11751169
columns.append(column, for: name.value)
11761170
}
11771171

@@ -1200,6 +1194,28 @@ extension StmtTypeChecker {
12001194
}
12011195
}
12021196

1197+
mutating func column(
1198+
for columnDef: ColumnDefSyntax,
1199+
columns: Columns,
1200+
tableName: Substring
1201+
) -> Column {
1202+
let type = typeFor(
1203+
column: columnDef,
1204+
tableColumns: columns,
1205+
tableName: tableName
1206+
)
1207+
1208+
var isGenerated = false
1209+
var hasDefault = false
1210+
1211+
for constraint in columnDef.constraints {
1212+
isGenerated = constraint.isGenerated || isGenerated
1213+
hasDefault = constraint.isDefault || isGenerated
1214+
}
1215+
1216+
return Column(type: type, hasDefault: hasDefault, isGenerated: isGenerated)
1217+
}
1218+
12031219
mutating func typeCheck(alterTable: AlterTableStmtSyntax) {
12041220
var tableName = qualifedName(for: alterTable.name, in: alterTable.schemaName)
12051221

@@ -1229,15 +1245,9 @@ extension StmtTypeChecker {
12291245
table.name = tableName
12301246
case let .renameColumn(oldName, newName):
12311247
table.columns.rename(oldName.value, to: newName.value)
1232-
case let .addColumn(column):
1233-
let newType = typeFor(
1234-
column: column,
1235-
tableColumns: table.columns,
1236-
tableName: table.name.name
1237-
)
1238-
let isGenerated = column.constraints.contains { $0.isGenerated }
1239-
let newColumn = Column(type: newType, isGenerated: isGenerated)
1240-
table.columns.append(newColumn, for: column.name.value)
1248+
case let .addColumn(def):
1249+
let column = column(for: def, columns: table.columns, tableName: table.name.name)
1250+
table.columns.append(column, for: def.name.value)
12411251
case let .dropColumn(column):
12421252
table.columns = Columns(table.columns.filter { $0.key != column.value })
12431253
}

Sources/Compiler/Syntax/ColumnConstraintSyntax.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@ struct ColumnConstraintSyntax: Syntax {
4747
default: return false
4848
}
4949
}
50+
51+
var isDefault: Bool {
52+
switch kind {
53+
case .default: return true
54+
default: return false
55+
}
56+
}
5057
}

Tests/CompilerTests/Compiler/CompileInsert.sql

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CREATE TABLE user (
22
id INTEGER,
33
name TEXT,
4+
age INTEGER NOT NULL DEFAULT 0,
45
description TEXT GENERATED ALWAYS AS (name || 'is a user')
56
);
67

@@ -14,9 +15,13 @@ CREATE TABLE user (
1415
-- CHECK: TYPE TEXT?
1516
-- CHECK: INDEX 2
1617
-- CHECK: NAME name
18+
-- CHECK: PARAMETER
19+
-- CHECK: TYPE INTEGER
20+
-- CHECK: INDEX 3
21+
-- CHECK: NAME age
1722
-- CHECK: TABLES
1823
-- CHECK: user
19-
INSERT INTO user (id, name) VALUES (?, ?);
24+
INSERT INTO user (id, name, age) VALUES (?, ?, ?);
2025

2126
-- CHECK: SIGNATURE
2227
-- CHECK: PARAMETERS
@@ -33,6 +38,7 @@ INSERT INTO user (id, name) VALUES (?, ?);
3338
-- CHECK: OUTPUT
3439
-- CHECK: id INTEGER?
3540
-- CHECK: name TEXT?
41+
-- CHECK: age INTEGER
3642
-- CHECK: description TEXT?
3743
-- CHECK: TABLES
3844
-- CHECK: user
@@ -69,6 +75,7 @@ INSERT INTO user (id, name) VALUES (?, ?) RETURNING *;
6975
-- CHECK: OUTPUT
7076
-- CHECK: id INTEGER?
7177
-- CHECK: name TEXT?
78+
-- CHECK: age INTEGER
7279
-- CHECK: description TEXT?
7380
-- CHECK: TABLES
7481
-- CHECK: user
@@ -84,9 +91,13 @@ INSERT INTO user (id, name) VALUES (?, ?), (?, ?), (?, ?) RETURNING *;
8491
-- CHECK: TYPE TEXT?
8592
-- CHECK: INDEX 2
8693
-- CHECK: NAME name
94+
-- CHECK: PARAMETER
95+
-- CHECK: TYPE INTEGER
96+
-- CHECK: INDEX 3
97+
-- CHECK: NAME age
8798
-- CHECK: TABLES
8899
-- CHECK: user
89-
INSERT INTO user VALUES (?, ?);
100+
INSERT INTO user VALUES (?, ?, ?);
90101

91102
-- CHECK: SIGNATURE
92103
-- CHECK: PARAMETERS

0 commit comments

Comments
 (0)