internal static void ImportSql(Stream sql) { // Import data (we only have CREATE TABLE and INSERT INTO statements) using (StreamReader reader = new StreamReader(sql)) using (BulkTransaction helper = BulkTransaction.Create()) { List <string> buffer = new List <string>(); while (!reader.EndOfStream) { string cmd = NextStatement(reader, buffer); if (cmd == null || cmd.Length == 0) { continue; } int index = cmd.ToUpper().IndexOf("CREATE TABLE"); if (index > -1) { ParseCreate(ref cmd, index); } //Run the command in the transaction. helper.Execute(cmd); } helper.Commit(); } }
unsafe bool DoSaveChanges(List <BlockPos> tempCache, char *ptr, string date, BulkTransaction transaction) { string template = "INSERT INTO `Block" + name + "` (Username, TimePerformed, X, Y, Z, type, deleted) VALUES (@Name, @Time, @X, @Y, @Z, @Tile, @Del)"; ushort x, y, z; IDbCommand cmd = transaction.CreateCommand(template); IDataParameter nameP = transaction.CreateParam("@Name", DbType.AnsiStringFixedLength); cmd.Parameters.Add(nameP); IDataParameter timeP = transaction.CreateParam("@Time", DbType.AnsiStringFixedLength); cmd.Parameters.Add(timeP); IDataParameter xP = transaction.CreateParam("@X", DbType.UInt16); cmd.Parameters.Add(xP); IDataParameter yP = transaction.CreateParam("@Y", DbType.UInt16); cmd.Parameters.Add(yP); IDataParameter zP = transaction.CreateParam("@Z", DbType.UInt16); cmd.Parameters.Add(zP); IDataParameter tileP = transaction.CreateParam("@Tile", DbType.Byte); cmd.Parameters.Add(tileP); IDataParameter delP = transaction.CreateParam("@Del", DbType.Boolean); cmd.Parameters.Add(delP); bool isNative = transaction is NativeBulkTransaction; for (int i = 0; i < tempCache.Count; i++) { BlockPos bP = tempCache[i]; IntToPos(bP.index, out x, out y, out z); DateTime time = Server.StartTimeLocal.AddTicks((bP.flags >> 2) * TimeSpan.TicksPerSecond); MakeInt(time.Year, 4, 0, ptr); MakeInt(time.Month, 2, 5, ptr); MakeInt(time.Day, 2, 8, ptr); MakeInt(time.Hour, 2, 11, ptr); MakeInt(time.Minute, 2, 14, ptr); MakeInt(time.Second, 2, 17, ptr); // For NativeParameter, we make the optimisation of avoiding boxing primitive types. if (!isNative) { nameP.Value = bP.name; timeP.Value = date; xP.Value = x; yP.Value = y; zP.Value = z; tileP.Value = (bP.flags & 2) != 0 ? Block.custom_block : bP.rawType; delP.Value = (bP.flags & 1) != 0; } else { ((NativeParameter)nameP).SetString(bP.name); ((NativeParameter)timeP).SetString(date); ((NativeParameter)xP).U16Value = x; ((NativeParameter)yP).U16Value = y; ((NativeParameter)zP).U16Value = z; ((NativeParameter)tileP).U8Value = (bP.flags & 2) != 0 ? Block.custom_block : bP.rawType; ((NativeParameter)delP).BoolValue = (bP.flags & 1) != 0; } if (!BulkTransaction.Execute(template, cmd)) { cmd.Dispose(); cmd.Parameters.Clear(); transaction.Rollback(); return(false); } } cmd.Dispose(); cmd.Parameters.Clear(); transaction.Commit(); return(true); }
static void ImportBulk(StreamReader reader) { BulkTransaction helper = null; List <string> buffer = new List <string>(); try { helper = Database.Backend.CreateBulk(); while (!reader.EndOfStream) { string cmd = NextStatement(reader, buffer); if (cmd == null || cmd.Length == 0) { continue; } int index = cmd.ToUpper().IndexOf("CREATE TABLE"); if (index > -1) { cmd = cmd.Remove(0, index); cmd = cmd.Replace(" unsigned", " UNSIGNED"); Database.Backend.ParseCreate(ref cmd); } //Run the command in the transaction. if (helper.Execute(cmd)) { continue; } // Something went wrong.. commit what we've imported so far. // We need to recreate connection otherwise every helper.Execute fails helper.Commit(); helper.Dispose(); helper = Database.Backend.CreateBulk(); } helper.Commit(); } finally { if (helper != null) { helper.Dispose(); } } }
public override void Use(Player p, string message) { if (message == "" || message.IndexOf(' ') == -1) { Help(p); return; } Level foundLevel = LevelInfo.Find(message.Split(' ')[0]); if (foundLevel == null) { Player.SendMessage(p, "Level not found"); return; } string newName = message.Split(' ')[1]; if (LevelInfo.ExistsOffline(newName)) { Player.SendMessage(p, "Level already exists."); return; } if (foundLevel == Server.mainLevel) { Player.SendMessage(p, "Cannot rename the main level."); return; } foundLevel.Unload(); File.Move(LevelInfo.LevelPath(foundLevel.name), LevelInfo.LevelPath(newName)); try { File.Move(LevelInfo.LevelPath(foundLevel.name) + ".backup", LevelInfo.LevelPath(newName) + ".backup"); } catch { } try { File.Move("levels/level properties/" + foundLevel.name + ".properties", "levels/level properties/" + newName + ".properties"); } catch { } try { File.Move("levels/level properties/" + foundLevel.name, "levels/level properties/" + newName + ".properties"); } catch { } try { if (File.Exists("blockdefs/lvl_" + foundLevel.name + ".json")) { File.Move("blockdefs/lvl_" + foundLevel.name + ".json", "blockdefs/lvl_" + newName + ".json"); } } catch {} //Move and rename backups try { string foundLevelDir, newNameDir; for (int i = 1; ; i++) { foundLevelDir = LevelInfo.BackupPath(foundLevel.name, i.ToString()); newNameDir = LevelInfo.BackupPath(newName, i.ToString()); if (File.Exists(foundLevelDir + foundLevel.name + ".lvl")) { Directory.CreateDirectory(newNameDir); File.Move(foundLevelDir + foundLevel.name + ".lvl", newNameDir + newName + ".lvl"); if (DirectoryEmpty(foundLevelDir)) { Directory.Delete(foundLevelDir); } } else { if (DirectoryEmpty(Server.backupLocation + "/" + foundLevel.name + "/")) { Directory.Delete(Server.backupLocation + "/" + foundLevel.name + "/"); } break; } } } catch { } //safe against SQL injections because foundLevel is being checked and, //newName is being split and partly checked on illegal characters reserved for Windows. if (Server.useMySQL) { Database.executeQuery(String.Format("RENAME TABLE `Block{0}` TO `Block{1}`, " + "`Portals{0}` TO `Portals{1}`, " + "`Messages{0}` TO `Messages{1}`, " + "`Zone{0}` TO `Zone{1}`", foundLevel.name.ToLower(), newName.ToLower())); } else { using (BulkTransaction helper = SQLiteBulkTransaction.Create()) { // ensures that it's either all work, or none work. helper.Execute(String.Format("ALTER TABLE `Block{0}` RENAME TO `Block{1}`", foundLevel.name.ToLower(), newName.ToLower())); helper.Execute(String.Format("ALTER TABLE `Portals{0}` RENAME TO `Portals{1}`", foundLevel.name.ToLower(), newName.ToLower())); helper.Execute(String.Format("ALTER TABLE `Messages{0}` RENAME TO `Messages{1}`", foundLevel.name.ToLower(), newName.ToLower())); helper.Execute(String.Format("ALTER TABLE `Zone{0}` RENAME TO `Zone{1}`", foundLevel.name.ToLower(), newName.ToLower())); helper.Commit(); } } try { Command.all.Find("load").Use(p, newName); } catch { } Player.GlobalMessage("Renamed " + foundLevel.name + " to " + newName); }