Skip to content

Instantly share code, notes, and snippets.

@vshapenko
Created April 25, 2017 14:13
Show Gist options
  • Save vshapenko/5c6611e157916a0a35a853bc37a854ec to your computer and use it in GitHub Desktop.
Save vshapenko/5c6611e157916a0a35a853bc37a854ec to your computer and use it in GitHub Desktop.
#r "System"
#r "System.IO"
#r "mscorlib.dll"
open System.Linq
open System.Collections
open System.Text.RegularExpressions
System.IO.Directory.SetCurrentDirectory (__SOURCE_DIRECTORY__)
let data=System.IO.Directory.EnumerateFiles("Data","*.sql")
let file=System.IO.File.Open("""Commands.fs""",System.IO.FileMode.Create)
printfn "file created"
let stream=new System.IO.StreamWriter(file)
stream.WriteLine("namespace Test")
stream.WriteLine("open FSharp.Data")
stream.WriteLine("module Commands=")
let getParameters (fileName:string)=
let reader=new System.IO.StreamReader(fileName)
let data=reader.ReadToEnd()
(System.Text.RegularExpressions.Regex.Matches(data,@"@[^\s]+") :> IEnumerable).OfType<Match>()|>Seq.map (fun x->x.Value.Trim('@'))
let getCommand fileName name=
sprintf """ type private %s = SqlCommandProvider<"%s",connStr>""" name fileName
let getInvokeCommand parameters cmd=
let paramstr = String.concat "," parameters
sprintf """ let Execute%s(%s) =
let cmd=new %s(connStr)
cmd.Execute(%s) """ cmd paramstr cmd paramstr
data|> Seq.iter(fun fileName->let name = System.IO.Path.GetFileNameWithoutExtension(fileName)
let parameters=getParameters fileName
[getCommand fileName;getInvokeCommand parameters]
|>Seq.iter (fun x-> let s= x name
stream.WriteLine(s))
stream.WriteLine()
)
stream.Flush()
stream.Close()
printfn "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment