Skip to content

Commit

Permalink
Add full FOREIGN KEY support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed Apr 25, 2024
1 parent c448766 commit 67086ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
12 changes: 6 additions & 6 deletions DbSeeder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ private static void Main()
{
const string sqlScript =
"""
CREATE TABLE users
CREATE TABLE profiles
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(122) NOT NULL UNIQUE,
profile_id INT,
FOREIGN KEY (profile_id) REFERENCES profiles(id)
nickname VARCHAR(122) NOT NULL UNIQUE
);

CREATE TABLE profiles
CREATE TABLE users
(
id INT AUTO_INCREMENT PRIMARY KEY,
nickname VARCHAR(122) NOT NULL UNIQUE
name VARCHAR(122) NOT NULL UNIQUE,
profile_id INT,
FOREIGN KEY (profile_id) REFERENCES profiles(id)
);
""";
var lexer = new SqlLexer(sqlScript);
Expand Down
16 changes: 10 additions & 6 deletions DbSeeder/Schema/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ public Column(string name,

constraints = constraints.Select(c => c.ToLower()).ToArray();
IsAutoIncrement = constraints.Contains("auto_increment", StringComparer.OrdinalIgnoreCase);
IsPrimaryKey = constraints.Contains("primary key", StringComparer.OrdinalIgnoreCase);
IsForeignKey = constraints.Contains("foreign key", StringComparer.OrdinalIgnoreCase);
IsNotNull = constraints.Contains("not null", StringComparer.OrdinalIgnoreCase);
IsUnique = constraints.Contains("unique", StringComparer.OrdinalIgnoreCase);
IsPrimaryKey = constraints.Contains("primary key", StringComparer.OrdinalIgnoreCase);

var fkConstraint = constraints.FirstOrDefault(

Check warning on line 20 in DbSeeder/Schema/Column.cs

View workflow job for this annotation

GitHub Actions / main

"Array.Find" static method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)
c => c.StartsWith("foreign key", StringComparison.OrdinalIgnoreCase));
IsForeignKey = fkConstraint is not null;

if (IsForeignKey)
{
// TODO[#15]: Update when working on FKs support.
// This will cause an exception during table's construction, as 'ref_TableName' won't be in a schema
const string refTableName = "ref_TableName";
const string refColumnName = "ref_ColumnName";
var refTableAndCol = fkConstraint![12..].Split("|");

var refTableName = refTableAndCol[0];
var refColumnName = refTableAndCol[1];

ForeignKeyRef = new ForeignKeyRef(refTableName, refColumnName);
}
}
Expand Down
3 changes: 2 additions & 1 deletion DbSeeder/Schema/SqlSchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ private Column GetColumn(SyntaxTreeNode columnNode)
constraints.Add(node.Value);
break;
case SyntaxTreeNodeType.ForeignKeyDefinition:
// TODO: Implement processing FK constraint correctly
var fkConstraint = $"foreign key {node.Children[1]?.Value}|{node.Children[2]?.Value}";
constraints.Add(fkConstraint);
break;
}
}
Expand Down

0 comments on commit 67086ed

Please sign in to comment.