You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CREATE TABLE [dbo].[UniqueIdentifier]
(
[Typ] NVARCHAR(50) NOT NULLPRIMARY KEY,
[CurrentMaxId] NVARCHAR(max) NOT NULL
)
seed
IF NOT EXISTS (SELECT1FROM [UniqueIdentifier] WHERE Typ ='Issue')
BEGININSERT INTO [UniqueIdentifier]
VALUES ('Issue', 'A000000')
END
generic UID generator - data access
moduleIdGeneratorDataAccess =openSystemopenSystem.DataopenSystem.Data.LinqopenMicrosoft.FSharp.Data.TypeProvidersopenMicrosoft.FSharp.LinqopenChessie.ErrorHandlingopenSystem.NetopenMicrosoft.FSharp.ReflectionopenDomainModels[<Literal>]letconnectionString="Data Source=(localdb)\V11.0;Initial Catalog=BRAVE;Integrated Security=SSPI;"typeprivatedbSchema= SqlDataConnection<connectionString>letprivatedb(connectionString:string)=letdb= dbSchema.GetDataContext(connectionString)// Enable the logging of database activity to the console.
db.DataContext.Log <- System.Console.Out
db
letgetUid connectionString typ generateNextId =use conn =new System.Data.SqlClient.SqlConnection(connectionString);
conn.Open()use tx = conn.BeginTransaction(IsolationLevel.Serializable)tryletdb= db connectionString
letidOption= db.UniqueIdentifier |> Seq.tryFind (fun x -> x.Typ = typ)match idOption with| Some uid ->letnextId= generateNextId uid.CurrentMaxId
nextId |> Trial.lift (fun id ->
uid.CurrentMaxId <- id
db.DataContext.SubmitChanges()
tx.Commit()
id)| None ->
tx.Rollback()
fail (DbQueryError "Coud not generate id because could not find entry")with| ex ->
tx.Rollback()"Could not generate next id. "+ ex.Message |> IdGenerationFailure |> fail
specific uid generator
letgenerateNextId(current:string)=let(|Int|_|)str =match System.Int32.TryParse(str)with|(true,int)-> Some(int)|_-> None
match current.Substring(1)with| Int i -> current.Substring(0,1)+(i +1).ToString().PadLeft(6,'0')|> ok
|_-> fail (IdGenerationFailure "Could not generate next id. Could not parse to int.")