Soma 0.1.0.1 のSQLログ機能の紹介
SQLのログを扱う機能はO/Rマッパーに必須だと思っています。
Somaでは、MsSqlConfigのLogメソッドをオーバーライドすることで自由にログを扱えます。扱える情報は3つあります。パラメータつきのSQL、パラメータが実際の値に置き換えられたSQL、パラメータ名をキーとし値とDbTypeのタプルをバリューとするマップです。
こういう感じです。10行目あたりのLogメソッドに注目してください。
open System上のプログラムを実行するとコンソールには次のように出力されます。
open System.Data
open Soma.Core
open Soma.Core.Db
open Soma.Core.Metalet dbConfig =
{ new MsSqlConfig() with
member this.ConnectionStrings = "Data Source=.;Initial Catalog=Soma.Core.IT;Integrated Security=True"
member this.Log(text, formattedText, parameters) =
printfn "text : %s\nformatted text : %s\nparameters : %A" text formattedText parameters }type Employee =
{ [<Id(Kind=IdKind.Identity)>]
EmployeeId : int option
EmployeeName : string
DepartmentId : int
[<Version>]
VersionNo : int }let main =
printfn "\nfind by primary key --------"
let emp = Db.find<Employee> dbConfig [2]Console.ReadKey()
find by primary key -------- text : select EmployeeId, EmployeeName, DepartmentId, VersionNo from Employee where EmployeeId = @p0 formatted text : select EmployeeId, EmployeeName, DepartmentId, VersionNo from Employee where EmployeeId = 2 parameters : map [("@p0", (2, Int32))]
このログはコネクションをオープンする直前で出力しています。
デフォルトだと(Logメソッドをオーバーライドしないと)formatted textだけがコンソールに出力されます。formatted textはコピーしてそのままSQLのツールで実行できるように整形したものです。0.1.0.1は整形の仕方が甘いのですが、今後改善予定です。
簡単なツールはデフォルトのままで、ちょっとしたアプリならロギングライブラリ経由で出力するようにするといいと思います。