Skip to content

Commit a12dd39

Browse files
committed
IndexOfStart for Odbc, Postgresql and SQLite. Odbc default and SQLite computed column check
1 parent 85b5432 commit a12dd39

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/SQLProvider.Runtime/Providers.Odbc.fs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,21 @@ type internal OdbcProvider(contextSchemaPath, quotechar : OdbcQuoteCharacter) =
306306
let maxlen = if i.[6] = box(DBNull.Value) then 0 else i.[6] :?> int
307307

308308
let pkColumn = (Array.isEmpty primaryKey |> not) && primaryKey.[0].[8] = box name
309+
// Try to detect defaults from ODBC metadata (COLUMN_DEF at index 12)
310+
let hasDefault =
311+
try
312+
if i.Length > 12 && (not (isNull i.[12])) && i.[12] <> box(DBNull.Value) then
313+
let defaultVal = i.[12].ToString()
314+
not(String.IsNullOrWhiteSpace defaultVal)
315+
else false
316+
with _ -> false
309317
let col =
310318
{ Column.Name = name
311319
TypeMapping = m
312320
IsNullable = let b = i.[17] :?> string in b = "YES"
313321
IsPrimaryKey = pkColumn
314322
IsAutonumber = pkColumn
315-
HasDefault = false
323+
HasDefault = hasDefault
316324
IsComputed = false
317325
TypeInfo =
318326
if maxlen < 1 then ValueSome dt

src/SQLProvider.Runtime/Providers.Postgresql.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,10 @@ type internal PostgresqlProvider(resolutionPath, contextSchemaPath, owner, refer
10071007
| Length -> sprintf "CHAR_LENGTH(%s)" column
10081008
| IndexOf(SqlConstant search) -> sprintf "STRPOS(%s,%s)" (fieldParam search) column
10091009
| IndexOf(SqlCol(al2, col2)) -> sprintf "STRPOS(%s,%s)" (fieldNotation al2 col2) column
1010+
| IndexOfStart(SqlConstant search, SqlConstant startPos) -> sprintf "CASE WHEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) > 0 THEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) + %s::integer - 1 ELSE 0 END" column (fieldParam startPos) (fieldParam search) column (fieldParam startPos) (fieldParam search) (fieldParam startPos)
1011+
| IndexOfStart(SqlConstant search, SqlCol(al2, col2)) -> sprintf "CASE WHEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) > 0 THEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) + %s::integer - 1 ELSE 0 END" column (fieldNotation al2 col2) (fieldParam search) column (fieldNotation al2 col2) (fieldParam search) (fieldNotation al2 col2)
1012+
| IndexOfStart(SqlCol(al2, col2), SqlConstant startPos) -> sprintf "CASE WHEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) > 0 THEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) + %s::integer - 1 ELSE 0 END" column (fieldParam startPos) (fieldNotation al2 col2) column (fieldParam startPos) (fieldNotation al2 col2) (fieldParam startPos)
1013+
| IndexOfStart(SqlCol(al2, col2), SqlCol(al3, col3)) -> sprintf "CASE WHEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) > 0 THEN STRPOS(SUBSTRING(%s FROM %s::integer), %s) + %s::integer - 1 ELSE 0 END" column (fieldNotation al3 col3) (fieldNotation al2 col2) column (fieldNotation al3 col3) (fieldNotation al2 col2) (fieldNotation al3 col3)
10101014
| CastVarchar -> sprintf "(%s::varchar)" column
10111015
| CastInt -> sprintf "(%s::int)" column
10121016
// Date functions

src/SQLProvider.Runtime/Providers.SQLite.fs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,19 @@ type internal SQLiteProvider(resolutionPath, contextSchemaPath, referencedAssemb
553553
match findDbType dt with
554554
| Some(m) ->
555555
let pkColumn = reader.GetBoolean(5)
556+
// Check if column is generated/computed (hidden column in pragma table_info is 6)
557+
let isComputed =
558+
try
559+
if reader.FieldCount > 6 then (reader.GetInt32 6) > 0
560+
else false
561+
with _ -> false
556562
let col =
557563
{ Column.Name = colName
558564
TypeMapping = m
559565
IsNullable = not <| reader.GetBoolean(3);
560566
IsPrimaryKey = pkColumn
561567
IsAutonumber = pkColumn
562-
IsComputed = false
568+
IsComputed = isComputed
563569
HasDefault = not (reader.IsDBNull 4)
564570
TypeInfo = ValueSome dtv }
565571
if col.IsPrimaryKey then
@@ -686,6 +692,10 @@ type internal SQLiteProvider(resolutionPath, contextSchemaPath, referencedAssemb
686692
| Length -> sprintf "LENGTH(%s)" column
687693
| IndexOf(SqlConstant search) -> sprintf "INSTR(%s,%s)" column (fieldParam search)
688694
| IndexOf(SqlCol(al2, col2)) -> sprintf "INSTR(%s,%s)" column (fieldNotation al2 col2)
695+
| IndexOfStart(SqlConstant search, SqlConstant startPos) -> sprintf "CASE WHEN INSTR(SUBSTR(%s, %s), %s) > 0 THEN INSTR(SUBSTR(%s, %s), %s) + %s - 1 ELSE 0 END" column (fieldParam startPos) (fieldParam search) column (fieldParam startPos) (fieldParam search) (fieldParam startPos)
696+
| IndexOfStart(SqlConstant search, SqlCol(al2, col2)) -> sprintf "CASE WHEN INSTR(SUBSTR(%s, %s), %s) > 0 THEN INSTR(SUBSTR(%s, %s), %s) + %s - 1 ELSE 0 END" column (fieldNotation al2 col2) (fieldParam search) column (fieldNotation al2 col2) (fieldParam search) (fieldNotation al2 col2)
697+
| IndexOfStart(SqlCol(al2, col2), SqlConstant startPos) -> sprintf "CASE WHEN INSTR(SUBSTR(%s, %s), %s) > 0 THEN INSTR(SUBSTR(%s, %s), %s) + %s - 1 ELSE 0 END" column (fieldParam startPos) (fieldNotation al2 col2) column (fieldParam startPos) (fieldNotation al2 col2) (fieldParam startPos)
698+
| IndexOfStart(SqlCol(al2, col2), SqlCol(al3, col3)) -> sprintf "CASE WHEN INSTR(SUBSTR(%s, %s), %s) > 0 THEN INSTR(SUBSTR(%s, %s), %s) + %s - 1 ELSE 0 END" column (fieldNotation al3 col3) (fieldNotation al2 col2) column (fieldNotation al3 col3) (fieldNotation al2 col2) (fieldNotation al3 col3)
689699
| CastVarchar -> sprintf "CAST(%s AS TEXT)" column
690700
| CastInt -> sprintf "CAST(%s AS INTEGER)" column
691701
// Date functions

0 commit comments

Comments
 (0)