本文整理汇总了C#中DbExpression.Accept方法的典型用法代码示例。如果您正苦于以下问题:C# DbExpression.Accept方法的具体用法?C# DbExpression.Accept怎么用?C# DbExpression.Accept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbExpression
的用法示例。
在下文中一共展示了DbExpression.Accept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryGenerateKey
internal static bool TryGenerateKey(DbExpression tree, out string key)
{
var keyGen = new ExpressionKeyGen();
try
{
tree.Accept(keyGen);
key = keyGen._key.ToString();
return true;
}
catch (NotSupportedException)
{
key = null;
return false;
}
}
示例2: TranslateConstantParameterForLike
/// <summary>
/// Function to translate the StartsWith, EndsWith and Contains canonical functions to LIKE expression in T-SQL
/// and also add the trailing ESCAPE '~' when escaping of the search string for the LIKE expression has occurred
/// </summary>
/// <param name="sqlgen"></param>
/// <param name="targetExpression"></param>
/// <param name="constSearchParamExpression"></param>
/// <param name="result"></param>
/// <param name="insertPercentStart"></param>
/// <param name="insertPercentEnd"></param>
private static void TranslateConstantParameterForLike(
SqlGenerator sqlgen, DbExpression targetExpression, DbConstantExpression constSearchParamExpression, SqlBuilder result,
bool insertPercentStart, bool insertPercentEnd)
{
result.Append(targetExpression.Accept(sqlgen));
result.Append(" LIKE ");
// If it's a DbConstantExpression then escape the search parameter if necessary.
bool escapingOccurred;
var searchParamBuilder = new StringBuilder();
if (insertPercentStart)
{
searchParamBuilder.Append("%");
}
searchParamBuilder.Append(
SqlProviderManifest.EscapeLikeText(constSearchParamExpression.Value as string, false, out escapingOccurred));
if (insertPercentEnd)
{
searchParamBuilder.Append("%");
}
var escapedSearchParamExpression = constSearchParamExpression.ResultType.Constant(searchParamBuilder.ToString());
result.Append(escapedSearchParamExpression.Accept(sqlgen));
// If escaping did occur (special characters were found), then append the escape character used.
if (escapingOccurred)
{
result.Append(" ESCAPE '" + SqlProviderManifest.LikeEscapeChar + "'");
}
}
示例3: AppendConvertToVarchar
/// <summary>
/// Helper method that wrapps the given expession with a conver to varchar(255)
/// </summary>
/// <param name="result"></param>
/// <param name="e"></param>
private static void AppendConvertToVarchar(SqlGenerator sqlgen, SqlBuilder result, DbExpression e)
{
result.Append("convert(varchar(255), ");
result.Append(e.Accept(sqlgen));
result.Append(")");
}
示例4: GenerateReturningSql
/// <summary>
/// Generates SQL fragment returning server-generated values.
/// Requires: translator knows about member values so that we can figure out
/// how to construct the key predicate.
/// <code>
/// Sample SQL:
///
/// select IdentityValue
/// from MyTable
/// where IdentityValue = @@identity
///
/// NOTE: not scope_identity() because we don't support it.
/// </code>
/// </summary>
/// <param name="commandText">Builder containing command text</param>
/// <param name="tree">Modification command tree</param>
/// <param name="translator">Translator used to produce DML SQL statement
/// for the tree</param>
/// <param name="returning">Returning expression. If null, the method returns
/// immediately without producing a SELECT statement.</param>
private static void GenerateReturningSql(
StringBuilder commandText, DbModificationCommandTree tree,
ExpressionTranslator translator, DbExpression returning)
{
if (returning != null)
{
commandText.Append("select ");
returning.Accept(translator);
commandText.AppendLine();
commandText.Append("from ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
commandText.Append("where ");
var target = ((DbScanExpression)tree.Target.Expression).Target;
var flag = false;
var isFirst = true;
foreach (var member in target.ElementType.KeyMembers)
{
DbParameter parameter;
if (!isFirst)
{
commandText.Append(" and ");
}
else
{
isFirst = false;
}
commandText.Append(GenerateMemberTSql(member));
commandText.Append(" = ");
if (translator.MemberValues.TryGetValue(member, out parameter))
{
commandText.Append(parameter.ParameterName);
}
else
{
if (flag)
{
throw ADP1.NotSupported(ADP1.Update_NotSupportedServerGenKey(target.Name));
}
if (!IsValidIdentityColumnType(member.TypeUsage))
{
throw ADP1.InvalidOperation(ADP1.Update_NotSupportedIdentityType(member.Name, member.TypeUsage.ToString()));
}
commandText.Append("@@IDENTITY");
flag = true;
}
}
}
}
示例5: GenerateReturningSql
/// <summary>
/// Generates SQL fragment returning server-generated values.
/// Requires: translator knows about member values so that we can figure out
/// how to construct the key predicate.
/// <code>Sample SQL:
///
/// select IdentityValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and IdentityValue = scope_identity()
///
/// or
///
/// select TimestampValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and Id = 1
///
/// Note that we filter on rowcount to ensure no rows are returned if no rows were modified.
///
/// On SQL Server 2005 and up, we have an additional syntax used for non integer return types:
///
/// declare @generatedValues table(ID uniqueidentifier)
/// insert dbo.MyTable
/// output ID into @generated_values
/// values (...);
/// select ID
/// from @generatedValues as g join dbo.MyTable as t on g.ID = t.ID
/// where @@ROWCOUNT > 0;</code>
/// </summary>
/// <param name="commandText"> Builder containing command text </param>
/// <param name="tree"> Modification command tree </param>
/// <param name="tableType"> Type of table. </param>
/// <param name="translator"> Translator used to produce DML SQL statement for the tree </param>
/// <param name="returning"> Returning expression. If null, the method returns immediately without producing a SELECT statement. </param>
private static void GenerateReturningSql(
StringBuilder commandText, DbModificationCommandTree tree, EntityType tableType,
ExpressionTranslator translator, DbExpression returning, bool useGeneratedValuesVariable)
{
// Nothing to do if there is no Returning expression
if (null == returning)
{
return;
}
// select
commandText.Append("select ");
if (useGeneratedValuesVariable)
{
translator.PropertyAlias = "t";
}
returning.Accept(translator);
if (useGeneratedValuesVariable)
{
translator.PropertyAlias = null;
}
commandText.AppendLine();
if (useGeneratedValuesVariable)
{
// from @generated_values
commandText.Append("from ");
commandText.Append(s_generatedValuesVariableName);
commandText.Append(" as g join ");
tree.Target.Expression.Accept(translator);
commandText.Append(" as t on ");
var separator = string.Empty;
foreach (var keyMember in tableType.KeyMembers)
{
commandText.Append(separator);
separator = " and ";
commandText.Append("g.");
var memberTSql = GenerateMemberTSql(keyMember);
commandText.Append(memberTSql);
commandText.Append(" = t.");
commandText.Append(memberTSql);
}
commandText.AppendLine();
commandText.Append("where @@ROWCOUNT > 0");
}
else
{
// from
commandText.Append("from ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
// where
commandText.Append("where @@ROWCOUNT > 0");
var table = ((DbScanExpression)tree.Target.Expression).Target;
var identity = false;
foreach (var keyMember in table.ElementType.KeyMembers)
{
commandText.Append(" and ");
commandText.Append(GenerateMemberTSql(keyMember));
commandText.Append(" = ");
// retrieve member value sql. the translator remembers member values
// as it constructs the DML statement (which precedes the "returning"
// SQL)
SqlParameter value;
if (translator.MemberValues.TryGetValue(keyMember, out value))
//.........这里部分代码省略.........
示例6: VisitExpression
public virtual void VisitExpression(DbExpression expression)
{
if (expression == null) throw new ArgumentException("expression");
expression.Accept(this);
}
示例7: GenerateReturningSql
/// <summary>
/// Generates SQL fragment returning server-generated values.
/// Requires: translator knows about member values so that we can figure out
/// how to construct the key predicate.
/// <code>
/// Sample SQL:
///
/// select IdentityValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and IdentityValue = scope_identity()
///
/// or
///
/// select TimestamptValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and Id = 1
///
/// Note that we filter on rowcount to ensure no rows are returned if no rows were modified.
/// </code>
/// </summary>
/// <param name="commandText">Builder containing command text</param>
/// <param name="tree">Modification command tree</param>
/// <param name="translator">Translator used to produce DML SQL statement
/// for the tree</param>
/// <param name="returning">Returning expression. If null, the method returns
/// immediately without producing a SELECT statement.</param>
private static void GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree,
ExpressionTranslator translator, DbExpression returning)
{
// Nothing to do if there is no Returning expression
if (null == returning) { return; }
// select
commandText.Append("SELECT ");
returning.Accept(translator);
commandText.AppendLine();
// from
commandText.Append("FROM ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
// where
commandText.Append("WHERE last_rows_affected() > 0");
EntitySetBase table = ((DbScanExpression)tree.Target.Expression).Target;
bool identity = false;
foreach (EdmMember keyMember in table.ElementType.KeyMembers)
{
commandText.Append(" AND ");
commandText.Append(GenerateMemberTSql(keyMember));
commandText.Append(" = ");
// retrieve member value sql. the translator remembers member values
// as it constructs the DML statement (which precedes the "returning"
// SQL)
DbParameter value;
if (translator.MemberValues.TryGetValue(keyMember, out value))
{
commandText.Append(value.ParameterName);
}
else
{
// if no value is registered for the key member, it means it is an identity
// which can be retrieved using the scope_identity() function
if (identity)
{
// there can be only one server generated key
throw new NotSupportedException(string.Format("Server generated keys are only supported for identity columns. More than one key column is marked as server generated in table '{0}'.", table.Name));
}
commandText.AppendLine("last_insert_rowid();");
identity = true;
}
}
}
示例8: HandleJoinExpression
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);
WrapJoinInputIfNecessary(join.Left, false);
join.Right = VisitInputExpression(right.Expression, right.VariableName, right.VariableType);
WrapJoinInputIfNecessary(join.Right, true);
// now handle the ON case
if (joinCondition != null)
join.Condition = joinCondition.Accept(this);
return join;
}
示例9: GenerateReturningSql
// <summary>
// Generates SQL fragment returning server-generated values.
// Requires: translator knows about member values so that we can figure out
// how to construct the key predicate.
// <code>Sample SQL:
//
// select IdentityValue
// from MyTable
// where IdentityValue = @@identity
//
// NOTE: not scope_identity() because we don't support it.</code>
// </summary>
// <param name="commandText"> Builder containing command text </param>
// <param name="tree"> Modification command tree </param>
// <param name="translator"> Translator used to produce DML SQL statement for the tree </param>
// <param name="returning"> Returning expression. If null, the method returns immediately without producing a SELECT statement. </param>
private static void GenerateReturningSql(
SqlStringBuilder commandText,
DbModificationCommandTree tree,
ExpressionTranslator translator,
DbExpression returning)
{
if (returning != null)
{
commandText.AppendKeyword("select ");
returning.Accept(translator);
commandText.AppendLine();
commandText.AppendKeyword("from ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
commandText.AppendKeyword("where ");
var target = ((DbScanExpression)tree.Target.Expression).Target;
var flag = false;
var isFirst = true;
foreach (var member in target.ElementType.KeyMembers)
{
if (!isFirst)
{
commandText.AppendKeyword(" and ");
}
else
{
isFirst = false;
}
commandText.Append(GenerateMemberTSql(member));
commandText.Append(" = ");
flag = HandleIdentity(commandText, translator, member, flag, target);
}
}
}
示例10: VisitBinaryExpression
protected override SqlFragment VisitBinaryExpression(DbExpression left, DbExpression right, string op)
{
BinaryFragment f = new BinaryFragment();
f.Operator = op;
f.Left = left.Accept(this);
f.WrapLeft = ShouldWrapExpression(left);
if (f.Left is ColumnFragment)
{
_columnsVisited.Push( (( DbPropertyExpression )left ).Property );
}
f.Right = right.Accept(this);
if (f.Left is ColumnFragment)
{
_columnsVisited.Pop();
}
f.WrapRight = ShouldWrapExpression(right);
return f;
}
示例11: Dump
/// <summary>
/// Dumps a DbExpression by visiting it.
/// </summary>
/// <param name="target">The DbExpression to dump</param>
internal void Dump(DbExpression target)
{
target.Accept(this);
}
示例12: GenerateReturningSql
/// <summary>
/// Generates SQL fragment returning server-generated values.
/// Requires: translator knows about member values so that we can figure out
/// how to construct the key predicate.
/// <code>
/// Sample SQL:
///
/// select IdentityValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and IdentityValue = scope_identity()
///
/// or
///
/// select TimestamptValue
/// from dbo.MyTable
/// where @@ROWCOUNT > 0 and Id = 1
///
/// Note that we filter on rowcount to ensure no rows are returned if no rows were modified.
/// </code>
/// </summary>
/// <param name="commandText">Builder containing command text</param>
/// <param name="tree">Modification command tree</param>
/// <param name="translator">Translator used to produce DML SQL statement
/// for the tree</param>
/// <param name="returning">Returning expression. If null, the method returns
/// immediately without producing a SELECT statement.</param>
/// <param name="wasInsert">
/// Non-zero if this method is being called as part of processing an INSERT;
/// otherwise (e.g. UPDATE), zero.
/// </param>
private static void GenerateReturningSql(StringBuilder commandText, DbModificationCommandTree tree,
ExpressionTranslator translator, DbExpression returning, bool wasInsert)
{
// Nothing to do if there is no Returning expression
if (null == returning) { return; }
// select
commandText.Append("SELECT ");
returning.Accept(translator);
commandText.AppendLine();
// from
commandText.Append("FROM ");
tree.Target.Expression.Accept(translator);
commandText.AppendLine();
// where
#if USE_INTEROP_DLL && INTEROP_EXTENSION_FUNCTIONS
commandText.Append("WHERE last_rows_affected() > 0");
#else
commandText.Append("WHERE changes() > 0");
#endif
EntitySetBase table = ((DbScanExpression)tree.Target.Expression).Target;
ReadOnlyMetadataCollection<EdmMember> keyMembers;
EdmMember primaryKeyMember;
EdmMember missingKeyMember;
// Model Types can be (at the time of this implementation):
// Binary, Boolean, Byte, DateTime, Decimal, Double, Guid, Int16,
// Int32, Int64,Single, String
if (IsIntegerPrimaryKey(table, out keyMembers, out primaryKeyMember))
{
//
// NOTE: This must be an INTEGER PRIMARY KEY (i.e. "rowid") table.
//
commandText.Append(" AND ");
commandText.Append(GenerateMemberTSql(primaryKeyMember));
commandText.Append(" = ");
DbParameter value;
if (translator.MemberValues.TryGetValue(primaryKeyMember, out value))
{
//
// NOTE: Use the integer primary key value that was specified as
// part the associated INSERT/UPDATE statement.
//
commandText.Append(value.ParameterName);
}
else if (wasInsert)
{
//
// NOTE: This was part of an INSERT statement and we know the table
// has an integer primary key. This should not fail unless
// something (e.g. a trigger) causes the last_insert_rowid()
// function to return an incorrect result.
//
commandText.AppendLine("last_insert_rowid()");
}
else /* NOT-REACHED? */
{
//
// NOTE: We cannot simply use the "rowid" at this point because:
//
// 1. The last_insert_rowid() function is only valid after
// an INSERT and this was an UPDATE.
//
throw new NotSupportedException(String.Format(
"Missing value for INSERT key member '{0}' in table '{1}'.",
//.........这里部分代码省略.........
示例13: VisitExpression
/// <summary>
/// Convenience method to visit the specified <see cref="DbExpression" />, if non-null.
/// </summary>
/// <param name="expression"> The expression to visit. </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="expression" />
/// is null
/// </exception>
public virtual void VisitExpression(DbExpression expression)
{
// #433613: PreSharp warning 56506: Parameter 'expression' to this public method must be validated: A null-dereference can occur here.
Check.NotNull(expression, "expression");
expression.Accept(this);
}
示例14: GetAffectedEntitySets
/// <summary>
/// Gets the affected entity sets.
/// </summary>
private static EntitySetBase[] GetAffectedEntitySets(DbExpression expression)
{
var visitor = new ScanExpressionVisitor();
expression.Accept(visitor);
return visitor.EntitySets.ToArray();
}
示例15: VisitExpression
/// <summary>
/// Convenience method to visit the specified <see cref="DbExpression"/>, if non-null.
/// </summary>
/// <param name="expression">The expression to visit.</param>
/// <exception cref="ArgumentNullException"><paramref name="expression"/> is null</exception>
public virtual void VisitExpression(DbExpression expression)
{
// #433613: PreSharp warning 56506: Parameter 'expression' to this public method must be validated: A null-dereference can occur here.
//Contract.Requires(expression != null);
expression.Accept(this);
}