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())); } }
public HttpQueryBuilder(Gale.Db.IDataActions databaseFactory, HttpRequestMessage request, GQLConfiguration configuration) : base(databaseFactory, request, configuration) { System.Collections.Specialized.NameValueCollection query = System.Web.HttpUtility.ParseQueryString(request.RequestUri.Query); //------------------------------------------------------------------------------------------ //--[ ORDER BY PARSER (FOR SQL SERVER WE NEED TO ENGAGE FIRST) this.RegisterParser <OData.Builders.SQLServer.Parsers.OrderBy>(this.Kql); //------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //--[ SELECT PARSER this.RegisterParser <OData.Builders.SQLServer.Parsers.Select>(this.Kql); //------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //--[ FROM PARSER (Send null query , the model , has converted automatically by constraint's) this.RegisterParser <OData.Builders.SQLServer.Parsers.From>(this.Kql); //------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //--[ WHERE PARSER (Filter's) this.RegisterParser <OData.Builders.SQLServer.Parsers.Where>(this.Kql); //------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //--[ APPEND THE ANSI SQL STATEMENTS (SELECT , FROM , WHERE, ORDER BY) String orderBy = ""; this.OnExecutedParser += new ExecutedParserEventHandler((args) => { #region OVERRIDE FRAGMENT //ORDER BY APPEND (CAPTURE AND SET NULL) if (args.Parser.GetType() == typeof(OData.Builders.SQLServer.Parsers.OrderBy)) { orderBy = args.ResultQueryFragment; args.ResultQueryFragment = ""; } //SELECT APPEND if (args.Parser.GetType() == typeof(OData.Builders.SQLServer.Parsers.Select)) { //INCLUDE PAGINATOR SQL SENTENCE String format = String.Format("SELECT ROW_NUMBER() OVER (ORDER BY {0}) AS ROWNUM, {1}", orderBy, args.ResultQueryFragment ); args.ResultQueryFragment = format;; } //WHERE APPEND if (args.Parser.GetType() == typeof(OData.Builders.SQLServer.Parsers.Where)) { if (args.ResultQueryFragment.Length > 0) { args.ResultQueryFragment = String.Concat(" WHERE ", args.ResultQueryFragment); } } #endregion }); //------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------ //--[ REGISTER OPERATOR'S (CAN BE EXECUTED IN ANY PARSER FOR THE "CALLOPERATOR" METHOD) IEnumerable <Type> operators = (from t in this.GetType().Assembly.GetTypes() where t.IsSubclassOf(typeof(OData.Builders.SQLServer.Operators.Operator)) select t); foreach (Type Qoperator in operators) { System.Reflection.MethodInfo register = this.GetType().GetMethod("RegisterOperator").MakeGenericMethod(Qoperator); register.Invoke(this, new object[] { }); } //------------------------------------------------------------------------------------------ }
public TypedItem(Type type, GQLConfiguration configuration) { this._type = type; this._configuration = configuration; }
internal void RegisterParser <T>(GQLConfiguration configuration) where T : Gale.REST.Queryable.Primitive.Parser { this._parsers.Add(new TypedItem(typeof(T), configuration)); }
/// <summary> /// Constructor /// </summary> /// <param name="request">HttpRequest Message</param> /// <param name="configuration">Gale Query Language Setup</param> public Read(HttpRequestMessage request, GQLConfiguration configuration) : base(request, configuration, typeof(TModel)) { }
/// <summary> /// Constructor /// </summary> /// <param name="id">Identifier Record in the Database</param> /// <param name="modelType">Model Type from the BluePrint Controller</param> public ReadBuilder(HttpRequestMessage request, GQLConfiguration configuration, Type modelType) { _modelType = modelType; _request = request; _configuration = configuration; }
public abstract String Parse(GQLConfiguration configuration, Model model);
/// <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)); }
/// <summary> /// Create a Queryable Endpoint Action Result /// </summary> /// <param name="request">Http Request Context</param> /// <param name="configuration">Base Configuration</param> public HttpQueryableActionResult(HttpRequestMessage request, GQLConfiguration configuration) { _request = request; _configuration = configuration; }