1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
// Copyright (c) 2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.Text;
using System.Diagnostics;
using System.Data.Common.CommandTrees;
using System.Data.Metadata.Edm;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Data;
namespace MySql.Data.Entity
{
class SelectGenerator : SqlGenerator
{
Stack<SelectStatement> selectStatements = new Stack<SelectStatement>();
#region Properties
private SelectStatement CurrentSelect
{
get { return selectStatements.Count == 0 ? null : selectStatements.Peek(); }
}
#endregion
public override string GenerateSQL(DbCommandTree tree)
{
DbQueryCommandTree commandTree = tree as DbQueryCommandTree;
SqlFragment fragment = null;
DbExpression e = commandTree.Query;
switch (commandTree.Query.ExpressionKind)
{
case DbExpressionKind.Project:
fragment = e.Accept(this);
Debug.Assert(fragment is SelectStatement);
break;
}
return fragment.ToString();
}
public override SqlFragment Visit(DbDistinctExpression expression)
{
SelectStatement select = VisitInputExpressionEnsureSelect(expression.Argument, null, null);
select.IsDistinct = true;
return select;
}
public override SqlFragment Visit(DbFilterExpression expression)
{
SelectStatement select = VisitInputExpressionEnsureSelect(expression.Input.Expression,
expression.Input.VariableName, expression.Input.VariableType);
select = WrapIfNotCompatible(select, expression.ExpressionKind);
select.Where = expression.Predicate.Accept(this);
return select;
}
public override SqlFragment Visit(DbGroupByExpression expression)
{
// first process the input
DbGroupExpressionBinding e = expression.Input;
SelectStatement innerSelect = VisitInputExpressionEnsureSelect(e.Expression, e.VariableName, e.VariableType);
scope.Add(e.GroupVariableName, innerSelect);
SelectStatement select = WrapIfNotCompatible(innerSelect, expression.ExpressionKind);
CollectionType ct = (CollectionType)expression.ResultType.EdmType;
RowType rt = (RowType)ct.TypeUsage.EdmType;
int propIndex = 0;
foreach (DbExpression key in expression.Keys)
{
var fragment = key.Accept(this);
select.AddGroupBy(fragment);
propIndex++;
var colFragment = fragment as ColumnFragment;
if (colFragment != null)
{
colFragment = colFragment.Clone();
colFragment.ColumnAlias = String.Format("K{0}", propIndex);
select.Columns.Add(colFragment);
}
}
for (int agg = 0; agg < expression.Aggregates.Count; agg++)
{
DbAggregate a = expression.Aggregates[agg];
DbFunctionAggregate fa = a as DbFunctionAggregate;
if (fa == null) throw new NotSupportedException();
string alias = rt.Properties[propIndex++].Name;
ColumnFragment functionCol = new ColumnFragment(null, null);
functionCol.Literal = HandleFunction(fa, a.Arguments[0].Accept(this));
functionCol.ColumnAlias = alias;
select.Columns.Add(functionCol);
}
return select;
}
private SqlFragment HandleFunction(DbFunctionAggregate fa, SqlFragment arg)
{
Debug.Assert(fa.Arguments.Count == 1);
if (fa.Function.NamespaceName != "Edm")
throw new NotSupportedException();
FunctionFragment fragment = new FunctionFragment();
fragment.Name = fa.Function.Name;
if (fa.Function.Name == "BigCount")
fragment.Name = "COUNT";
else
fragment.Name = fa.Function.Name.ToUpperInvariant();
fragment.Distinct = fa.Distinct;
fragment.Argmument = arg;
return fragment;
//return new CastExpression(aggregate, GetDbType(functionAggregate.ResultType.EdmType));
}
public override SqlFragment Visit(DbCrossJoinExpression expression)
{
Debug.Assert(expression.Inputs.Count == 2);
return HandleJoinExpression(expression.Inputs[0], expression.Inputs[1],
expression.ExpressionKind, null);
}
public override SqlFragment Visit(DbJoinExpression expression)
{
return HandleJoinExpression(expression.Left, expression.Right,
expression.ExpressionKind, expression.JoinCondition);
}
private SqlFragment HandleJoinExpression(DbExpressionBinding left, DbExpressionBinding right,
DbExpressionKind joinType, DbExpression joinCondition)
{
JoinFragment join = new JoinFragment();
join.JoinType = Metadata.GetOperator(joinType);
join.Left = VisitInputExpression(left.Expression, left.VariableName, left.VariableType);
join.Left = WrapJoinInputIfNecessary(join.Left, false);
join.Right = VisitInputExpression(right.Expression, right.VariableName, right.VariableType);
join.Right = WrapJoinInputIfNecessary(join.Right, true);
if (join.Right is SelectStatement)
{
SelectStatement select = join.Right as SelectStatement;
if (select.IsWrapped)
select.Name = right.VariableName;
}
// now handle the ON case
if (joinCondition != null)
join.Condition = joinCondition.Accept(this);
return join;
}
public SelectStatement WrapIfNotCompatible(SelectStatement select, DbExpressionKind expressionKind)
{
if (select.IsCompatible(expressionKind)) return select;
SelectStatement newSelect = new SelectStatement();
select.Wrap(scope);
select.Scoped = true;
newSelect.From = select;
return newSelect;
}
private InputFragment WrapJoinInputIfNecessary(InputFragment fragment, bool isRightPart)
{
if (fragment is SelectStatement || fragment is UnionFragment)
{
fragment.Wrap(scope);
fragment.Scoped = true;
}
else if (fragment is JoinFragment && isRightPart)
{
SelectStatement select = new SelectStatement();
select.From = fragment;
select.Name = fragment.Name;
select.Wrap(scope);
return select;
}
return fragment;
}
public override SqlFragment Visit(DbNewInstanceExpression expression)
{
Debug.Assert(expression.ResultType.EdmType is CollectionType);
SelectStatement s = new SelectStatement();
ColumnFragment c = new ColumnFragment(null, null);
if (expression.Arguments.Count != 0)
c.Literal = (LiteralFragment)expression.Arguments[0].Accept(this);
else
c.Literal = new LiteralFragment("NULL");
c.ColumnAlias = "X";
s.Columns.Add(c);
return s;
}
public override SqlFragment Visit(DbProjectExpression expression)
{
SelectStatement select = VisitInputExpressionEnsureSelect(expression.Input.Expression,
expression.Input.VariableName, expression.Input.VariableType);
// see if we need to wrap this select inside a new select
select = WrapIfNotCompatible(select, expression.ExpressionKind);
Debug.Assert(expression.Projection is DbNewInstanceExpression);
VisitNewInstanceExpression(select, expression.Projection as DbNewInstanceExpression);
return select;
}
private SelectStatement VisitInputExpressionEnsureSelect(DbExpression e, string name, TypeUsage type)
{
InputFragment fragment = VisitInputExpression(e, name, type);
if (fragment is SelectStatement) return (fragment as SelectStatement);
SelectStatement s = new SelectStatement();
// if the fragment is a union then it needs to be wrapped
if (fragment is UnionFragment)
(fragment as UnionFragment).Wrap(scope);
s.From = fragment;
return s;
}
public override SqlFragment Visit(DbElementExpression expression)
{
SelectStatement s = VisitInputExpressionEnsureSelect(expression.Argument, null, null);
s.Wrap(scope);
return s;
}
public override SqlFragment Visit(DbSortExpression expression)
{
SelectStatement select = VisitInputExpressionEnsureSelect(expression.Input.Expression,
expression.Input.VariableName, expression.Input.VariableType);
select = WrapIfNotCompatible(select, expression.ExpressionKind);
foreach (DbSortClause sortClause in expression.SortOrder)
{
select.AddOrderBy(new SortFragment(
sortClause.Expression.Accept(this), sortClause.Ascending));
}
return select;
}
public override SqlFragment Visit(DbLimitExpression expression)
{
SelectStatement select = (SelectStatement)VisitInputExpressionEnsureSelect(
expression.Argument, null, null);
select = WrapIfNotCompatible(select, expression.ExpressionKind);
select.Limit = expression.Limit.Accept(this);
return select;
}
public override SqlFragment Visit(DbSkipExpression expression)
{
SelectStatement select = VisitInputExpressionEnsureSelect(expression.Input.Expression, expression.Input.VariableName,
expression.Input.VariableType);
select = WrapIfNotCompatible(select, DbExpressionKind.Sort);
foreach (DbSortClause sortClause in expression.SortOrder)
{
select.AddOrderBy(
new SortFragment(sortClause.Expression.Accept(this), sortClause.Ascending));
}
// if we wrapped above, then this wrap will not create a new one so there
// is no harm in calling it
select = WrapIfNotCompatible(select, expression.ExpressionKind);
select.Skip = expression.Count.Accept(this);
return select;
}
public override SqlFragment Visit(DbUnionAllExpression expression)
{
UnionFragment f = new UnionFragment();
Debug.Assert(expression.Left is DbProjectExpression);
Debug.Assert(expression.Right is DbProjectExpression);
SelectStatement left = VisitInputExpressionEnsureSelect(expression.Left, null, null);
Debug.Assert(left.Name == null);
// left.Wrap(null);
SelectStatement right = VisitInputExpressionEnsureSelect(expression.Right, null, null);
Debug.Assert(right.Name == null);
// right.Wrap(null);
f.Left = left;
f.Right = right;
return f;
}
}
}
|