Commit 18b8e691 authored by Sean's avatar Sean 🎨

Initial data loader

parent 1cfbdc69
// Learn more about F# at http://fsharp.org
open FSharp.Data.Sql
open System
[<Literal>]
let DbVendor = Common.DatabaseProviderTypes.POSTGRESQL
[<Literal>]
let ConnString = "Host=localhost;Port=5432;Database=postgres;Username=test_app;Password=pass"
[<Literal>]
let Schema = "test"
[<Literal>]
let ResPath = __SOURCE_DIRECTORY__ + @"./lib"
[<Literal>]
let IndivAmount = 100
[<Literal>]
let UseOptTypes = true
type DB =
SqlDataProvider<DatabaseVendor=DbVendor, ConnectionString=ConnString, ResolutionPath=ResPath, IndividualsAmount=IndivAmount, UseOptionTypes=UseOptTypes, Owner=Schema>
let ctx =
DB.GetDataContext(selectOperations = SelectOperations.DatabaseSide)
let catDb = ctx.Test
// Domain modelling - normal F#!
/// A type of cat (ignoring attributes for now)
type Breed = {
id : int option
name : string
}
/// KITTEH
type Cat = {
id : int option
name : string
breed : Breed
}
/// Make a cat with just a name and breed name, with None for ids
let mkCat catName breedName =
{ id = None; name = catName; breed = { id = None; name = breedName } }
/// Turn a cat to a string - my how the turn tables!
let catToStr cat =
sprintf "%s(%d)" cat.name (Option.defaultValue -1 cat.id)
// plain tables - query on these or get other functions
let Attributes = catDb.Attributes
let Cats = catDb.Cats
let Breeds = catDb.Breeds
let Owners = catDb.Owners
// join tables
let BreedAttrs = catDb.BreedAttributes
let OwnerCats = catDb.OwnerCats
// creating entity functions
let createBreed = Breeds.``Create(name)``
let createAttribute = Attributes.``Create(description, name)``
let createCat = Cats.``Create(breedid, name)``
let createOwner = Owners.``Create(age, name)``
let createBreedAttr = BreedAttrs.``Create(attributeid, breedid)``
let createOwnerCat = Owners.``Create(age, name)``
let findBreedIdByName bn =
query {
for b in Breeds do
where (b.Name = bn)
take 1
select b.Id
}
// DB functions
let insertBreed breedName () =
let breedEntity = createBreed (breedName)
ctx.SubmitUpdates()
breedEntity.Id
let insertCat cat =
let breedId =
findBreedIdByName cat.breed.name
|> Seq.tryHead
|> Option.defaultWith (insertBreed cat.breed.name)
let catEntity = createCat (breedId, cat.name)
ctx.SubmitUpdates()
{ cat with id = Some catEntity.Id; breed = { cat.breed with id = Some breedId } }
// Helpers
/// Take a function, and a tuple, and call the function with the destructured tuple
let withTuple f (a, b) =
f a b
/// Take a tuple and make a cat in the db, then turn it into a string
let tupleToCatStr =
withTuple mkCat
>> insertCat
>> catToStr
/// Take anything, and give back a zero - a success exit code
let returnZero _ = 0
let rawCatData =
[
("Denton", "Gray Tabby")
("Mitzie", "Tuxedo")
("Saphire", "Blue Russian")
("Ailee", "Siamese")
("Oreo", "Tuxedo")
("Frisky", "Tuxedo")
]
[<EntryPoint>]
let main argv =
printfn "Hello World from F#!"
0 // return an integer exit code
rawCatData
|> List.map tupleToCatStr
|> String.concat ", "
|> printfn "Made a bunch o cats!\n***\n%s\n***\n"
|> returnZero
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment