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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
// 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.Diagnostics;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Data.Common.CommandTrees;
using System.Data.Metadata.Edm;
using MySql.Data.MySqlClient;
using System.Globalization;
namespace MySql.Data.Entity
{
abstract class SqlGenerator : DbExpressionVisitor<SqlFragment>
{
protected string tabs = String.Empty;
private int parameterCount = 1;
protected Scope scope = new Scope();
protected int propertyLevel;
protected Dictionary<EdmMember, SqlFragment> values;
public SqlGenerator()
{
Parameters = new List<MySqlParameter>();
}
#region Properties
public List<MySqlParameter> Parameters { get; private set; }
// protected SymbolTable Symbols { get; private set; }
#endregion
public virtual string GenerateSQL(DbCommandTree commandTree)
{
throw new NotImplementedException();
}
protected string CreateUniqueParameterName()
{
return String.Format("@gp{0}", parameterCount++);
}
#region DbExpressionVisitor Base Implementations
public override SqlFragment Visit(DbVariableReferenceExpression expression)
{
PropertyFragment fragment = new PropertyFragment();
fragment.Properties.Add(expression.VariableName);
return fragment;
}
public override SqlFragment Visit(DbPropertyExpression expression)
{
propertyLevel++;
PropertyFragment fragment = expression.Instance.Accept(this) as PropertyFragment;
fragment.Properties.Add(expression.Property.Name);
propertyLevel--;
// if we are not at the top level property then just return
if (propertyLevel > 0) return fragment;
ColumnFragment column = new ColumnFragment(null, fragment.LastProperty);
column.PropertyFragment = fragment;
InputFragment input = scope.FindInputFromProperties(fragment);
if (input != null)
column.TableName = input.Name;
// now we need to check if our column name was possibly renamed
if (input is TableFragment) return column;
SelectStatement select = input as SelectStatement;
UnionFragment union = input as UnionFragment;
if (select != null)
select.HasDifferentNameForColumn(column);
else if (union != null)
union.HasDifferentNameForColumn(column);
// input is a table, selectstatement, or unionstatement
return column;
}
public override SqlFragment Visit(DbScanExpression expression)
{
EntitySetBase target = expression.Target;
TableFragment fragment = new TableFragment();
MetadataProperty property;
bool propExists = target.MetadataProperties.TryGetValue("DefiningQuery", true, out property);
if (propExists && property.Value != null)
fragment.DefiningQuery = new LiteralFragment(property.Value as string);
else
{
fragment.Schema = target.EntityContainer.Name;
fragment.Table = target.Name;
propExists = target.MetadataProperties.TryGetValue("Schema", true, out property);
if (propExists && property.Value != null)
fragment.Schema = property.Value as string;
propExists = target.MetadataProperties.TryGetValue("Table", true, out property);
if (propExists && property.Value != null)
fragment.Table = property.Value as string;
}
return fragment;
}
public override SqlFragment Visit(DbParameterReferenceExpression expression)
{
return new LiteralFragment("@" + expression.ParameterName);
}
public override SqlFragment Visit(DbNotExpression expression)
{
SqlFragment f = expression.Argument.Accept(this);
Debug.Assert(f is NegatableFragment);
NegatableFragment nf = f as NegatableFragment;
nf.Negate();
return nf;
}
public override SqlFragment Visit(DbIsEmptyExpression expression)
{
ExistsFragment f = new ExistsFragment(expression.Argument.Accept(this));
f.Negate();
return f;
}
public override SqlFragment Visit(DbFunctionExpression expression)
{
FunctionProcessor gen = new FunctionProcessor();
return gen.Generate(expression, this);
}
public override SqlFragment Visit(DbConstantExpression expression)
{
PrimitiveTypeKind pt = ((PrimitiveType)expression.ResultType.EdmType).PrimitiveTypeKind;
string literal = Metadata.GetNumericLiteral(pt, expression.Value);
if (literal != null)
return new LiteralFragment(literal);
else if (pt == PrimitiveTypeKind.Boolean)
return new LiteralFragment((bool)expression.Value ? "1" : "0");
else
{
// use a parameter for non-numeric types so we get proper
// quoting
MySqlParameter p = new MySqlParameter();
p.ParameterName = CreateUniqueParameterName();
p.DbType = Metadata.GetDbType(expression.ResultType);
p.Value = Metadata.NormalizeValue(expression.ResultType, expression.Value);
Parameters.Add(p);
return new LiteralFragment(p.ParameterName);
}
}
public override SqlFragment Visit(DbComparisonExpression expression)
{
return VisitBinaryExpression(expression.Left, expression.Right,
Metadata.GetOperator(expression.ExpressionKind));
}
public override SqlFragment Visit(DbAndExpression expression)
{
return VisitBinaryExpression(expression.Left, expression.Right, "AND");
}
public override SqlFragment Visit(DbOrExpression expression)
{
return VisitBinaryExpression(expression.Left, expression.Right, "OR");
}
public override SqlFragment Visit(DbCastExpression expression)
{
//TODO: handle casting
return expression.Argument.Accept(this);
}
public override SqlFragment Visit(DbLikeExpression expression)
{
LikeFragment f = new LikeFragment();
f.Argument = expression.Argument.Accept(this);
f.Pattern = expression.Pattern.Accept(this);
if (expression.Escape.ExpressionKind != DbExpressionKind.Null)
f.Escape = expression.Escape.Accept(this);
return f;
}
public override SqlFragment Visit(DbCaseExpression expression)
{
CaseFragment c = new CaseFragment();
Debug.Assert(expression.When.Count == expression.Then.Count);
for (int i = 0; i < expression.When.Count; ++i)
{
c.When.Add(expression.When[i].Accept(this));
c.Then.Add(expression.Then[i].Accept(this));
}
if (expression.Else != null && !(expression.Else is DbNullExpression))
c.Else = expression.Else.Accept(this);
return c;
}
public override SqlFragment Visit(DbIsNullExpression expression)
{
IsNullFragment f = new IsNullFragment();
f.Argument = expression.Argument.Accept(this);
return f;
}
public override SqlFragment Visit(DbIntersectExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbNullExpression expression)
{
return new LiteralFragment("NULL");
}
public override SqlFragment Visit(DbArithmeticExpression expression)
{
if (expression.ExpressionKind == DbExpressionKind.UnaryMinus)
{
ListFragment f = new ListFragment();
f.Append("-(");
f.Append(expression.Arguments[0].Accept(this));
f.Append(")");
return f;
}
string op = String.Empty;
switch (expression.ExpressionKind)
{
case DbExpressionKind.Divide:
op = "/"; break;
case DbExpressionKind.Minus:
op = "-"; break;
case DbExpressionKind.Modulo:
op = "%"; break;
case DbExpressionKind.Multiply:
op = "*"; break;
case DbExpressionKind.Plus:
op = "+"; break;
default:
throw new NotSupportedException();
}
return VisitBinaryExpression(expression.Arguments[0], expression.Arguments[1], op);
}
protected void VisitNewInstanceExpression(SelectStatement select,
DbNewInstanceExpression expression)
{
Debug.Assert(expression.ResultType.EdmType is RowType);
RowType row = expression.ResultType.EdmType as RowType;
for (int i = 0; i < expression.Arguments.Count; i++)
{
ColumnFragment col;
SqlFragment fragment = expression.Arguments[i].Accept(this);
if (fragment is ColumnFragment)
col = fragment as ColumnFragment;
else
{
col = new ColumnFragment(null, null);
col.Literal = fragment;
}
col.ColumnAlias = row.Properties[i].Name;
select.Columns.Add(col);
}
}
public override SqlFragment Visit(DbTreatExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbRelationshipNavigationExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbRefExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbOfTypeExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbIsOfExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbRefKeyExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbEntityRefExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbExceptExpression expression)
{
throw new NotSupportedException();
}
public override SqlFragment Visit(DbExpression expression)
{
throw new InvalidOperationException();
}
public override SqlFragment Visit(DbDerefExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbApplyExpression expression)
{
throw new NotSupportedException();
}
#endregion
#region DBExpressionVisitor methods normally overridden
public override SqlFragment Visit(DbUnionAllExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbSortExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbSkipExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbQuantifierExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbProjectExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbNewInstanceExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbLimitExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbJoinExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbGroupByExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbFilterExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbElementExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbDistinctExpression expression)
{
throw new NotImplementedException();
}
public override SqlFragment Visit(DbCrossJoinExpression expression)
{
throw new NotImplementedException();
}
#endregion
protected InputFragment VisitInputExpression(DbExpression e, string name, TypeUsage type)
{
SqlFragment f = e.Accept(this);
Debug.Assert(f is InputFragment);
InputFragment inputFragment = f as InputFragment;
inputFragment.Name = name;
if (inputFragment is TableFragment && type != null)
(inputFragment as TableFragment).Type = type;
if (name != null)
scope.Add(name, inputFragment);
return inputFragment;
}
protected SelectStatement GenerateReturningSql(DbModificationCommandTree tree, DbExpression returning)
{
SelectStatement select = new SelectStatement();
Debug.Assert(returning is DbNewInstanceExpression);
VisitNewInstanceExpression(select, returning as DbNewInstanceExpression);
select.From = (InputFragment)tree.Target.Expression.Accept(this);
ListFragment where = new ListFragment();
where.Append(" row_count() > 0");
EntitySetBase table = ((DbScanExpression)tree.Target.Expression).Target;
bool foundIdentity = false;
foreach (EdmMember keyMember in table.ElementType.KeyMembers)
{
SqlFragment value;
if (!values.TryGetValue(keyMember, out value))
{
if (foundIdentity)
throw new NotSupportedException();
foundIdentity = true;
value = new LiteralFragment("last_insert_id()");
}
where.Append(String.Format(" AND `{0}`=", keyMember));
where.Append(value);
}
select.Where = where;
return select;
}
#region Private Methods
SqlFragment VisitBinaryExpression(DbExpression left, DbExpression right, string op)
{
BinaryFragment f = new BinaryFragment();
f.Operator = op;
f.Left = left.Accept(this);
f.WrapLeft = ShouldWrapExpression(left);
f.Right = right.Accept(this);
f.WrapRight = ShouldWrapExpression(right);
return f;
}
private bool ShouldWrapExpression(DbExpression e)
{
switch (e.ExpressionKind)
{
case DbExpressionKind.Property:
case DbExpressionKind.ParameterReference:
case DbExpressionKind.Constant:
return false;
}
return true;
}
#endregion
}
}
|