Skip to content

Commit b437bc7

Browse files
committed
#568 Add support for SQLite (now reverse engineers db)
1 parent 1a9bed3 commit b437bc7

File tree

3 files changed

+67
-43
lines changed

3 files changed

+67
-43
lines changed

Generator.Tests.Integration/SingleDatabaseTestSQLite.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ real1 REAL CHECK (real1 > 123),
6565
6666
num1 NUMERIC,
6767
num2 DECIMAL(10, 5),
68-
num22 DECIMAL(10, 5),
69-
num222 DECIMAL(10, 5),
68+
num22 DECIMAL (10, 5),
69+
num222 DECIMAL (10, 5),
7070
num3 BOOLEAN,
71-
num4 DATE,
72-
num5 DATETIME,
71+
72+
date1 DATE,
73+
date2 DATETIME,
7374
7475
CONSTRAINT [PK_Efrpg] PRIMARY KEY (Id)
7576
);
@@ -120,10 +121,10 @@ public void ReverseEngineer()
120121
SetupDatabase("MyDbContext", "MyDbContext", TemplateType.EfCore7, GeneratorType.EfCore, ForeignKeyNamingStrategy.Legacy);
121122

122123
// Act
123-
Run("InMemory", ".SQLite", typeof(EfCoreFileManager), null);
124+
Run("EfrpgSQLite", ".SQLite", typeof(EfCoreFileManager), null);
124125

125126
// Assert
126-
CompareAgainstTestComparison("InMemoryTest");
127+
CompareAgainstTestComparison("SQLiteTest");
127128
}
128129
}
129130
}

Generator/LanguageMapping/SQLiteToCSharp.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,32 @@ public Dictionary<string, string> GetMapping()
1010
return new Dictionary<string, string>
1111
{
1212
{ string.Empty, "string" }, // default
13-
{ "blob", "byte[]" },
14-
{ "real", "double" },
15-
{ "integer", "int" },
16-
{ "text", "string" }
13+
{ "bigint", "long" },
14+
{ "blob", "byte[]" },
15+
{ "boolean", "bool" },
16+
{ "character", "string" },
17+
{ "clob", "string" },
18+
{ "date", "DateTime" },
19+
{ "datetime", "DateTime" },
20+
{ "decimal", "double" },
21+
{ "double", "double" },
22+
{ "double precision", "double" },
23+
{ "float", "double" },
24+
{ "int", "long" },
25+
{ "int2", "long" },
26+
{ "int8", "long" },
27+
{ "integer", "long" },
28+
{ "mediumint", "long" },
29+
{ "native character", "string" },
30+
{ "nchar", "string" },
31+
{ "numeric", "decimal" },
32+
{ "nvarchar", "string" },
33+
{ "real", "double" },
34+
{ "smallint", "int" },
35+
{ "text", "string" },
36+
{ "unsigned big int", "long" },
37+
{ "varchar", "string" },
38+
{ "varying character", "string" }
1739
};
1840
}
1941

Generator/Readers/SQLiteDatabaseReader.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ protected override string TableSQL()
4343
{
4444
return @"
4545
SELECT 'main' AS SchemaName,
46-
tbl_name AS TableName,
46+
TableName,
4747
TableType,
4848
0 AS TableTemporalType,
49-
cid AS Ordinal,
49+
Ordinal,
5050
ColumnName,
5151
IsNullable,
5252
REPLACE(type, '(' || length || ')', '') AS TypeName,
53-
CASE WHEN INSTR(type, ',') > 0 THEN 0 ELSE length END AS [MaxLength],
53+
length AS [MaxLength],
5454
Precision,
55-
dflt_value AS [Default],
55+
[Default],
5656
0 AS DateTimePrecision,
5757
Scale,
5858
pk AS IsIdentity,
@@ -63,22 +63,22 @@ CASE WHEN INSTR(type, ',') > 0 THEN 0 ELSE length END AS [MaxLength],
6363
pk AS PrimaryKey,
6464
pk AS PrimaryKeyOrdinal,
6565
0 AS IsForeignKey
66-
FROM (SELECT m.tbl_name,
67-
CASE WHEN m.type = 'table' THEN 'BASE TABLE' ELSE 'VIEW' END AS TableType,
68-
c.cid,
69-
c.name AS ColumnName,
70-
LOWER(c.type) AS type,
71-
1 - c.""notnull"" AS IsNullable,
72-
c.dflt_value,
73-
c.pk,
74-
SUBSTR(c.type, INSTR(c.type, '(') + 1, INSTR(c.type, ')') - INSTR(c.type, '(') - 1) AS length,
75-
CAST(SUBSTR(c.type, INSTR(c.type, '(') + 1, INSTR(c.type, ',') - INSTR(c.type, '(') - 1) AS INTEGER) AS Precision,
76-
CAST(SUBSTR(c.type, INSTR(c.type, ',') + 1, INSTR(c.type, ')') - INSTR(c.type, ',') - 1) AS INTEGER) AS Scale
77-
FROM sqlite_master m
78-
JOIN PRAGMA_TABLE_INFO(m.name) c
79-
WHERE m.type IN ('table', 'view')
80-
AND m.name NOT LIKE 'sqlite_%')
81-
ORDER BY tbl_name, cid;";
66+
FROM (SELECT m.tbl_name AS TableName,
67+
CASE WHEN m.type = 'table' THEN 'BASE TABLE' ELSE 'VIEW' END AS TableType,
68+
c.cid AS Ordinal,
69+
c.name AS ColumnName,
70+
IIF(INSTR(c.type, '(') > 0, SUBSTR(LOWER(c.type), 0, INSTR(c.type, '(')), LOWER(c.type)) as type,
71+
1 - c.""notnull"" AS IsNullable,
72+
c.dflt_value AS [Default],
73+
c.pk AS pk,
74+
CAST(SUBSTR(c.type, INSTR(c.type, '(') + 1, INSTR(c.type, ')') - INSTR(c.type, '(') - 1) AS INTEGER) AS length,
75+
CAST(SUBSTR(c.type, INSTR(c.type, '(') + 1, INSTR(c.type, ',') - INSTR(c.type, '(') - 1) AS INTEGER) AS Precision,
76+
CAST(SUBSTR(c.type, INSTR(c.type, ',') + 1, INSTR(c.type, ')') - INSTR(c.type, ',') - 1) AS INTEGER) AS Scale
77+
FROM sqlite_master m
78+
JOIN PRAGMA_TABLE_INFO(m.name) c
79+
WHERE m.type IN ('table', 'view')
80+
AND m.name NOT LIKE 'sqlite_%')
81+
ORDER BY TableName, Ordinal;";
8282
}
8383

8484
protected override string ForeignKeySQL()
@@ -115,12 +115,12 @@ protected override string IndexSQL()
115115
{
116116
return @"
117117
WITH split(TableName, IndexName, KeyOrdinal, ColumnName, csv) AS (
118-
SELECT TableName,
118+
SELECT TableName,
119119
IndexName,
120120
KeyOrdinal,
121121
'',
122122
column_name || ','
123-
FROM (SELECT TableName,
123+
FROM (SELECT TableName,
124124
IndexName,
125125
0 AS KeyOrdinal,
126126
SUBSTR(sql, INSTR(sql, '(') + 1, INSTR(sql, ')') - INSTR(sql, '(') - 1) AS column_name
@@ -130,26 +130,27 @@ WITH split(TableName, IndexName, KeyOrdinal, ColumnName, csv) AS (
130130
FROM sqlite_master
131131
WHERE type = 'index'
132132
AND name NOT LIKE 'sqlite_%'))
133-
UNION ALL
134-
SELECT TableName,
133+
UNION ALL
134+
SELECT TableName,
135135
IndexName,
136136
KeyOrdinal + 1,
137137
SUBSTR(csv, 0, INSTR(csv, ',')),
138138
SUBSTR(csv, INSTR(csv, ',') + 1)
139-
FROM split
140-
WHERE csv != '')
139+
FROM split
140+
WHERE csv != '')
141141
SELECT 'main' AS TableSchema,
142142
s.TableName,
143143
s.IndexName,
144144
s.KeyOrdinal,
145-
TRIM(s.ColumnName),
146-
0 AS IsUnique,
147-
0 AS IsPrimaryKey,
148-
0 AS IsUniqueConstraint,
145+
TRIM(s.ColumnName) as ColumnName,
149146
(SELECT MAX(KeyOrdinal)
150147
FROM split x
151148
WHERE x.TableName = s.TableName
152-
AND x.IndexName = s.IndexName) AS ColumnCount
149+
AND x.IndexName = s.IndexName) AS ColumnCount,
150+
0 AS IsUnique,
151+
0 AS IsPrimaryKey,
152+
0 AS IsUniqueConstraint,
153+
0 as IsClustered
153154
FROM split s
154155
WHERE ColumnName != ''";
155156
}
@@ -182,9 +183,9 @@ protected override string SequenceSQL()
182183
protected override string TriggerSQL()
183184
{
184185
return @"
185-
SELECT 'main' AS TableSchema, tbl_name as TableName, name as TriggerName
186+
SELECT 'main' AS SchemaName, tbl_name AS TableName, name AS TriggerName
186187
FROM sqlite_master
187-
WHERE type = 'trigger';";
188+
WHERE type = 'trigger'";
188189
}
189190

190191
protected override string[] MemoryOptimisedSQL()

0 commit comments

Comments
 (0)