/// <summary> /// Pass command? /// </summary> /// <param name="command">Command.</param> /// <param name="table">Table.</param> /// <param name="row">Row.</param> /// <param name="from">Use state FROM keyword.</param> public static bool Pass(string command, MochaTableResult table, MochaRow row, bool from) { string[] parts = Mhql_LEXER.SplitFunctionParameters(command); if (parts.Length != 3) { throw new ArgumentOutOfRangeException("The BETWEEN function can only take 3 parameters!"); } int dex = Mhql_GRAMMAR.GetIndexOfColumn(parts[0], table.Columns, from); decimal range1, range2, value; if (!decimal.TryParse(parts[1].Trim(), out range1) || !decimal.TryParse(parts[2].Trim(), out range2) || !decimal.TryParse(row.Datas[dex].Data.ToString(), out value)) { throw new ArithmeticException("The parameter of the BETWEEN command was not a number!"); } return (range1 <= range2 ? range1 <= value && value <= range2 : range2 <= value && value <= range1); }
/// <summary> /// Pass command? /// </summary> /// <param name="command">Command.</param> /// <param name="table">Table.</param> /// <param name="row">Row.</param> /// <param name="from">Use state FROM keyword.</param> public static bool Pass(string command, MochaTableResult table, MochaRow row, bool from) { string[] parts = Mhql_LEXER.SplitFunctionParameters(command); int dex = Mhql_GRAMMAR.GetIndexOfColumn(parts[0], table.Columns, from); for (int index = 1; index < parts.Length; ++index) { if (row.Datas[dex].Data.ToString().StartsWith(parts[index])) { return(true); } } return(false); }
/// <summary> /// Returns command must result. /// </summary> /// <param name="command">Command.</param> /// <param name="row">Row.</param> public static bool IsPassTable(ref string command, MochaRow row) { command = command.Trim(); if (char.IsNumber(command.FirstChar())) { return(MhqlMust_REGEX.Match( MhqlMust_REGEX.GetCommand(command), GetDataFromCommand(command, row).ToString())); } else if (command.StartsWith("$BETWEEN(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_BETWEEN.Pass(command.Substring(9, command.Length - 10), row)); } else if (command.StartsWith("$BIGGER(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_BIGGER.Pass(command.Substring(8, command.Length - 9), row)); } else if (command.StartsWith("$LOWER(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_LOWER.Pass(command.Substring(7, command.Length - 8), row)); } else if (command.StartsWith("$EQUAL(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_EQUAL.Pass(command.Substring(7, command.Length - 8), row)); } else if (command.StartsWith("$NOTEQUAL(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_NOTEQUAL.Pass(command.Substring(10, command.Length - 11), row)); } else if (command.StartsWith("$STARTW(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_STARTW.Pass(command.Substring(8, command.Length - 9), row)); } else if (command.StartsWith("$ENDW(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_ENDW.Pass(command.Substring(6, command.Length - 7), row)); } else if (command.StartsWith("$CONTAINS(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_CONTAINS.Pass(command.Substring(10, command.Length - 11), row)); } else if (command.StartsWith("$NOTCONTAINS(", StringComparison.OrdinalIgnoreCase) && command.LastChar() == ')') { return(MhqlFunc_NOTCONTAINS.Pass(command.Substring(13, command.Length - 14), row)); } else { throw new MochaException($"'{command}' is cannot processed!"); } }
/// <summary> /// Set datas by row datas. /// </summary> internal 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++) { Columns[columnIndex].Datas.collection.Add(currentRow.Datas[columnIndex]); } } }
/// <summary> /// Pass command? /// </summary> /// <param name="command">Command.</param> /// <param name="row">Row.</param> public static bool Pass(string command, MochaRow row) { var parts = command.Split(','); if (parts.Length < 2 || parts.Length > 2) { throw new MochaException("CONTAINS function is cannot processed!"); } int dex; if (!int.TryParse(parts[0].Trim(), out dex)) { throw new MochaException("CONTAINS function is cannot processed!"); } return(row.Datas[dex].Data.ToString().Contains(parts[1])); }
/// <summary> /// Returns data by command. /// </summary> /// <param name="command">Command.</param> /// <param name="row">Base row.</param> public static MochaData GetDataFromCommand(string command, MochaRow row) { command = command.Trim(); if (!char.IsNumber(command.FirstChar())) { throw new MochaException("Column is not defined!"); } var dex = int.Parse(command.FirstChar().ToString()); if (dex < 0) { throw new MochaException("Index is cannot lower than zero!"); } else if (dex > row.Datas.MaxIndex()) { throw new MochaException("The specified index is more than the number of columns!"); } return(row.Datas[dex]); }
/// <summary> /// Must by tables. /// </summary> /// <param name="command">Must command.</param> /// <param name="table">Table.</param> /// <param name="from">Use state FROM keyword.</param> public void MustTable(string command, ref MochaTableResult table, bool from) { command = command.Trim(); List <string> parts = Mhql_AND.GetParts(command); for (int index = 0; index < parts.Count; ++index) { string partcmd = parts[index]; MhqlEng_MUST.ProcessPart(ref partcmd, table, from); List <MochaRow> rows = new List <MochaRow>(); for (int dex = 0; dex < table.Rows.Length; ++dex) { MochaRow row = table.Rows[dex]; if (MhqlEng_MUST.IsPassTable(Tdb, ref partcmd, table, row, @from)) { rows.Add(row); } } table.Rows = rows.ToArray(); } table.SetDatasByRows(); }
/// <summary> /// Pass command? /// </summary> /// <param name="command">Command.</param> /// <param name="row">Row.</param> public static bool Pass(string command, MochaRow row) { var parts = command.Split(','); if (parts.Length < 3 || parts.Length > 3) { throw new MochaException("BETWEEN function is cannot processed!"); } int dex; decimal range1, range2; if (!int.TryParse(parts[0].Trim(), out dex)) { throw new MochaException("BETWEEN function is cannot processed!"); } if (!decimal.TryParse(parts[1].Trim(), out range1)) { throw new MochaException("BETWEEN function is cannot processed!"); } if (!decimal.TryParse(parts[2].Trim(), out range2)) { throw new MochaException("BETWEEN function is cannot processed!"); } decimal value; if (!decimal.TryParse(row.Datas[dex].Data.ToString(), out value)) { throw new MochaException("BETWEEN function is cannot processed!"); } return (range1 <= range2 ? range1 <= value && value <= range2 : range2 <= value && value <= range1); }
/// <summary> /// Returns command must result. /// </summary> /// <param name="tdb">Target database.</param> /// <param name="command">Command.</param> /// <param name="table">Table.</param> /// <param name="row">Row.</param> /// <param name="from">Use state FROM keyword.</param> public static bool IsPassTable(MochaDatabase tdb, ref string command, MochaTableResult table, MochaRow row, bool from) { command = command.Trim(); if (Mhql_INEQ.IsINEQ(command)) { return(Mhql_INEQ.Process(tdb, command, table, row, from, false)); } else if (Mhql_INEQ.IsIN(command)) { return(Mhql_INEQ.Process(tdb, command, table, row, from, true)); } else if (MhqlEng_CONDITION.IsCondition(command, out _)) { return(MhqlEng_CONDITION.Process(command, table, row, from)); } else if (char.IsNumber(command[0])) { return(new Regex(command.Substring(2).Remove(command.Length - 3, 1)) .IsMatch(GetDataFromCommand(command, row).ToString())); } else if (command.StartsWith("BETWEEN(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_BETWEEN.Pass(command.Substring(8, command.Length - 9), table, row, from)); } else if (command.StartsWith("BIGGER(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_BIGGER.Pass(command.Substring(7, command.Length - 8), table, row, from)); } else if (command.StartsWith("LOWER(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_LOWER.Pass(command.Substring(6, command.Length - 7), table, row, from)); } else if (command.StartsWith("EQUAL(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_EQUAL.Pass(command.Substring(6, command.Length - 7), table, row, from)); } else if (command.StartsWith("NOTEQUAL(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_NOTEQUAL.Pass(command.Substring(9, command.Length - 10), table, row, from)); } else if (command.StartsWith("STARTW(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_STARTW.Pass(command.Substring(7, command.Length - 8), table, row, from)); } else if (command.StartsWith("ENDW(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_ENDW.Pass(command.Substring(5, command.Length - 6), table, row, from)); } else if (command.StartsWith("CONTAINS(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_CONTAINS.Pass(command.Substring(9, command.Length - 10), table, row, from)); } else if (command.StartsWith("NOTCONTAINS(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_NOTCONTAINS.Pass(command.Substring(12, command.Length - 13), table, row, from)); } else if (command.StartsWith("NOTSTARTW(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_NOTSTARTW.Pass(command.Substring(10, command.Length - 11), table, row, from)); } else if (command.StartsWith("NOTENDW(", StringComparison.OrdinalIgnoreCase) && command[command.Length - 1] == ')') { return(MhqlMustFunc_NOTENDW.Pass(command.Substring(8, command.Length - 9), table, row, from)); } else { throw new InvalidOperationException($"'{command}' is cannot processed!"); } }
/// <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> /// 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()); } }