Select() public method

Adds a field of a given table alias to the SELECT statement.
No column name is used for the field or expression.
public Select ( IAlias alias, string fieldName ) : SqlQuery
alias IAlias A table alias that will be prepended to the field name with "." between
fieldName string A field name of the aliased table.
return SqlQuery
Exemplo n.º 1
0
        /// <summary>
        ///   Adds actual table fields in a row to select list of a query.</summary>
        /// <param name="query">
        ///   Query to select fields into (required).</param>
        /// <param name="row">
        ///   Row with fields to be selected (required).</param>
        /// <param name="exclude">
        ///   Fields to be excluded (optional).</param>
        public static SqlQuery SelectTableFields(this SqlQuery query, Row row, params Field[] exclude)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            if (row == null)
            {
                throw new ArgumentNullException("row");
            }

            HashSet <Field> excludeFields =
                (exclude != null && exclude.Length > 0) ? new HashSet <Field>(exclude) : null;

            var fields = row.GetFields();

            for (int i = 0; i < row.FieldCount; i++)
            {
                Field field = fields[i];
                if (EntityFieldExtensions.IsTableField(field))
                {
                    if (excludeFields == null ||
                        !excludeFields.Contains(field))
                    {
                        query.Select(field);
                    }
                }
            }

            return(query);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a field of a given table alias to the SELECT statement.
        /// </summary>
        /// <param name="alias">A table alias that will be prepended to the field name with "." between</param>
        /// <param name="field">A field that only name will be used. It won't be set as a target.</param>
        /// <returns>The query itself.</returns>
        /// <remarks>No column name is set for the selected field.
        /// Also field is not set as a target, unlike field only overload, only field name is used.</remarks>
        public static SqlQuery Select(this SqlQuery query, IAlias alias, IField field)
        {
            if (alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            return(query.Select(alias.NameDot + field));
        }
Exemplo n.º 3
0
        /// <summary>
        ///   Adds foreign / calculated table fields in a row to select list of a query.</summary>
        /// <param name="query">
        ///   Query to select fields into (required).</param>
        /// <param name="row">
        ///   Row with fields to be selected (required).</param>
        /// <param name="exclude">
        ///   Fields to be excluded (optional).</param>
        public static SqlQuery SelectNonTableFields(this SqlQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            foreach (var field in ((Row)query.FirstIntoRow).GetFields())
            {
                if (!FieldExtensions.IsTableField(field) &&
                    (field.Flags & FieldFlags.ClientSide) != FieldFlags.ClientSide)
                {
                    query.Select(field);
                }
            }

            return(query);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds a field of a given table alias to the SELECT statement.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="alias">A table alias that will be prepended to the field name with "." between</param>
        /// <param name="field">A field that only its field name will be used. It won't be set as a target.</param>
        /// <param name="columnName">A column name</param>
        /// <returns>
        /// The query itself.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// alias
        /// or
        /// field
        /// or
        /// columnName
        /// </exception>
        /// <remarks>
        /// Field is not set as a target, unlike field only overload, only field name is used.
        /// </remarks>
        public static SqlQuery Select(this SqlQuery query, IAlias alias, IField field, string columnName)
        {
            if (alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            if (columnName == null)
            {
                throw new ArgumentNullException("columnName");
            }

            return(query.Select(alias.NameDot + SqlSyntax.AutoBracket(field.Name), columnName));
        }
Exemplo n.º 5
0
        /// <summary>
        ///   Adds foreign / calculated table fields in a row to select list of a query.</summary>
        /// <param name="query">
        ///   Query to select fields into (required).</param>
        /// <param name="row">
        ///   Row with fields to be selected (required).</param>
        /// <param name="exclude">
        ///   Fields to be excluded (optional).</param>
        public static SqlQuery SelectNonTableFields(this SqlQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            var ext = (ISqlQueryExtensible)query;

            foreach (var field in ((Row)ext.FirstIntoRow).GetFields())
            {
                if (!EntityFieldExtensions.IsTableField(field) &&
                    (field.Flags & FieldFlags.NotMapped) != FieldFlags.NotMapped)
                {
                    query.Select(field);
                }
            }

            return(query);
        }
        public override void OnPrepareQuery(ISaveRequestHandler handler, SqlQuery query)
        {
            base.OnPrepareQuery(handler, query);

            if (this.replaceFields != null)
            {
                foreach (var field in replaceFields.Values)
                {
                    if (!field.IsTableField() &&
                        (!(query is ISqlQueryExtensible) ||
                          ((ISqlQueryExtensible)query).GetSelectIntoIndex(field) <= 0))
                        query.Select(field);
                }
            }
        }
Exemplo n.º 7
0
        internal static string ProcessReplaceFields(string s, Dictionary<string, Field> replaceFields, ISaveRequestHandler handler)
        {
            if (replaceFields == null)
                return s;

            var row = handler.Row;

            // foreign / calculated fields might not be available yet in new row
            // so load them from database 

            // TODO: if referenced foreign fields changed on update, 
            // values might be wrong in before update where we set filename
            // so need to handle update in AfterSave just like create

            if (handler.IsCreate &&
                replaceFields.Values.Any(x => !x.IsTableField()))
            {
                var idField = (Field)(((IIdRow)handler.Row).IdField);

                row = handler.Row.Clone();
                var query = new SqlQuery()
                    .From(row);

                foreach (var field in replaceFields.Values)
                    query.Select(field);

                query.Where(idField == new ValueCriteria(idField.AsObject(row)));

                query.GetFirst(handler.Connection);
            }

            foreach (var p in replaceFields)
            {
                var val = p.Value.AsObject(row);
                string str;

                var colon = p.Key.IndexOf(":");
                if (colon >= 0)
                    str = String.Format("{0:" + p.Key.Substring(colon + 1, p.Key.Length - colon - 2) + "}", val);
                else
                    str = Convert.ToString(val ?? "", CultureInfo.InvariantCulture);

                str = StringHelper.SanitizeFilename(str).Replace('\\', '_').Replace("..", "_");
                if (string.IsNullOrWhiteSpace(str))
                    str = "_";

                while (str.EndsWith("."))
                    str = str.Substring(0, str.Length - 1) + "_";

                s = s.Replace(p.Key, str);
            }

            while (s.IndexOf("//") > 0)
                s = s.Replace("//", "/_/");

            return s;
        }