<commit_before>﻿namespace FSharp.Data.Sql.Providers

open System
open System.Collections.Generic
open System.Data

open FSharp.Data.Sql
open FSharp.Data.Sql.Schema
open FSharp.Data.Sql.Common

type internal PostgresqlProvider(resolutionPath) as this =
    // note we intentionally do not hang onto a connection object at any time,
    // as the type provider will dicate the connection lifecycles 
    let pkLookup =     Dictionary<string,string>()
    let tableLookup =  Dictionary<string,Table>()
    let columnLookup = Dictionary<string,Column list>()    
    let relationshipLookup = Dictionary<string,Relationship list * Relationship list>()

    let loadAssembly (name:string) = 
        // first try the gac (naively)
        try Reflection.Assembly.Load name
        with
        | _ ->
             Reflection.Assembly.LoadFrom(
                if String.IsNullOrEmpty resolutionPath then name + ".dll" 
                else System.IO.Path.Combine(resolutionPath,name+".dll"))
    // Dynamically load the SQLite assembly so we don't have a dependency on it in the project
    let assembly =  loadAssembly "Npgsql"
    let monoSecurity = loadAssembly "Mono.Security"
   
    let connectionType =  (assembly.GetTypes() |> Array.find(fun t -> t.Name = "NpgsqlConnection"))
    let commandType =     (assembly.GetTypes() |> Array.find(fun t -> t.Name = "NpgsqlCommand"))
    let paramterType =    (assembly.GetTypes() |> Array.find(fun t -> t.Name = "NpgsqlParameter"))
    let getSchemaMethod = (connectionType.GetMethod("GetSchema",[|typeof<string>|]))

    let mutable clrToEnum : (string -> DbType option)  = fun _ -> failwith "!"
    let mutable sqlToEnum : (string -> DbType option)  = fun _ -> failwith "!"
    let mutable sqlToClr :  (string -> Type option)    = fun _ -> failwith "!"

    let createTypeMappings () =        
        // there doesn't seem to be any mapping in the schema
        // so we are stuck doing this the old fashioned way
        let sqlToClr' = function
            | "int8"       -> Some typeof<Int64>
            | "bool"       -> Some typeof<Boolean>
            | "Box"           
            | "Circle"        
            | "Line"          
            | "LSeg"          
            | "Path"          
            | "Point"         
            | "Polygon"    -> Some typeof<Object>
            | "bytea"      -> Some typeof<Byte[]>            
            | "float8"     -> Some typeof<Double>
            | "integer"
            | "int4"       -> Some typeof<Int32>
            | "money"      
            | "numeric"    -> Some typeof<Decimal>
            | "float4"     -> Some typeof<Single>
            | "int2"       -> Some typeof<Int16>
            | "text"       -> Some typeof<String>
            | "date"       
            | "time"       
            | "timetz"     
            | "timestamp"  
            | "timestamptz"-> Some typeof<DateTime>
            | "interval"   -> Some typeof<TimeSpan>
            | "character"  
            | "varchar"    -> Some typeof<String>
            | "inet"       -> Some typeof<System.Net.IPAddress>            
            | "bit"        -> Some typeof<Boolean>           
            | "uuid"       -> Some typeof<Guid>
            | _ -> None
        
        let sqlToEnum' = function
            | "int8"       -> Some DbType.Int64
            | "bit"        
            | "bool"       -> Some DbType.Boolean
            | "Box"           
            | "Circle"        
            | "Line"          
            | "LSeg"          
            | "Path"          
            | "Point"         
            | "interval"   
            | "inet"       
            | "Polygon"    -> Some DbType.Object
            | "bytea"      -> Some DbType.Binary            
            | "float8"     -> Some DbType.Double
            | "integer"
            | "int4"       -> Some DbType.Int32
            | "money"      
            | "numeric"    -> Some DbType.Decimal
            | "float4"     -> Some DbType.Single
            | "int2"       -> Some DbType.Int16            
            | "timestamp" 
            | "timestamptz"
            | "date"       -> Some DbType.DateTime
            | "time"       
            | "timetz"     -> Some DbType.Time 
            | "text"     
            | "character"    
            | "varchar"    -> Some DbType.String            
            | "uuid"       -> Some DbType.Guid
            | _ -> None      
        
        let clrToEnum' n = 
            if   n = typeof<Int64>.Name then Some DbType.Int64
            elif n = typeof<Int32>.Name then Some DbType.Int32
            elif n = typeof<Int16>.Name then Some DbType.Int16
            elif n = typeof<Boolean>.Name then Some DbType.Boolean
            elif n = typeof<Object>.Name then Some DbType.Object
            elif n = typeof<Byte[]>.Name then Some DbType.Binary
            elif n = typeof<Double>.Name then Some DbType.Double
            elif n = typeof<Decimal>.Name then Some DbType.Decimal
            elif n = typeof<Single>.Name then Some DbType.Single
            elif n = typeof<String>.Name then Some DbType.String
            elif n = typeof<DateTime>.Name then Some DbType.DateTime
            elif n = typeof<TimeSpan>.Name then Some DbType.Object
            elif n = typeof<System.Net.IPAddress>.Name then Some DbType.Object
            elif n = typeof<Guid>.Name then Some DbType.Guid
            else None

        // set lookup functions         
        sqlToClr <-  sqlToClr'<commit_msg>Added some missing type mappings and aliases for existing ones.<commit_after>﻿namespace FSharp.Data.Sql.Providers

open System
open System.Collections.Generic
open System.Data

open FSharp.Data.Sql
open FSharp.Data.Sql.Schema
open FSharp.Data.Sql.Common

type internal PostgresqlProvider(resolutionPath) as this =
    // note we intentionally do not hang onto a connection object at any time,
    // as the type provider will dicate the connection lifecycles 
    let pkLookup =     Dictionary<string,string>()
    let tableLookup =  Dictionary<string,Table>()
    let columnLookup = Dictionary<string,Column list>()    
    let relationshipLookup = Dictionary<string,Relationship list * Relationship list>()

    let loadAssembly (name:string) = 
        // first try the gac (naively)
        try Reflection.Assembly.Load name
        with
        | _ ->
             Reflection.Assembly.LoadFrom(
                if String.IsNullOrEmpty resolutionPath then name + ".dll" 
                else System.IO.Path.Combine(resolutionPath,name+".dll"))
    // Dynamically load the Npgsql assembly so we don't have a dependency on it in the project
    let assembly =  loadAssembly "Npgsql"
    let monoSecurity = loadAssembly "Mono.Security"
   
    let connectionType =  (assembly.GetTypes() |> Array.find(fun t -> t.Name = "NpgsqlConnection"))
    let commandType =     (assembly.GetTypes() |> Array.find(fun t -> t.Name = "NpgsqlCommand"))
    let paramterType =    (assembly.GetTypes() |> Array.find(fun t -> t.Name = "NpgsqlParameter"))
    let getSchemaMethod = (connectionType.GetMethod("GetSchema",[|typeof<string>|]))

    let mutable clrToEnum : (string -> DbType option)  = fun _ -> failwith "!"
    let mutable sqlToEnum : (string -> DbType option)  = fun _ -> failwith "!"
    let mutable sqlToClr :  (string -> Type option)    = fun _ -> failwith "!"

    let createTypeMappings () =        
        // there doesn't seem to be any mapping in the schema
        // so we are stuck doing this the old fashioned way
        let sqlToClr' = function
            | "bigint"
            | "int8"       -> Some typeof<Int64>
            | "bit"           // Doesn't seem to correspond to correct type - fixed-length bit string (Npgsql.BitString)
            | "varbit"        // Doesn't seem to correspond to correct type - variable-length bit string (Npgsql.BitString)
            | "boolean"
            | "bool"       -> Some typeof<Boolean>
            | "Box"
            | "Circle"
            | "Line"
            | "LSeg"
            | "Path"
            | "Point"
            | "Polygon"    -> Some typeof<Object>
            | "bytea"      -> Some typeof<Byte[]>
            | "double"
            | "float8"     -> Some typeof<Double>
            | "integer"
            | "int"
            | "int4"       -> Some typeof<Int32>
            | "money"
            | "numeric"    -> Some typeof<Decimal>
            | "real"
            | "float4"     -> Some typeof<Single>
            | "smallint"
            | "int2"       -> Some typeof<Int16>
            | "text"       -> Some typeof<String>
            | "date"
            | "time"
            | "timetz"
            | "timestamp"
            | "timestamptz"-> Some typeof<DateTime>
            | "interval"   -> Some typeof<TimeSpan>
            | "character"
            | "varchar"    -> Some typeof<String>
            | "inet"       -> Some typeof<System.Net.IPAddress>
            | "uuid"       -> Some typeof<Guid>
            | "xml"        -> Some typeof<String>
            | _ -> None
        
        let sqlToEnum' = function
            | "bigint"
            | "int8"       -> Some DbType.Int64
            | "bit"           // Doesn't seem to correspond to correct type - fixed-length bit string (Npgsql.BitString)
            | "varbit"        // Doesn't seem to correspond to correct type - variable-length bit string (Npgsql.BitString)
            | "boolean"
            | "bool"       -> Some DbType.Boolean
            | "Box"
            | "Circle"
            | "Line"
            | "LSeg"
            | "Path"
            | "Point"
            | "interval"
            | "inet"
            | "Polygon"    -> Some DbType.Object
            | "bytea"      -> Some DbType.Binary
            | "double"
            | "float8"     -> Some DbType.Double
            | "integer"
            | "int"
            | "int4"       -> Some DbType.Int32
            | "money"
            | "numeric"    -> Some DbType.Decimal
            | "real"
            | "float4"     -> Some DbType.Single
            | "smallint"
            | "int2"       -> Some DbType.Int16
            | "timestamp" 
            | "timestamptz"
            | "date"       -> Some DbType.DateTime
            | "time"
            | "timetz"     -> Some DbType.Time 
            | "text"
            | "character"
            | "varchar"    -> Some DbType.String
            | "uuid"       -> Some DbType.Guid
            | "xml"        -> Some DbType.Xml
            | _ -> None
        
        let clrToEnum' n = 
            if   n = typeof<Int64>.Name then Some DbType.Int64
            elif n = typeof<Int32>.Name then Some DbType.Int32
            elif n = typeof<Int16>.Name then Some DbType.Int16
            elif n = typeof<Boolean>.Name then Some DbType.Boolean
            elif n = typeof<Object>.Name then Some DbType.Object
            elif n = typeof<Byte[]>.Name then Some DbType.Binary
            elif n = typeof<Double>.Name then Some DbType.Double
            elif n = typeof<Decimal>.Name then Some DbType.Decimal
            elif n = typeof<Single>.Name then Some DbType.Single
            elif n = typeof<String>.Name then Some DbType.String
            elif n = typeof<DateTime>.Name then Some DbType.DateTime
            elif n = typeof<TimeSpan>.Name then Some DbType.Object
            elif n = typeof<System.Net.IPAddress>.Name then Some DbType.Object
            elif n = typeof<Guid>.Name then Some DbType.Guid
            else None

        // set lookup functions         
        sqlToClr <-  sqlToClr'
{'fraction_non_alphanumeric': 0.10574356324808404, 'fraction_numerical': 0.005855506759665892, 'mean_word_length': 1.5089652192698206, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 162, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2125171', 'n_tokens_mistral': 3302, 'n_tokens_neox': 3147, 'n_words': 850}