示例#1
0
        public override string BuildQuerySQL(SQLCeVisitor <TextContent> visitor, out IEnumerable <Parameter> parameters)
        {
            string selectClause = "*";

            if (visitor.SelectFields != null && visitor.SelectFields.Length > 0)
            {
                selectClause = string.Join(",", visitor.SelectFields);
            }


            string whereClause = visitor.WhereClause;

            if (textContentQuery.Folder != null)
            {
                var paraName = visitor.AppendParameter(textContentQuery.Folder.FullName);
                whereClause = whereClause + " AND FolderName=" + paraName;
            }

            var categoryClauses = string.Join(" AND ", GetCategoryClause(textContentQuery.Repository, visitor.CategoryQueries, visitor.Parameters).ToArray());

            if (!string.IsNullOrEmpty(categoryClauses))
            {
                whereClause = whereClause + " AND " + categoryClauses;
            }


            parameters = visitor.Parameters;

            string sql = string.Format("SELECT {0} FROM [{1}] content WHERE {2} ", selectClause, textContentQuery.Schema.GetTableName(), whereClause);


            return(sql);
        }
示例#2
0
        private IEnumerable <T> List(SQLCeVisitor <T> visitor)
        {
            var             command          = BuildCommand(visitor);
            var             connectionString = ContentQuery.Repository.GetConnectionString();
            List <T>        list             = new List <T>();
            SqlCeConnection connection;

            using (var dataReader = SQLCeHelper.ExecuteReader(connectionString, command, out connection))
            {
                try
                {
                    while (dataReader.Read())
                    {
                        T content = new T();
                        list.Add(dataReader.ToContent(content));
                    }
                }
                finally
                {
                    dataReader.Close();
                    connection.Close();
                }
            }
            return(list);
        }
示例#3
0
        public virtual object Execute()
        {
            SQLCeVisitor <T> visitor = new SQLCeVisitor <T>();

            visitor.Visite(ContentQuery.Expression);

            switch (visitor.CallType)
            {
            case Kooboo.CMS.Content.Query.Expressions.CallType.Count:
                return(Count(visitor));

            case Kooboo.CMS.Content.Query.Expressions.CallType.First:
                return(First(visitor));

            case Kooboo.CMS.Content.Query.Expressions.CallType.LastOrDefault:
                return(LastOrDefault(visitor));

            case Kooboo.CMS.Content.Query.Expressions.CallType.FirstOrDefault:
                return(FirstOrDefault(visitor));

            case Kooboo.CMS.Content.Query.Expressions.CallType.Last:
                return(Last(visitor));

            case Kooboo.CMS.Content.Query.Expressions.CallType.Unspecified:
            default:
                return(List(visitor));
            }
        }
示例#4
0
        private T Last(SQLCeVisitor <T> visitor)
        {
            var content = LastOrDefault(visitor);

            if (content == null)
            {
                throw new InvalidOperationException(SR.GetString("NoElements"));
            }
            return(content);
        }
示例#5
0
        private SQLCeVisitor <T> VisitInner(IExpression expression)
        {
            SQLCeVisitor <T> visitor = new SQLCeVisitor <T>(this.Parameters);

            visitor.Visite(expression);

            //combine the order expressions.
            this.OrderClauses.AddRange(visitor.OrderClauses);

            this.CategoryQueries = this.CategoryQueries.Concat(visitor.CategoryQueries);

            return(visitor);
        }
示例#6
0
        private int Count(SQLCeVisitor <T> visitor)
        {
            IEnumerable <Parameter> parameters;
            var sql = BuildQuerySQL(visitor, out parameters);

            var connectionString = ContentQuery.Repository.GetConnectionString();

            sql = string.Format("SELECT COUNT(Id) FROM ({0})T", sql);

            var command = BuildCommand(sql, parameters);

            return((int)SQLCeHelper.ExecuteScalar(connectionString, command));
        }
示例#7
0
        private T LastOrDefault(SQLCeVisitor <T> visitor)
        {
            IEnumerable <Parameter> parameters;
            var sql = BuildQuerySQL(visitor, out parameters);

            var    connectionString = ContentQuery.Repository.GetConnectionString();
            string orderBy          = "Id ASC";

            if (visitor.OrderClauses != null && visitor.OrderClauses.Count > 0)
            {
                orderBy = ToReverseOrderString(visitor);
            }
            if (visitor.Skip != 0)
            {
                sql = string.Format("SELECT * FROM ({0})T ORDER BY {1}  OFFSET {2} ROWS FETCH NEXT 1 ROWS ONLY",
                                    sql, orderBy, visitor.Skip);
            }
            else
            {
                sql = string.Format("SELECT TOP 1 * FROM ({0})T ORDER BY {1}", sql, orderBy);
            }

            var command = BuildCommand(sql, parameters);

            T content = null;
            SqlCeConnection connection;

            using (var dataReader = SQLCeHelper.ExecuteReader(connectionString, command, out connection))
            {
                try
                {
                    if (dataReader.Read())
                    {
                        content = new T();
                        dataReader.ToContent(content);
                    }
                }
                finally
                {
                    dataReader.Close();
                    connection.Close();
                }
            }
            return(content);
        }
示例#8
0
        protected virtual SqlCeCommand BuildCommand(SQLCeVisitor <T> visitor)
        {
            string selectClause = "*";

            if (visitor.SelectFields != null && visitor.SelectFields.Length > 0)
            {
                selectClause = string.Join(",", visitor.SelectFields);
            }

            string whereClause = "1=1";

            if (!string.IsNullOrEmpty(visitor.WhereClause))
            {
                whereClause = visitor.WhereClause;
            }
            string orderBy = "Id DESC";

            if (visitor.OrderClauses != null && visitor.OrderClauses.Count() > 0)
            {
                orderBy = ToOrderString(visitor);
            }
            IEnumerable <Parameter> parameters;
            string sql = string.Format("{0} ORDER BY {1}  OFFSET {2} ROWS",
                                       BuildQuerySQL(visitor, out parameters), orderBy, visitor.Skip);

            if (visitor.Take != 0)
            {
                sql = sql + string.Format(" FETCH NEXT {0} ROWS ONLY", visitor.Take);
            }

            var command = new SqlCeCommand();

            command.CommandText = sql;

            foreach (var item in parameters)
            {
                command.Parameters.Add(new SqlCeParameter(item.Name, item.Value));
            }

            return(BuildCommand(sql, parameters));
        }
示例#9
0
        public override string BuildQuerySQL(SQLCeVisitor <Models.TextContent> visitor, out IEnumerable <Parameter> parameters)
        {
            var innerVisitor = new SQLCeVisitor <Models.TextContent>(visitor.Parameters);

            innerVisitor.Visite(childrenQuery.ParentQuery.Expression);
            var innerExecutor = (new TextContentTranslator()).Translate(childrenQuery.ParentQuery);
            var innerQuerySQL = innerExecutor.BuildQuerySQL(innerVisitor, out parameters);

            string selectClause = "*";

            if (visitor.SelectFields != null && visitor.SelectFields.Length > 0)
            {
                selectClause = string.Join(",", visitor.SelectFields);
            }

            string whereClause = visitor.WhereClause;

            if (childrenQuery.EmbeddedFolder != null)
            {
                var paraName = visitor.AppendParameter(childrenQuery.EmbeddedFolder.FullName);
                whereClause = whereClause + " AND FolderName=" + paraName;
            }
            //var paraName = visitor.AppendParameter(parentQuery.ChildrenQuery.FullName);
            //whereClause = whereClause + "AND FolderName=" + paraName;

            string sql = string.Format(@"
            SELECT {0} FROM [{1}] children                            
               WHERE  EXISTS(
                        SELECT UUID
                            FROM ({2})parent
                            WHERE parent.UUID = children.ParentUUID 
                      ) AND {3}", selectClause
                                       , childrenQuery.ChildSchema.GetTableName()
                                       , innerQuerySQL
                                       , whereClause);

            return(sql);
        }
示例#10
0
        public override string BuildQuerySQL(SQLCeVisitor <TextContent> visitor, out IEnumerable <Parameter> parameters)
        {
            var innerVisitor = new SQLCeVisitor <Models.TextContent>(visitor.Parameters);

            innerVisitor.Visite(categoriesQuery.InnerQuery.Expression);
            var innerExecutor = (new TextContentTranslator()).Translate(categoriesQuery.InnerQuery);
            var innerQuerySQL = innerExecutor.BuildQuerySQL(innerVisitor, out parameters);

            string selectClause = "*";

            if (visitor.SelectFields != null && visitor.SelectFields.Length > 0)
            {
                selectClause = string.Join(",", visitor.SelectFields);
            }

            string whereClause = visitor.WhereClause;

            var paraName = visitor.AppendParameter(categoriesQuery.CategoryFolder.FullName);

            whereClause = whereClause + " AND FolderName=" + paraName;


            string sql = string.Format(@"
            SELECT {0} FROM [{1}] category
               WHERE  EXISTS(
                        SELECT ContentCategory.CategoryUUID 
                            FROM [{2}] ContentCategory,
                                ({3})content
                            WHERE content.UUID = ContentCategory.UUID AND ContentCategory.CategoryUUID = category.UUID 
                      ) AND {4}", selectClause
                                       , categoriesQuery.CategoryFolder.GetSchema().GetTableName()
                                       , categoriesQuery.Repository.GetCategoryTableName()
                                       , innerQuerySQL
                                       , whereClause);

            return(sql);
        }
示例#11
0
        public override string BuildQuerySQL(SQLCeVisitor <MediaContent> visitor, out IEnumerable <Parameter> parameters)
        {
            string selectClause = "*";

            if (visitor.SelectFields != null && visitor.SelectFields.Length > 0)
            {
                selectClause = string.Join(",", visitor.SelectFields);
            }


            string whereClause = visitor.WhereClause;

            var paraName = visitor.AppendParameter(mediaContentQuery.MediaFolder.FullName);

            whereClause = whereClause + "AND FolderName=" + paraName;


            parameters = visitor.Parameters;
            string sql = string.Format("SELECT {0} FROM {1} WHERE {2} ", selectClause
                                       , mediaContentQuery.Repository.GetMediaContentTableName()
                                       , whereClause);

            return(sql);
        }
示例#12
0
        private IEnumerable <string> GetCategoryClause(Repository repository, IEnumerable <IContentQuery <TextContent> > categoryQueries, List <Parameter> parameters)
        {
            TextContentTranslator translator        = new TextContentTranslator();
            List <string>         categoryQueryList = new List <string>();

            foreach (var categoryQuery in categoryQueries)
            {
                var executor        = translator.Translate(categoryQuery);
                var categoryVisitor = new SQLCeVisitor <TextContent>(parameters);
                categoryVisitor.Visite(categoryQuery.Expression);

                IEnumerable <Parameter> outParameters;
                var categoryQuerySQL = executor.BuildQuerySQL(categoryVisitor, out outParameters);

                categoryQueryList.Add(string.Format(@"EXISTS(
                        SELECT ContentCategory.CategoryUUID 
                            FROM [{0}] ContentCategory,
                                ({1})category
                            WHERE content.UUID = ContentCategory.UUID AND ContentCategory.CategoryUUID = category.UUID 
                      )", repository.GetCategoryTableName()
                                                    , categoryQuerySQL));
            }
            return(categoryQueryList);
        }
示例#13
0
 public abstract string BuildQuerySQL(SQLCeVisitor <T> visitor, out IEnumerable <Parameter> parameters);
示例#14
0
 protected static string ToReverseOrderString(SQLCeVisitor <T> visitor)
 {
     return(string.Join(",", visitor.OrderClauses.Distinct(new OrderClauseComparer()).Select(it => "[" + it.FieldName + "] " + (it.Descending ? "ASC" : "DESC")).ToArray()));
 }