public override string Parse(GQLConfiguration configuration, Gale.REST.Queryable.Primitive.Reflected.Model model) { //FROM PARSER QUERY System.Text.StringBuilder builder = new StringBuilder(); List <String> SelectAppends = new List <string>(); //Add Primary Table (First Position) SelectAppends.Add(String.Format(" FROM {0} T1 \n\n", model.Tables[0].Key)); model.Constraints.ForEach((constraint) => { SelectAppends.Insert(0, String.Concat(", ", constraint.Table.Key, ".* ")); //LEFT JOIN TABLE builder.Append(String.Format(" LEFT JOIN {0} ON T1.{1} = {0}.{2} \n\n", constraint.Table.Key, constraint.ThisField.Key, constraint.OtherField.Key )); }); return(String.Concat(String.Join("", SelectAppends), builder.ToString())); }
public override string Parse(GQLConfiguration configuration, Gale.REST.Queryable.Primitive.Reflected.Model model) { if (configuration.filters.Count > 0) { //WHERE PARSER QUERY List <String> builder = new List <string>(); configuration.filters.ForEach((filter) => { //FK Constraint's Filter [ format: fk:(fk_column operator values) ] if (filter.field.IndexOf(":(") > 0) { var foreignFieldMatch = String.Format("{0})", filter.field.Substring(0, filter.field.IndexOf(" "))); var filteredField = (from field in model.Fields where field.Name == foreignFieldMatch select field).FirstOrDefault(); if (filteredField == null) { throw new Exception.GaleException("API009", foreignFieldMatch); } //replace the first space with ")", so the format be "foreign:(foreignField) operator value" String innerFilter = filter.field.Insert(filter.field.IndexOf(" "), ")").Substring(0, filter.field.Length); String[] values = innerFilter.Trim().Split(' '); if (values.Length != 3) { throw new Exception.GaleException("API010", filter.ToString()); } string _filter = String.Concat( filteredField.Table.Key, ".", CallOperator(innerFilter, model) ); builder.Add(_filter); } else { //Normal operator [ format: column operator values } builder.Add(CallOperator(filter.ToString())); } }); return(String.Join(" AND ", builder)); } else { return(""); } }
/// <summary> /// Parse SQL String /// </summary> /// <param name="query"></param> /// <param name="model"></param> /// <returns></returns> public override string Parse(GQLConfiguration configuration, Gale.REST.Queryable.Primitive.Reflected.Model model) { if (configuration.orderBy == null) { return("SYSDATETIME() desc \n\n"); } else { Gale.REST.Queryable.Primitive.Reflected.Field _field = model.Fields.FirstOrDefault((field) => { return(field.Name.ToLower() == configuration.orderBy.name.ToLower()); }); if (_field == null) { throw new Exception.GaleException("API015", configuration.orderBy.name); } return(String.Format("{0} {1} \n\n", _field.Key, configuration.orderBy.order.ToString())); } }
/// <summary> /// Parse SELECT /// </summary> /// <param name="configuration">Gale Query Language Configuration</param> /// <param name="model">Model</param> /// <returns></returns> public override string Parse(GQLConfiguration configuration, Gale.REST.Queryable.Primitive.Reflected.Model model) { //SELECT PARSER QUERY List <String> builder = new List <string>(); if (configuration.fields.Count == 0) { configuration.fields.Add("*"); } #region SELECT FIELD //---- SELECT FIELD Action <Gale.REST.Queryable.Primitive.Reflected.Field> SelectField = new Action <Gale.REST.Queryable.Primitive.Reflected.Field>((field) => { //The Main Primary Key , dont'need to add to the selection if (field.Specification == Gale.REST.Queryable.Primitive.Reflected.Field.SpecificationEnum.Pk) { return; } //Only if, his, from the primary Table, add to selection, //because all Foreign key Table, are Getting from the source (ForeignTable.*) if (field.Table.IsForeign == false) { builder.Add(field.Key); } field.Select(); }); //--------------------------------- #endregion //--- FIRST ADD THE PK builder.Insert(0, model.Tables.First().PrimaryKey.Key); //--- foreach (String fieldName in configuration.fields) { string _fieldName = fieldName.Trim(); //---[ Get all field from all tables :P if (_fieldName == "*.*") { model.Fields.ForEach((f) => { SelectField(f); }); break; } //If query is * , bring all field's if (_fieldName.Contains("*")) { Type searchedTable = null; //Get all field from the Primary Table if (_fieldName == "*") { searchedTable = model.Tables.First().Type; //Main Table } else { //try to get all field from a foreign table if (!_fieldName.Contains(":(")) { throw new Exception.GaleException("API012", _fieldName); } _fieldName = _fieldName.Substring(0, _fieldName.IndexOf(":(")); var fk = model.Constraints.FirstOrDefault(constraint => constraint.ThisField.Name == _fieldName); if (fk == null) { throw new Exception.GaleException("API013", _fieldName); } searchedTable = fk.Table.Type; } model.Fields.ForEach((f) => { if (f.Table.Type == searchedTable) { SelectField(f); } }); continue; } //Check if field exist's Gale.REST.Queryable.Primitive.Reflected.Field field = model.Fields.FirstOrDefault((f) => { return(f.Name.ToLower() == _fieldName); }); //If field is not exists, throw exception if (field == null) { throw new Exception.GaleException("API013", _fieldName); } //Primary Key if (field.Specification == Gale.REST.Queryable.Primitive.Reflected.Field.SpecificationEnum.Pk) { throw new Exception.GaleException("API014", _fieldName); } //Select Field SelectField(field); } return(String.Join(",", builder)); }