static void ImportProvinceLayer(string path) { using (var shapefile = new Shapefile(path)) { foreach (var shape in shapefile) { ShapePolygon polygon = shape as ShapePolygon; Province province = new Province(); province.Name = shape.GetMetadata("name"); province.Country = shape.GetMetadata("country"); province.Shape = Poly2Str(polygon.Parts); province.ID = db.Id(1); long len = 0; foreach (var i in polygon.Parts) { len = len + i.Length; } Assert.Test(len < 65535, () => province.Country + len); db.Insert <Province>("Province", province); } } }
public CommandResult Execute(NewPostCommand command) { var markdown = new MarkdownSharp.Markdown(); //TODO:应该验证TitleSlug是否唯一 var post = new BlogPost { Id = ObjectId.NewObjectId(), AuthorEmail = command.Author.Email, AuthorDisplayName = command.Author.DisplayName, MarkDown = command.MarkDown, Content = markdown.Transform(command.MarkDown), PubDate = command.PubDate.CloneToUtc(), Status = command.Published ? PublishStatus.Published : PublishStatus.Draft, Title = command.Title, TitleSlug = command.TitleSlug.IsNullOrWhitespace() ? command.Title.Trim().ToSlug() : command.TitleSlug.Trim().ToSlug(), DateUTC = DateTime.UtcNow }; if (!command.Tags.IsNullOrWhitespace()) { var tags = command.Tags.Trim().Split(',').Select(s => s.Trim()); post.Tags = tags.Select(s => s.ToSlug()).ToArray(); foreach (var tag in tags) { var slug = tag.ToSlug(); var tagEntry = _db.SelectKey <Tag>(DBTableNames.Tags, slug); if (tagEntry == null) { tagEntry = new Tag { Slug = slug, Name = tag, PostCount = 1 }; _db.Insert(DBTableNames.Tags, tagEntry); } else { tagEntry.PostCount++; _db.Update(DBTableNames.Tags, tagEntry); } } } else { post.Tags = new string[] { } }; var result = _db.Insert(DBTableNames.BlogPosts, post); return(CommandResult.SuccessResult); } }
public LoginCommandResult Execute(LoginCommand loginCommand) { var hashedPassword = Hasher.GetMd5Hash(loginCommand.Password); if (_db.SelectCount("from " + DBTableNames.Authors) == 0) { _db.Insert(DBTableNames.Authors, new Author { Email = "*****@*****.**", DisplayName = "mzblog", Roles = new[] { "admin" }, HashedPassword = Hasher.GetMd5Hash("mzblog") }); } var author = from u in _db.Select <Author>("from " + DBTableNames.Authors) where u.Email == loginCommand.Email && u.HashedPassword == hashedPassword select u; if (author.Count() > 0) { return new LoginCommandResult() { Author = author.FirstOrDefault() } } ; return(new LoginCommandResult(trrorMessage: "用户名或密码不正确") { }); } }
public string CreateTick(string key) { var tick = ObjectId.NewObjectId().ToString(); var spamHash = new SpamHash { Id = tick, PostKey = key, CreatedTime = DateTime.UtcNow }; _db.Insert(DBTableNames.SpamHashes, spamHash); return(tick); }
/// <summary> /// 保存一条数据 /// </summary> public void SaveData <T>(T dbObj, object keyValue) where T : class { Type type = typeof(T); string tableName = type.Name; string keyName = _keyMap[type]; if (_autoBox.SelectCount(string.Format("from {0} where {1} == ?", tableName, keyName), keyValue) <= 0) { _autoBox.Insert(tableName, dbObj); } else { _autoBox.Update(tableName, dbObj); } }
public CommandResult Execute(NewCommentCommand command) { if (_spamShield.IsSpam(command.SpamShield)) { return(new CommandResult("You are a spam!")); } var comment = new BlogComment { Id = command.Id, Email = command.Email, NickName = command.NickName, Content = command.Content, IPAddress = command.IPAddress, PostId = command.PostId, SiteUrl = command.SiteUrl, CreatedTime = DateTime.UtcNow }; var result = _db.Insert(DBTableNames.BlogComments, comment); return(CommandResult.SuccessResult); }
public static bool Test(bool background) { var bakAddr = 0 - Math.Abs(DateTime.Now.Ticks); DDebug.DeleteDBFiles(1); DB server = new DB(1); server.SetBoxRecycler(new FileBackupBoxRecycler()); server.GetConfig().EnsureTable <DBObject>("DBObject", "ID"); DB.AutoBox auto = server.Open(); Parallel.For(0, 300, (i) => { var obj = new DBObject(); obj.ID = auto.NewId(0); obj.Value = "Value " + obj.ID; obj.DT = DateTime.Now; auto.Insert("DBObject", obj); }); // Export if (background) { Thread backupThread = new Thread(() => { ((FileBackupBoxRecycler)auto.GetDatabase().GetBoxRecycler()).Phase1(auto.GetDatabase(), bakAddr); }); backupThread.Start(); Parallel.For(0, 300, (i) => { var obj = new DBObject(); obj.ID = auto.NewId(0); obj.Value = "Value " + obj.ID; obj.DT = DateTime.Now; auto.Insert("DBObject", obj); }); backupThread.Join(); ((FileBackupBoxRecycler)auto.GetDatabase().GetBoxRecycler()).Phase2(); } else { ((FileBackupBoxRecycler)auto.GetDatabase().GetBoxRecycler()).Backup(auto.GetDatabase(), bakAddr); } //Import DB bakserver = new DB(bakAddr); bakserver.GetConfig().DBConfig.SwapFileBuffer = 0; DB.AutoBox bakauto = bakserver.Open(); DBObject[] s1 = auto.Select <DBObject>("from DBObject").ToArray(); DBObject[] s2 = bakauto.Select <DBObject>("from DBObject").ToArray(); server.Dispose(); bakserver.Dispose(); DDebug.DeleteDBFiles(bakAddr); return(s1.SequenceEqual(s2)); }
public CommandResult Execute(EditPostCommand command) { var post = _db.SelectKey <BlogPost>(DBTableNames.BlogPosts, command.PostId); if (post == null) { throw new ApplicationException("Post with id: {0} was not found".FormatWith(command.PostId)); } if (post.Tags != null) { foreach (var tag in post.Tags) { var slug = tag.ToSlug(); var tagEntry = _db.SelectKey <Tag>(DBTableNames.Tags, slug); if (tagEntry != null) { tagEntry.PostCount--; _db.Update(DBTableNames.Tags, tagEntry); } } } var markdown = new MarkdownSharp.Markdown(); //TODO:应该验证TitleSlug是否是除了本文外唯一的 post.MarkDown = command.MarkDown; post.Content = markdown.Transform(command.MarkDown); post.PubDate = command.PubDate.CloneToUtc(); post.Status = command.Published ? PublishStatus.Published : PublishStatus.Draft; post.Title = command.Title; post.TitleSlug = command.TitleSlug.Trim().ToSlug(); if (!command.Tags.IsNullOrWhitespace()) { var tags = command.Tags.Trim().Split(',').Select(s => s.Trim()); post.Tags = tags.Select(s => s.ToSlug()).ToArray(); foreach (var tag in tags) { var slug = tag.ToSlug(); var tagEntry = _db.SelectKey <Tag>(DBTableNames.Tags, slug); if (tagEntry == null) { tagEntry = new Tag { Slug = slug, Name = tag, PostCount = 1 }; _db.Insert(DBTableNames.Tags, tagEntry); } else { tagEntry.PostCount++; _db.Update(DBTableNames.Tags, tagEntry); } } } else { post.Tags = new string[] { } }; _db.Update(DBTableNames.BlogPosts, post); return(CommandResult.SuccessResult); } }
/// <summary> /// Saves the data list. /// </summary> /// <typeparam name="T">The type of data.</typeparam> /// <param name="tableName">Name of the table.</param> /// <param name="dataList">The data list.</param> /// <param name="index">The index.</param> private static void SaveDataList <T>(string tableName, List <ConfigMetadata> dataList, int index) where T : class { // Create new database. Type type = ReflectionUtility.GetType(GetMetadataFullName(tableName)); long localAddress = ConfigMetadata.IndexTableLocalAddress + index + 1; if (type != null) { // Insert table index. if (tableIndexDB != null) { bool success = tableIndexDB.Insert(MetadataLocalAddress.TableName, new MetadataLocalAddress() { typeName = type.Name, typeNamespace = type.Namespace, localAddress = localAddress }); if (success) { // Insert data list into new table by localAddress. DB dataServer = new DB(localAddress); if (metadataKeyFormatter != null) { primaryKey = metadataNameFormatter(primaryKey); } ReflectionUtility.InvokeGenericMethod(dataServer.GetConfig(), "EnsureTable", type, new object[] { tableName, new string[] { primaryKey } }); dataServer.MinConfig(); DB.AutoBox dataDb = dataServer.Open(); foreach (object configMetadata in dataList) { success = dataDb.Insert(tableName, (T)configMetadata); if (success) { if (debugMode) { Debug.LogFormat("Insert Metadata object success: [tableName={0}, type={1}, object={2}]", tableName, type.FullName, configMetadata); } } else { Debug.LogErrorFormat("Insert Metadata object failed: [tableName={0}, type={1}]", tableName, type.FullName); } } dataServer.Dispose(); dataServer = null; } else { Debug.LogErrorFormat("Insert MetadataLocalAddress object into table [{0}] failed: [typeName={1}, localAddress={2}]", tableName, type.FullName, localAddress); } } } }