Exemple #1
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="expr">The expression to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="parameter">The parameter to consider.</param>
 public static DbField FieldAsParameter <T>(
     Expression <Func <T, object> > expr,
     DbTable table,
     ScalarElement parameter) where T : class
 {
     return(DbFluent.Field <T>(expr, table).AsParameter(parameter));
 }
Exemple #2
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="expr">The expression to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="otherField">The other field to consider.</param>
 public static DbField FieldAsOther <T>(
     Expression <Func <T, object> > expr,
     DbTable table,
     DbField otherField) where T : class
 {
     return(DbFluent.Field <T>(expr, table).AsOther(otherField));
 }
Exemple #3
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="expr">The expression to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="parameterIndex">The parameter index to consider.</param>
 public static DbField FieldAsParameter <T>(
     Expression <Func <T, object> > expr,
     DbTable table,
     byte parameterIndex) where T : class
 {
     return(DbFluent.Field <T>(expr, table).AsParameter(parameterIndex));
 }
Exemple #4
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="expr">The expression to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="query">The query to consider.</param>
 public static DbField FieldAsQuery <T>(
     Expression <Func <T, object> > expr,
     DbTable table,
     IDbQuery query) where T : class
 {
     return(DbFluent.Field <T>(expr, table).AsQuery(query));
 }
Exemple #5
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="name">The name to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="otherField">The other field to consider.</param>
 public static DbField FieldAsOther(
     string name,
     DbTable table,
     DbField otherField)
 {
     return(DbFluent.Field(name, table).AsOther(otherField));
 }
Exemple #6
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="name">The name to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="query">The query to consider.</param>
 public static DbField FieldAsQuery(
     string name,
     DbTable table,
     IDbQuery query)
 {
     return(DbFluent.Field(name, table).AsQuery(query));
 }
Exemple #7
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="expr">The expression to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="script">The script to consider.</param>
 public static DbField FieldAsScript <T>(
     Expression <Func <T, object> > expr,
     DbTable table,
     string script) where T : class
 {
     return(DbFluent.Field <T>(expr, table).AsScript(script));
 }
Exemple #8
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="name">The name to consider.</param>
 /// <param name="table">The data table to consider.</param>
 /// <param name="value">The value to consider.</param>
 /// <param name="valueType">The value type to consider.</param>
 public static DbField FieldAsLiteral(
     string name,
     DbTable table,
     object value,
     DataValueTypes valueType = DataValueTypes.Any)
 {
     return(DbFluent.Field(name, table).AsLiteral(value, valueType));
 }
Exemple #9
0
        /// <summary>
        /// Creates a new instance of the DbField class.
        /// </summary>
        /// <param name="expr">The expression to consider.</param>
        /// <param name="table">The data table to consider.</param>
        /// <param name="value">The value to consider.</param>
        public static DbField FieldAsLiteral <T>(
            Expression <Func <T, object> > expr,
            DbTable table,
            object value) where T : class
        {
            var field = DbFluent.Field <T>(expr, table);

            return(field.AsLiteral(value, field?.ValueType ?? DataValueTypes.None));
        }
Exemple #10
0
        /// <summary>
        /// Creates a new instance of the DbField class.
        /// </summary>
        /// <param name="name">The name to consider.</param>
        /// <param name="table">The data table to consider.</param>
        /// <param name="script">The script to consider.</param>
        public static DbField FieldAsScript(
            string name,
            DbTable table,
            string script)
        {
            var field = DbFluent.Field(name, table);

            field.AsScript(script);

            return(field);
        }
Exemple #11
0
        /// <summary>
        /// Sorts the specified query considering the specified query script.
        /// </summary>
        /// <param name="query">The database query to consider.</param>
        /// <param name="sortQuery">The sort query text to consider.</param>
        /// <param name="definition">The definition to consider.</param>
        /// <param name="log">The log to consider.</param>
        /// <returns>The built query.</returns>
        public static IDbSingleQuery Sort(
            this IDbSingleQuery query,
            string sortQuery,
            DbApiSortDefinition definition = null,
            IBdoLog log = null)
        {
            if (query != null && !string.IsNullOrEmpty(sortQuery))
            {
                query.OrderByClause = new DbQueryOrderByClause();

                foreach (string fieldItem in sortQuery.Split(','))
                {
                    var statement       = new DbQueryOrderByStatement();
                    var fieldItemParams = fieldItem?.Trim().Split(' ');
                    if (fieldItemParams.Length > 0)
                    {
                        string fieldName = fieldItemParams[0]?.Trim();
                        if (!definition.ContainsKey(fieldName))
                        {
                            log?.AddError("Undefined field '" + fieldName + "' in order statement", resultCode: "user");
                        }
                        else
                        {
                            statement.Field   = definition?[fieldName]?.Field ?? DbFluent.Field(fieldName);
                            statement.Sorting = DataSortingModes.Ascending;

                            if (fieldItemParams.Length > 1)
                            {
                                string direction = fieldItemParams[1]?.Trim();
                                if (string.Equals(direction, "desc"))
                                {
                                    statement.Sorting = DataSortingModes.Descending;
                                    query.OrderByClause.Statements.Add(statement);
                                }
                                else if (!string.Equals(direction, "asc"))
                                {
                                    log?.AddError("Invalid order direction '" + direction + "'", resultCode: "user");
                                }
                                else
                                {
                                    query.OrderByClause.Statements.Add(statement);
                                }
                            }
                        }
                    }
                }
            }

            return(query);
        }
Exemple #12
0
        /// <summary>
        /// Creates a new instance of the DbField class.
        /// </summary>
        /// <param name="expr">The expression to consider.</param>
        /// <param name="table">The data table to consider.</param>
        /// <typeparam name="T">The class to consider.</typeparam>
        public static DbField Field <T>(
            Expression <Func <T, object> > expr,
            DbTable table = null) where T : class
        {
            var propertyInfo = expr.GetProperty();
            var name         = propertyInfo?.Name;
            var valueType    = propertyInfo?.PropertyType.GetValueType() ?? DataValueTypes.None;

            if (propertyInfo?.GetCustomAttribute(typeof(BdoDbFieldAttribute)) is BdoDbFieldAttribute fieldAttribute)
            {
                name      = fieldAttribute.Name;
                valueType = fieldAttribute.ValueType;
            }

            return(DbFluent.Field(name, table).WithValueType(valueType));
        }
Exemple #13
0
        /// <summary>
        /// Converts the specifed search query into an extension script.
        /// </summary>
        /// <param name="searchQuery">The search query to consider.</param>
        /// <param name="log">The </param>
        /// <param name="definition">The clause statement to consider.</param>
        /// <param name="i"></param>
        /// <returns></returns>
        internal static string ConvertToExtensionScript(
            this string searchQuery,
            IBdoLog log = null,
            DbApiFilterDefinition definition = null,
            int i = 0)
        {
            string script = searchQuery;

            if (!string.IsNullOrEmpty(script))
            {
                // boolean instructions

                foreach (string instruction in new string[] { "Or", "And", "Not" })
                {
                    int           j       = i;
                    List <string> clauses = new List <string>();
                    script.IndexOfNextString(" " + instruction + " ", ref j);
                    while (j < script.Length - 1)
                    {
                        string clause = script.Substring(i, j - i + 1);
                        clause = clause.ConvertToExtensionScript(log, definition, 0);
                        clauses.Add(clause);
                        j = i = j + (" " + instruction + " ").Length;
                        script.IndexOfNextString(" " + instruction + " ", ref j);
                        if (j == script.Length)
                        {
                            clause = script.Substring(i);
                            clause = clause.ConvertToExtensionScript(log, definition, 0);
                            clauses.Add(clause);
                        }
                    }
                    if (clauses.Count > 0)
                    {
                        script = "$sql" + instruction + "(" + clauses.Aggregate((p, q) => p + "," + q) + ")";
                    }
                }

                if (i == 0)
                {
                    DataOperators aOperator = DataOperators.None;

                    int    k = script.Length;
                    string scriptOperator = null;
                    foreach (DataOperators currentOperator in new DataOperators[] {
                        DataOperators.Exists,
                        DataOperators.Contains,
                        DataOperators.Different, DataOperators.Equal,
                        DataOperators.GreaterOrEqual, DataOperators.Greater,
                        DataOperators.LesserOrEqual, DataOperators.Lesser,
                        DataOperators.Has, DataOperators.In
                    })
                    {
                        int    k1 = 0;
                        string currentScriptOperator = DbQueryExtension.GetInstruction(currentOperator);
                        script.IndexOfNextString(currentScriptOperator, ref k1);
                        if (k1 < k)
                        {
                            scriptOperator = currentScriptOperator;
                            aOperator      = currentOperator;
                            k = k1;
                        }
                    }
                    if (k == script.Length)
                    {
                        log.AddError("No operator found in clause '" + searchQuery + "'", resultCode: "user");
                    }
                    else
                    {
                        string scriptFunction = DbQueryExtension.GetScriptFunction(aOperator)?.Trim();
                        string fieldName      = script.Substring(0, k)?.Trim();
                        string value          = script.Substring(k + scriptOperator.Length)?.Trim();

                        if (value.Length > 2 && value.StartsWith("'") && value.EndsWith("'"))
                        {
                            value = "$sqlText('" + value.Substring(1, value.Length - 2) + "')";
                        }

                        // check that the field is in the dictionary
                        if (!definition.ContainsKey(fieldName))
                        {
                            log.AddError("Undefined field '" + fieldName + "' in clause '" + searchQuery + "''", resultCode: "user");
                        }
                        else
                        {
                            DbApiFilterClause clause = definition?[fieldName];

                            // check the instruction found corresponds to the definition in dictionary
                            if (!clause.Operators.Any(p => p == aOperator))
                            {
                                log.AddError("Undefined operator '" + aOperator.ToString() + "' for field '" + fieldName + "'", resultCode: "user");
                            }
                            else
                            {
                                if (clause.Field == null)
                                {
                                    clause.Field = DbFluent.Field(fieldName);
                                }

                                if (aOperator == DataOperators.Has)
                                {
                                    if (value.Length > 2 && value.StartsWith("{") && value.EndsWith("}"))
                                    {
                                        value = value.Substring(1, value.Length - 2);
                                    }
                                    value  = value.ConvertToExtensionScript(log, clause.FilterDefinition, 0);
                                    script = "(" + value + ")";
                                }
                                else
                                {
                                    script = scriptFunction + clause.Field;
                                }
                            }
                        }
                    }
                }
            }
            return(script);
        }
Exemple #14
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="expr">The expression to consider.</param>
 /// <param name="table">The data table to consider.</param>
 public static DbField FieldAsNull <T>(
     Expression <Func <T, object> > expr,
     DbTable table) where T : class
 {
     return(DbFluent.Field <T>(expr, table).AsNull());
 }
Exemple #15
0
 /// <summary>
 /// Creates a new instance of the DbField class.
 /// </summary>
 /// <param name="name">The name to consider.</param>
 /// <param name="table">The data table to consider.</param>
 public static DbField FieldAsNull(
     string name, DbTable table)
 {
     return(DbFluent.Field(name, table).AsNull());
 }
Exemple #16
0
        // As All ---------------------------------

        /// <summary>
        /// Creates a new instance of the DbField class.
        /// </summary>
        /// <param name="table">The data table to consider.</param>
        public static DbField FieldAsAll(DbTable table)
        {
            return(DbFluent.Field(null, table).AsAll());
        }