public virtual BsonDocument PreSave(BsonDocument doc, string[] references = null) { var tempId = doc.GetString("_id", ""); if (tempId == "" || tempId == "0" || doc.Get("_id") == BsonNull.Value || tempId == null) { doc.Set("_id", SequenceNo.Get(this.TableName).ClaimAsInt()); } return(doc); }
public static SequenceNo Get(string id) { var q = new FilterDefinitionBuilder <BsonDocument>().Eq("_id", id); SequenceNo ret = DataHelper.Populate <SequenceNo>("SequenceNos", q).FirstOrDefault(); if (ret == null) { ret = new SequenceNo { _id = id, Title = id, NextNo = 1, Format = "" }; } return(ret); }
public int ClaimAsInt(bool commit = true) { int ret = 0; var q = new FilterDefinitionBuilder <BsonDocument>().Eq("_id", _id); SequenceNo sn = DataHelper.Populate <SequenceNo>("SequenceNos", q).FirstOrDefault(); if (sn != null) { NextNo = sn.NextNo; } ret = NextNo; if (commit) { this.NextNo++; DataHelper.Save("SequenceNos", new BsonDocument[] { this.ToBsonDocument() }); } return(ret); }
public string ClaimAsString(string format = "", bool commit = true) { string ret = ""; var q = new FilterDefinitionBuilder <BsonDocument>().Eq("_id", _id); SequenceNo sn = DataHelper.Populate <SequenceNo>("SequenceNos", q).FirstOrDefault(); if (sn != null) { NextNo = sn.NextNo; } if (String.IsNullOrEmpty(format)) { format = Format; } ret = (format.Equals("")) ? NextNo.ToString() : String.Format(format, NextNo); if (commit) { this.NextNo++; DataHelper.Save("SequenceNos", new BsonDocument[] { this.ToBsonDocument() }); } return(ret); }