/// <summary> /// Read returned results. /// </summary> public MochaReader <object> ExecuteReader() { CheckConnection(); var reader = new MochaReader <object>(); if (!RETURN.IsReturnableCmd()) { return(reader); } bool fromkw, orderby = false, groupby = false; string lastcommand; var tags = Mhql_AT.GetATS(Command, out lastcommand); if (lastcommand.StartsWith("USE", StringComparison.OrdinalIgnoreCase)) { if (tags.Length > 1) { throw new MochaException("Multi tags is cannot used with USE keyword!"); } string tag = tags.Length == 0 ? string.Empty : tags.GetFirst(); if (tag.Equals("@STACKS", StringComparison.OrdinalIgnoreCase)) { throw new MochaException("@STACKS is cannot target if used with USE keyword!"); } var use = USE.GetUSE(out lastcommand); fromkw = use.IndexOf("FROM", StringComparison.OrdinalIgnoreCase) != -1; var table = string.IsNullOrEmpty(tag) || tag.Equals("@TABLES", StringComparison.OrdinalIgnoreCase) ? USE.GetTable(use, fromkw) : tag.Equals("@SECTORS", StringComparison.OrdinalIgnoreCase) ? USE.GetSector(use, fromkw) : throw new MochaException("@ mark is cannot processed!"); do { //Orderby. if (ORDERBY.IsORDERBY(lastcommand)) { orderby = true; if (groupby) { throw new MochaException("GROUPBY keyword must be specified before ORDERBY!"); } ORDERBY.OrderBy(ORDERBY.GetORDERBY(lastcommand, out lastcommand), ref table); } //Groupby. else if (GROUPBY.IsGROUPBY(lastcommand)) { groupby = true; GROUPBY.GroupBy(GROUPBY.GetGROUPBY(lastcommand, out lastcommand), ref table); } //Must. else if (MUST.IsMUST(lastcommand)) { if (orderby) { throw new MochaException("MUST keyword must be specified before ORDERBY!"); } else if (groupby) { throw new MochaException("MUST keyword must be specified before GROUPBY!"); } MUST.MustTable(MUST.GetMUST(lastcommand, out lastcommand), ref table, fromkw); } //Return. else if (lastcommand.Equals("RETURN", StringComparison.OrdinalIgnoreCase)) { break; } else { throw new MochaException($"'{lastcommand}' command is cannot processed!"); } } while(true); reader.array = new MochaArray <object>(table); } else if (lastcommand.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase)) { var select = SELECT.GetSELECT(out lastcommand); fromkw = select.IndexOf("FROM", StringComparison.OrdinalIgnoreCase) != -1; if (fromkw) { throw new MochaException("FROM keyword is cannot use with SELECT keyword!"); } List <object> collection = new List <object>(); if (tags.Length == 0) { collection.AddRange(SELECT.GetTables(select)); } else { bool tables = false, sectors = false, stacks = false; for (int index = 0; index < tags.Length; index++) { if (tags.ElementAt(index).Equals("@TABLES", StringComparison.OrdinalIgnoreCase)) { if (tables) { throw new MochaException("@TABLES cannot be targeted more than once!"); } tables = true; collection.AddRange(SELECT.GetTables(select)); } else if (tags.ElementAt(index).Equals("@SECTORS", StringComparison.OrdinalIgnoreCase)) { if (sectors) { throw new MochaException("@SECTORS cannot be targeted more than once!"); } sectors = true; collection.AddRange(SELECT.GetSectors(select)); } else if (tags.ElementAt(index).Equals("@STACKS", StringComparison.OrdinalIgnoreCase)) { if (stacks) { throw new MochaException("@STACKS cannot be targeted more than once!"); } stacks = true; collection.AddRange(SELECT.GetStacks(select)); } else { throw new MochaException(tags.ElementAt(index)); } } } do { //Orderby. if (ORDERBY.IsORDERBY(lastcommand)) { throw new MochaException("ORDERBY keyword is canot used with SELECT keyword!"); } //Groupby. else if (GROUPBY.IsGROUPBY(lastcommand)) { throw new MochaException("GROUPBY keyword is canot used with SELECT keyword!"); } //Must. else if (MUST.IsMUST(lastcommand)) { throw new MochaException("MUST keyword is canot used with SELECT keyword!"); } //Return. else if (lastcommand.Equals("RETURN", StringComparison.OrdinalIgnoreCase)) { break; } else { throw new MochaException($"'{lastcommand}' command is cannot processed!"); } } while(true); reader.array = new MochaArray <object>(collection); } else { throw new MochaException("MHQL is cannot processed!"); } return(reader); }
/// <summary> /// Run command. /// </summary> public void ExecuteCommand() { CheckConnection(); if (RETURN.IsReturnableCmd()) { return; } string lastcommand; var tags = Mhql_AT.GetATS(Command, out lastcommand); if (lastcommand.StartsWith("USE", StringComparison.OrdinalIgnoreCase)) { throw new MochaException("USE keyword is not supported by execute command!"); } else if (lastcommand.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase)) { var select = SELECT.GetSELECT(out lastcommand); List <object> collection = new List <object>(); if (tags.Length == 0) { collection.AddRange(SELECT.GetTables(select)); } else { bool tables = false, sectors = false, stacks = false; for (int index = 0; index < tags.Length; index++) { if (tags.ElementAt(index).Equals("@TABLES", StringComparison.OrdinalIgnoreCase)) { if (tables) { throw new MochaException("@TABLES cannot be targeted more than once!"); } tables = true; collection.AddRange(SELECT.GetTables(select)); } else if (tags.ElementAt(index).Equals("@SECTORS", StringComparison.OrdinalIgnoreCase)) { if (sectors) { throw new MochaException("@SECTORS cannot be targeted more than once!"); } sectors = true; collection.AddRange(SELECT.GetSectors(select)); } else if (tags.ElementAt(index).Equals("@STACKS", StringComparison.OrdinalIgnoreCase)) { if (stacks) { throw new MochaException("@STACKS cannot be targeted more than once!"); } stacks = true; collection.AddRange(SELECT.GetStacks(select)); } else { throw new MochaException("@ mark is cannot processed!"); } } } do { //Orderby. if (ORDERBY.IsORDERBY(lastcommand)) { throw new MochaException("ORDERBY keyword is canot used with SELECT keyword!"); } //Groupby. else if (GROUPBY.IsGROUPBY(lastcommand)) { throw new MochaException("GROUPBY keyword is canot used with SELECT keyword!"); } //Must. else if (MUST.IsMUST(lastcommand)) { throw new MochaException("MUST keyword is canot used with SELECT keyword!"); } //Remove. else if (lastcommand.Equals("REMOVE", StringComparison.OrdinalIgnoreCase)) { for (int index = 0; index < collection.Count; index++) { var item = (IMochaDatabaseItem)collection[index]; Database.RemoveDatabaseItem(item); } return; } else { throw new MochaException($"'{lastcommand}' command is cannot processed!"); } } while(true); } else { throw new MochaException("MHQL is cannot processed!"); } }