18 using System.Collections.Generic;
28 namespace Deveel.Data.Sql.Statements {
31 : this(viewName, null, queryExpression) {
36 throw new ArgumentNullException(
"viewName");
37 if (queryExpression == null)
38 throw new ArgumentNullException(
"queryExpression");
41 ColumnNames = columnNames;
42 QueryExpression = queryExpression;
45 public string ViewName {
get;
private set; }
47 public IEnumerable<string> ColumnNames {
get;
private set; }
51 public bool ReplaceIfExists {
get; set; }
54 var viewName = context.
Query.ResolveTableName(ViewName);
57 var queryPlan = context.
Query.
Context.QueryPlanner().PlanQuery(
new QueryInfo(context, QueryExpression));
59 var colList = ColumnNames == null ?
new string[0] : ColumnNames.ToArray();
63 int sz = colList.Length;
64 var originalNames = queryFrom.GetResolvedColumns();
65 var newColumnNames =
new ObjectName[originalNames.Length];
68 if (sz != originalNames.Length)
69 throw new InvalidOperationException(
"Column list is not the same size as the columns selected.");
71 for (
int i = 0; i < sz; ++i) {
72 var colName = colList[i];
73 newColumnNames[i] =
new ObjectName(viewName, colName);
76 sz = originalNames.Length;
77 for (
int i = 0; i < sz; ++i) {
78 newColumnNames[i] =
new ObjectName(viewName, originalNames[i].Name);
83 for (
int i = 0; i < sz; ++i) {
84 var columnName = newColumnNames[i];
85 for (
int n = i + 1; n < sz; ++n) {
86 if (newColumnNames[n].Equals(columnName))
87 throw new InvalidOperationException(String.Format(
"Duplicate column name '{0}' in view. A view may not contain duplicate column names.", columnName));
92 queryPlan =
new SubsetNode(queryPlan, originalNames, newColumnNames);
94 return new Prepared(viewName, QueryExpression, queryPlan, ReplaceIfExists);
103 QueryPlan = queryPlan;
104 ReplaceIfExists = replaceIfExists;
105 QueryExpression = queryExpression;
111 ReplaceIfExists = data.
GetBoolean(
"ReplaceIfExists");
119 public bool ReplaceIfExists {
get; set; }
125 data.
SetValue(
"QueryPlan", QueryPlan);
126 data.
SetValue(
"QueryExpression", QueryExpression);
127 data.
SetValue(
"ReplaceIfExists", ReplaceIfExists);
133 var table = QueryPlan.Evaluate(context.
Request);
134 var tableInfo = table.TableInfo.Alias(ViewName);
136 var viewInfo =
new ViewInfo(tableInfo, QueryExpression, QueryPlan);
137 context.
Request.
Query.DefineView(viewInfo, ReplaceIfExists);
143 #region PreparedSerializer
override void GetData(SerializeData data)
Prepared(ObjectName viewName, SqlQueryExpression queryExpression, IQueryPlanNode queryPlan, bool replaceIfExists)
override void ExecuteStatement(ExecutionContext context)
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
CreateViewStatement(string viewName, SqlQueryExpression queryExpression)
Prepared(ObjectData data)
A node element of a query plan tree. /summary>
Represents the foundation class of SQL statements to be executed.
static QueryExpressionFrom Create(IRequest context, SqlQueryExpression expression)
bool GetBoolean(string key)
object GetValue(string key)
IStatement Prepare(IRequest request)
new IQueryContext Context
CreateViewStatement(string viewName, IEnumerable< string > columnNames, SqlQueryExpression queryExpression)