<commit_before>﻿module PostgresDoc.Doc

open Npgsql
open Newtonsoft.Json

[<CLIMutable>]
type Store = { connString: string }

type KeyedValue<'a> = 'a * obj

type Operation<'a> =
    | Insert of KeyedValue<'a>
    | Update of KeyedValue<'a>
    | Delete of KeyedValue<'a>

type UnitOfWork<'a> = Operation<'a> list

let insert (key:'a) (value:'b) =
    Insert (key, box value)

let update (key:'a) (value:'b) =
    Update (key, box value)

let delete (key:'a) (value:'b) =
    Delete (key, box value)

let private tableName o = 
    o.GetType().Name

let commit (store:Store) (uow:UnitOfWork<'a>) = 
    use conn = new NpgsqlConnection(store.connString)
    conn.Open()
    let transaction = conn.BeginTransaction()

    let insert id o =
        let pattern = o |> tableName |> sprintf "insert into %s (id, data) values(:id, :data)"
        let command = new NpgsqlCommand(pattern, conn)
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "id", Value = id)) |> ignore
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "data", Value = JsonConvert.SerializeObject(o))) |> ignore
        command.ExecuteNonQuery()

    let update id o = 
        let pattern = o |> tableName |> sprintf "update %s set data = :data where id = :id"
        let command = new NpgsqlCommand(pattern, conn)
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "data", Value = JsonConvert.SerializeObject(o))) |> ignore
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "id", Value = id)) |> ignore
        command.ExecuteNonQuery()

    let delete id o =
        let pattern = o |> tableName |> sprintf "delete from %s where id = :id"<commit_msg>Missed a 'use'<commit_after>﻿module PostgresDoc.Doc

open Npgsql
open Newtonsoft.Json

[<CLIMutable>]
type Store = { connString: string }

type KeyedValue<'a> = 'a * obj

type Operation<'a> =
    | Insert of KeyedValue<'a>
    | Update of KeyedValue<'a>
    | Delete of KeyedValue<'a>

type UnitOfWork<'a> = Operation<'a> list

let insert (key:'a) (value:'b) =
    Insert (key, box value)

let update (key:'a) (value:'b) =
    Update (key, box value)

let delete (key:'a) (value:'b) =
    Delete (key, box value)

let private tableName o = 
    o.GetType().Name

let commit (store:Store) (uow:UnitOfWork<'a>) = 
    use conn = new NpgsqlConnection(store.connString)
    conn.Open()
    use transaction = conn.BeginTransaction()

    let insert id o =
        let pattern = o |> tableName |> sprintf "insert into %s (id, data) values(:id, :data)"
        let command = new NpgsqlCommand(pattern, conn)
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "id", Value = id)) |> ignore
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "data", Value = JsonConvert.SerializeObject(o))) |> ignore
        command.ExecuteNonQuery()

    let update id o = 
        let pattern = o |> tableName |> sprintf "update %s set data = :data where id = :id"
        let command = new NpgsqlCommand(pattern, conn)
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "data", Value = JsonConvert.SerializeObject(o))) |> ignore
        command.Parameters.Add(new NpgsqlParameter(ParameterName = "id", Value = id)) |> ignore
        command.ExecuteNonQuery()

    let delete id o =
        let pattern = o |> tableName |> sprintf "delete from %s where id = :id"
{'fraction_non_alphanumeric': 0.12993177098783742, 'fraction_numerical': 0.0, 'mean_word_length': 3.4780876494023905, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 41, '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': '7294881', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1095, 'n_words': 311}