<commit_before>﻿#r "bin\debug\FsSql.dll"
#r "bin\debug\System.Data.SQlite.dll"

open System.Data

let openConn() =
    let conn = new System.Data.SQLite.SQLiteConnection("Data Source=test.db;Version=3;New=True;")
    conn.Open()
    conn :> IDbConnection

let connMgr = Sql.withNewConnection openConn
let exec a = Sql.execNonQuery connMgr a [] |> ignore
exec "drop table if exists user"
exec "create table user (id int primary key not null, name varchar not null, address varchar null)"

let insertUser connMgr id name address = 
    Sql.execNonQuery connMgr 
        "insert into user (id,name,address) values (@id,@name,@address)"
        (Sql.parameters ["@id", box id; "@name", box name; "@address", Sql.writeOption address])

let insertNUsers n conn =
    let insertUser = insertUser conn
    for i in 1..n do
        let name = sprintf "pepe %d" i
        let address = 
            if i % 2 = 0
                then None
                else Some (sprintf "fake st %d" i)
        insertUser i name address |> ignore

let insertNUsers2 n = insertNUsers n |> Sql.transactional connMgr

insertNUsers2 50 ()

let countUsers(): int64 =
    Sql.execScalar connMgr "select count(*) from user" []

printfn "%d users" (countUsers())

let printUser (dr: IDataRecord) =
    let id = (dr?id).Value
    let name = (dr?name).Value
    let address = 
        match dr?address with
        | None -> "No registered address"
        | Some x -> x
    printfn "Id: %d; Name: %s; Address: %s" id name address

Sql.execReader connMgr "select * from user" []
|> Seq.ofDataReader
|> Seq.iter printUser<commit_msg>partial application of common functions. Demo of execNonQueryf<commit_after>﻿#r "bin\debug\FsSql.dll"
#r "bin\debug\System.Data.SQlite.dll"

open System.Data

let openConn() =
    let conn = new System.Data.SQLite.SQLiteConnection("Data Source=test.db;Version=3;New=True;")
    conn.Open()
    conn :> IDbConnection

let connMgr = Sql.withNewConnection openConn
let inTransaction a = Sql.transactional connMgr a
let execScalar sql = Sql.execScalar connMgr sql
let execReader sql = Sql.execReader connMgr sql
let execReaderf sql = Sql.execReaderF connMgr sql
let execNonQueryf sql = Sql.execNonQueryF connMgr sql
let exec a = Sql.execNonQuery connMgr a [] |> ignore
exec "drop table if exists user"
exec "create table user (id int primary key not null, name varchar not null, address varchar null)"

let insertUser connMgr id name address = 
    Sql.execNonQuery connMgr 
        "insert into user (id,name,address) values (@id,@name,@address)"
        (Sql.parameters ["@id", box id; "@name", box name; "@address", Sql.writeOption address])

let insertNUsers n conn =
    let insertUser = insertUser conn
    for i in 1..n do
        let name = sprintf "pepe %d" i
        let address = 
            if i % 2 = 0
                then None
                else Some (sprintf "fake st %d" i)
        insertUser i name address |> ignore

let insertNUsers2 n = insertNUsers n |> inTransaction

insertNUsers2 50 ()

let countUsers(): int64 =
    execScalar "select count(*) from user" []

printfn "%d users" (countUsers())

let printUser (dr: IDataRecord) =
    let id = (dr?id).Value
    let name = (dr?name).Value
    let address = 
        match dr?address with
        | None -> "No registered address"
        | Some x -> x
    printfn "Id: %d; Name: %s; Address: %s" id name address

execReader "select * from user" []
|> Seq.ofDataReader
|> Seq.iter printUser

execNonQueryf "delete from user where id > %d" 10 |> ignore

printfn "Now there are %d users" (countUsers())
{'fraction_non_alphanumeric': 0.10787335388063883, 'fraction_numerical': 0.006164191650322219, 'mean_word_length': 3.1081703107019565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '624804', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1121, 'n_words': 399}