(* * This file is part of the ExtensionDsLab project. * Copyright (c) 2017 LogicalHacking.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . *) namespace LogicalHacking.ExtensionDsLab.Archive open FSharp.Data.Sql open System.IO module SqlConnector = let [] DevelopmentDB = "aa-ac.sqlite" let [] FullDB = "full.sqlite" let [] DatabaseDir = "archive/db" // static (compile time) ConnectionString let [] ConnectionString = @"Data Source=" + __SOURCE_DIRECTORY__ + @"/../resources/extensions-schema.sqlite" + @";Version=3" // create a type alias with the connection string and database vendor settings type ExtensionDbProvider = SqlDataProvider< ConnectionString = ConnectionString, DatabaseVendor = Common.DatabaseProviderTypes.SQLITE, // ResolutionPath = ResolutionPath, IndividualsAmount = 500, UseOptionTypes = true > type ExtensionDbType = ExtensionDbProvider.dataContext type Db = Dev | Prod | Auto | Custom of string let rec getCtx dir = let archiveDir = function | None -> __SOURCE_DIRECTORY__ + @"/../.." | Some s -> s function | Dev -> ExtensionDbProvider.GetDataContext(sprintf "Data Source=%s/%s/%s;Version=3" (archiveDir dir) DatabaseDir DevelopmentDB) | Prod -> ExtensionDbProvider.GetDataContext(sprintf "Data Source=%s/%s/%s;Version=3" (archiveDir dir) DatabaseDir FullDB) | Custom db -> ExtensionDbProvider.GetDataContext(sprintf "Data Source=%s/%s/%s;Version=3" (archiveDir dir) DatabaseDir db) | Auto -> if File.Exists(sprintf "%s/%s/%s" (archiveDir dir) DatabaseDir FullDB) then getCtx dir Prod else getCtx dir Dev let getDownloads (ctx:ExtensionDbType) extid = query { for order in ctx.Main.Extension do where (order.Extid = Some extid) select (order.Date, order.Downloads) } |> Seq.filter (fun (date, download) -> date.IsSome && download.IsSome) |> Seq.map (fun (date, download) -> (System.DateTime.Parse(date.Value), download.Value)) |> Seq.toArray let getAllExtIds (ctx:ExtensionDbType) = query { for ext in ctx.Main.Extension do select ext.Extid } |> Seq.distinct |> Seq.choose id |> Seq.toList