Esempio n. 1
0
        protected override void Prepare()
        {
            if (Reader != null)
            {
                Reader.Dispose();
                Reader = null;
            }

            if (Query == null && QueryDef != null)
            {
                Query = _sqlQueryBuilder.Build(QueryDef); // SqlQueryBuilder.Build(DataContext, QueryDef, (Guid) UserId);
                PrepareQueryAttributes(Query);
            }
            Reader = new SqlQueryReader(DataContext, Query);
            Reader.Open();
            PrepareFields(Reader);
        }
Esempio n. 2
0
        /// <summary>
        /// Searches the index with the specified query.
        /// </summary>
        /// <param name="query">The query text.</param>
        /// <param name="options">The paging options.</param>
        /// <returns>Page of results.</returns>
        /// <exception cref="ArgumentNullException">query or options</exception>
        public DataPage <ItemInfo> Search(string query, PagingOptions options)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (_queryBuilder == null)
            {
                _queryBuilder = GetQueryBuilder();
            }
            if (_queryBuilder == null)
            {
                return(null);
            }

            var pageAndTot = _queryBuilder.Build(query, options);

            if (Connection == null)
            {
                Connection = GetConnection();
                Connection.Open();
            }

            DbCommand pageCommand = GetCommand();

#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
            pageCommand.CommandText = pageAndTot.Item1;
            pageCommand.Connection  = Connection;

            DbCommand totCommand = GetCommand();
            totCommand.CommandText = pageAndTot.Item2;
            totCommand.Connection  = Connection;
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

            List <ItemInfo> items = new List <ItemInfo>();

            int total = Convert.ToInt32(totCommand.ExecuteScalar());
            if (total == 0)
            {
                return(new DataPage <ItemInfo>(
                           options.PageNumber,
                           options.PageSize,
                           0,
                           items));
            }

            using (DbDataReader reader = pageCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                    ItemInfo item = new ItemInfo
                    {
                        // for some reason, the type from MySql is GUID here
                        Id = reader.GetValue(reader.GetOrdinal("id")).ToString(),
                        //Id = reader.GetFieldValue<string>(
                        //    reader.GetOrdinal("id")),
                        Title = reader.GetFieldValue <string>(
                            reader.GetOrdinal("title")),
                        Description = reader.GetFieldValue <string>(
                            reader.GetOrdinal("description")),
                        FacetId = reader.GetFieldValue <string>(
                            reader.GetOrdinal("facetId")),
                        GroupId = reader.GetFieldValue <string>(
                            reader.GetOrdinal("groupId")),
                        SortKey = reader.GetFieldValue <string>(
                            reader.GetOrdinal("sortKey")),
                        Flags = reader.GetFieldValue <int>(
                            reader.GetOrdinal("flags")),
                        TimeCreated = reader.GetFieldValue <DateTime>(
                            reader.GetOrdinal("timeCreated")),
                        CreatorId = reader.GetFieldValue <string>(
                            reader.GetOrdinal("creatorId")),
                        TimeModified = reader.GetFieldValue <DateTime>(
                            reader.GetOrdinal("timeModified")),
                        UserId = reader.GetFieldValue <string>(
                            reader.GetOrdinal("userId"))
                    };
                    items.Add(item);
                }
            }
            return(new DataPage <ItemInfo>(
                       options.PageNumber,
                       options.PageSize,
                       total, items));
        }