<commit_before>﻿module FSharp.Data.SqlClient.Configuration

open System.Configuration
open System.IO
open System

let getConnectionString resolutionFolder connectionString connectionStringName configFile  =
    if connectionString <> "" then connectionString
    else
        if connectionStringName = "" then failwithf "Either ConnectionString or ConnectionStringName is required"
        let getMappedConfig file = 
            let path = Path.Combine(resolutionFolder, file)            
            let map = new ExeConfigurationFileMap()
            map.ExeConfigFilename <- path
            ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).ConnectionStrings.ConnectionStrings.[connectionStringName]
        let configSection = 
            match configFile, ConfigurationManager.ConnectionStrings.[connectionStringName] with
            | "", null -> 
                let c = getMappedConfig "app.config"
                if c = null then getMappedConfig "web.config" else c
            | "", c -> c
            | file, _ -> getMappedConfig file
        if configSection = null 
        then failwithf "Connection string %s is not found." connectionStringName
        else configSection.ConnectionString


let private invalidChars = [ 
    yield! Path.GetInvalidFileNameChars() 
    yield! Path.GetInvalidPathChars()] |> set

let parseTextAtDesignTime (commandTextOrPath : string) resolutionFolder invalidate =
    if commandTextOrPath |> Seq.exists (fun c-> invalidChars.Contains c)
    then commandTextOrPath, None
    else
        let path = Path.Combine(resolutionFolder, commandTextOrPath)
        if File.Exists(path) |> not 
        then commandTextOrPath, None
        else
            if  Path.GetExtension(commandTextOrPath) <> ".sql" then failwith "Only files with .sql extension are supported"
            let watcher = new FileSystemWatcher(Filter = commandTextOrPath, Path = resolutionFolder)
            watcher.Changed.Add(fun _ -> invalidate())
            watcher.EnableRaisingEvents <- true                    
            File.ReadAllText(path), Some watcher
            <commit_msg>Fixed bug where external files could not be located in a subfolder.<commit_after>﻿module FSharp.Data.SqlClient.Configuration

open System.Configuration
open System.IO
open System

let getConnectionString resolutionFolder connectionString connectionStringName configFile  =
    if connectionString <> "" then connectionString
    else
        if connectionStringName = "" then failwithf "Either ConnectionString or ConnectionStringName is required"
        let getMappedConfig file = 
            let path = Path.Combine(resolutionFolder, file)            
            let map = new ExeConfigurationFileMap()
            map.ExeConfigFilename <- path
            ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).ConnectionStrings.ConnectionStrings.[connectionStringName]
        let configSection = 
            match configFile, ConfigurationManager.ConnectionStrings.[connectionStringName] with
            | "", null -> 
                let c = getMappedConfig "app.config"
                if c = null then getMappedConfig "web.config" else c
            | "", c -> c
            | file, _ -> getMappedConfig file
        if configSection = null 
        then failwithf "Connection string %s is not found." connectionStringName
        else configSection.ConnectionString


let private invalidChars = [ 
    yield! Path.GetInvalidPathChars()] |> set

let parseTextAtDesignTime (commandTextOrPath : string) resolutionFolder invalidate =
    if commandTextOrPath |> Seq.exists (fun c-> invalidChars.Contains c)
    then commandTextOrPath, None
    else
        let path = Path.Combine(resolutionFolder, commandTextOrPath)
        if File.Exists(path) |> not 
        then commandTextOrPath, None
        else
            if  Path.GetExtension(commandTextOrPath) <> ".sql" then failwith "Only files with .sql extension are supported"
            let watcher = new FileSystemWatcher(Filter = commandTextOrPath, Path = resolutionFolder)
            watcher.Changed.Add(fun _ -> invalidate())
            watcher.EnableRaisingEvents <- true                    
            File.ReadAllText(path), Some watcher
            
{'fraction_non_alphanumeric': 0.06735026800279655, 'fraction_numerical': 0.0, 'mean_word_length': 2.7289313640312773, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1199254', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1011, 'n_words': 297}