/// <summary> /// Set rows by datas of columns. /// </summary> internal protected virtual void SetRowsByDatas() { if (Columns.Length > 0 && Columns[0].Datas.Count > 0) { MochaColumn firstcolumn = Columns[0]; MochaRow[] rows = new MochaRow[firstcolumn.Datas.Count]; //Process rows. for (int dataindex = 0; dataindex < firstcolumn.Datas.Count; ++dataindex) { MochaData[] datas = new MochaData[Columns.Length]; for (int columnindex = 0; columnindex < Columns.Length; ++columnindex) { MochaColumn column = Columns[columnindex]; datas[columnindex] = column.Datas.Count < dataindex + 1 ? new MochaData { data = string.Empty, dataType = MochaDataType.String } : column.Datas[dataindex]; } rows[dataindex] = new MochaRow(datas); } Rows = rows; return; } else { Rows = new MochaRow[0]; } }
/// <summary> /// Set datas by row datas. /// </summary> internal protected virtual void SetDatasByRows() { for (int columnIndex = 0; columnIndex < Columns.Length; ++columnIndex) { Columns[columnIndex].Datas.collection.Clear(); } for (int rowIndex = 0; rowIndex < Rows.Length; ++rowIndex) { MochaRow currentRow = Rows[rowIndex]; for (int columnIndex = 0; columnIndex < Columns.Length; ++columnIndex) { MochaColumn currentColumn = Columns[columnIndex]; if (columnIndex >= currentRow.Datas.Count) { currentRow.Datas.collection.Add(new MochaData() { dataType = currentColumn.DataType, data = MochaData.TryGetData(currentColumn.DataType, null) }); } currentColumn.Datas.collection.Add(currentRow.Datas[columnIndex]); } } }
private void TableGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { if (tableGrid.Updating) { return; } tableGrid.Updating = true; MochaData[] datas = new MochaData[tableGrid.ColumnCount]; for (int index = 0; index < tableGrid.ColumnCount; index++) { MochaDataType dataType = Database.GetColumnDataType(TableName, tableGrid.Columns[index].HeaderText); if (dataType == MochaDataType.AutoInt) { int autoIntValue = Database.GetColumnAutoIntState(TableName, tableGrid.Columns[index].HeaderText); tableGrid.Rows[e.RowIndex - 1].Cells[index].Value = autoIntValue + 1; datas[index] = null; continue; } var data = new MochaData(dataType, MochaData.TryGetData(dataType, "")); datas[index] = data; tableGrid.Rows[e.RowIndex - 1].Cells[index].Value = data.Data; } tableGrid.Tag = datas[tableGrid.CurrentCell.ColumnIndex]; Database.AddRow(TableName, new MochaRow(datas)); tableGrid.Updating = false; }
/// <summary> /// Process in keyword. /// </summary> /// <param name="tdb">Target database.</param> /// <param name="command">Command.</param> /// <param name="table">Destination table.</param> /// <param name="row">Destination row.</param> /// <param name="from">Use state of FROM keyword.</param> /// <param name="inmode">Set true if command is execute as in mode, set false if ineq mode.</param> /// <returns>True if subquery is success, false if not.</returns> public static bool Process(MochaDatabase tdb, string command, MochaTableResult table, MochaRow row, bool from, bool inmode) { command = command.Substring(inmode ? 2 : 4).TrimStart(); int obrace = command.IndexOf(Mhql_LEXER.LBRACE); if (obrace == -1) { throw new Exception($"{Mhql_LEXER.LBRACE} is not found!"); } MochaColumn column = table.Columns[Mhql_GRAMMAR.GetIndexOfColumn( command.Substring(0, obrace).Trim(), table.Columns, from)]; MochaTableResult result = new MochaDbCommand(tdb).ExecuteScalar(Mhql_LEXER.RangeSubqueryBrace( command.Substring(obrace))); if (result.Columns.Length != 1) { throw new Exception("Subqueries should only return one column!"); } else if (MochaData.IsNumericType(column.DataType) != MochaData.IsNumericType(result.Columns[0].DataType) && column.DataType != result.Columns[0].DataType) { throw new Exception("Column data type is not same of subquery result!"); } if (inmode) { for (int index = 0; index < row.Datas.Count; ++index) { for (int rindex = 0; rindex < result.Columns[0].Datas.Count; ++rindex) { if (row.Datas[index].Data.ToString() == result.Columns[0].Datas[rindex].Data.ToString()) { return(true); } } } return(false); } else { if (result.Rows.Length != 1) { return(false); } return(row.Datas[0].Data.ToString() == result.Columns[0].Datas[0].Data.ToString()); } }
/// <summary> /// Compare arguments by compare mark. /// </summary> /// <param name="mark">Compare mark.</param> /// <param name="arg1">Argument 1.</param> /// <param name="arg2">Argument 2.</param> /// <param name="dex">Index of line.</param> internal bool CompareArguments(MochaScriptComparisonMark mark, object arg1, object arg2, int dex) { try { if (mark == MochaScriptComparisonMark.Undefined) { throw Throw(dex + 1, "|| ComparisonMark failed, no such ComparisonMark!"); } if (mark == MochaScriptComparisonMark.Equal) { return(arg1.Equals(arg2)); } if (mark == MochaScriptComparisonMark.NotEqual) { return(!arg1.Equals(arg2)); } var a1 = (decimal)MochaData.GetDataFromString(MochaDataType.Decimal, arg1.ToString()); var a2 = (decimal)MochaData.GetDataFromString(MochaDataType.Decimal, arg2.ToString()); if (mark == MochaScriptComparisonMark.EqualBigger) { return(a1 >= a2); } if (mark == MochaScriptComparisonMark.EqualSmaller) { return(a1 <= a2); } if (mark == MochaScriptComparisonMark.Bigger) { return(a1 > a2); } else { return(a1 < a2); } } catch { return(false); } }
/// <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> /// Returns value. /// </summary> /// <param name="value">Value.</param> /// <param name="table">Table.</param> /// <param name="row">Row.</param> /// <param name="from">Use state FROM keyword.</param> public static Expressional GetValue(string value, MochaTableResult table, MochaRow row, bool from) { if (value.StartsWith("'")) { MhqlEngVal_CHAR.Process(ref value); return(new Expressional { Type = ExpressionType.Char, Value = value }); } else if (value.StartsWith("\"")) { MhqlEngVal_STRING.Process(ref value); return(new Expressional { Type = ExpressionType.String, Value = value }); } else if (value.StartsWith("#")) { decimal val; if (!decimal.TryParse(value.Substring(1).Replace('.', ','), out val)) { throw new ArithmeticException("Value is not arithmetic value!"); } return(new Expressional { Type = ExpressionType.Arithmetic, Value = val }); } else if (value == "TRUE") { return new Expressional { Type = ExpressionType.Boolean, Value = true } } ; else if (value == "FALSE") { return new Expressional { Type = ExpressionType.Boolean, Value = false } } ; if (from) { IEnumerable <MochaColumn> result = table.Columns.Where(x => x.Name == value); if (!result.Any()) { goto index; } MochaData data = row.Datas[Array.IndexOf(table.Columns, result.First())]; return(new Expressional { Type = data.dataType == MochaDataType.Unique || data.dataType == MochaDataType.String || data.dataType == MochaDataType.DateTime ? ExpressionType.String : data.dataType == MochaDataType.Char ? ExpressionType.Char : data.dataType == MochaDataType.Boolean ? ExpressionType.Boolean : ExpressionType.Arithmetic, Value = data.data }); } index: if (!char.IsNumber(value[0])) { throw new ArgumentException("Column is not defined!"); } int dex = int.Parse(value); if (dex < 0) { throw new ArgumentOutOfRangeException("Index is cannot lower than zero!"); } else if (dex > row.Datas.Count - 1) { throw new ArgumentOutOfRangeException("The specified index is more than the number of columns!"); } MochaData _data = row.Datas[dex]; return(new Expressional { Type = _data.dataType == MochaDataType.Unique || _data.dataType == MochaDataType.String || _data.dataType == MochaDataType.DateTime ? ExpressionType.String : _data.dataType == MochaDataType.Char ? ExpressionType.Char : _data.dataType == MochaDataType.Boolean ? ExpressionType.Boolean : ExpressionType.Arithmetic, Value = _data.data }); } #endregion Members }
/// <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!"); } }