private void CreateButton_Click(object sender, EventArgs e) { bool ok = true; string name = nameTB.Text.TrimStart(); if (string.IsNullOrEmpty(name)) { nameTB.BorderColor = Color.Red; ok = false; } if (string.IsNullOrEmpty(pathTB.Text)) { pathTB.BorderColor = Color.Red; ok = false; } if (!ok) { return; } string path = fs.Combine(pathTB.Text, name); if (fs.ExistsFile(path + ".mhdb")) { errorbox.Show( $@"[MochaDB-Studio] A database with this name already exists on this path."); return; } try { MochaDatabase.CreateMochaDB(path, descriptionTB.Text, passwordTB.Text); MochaDatabase db = new MochaDatabase($"path={path}; password={passwordTB.Text};logs=false;AutoConnect=true"); var connectionPanel = new cncpanel(db); CNCList.AddItem(new sbutton() { Text = name, Tag = connectionPanel }); Close(); } catch (MochaException excep) { if (excep.Message == "The password did not meet the password conventions!") { passwordTB.BorderColor = Color.Red; } errorbox.Show("[MochaException]\n" + excep.Message); } catch (Exception excep) { errorbox.Show("[Exception]\n" + excep.Message + excep); } }
/// <summary> /// Runs the active MochaQ query. Even if there is an incoming value, it will not return. /// </summary> public void Run() { if (!MochaQ.IsRunQuery()) { throw new MochaException(@"This MochaQ command is not ""Run"" type command."); } Database.OnConnectionCheckRequired(this, new EventArgs()); if (Database.Provider.Readonly) { throw new MochaException("This connection is can read only, cannot task of write!"); } //Check null. if (string.IsNullOrEmpty(MochaQ)) { throw new MochaException("This MochaQ query is empty, invalid!"); } //Check BREAKQUERY. if (MochaQ.Command.Contains("BREAKQUERY")) { return; } string[] queryPaths = MochaQ.Command.Split(':'); queryPaths[0] = queryPaths[0].ToUpperInvariant(); //File system. if (queryPaths[0].StartsWith("FILESYSTEM_")) { if (queryPaths.Length == 1) { if (queryPaths[0] == "FILESYSTEM_CLEARDISKS") { Database.FileSystem.ClearDisks(); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } else if (queryPaths.Length == 2) { if (queryPaths[0] == "FILESYSTEM_REMOVEDISK") { Database.FileSystem.RemoveDisk(queryPaths[1]); return; } else if (queryPaths[0] == "FILESYSTEM_REMOVEDIRECTORY") { Database.FileSystem.RemoveDirectory(queryPaths[1]); return; } else if (queryPaths[0] == "FILESYSTEM_REMOVEFILE") { Database.FileSystem.RemoveFile(queryPaths[1]); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } else if (queryPaths.Length == 3) { if (queryPaths[0] == "FILESYSTEM_CREATEDISK") { Database.FileSystem.CreateDisk(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "FILESYSTEM_CREATEDIRECTORY") { Database.FileSystem.CreateDirectory(queryPaths[1], queryPaths[2]); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } if (queryPaths.Length == 1) { if (queryPaths[0] == "RESETMOCHA") { Database.Reset(); return; } else if (queryPaths[0] == "RESETTABLES") { Database.OnChanging(this, new EventArgs()); IEnumerable <XElement> tableRange = Database.Doc.Root.Element("Tables").Elements(); for (int index = 0; index < tableRange.Count(); index++) { tableRange.ElementAt(index).Elements().Remove(); } Database.Save(); return; } else if (queryPaths[0] == "CLEARSECTORS") { Database.ClearSectors(); return; } else if (queryPaths[0] == "CLEARSTACKS") { Database.ClearStacks(); return; } else if (queryPaths[0] == "CLEARTABLES") { Database.ClearTables(); return; } else if (queryPaths[0] == "CLEARALL") { Database.ClearAll(); return; } else if (queryPaths[0] == "CLEARLOGS") { Database.ClearLogs(); return; } else if (queryPaths[0] == "RESTORETOFIRSTLOG") { Database.RestoreToFirstLog(); return; } else if (queryPaths[0] == "RESTORETOLASTLOG") { Database.RestoreToLastLog(); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } else if (queryPaths.Length == 2) { if (queryPaths[0] == "REMOVETABLE") { Database.RemoveTable(queryPaths[1]); return; } else if (queryPaths[0] == "CREATETABLE") { Database.CreateTable(queryPaths[1]); return; } else if (queryPaths[0] == "CREATESTACK") { Database.AddStack(new MochaStack(queryPaths[1])); return; } else if (queryPaths[0] == "REMOVESECTOR") { Database.RemoveSector(queryPaths[1]); return; } else if (queryPaths[0] == "REMOVESTACK") { Database.RemoveStack(queryPaths[1]); return; } else if (queryPaths[0] == "SETPASSWORD") { Database.SetPassword(queryPaths[1]); return; } else if (queryPaths[0] == "SETDESCRIPTION") { Database.SetDescription(queryPaths[1]); return; } else if (queryPaths[0] == "RESTORETOLOG") { Database.RestoreToLog(queryPaths[1]); return; } else if (queryPaths[0] == "CLEARROWS") { Database.ClearRows(queryPaths[1]); return; } else if (queryPaths[0] == "RESETTABLE") { if (!Database.ExistsTable(queryPaths[1])) { throw new MochaException("Table not found in this name!"); } Database.OnChanging(this, new EventArgs()); Database.Doc.Root.Element("Tables").Elements(queryPaths[1]).Elements().Remove(); Database.Save(); return; } else if (queryPaths[0] == "CREATEMOCHA") { MochaDatabase.CreateMochaDB(Path.Combine(queryPaths[1]) + ".bjay", string.Empty, string.Empty); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } else if (queryPaths.Length == 3) { if (queryPaths[0] == "REMOVECOLUMN") { Database.RemoveColumn(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "REMOVETABLEATTRIBUTE") { Database.RemoveTableAttribute(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "REMOVESTACKATTRIBUTE") { Database.RemoveStackAttribute(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "REMOVESECTORATTRIBUTE") { Database.RemoveSectorAttribute(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "SETSECTORDATA") { Database.SetSectorData(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "SETSECTORDESCRIPTION") { Database.SetSectorDescription(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "SETSTACKDESCRIPTION") { Database.SetStackDescription(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "SETTABLEDESCRIPTION") { Database.SetTableDescription(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "REMOVEROW") { Database.RemoveRow(queryPaths[1], int.Parse(queryPaths[2])); return; } else if (queryPaths[0] == "RENAMETABLE") { Database.RenameTable(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "REMOVESTACKITEM") { Database.RemoveStackItem(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "RENAMESECTOR") { Database.RenameSector(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "RENAMESTACK") { Database.RenameStack(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "CREATECOLUMN") { Database.CreateColumn(queryPaths[1], queryPaths[2]); return; } else if (queryPaths[0] == "CREATEMOCHA") { MochaDatabase.CreateMochaDB(Path.Combine(queryPaths[1] + queryPaths[2]) + ".bjay", string.Empty, string.Empty); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } else if (queryPaths.Length == 4) { if (queryPaths[0] == "RENAMECOLUMN") { Database.RenameColumn(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "REMOVECOLUMNATTRIBUTE") { Database.RemoveColumnAttribute(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "REMOVESTACKITEMATTRIBUTE") { Database.RemoveStackItemAttribute(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "SETCOLUMNDESCRIPTION") { Database.SetColumnDescription(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "SETCOLUMNDATATYPE") { Database.SetColumnDataType(queryPaths[1], queryPaths[2], MochaData.GetDataTypeFromName(queryPaths[3])); return; } else if (queryPaths[0] == "ADDDATA") { Console.WriteLine((queryPaths[3] != "") + "||'" + queryPaths[3] + "'"); if (queryPaths[3] != "") { Database.AddData(queryPaths[1], queryPaths[2], MochaData.GetDataFromString( Database.GetColumnDataType(queryPaths[1], queryPaths[2]) , queryPaths[3])); } else { Database.AddData(queryPaths[1], queryPaths[2], null); } return; } else if (queryPaths[0] == "CREATESTACKITEM") { Database.AddStackItem(queryPaths[1], queryPaths[3], new MochaStackItem(queryPaths[2])); return; } else if (queryPaths[0] == "RENAMESTACKITEM") { Database.RenameStackItem(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "SETSTACKITEMVALUE") { Database.SetStackItemValue(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "SETSTACKITEMDESCRIPTION") { Database.SetStackItemDescription(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "UPDATEFIRSTDATA") { Database.UpdateFirstData(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "UPDATELASTDATA") { Database.UpdateLastData(queryPaths[1], queryPaths[2], queryPaths[3]); return; } else if (queryPaths[0] == "ADDSECTOR") { MochaSector Sector = new MochaSector(queryPaths[1], queryPaths[2], queryPaths[3]); Database.AddSector(Sector); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } else if (queryPaths.Length == 5) { if (queryPaths[0] == "UPDATEDATA") { Database.UpdateData(queryPaths[1], queryPaths[2], int.Parse(queryPaths[3]), queryPaths[4]); return; } else { throw new MochaException("Invalid query. The content of the query could not be processed, wrong!"); } } }
/// <summary> /// Runs the active MochaQ query. Even if there is an incoming value, it will not return. /// </summary> public virtual void Run() { Database.OnConnectionCheckRequired(this, new EventArgs()); //Check null. if (string.IsNullOrEmpty(Command)) { return; } string[] queryPaths = Command.Split(':'); queryPaths[0] = queryPaths[0].ToUpperInvariant(); switch (queryPaths.Length) { case 1: switch (queryPaths[0]) { case "RESETMOCHA": Database.Reset(); return; case "RESETTABLES": Database.OnChanging(this, new EventArgs()); foreach (XElement element in Database.CDoc.Root.Element("Tables").Elements()) { element.Elements().Remove(); } Database.Save(); return; case "CLEARTABLES": Database.ClearTables(); return; case "CLEARALL": Database.ClearAll(); return; case "CLEARLOGS": Database.ClearLogs(); return; case "RESTORETOFIRSTLOG": Database.RestoreToFirstLog(); return; case "RESTORETOLASTLOG": Database.RestoreToLastLog(); return; default: throw new InvalidOperationException("Invalid query. The content of the query could not be processed, wrong!"); } case 2: switch (queryPaths[0]) { case "REMOVETABLE": Database.RemoveTable(queryPaths[1]); return; case "CREATETABLE": Database.CreateTable(queryPaths[1]); return; case "SETPASSWORD": Database.SetPassword(queryPaths[1]); return; case "SETDESCRIPTION": Database.SetDescription(queryPaths[1]); return; case "RESTORETOLOG": Database.RestoreToLog(queryPaths[1]); return; case "CLEARROWS": Database.ClearRows(queryPaths[1]); return; case "RESETTABLE": if (!Database.ExistsTable(queryPaths[1])) { throw new Exception("Table not found in this name!"); } Database.OnChanging(this, new EventArgs()); Database.CDoc.Root.Element("Tables").Elements(queryPaths[1]).Elements().Remove(); Database.Save(); return; case "CREATEMOCHA": MochaDatabase.CreateMochaDB(queryPaths[1], string.Empty, string.Empty); return; default: throw new InvalidOperationException("Invalid query. The content of the query could not be processed, wrong!"); } case 3: switch (queryPaths[0]) { case "REMOVECOLUMN": Database.RemoveColumn(queryPaths[1], queryPaths[2]); return; case "SETTABLEDESCRIPTION": Database.SetTableDescription(queryPaths[1], queryPaths[2]); return; case "REMOVEROW": Database.RemoveRow(queryPaths[1], int.Parse(queryPaths[2])); return; case "RENAMETABLE": Database.RenameTable(queryPaths[1], queryPaths[2]); return; case "CREATECOLUMN": Database.CreateColumn(queryPaths[1], queryPaths[2]); return; case "CREATEMOCHA": MochaDatabase.CreateMochaDB(Path.Combine(queryPaths[1] + queryPaths[2]), string.Empty, string.Empty); return; default: throw new InvalidOperationException("Invalid query. The content of the query could not be processed, wrong!"); } case 4: switch (queryPaths[0]) { case "RENAMECOLUMN": Database.RenameColumn(queryPaths[1], queryPaths[2], queryPaths[3]); return; case "SETCOLUMNDESCRIPTION": Database.SetColumnDescription(queryPaths[1], queryPaths[2], queryPaths[3]); return; case "SETCOLUMNDATATYPE": Database.SetColumnDataType(queryPaths[1], queryPaths[2], MochaData.GetDataTypeFromName(queryPaths[3])); return; case "ADDDATA": if (queryPaths[3] != string.Empty) { Database.AddData(queryPaths[1], queryPaths[2], queryPaths[3]); } else { Database.AddData(queryPaths[1], queryPaths[2], null); } return; case "UPDATEFIRSTDATA": Database.UpdateFirstData(queryPaths[1], queryPaths[2], queryPaths[3]); return; case "UPDATELASTDATA": Database.UpdateLastData(queryPaths[1], queryPaths[2], queryPaths[3]); return; default: throw new InvalidOperationException("Invalid query. The content of the query could not be processed, wrong!"); } case 5: switch (queryPaths[0]) { case "UPDATEDATA": Database.UpdateData(queryPaths[1], queryPaths[2], int.Parse(queryPaths[3]), queryPaths[4]); return; default: throw new InvalidOperationException("Invalid query. The content of the query could not be processed, wrong!"); } default: throw new InvalidOperationException("Invalid query. The content of the query could not be processed, wrong!"); } }