/// <summary> /// Gets escaped TSql identifier describing this entity set. /// </summary> /// <returns></returns> internal static string GetTargetTSql(EntitySetBase entitySetBase) { // construct escaped T-SQL referencing entity set var builder = new StringBuilder(50); var definingQuery = MetadataHelpers.TryGetValueForMetadataProperty <string>(entitySetBase, "DefiningQuery"); if (!string.IsNullOrEmpty(definingQuery)) { builder.Append("("); builder.Append(definingQuery); builder.Append(")"); } else { var tableName = MetadataHelpers.TryGetValueForMetadataProperty <string>(entitySetBase, "Table"); if (!string.IsNullOrEmpty(tableName)) { builder.Append(SQLiteProviderManifestHelper.QuoteIdentifier(tableName)); } else { builder.Append(SQLiteProviderManifestHelper.QuoteIdentifier(entitySetBase.Name)); } } return(builder.ToString()); }
public void AppendIdentifier(string identifier) { string correctIdentifier; correctIdentifier = identifier.ToLower().StartsWith("dbo.") ? identifier.Substring(4) : identifier; if (correctIdentifier.Length > SQLiteProviderManifestHelper.MaxObjectNameLength) { string guid = Guid.NewGuid().ToString().Replace("-", ""); correctIdentifier = correctIdentifier.Substring(0, SQLiteProviderManifestHelper.MaxObjectNameLength - guid.Length) + guid; } AppendSql(SQLiteProviderManifestHelper.QuoteIdentifier(correctIdentifier)); }
public void AppendIdentifier(string identifier) { string correctIdentifier; correctIdentifier = SQLiteProviderManifestHelper.RemoveDbo(identifier); if (correctIdentifier.Length > SQLiteProviderManifestHelper.MaxObjectNameLength) { string guid = Guid.NewGuid().ToString().Replace("-", ""); correctIdentifier = correctIdentifier.Substring(0, SQLiteProviderManifestHelper.MaxObjectNameLength - guid.Length) + guid; } AppendSql(SQLiteProviderManifestHelper.QuoteIdentifier(correctIdentifier)); }
public override void Visit(DbPropertyExpression expression) { this._commandText.Append(SQLiteProviderManifestHelper.QuoteIdentifier(expression.Property.Name)); }
internal static string GenerateInsertSql(DbInsertCommandTree tree, out List <DbParameter> parameters, bool insertParametersValuesInSql = false) { var commandText = new StringBuilder(COMMANDTEXT_STRINGBUILDER_INITIALCAPACITY); var translator = new ExpressionTranslator(commandText, tree, tree.Returning != null, insertParametersValuesInSql); commandText.Append("insert into "); tree.Target.Expression.Accept(translator); // Actually is not possible to retrieve the last inserted guid from Access // We can understand if there is a guid checking the returning value of the insert // statement string guidAutogeneratedColumn = null; if (tree.Returning is DbNewInstanceExpression) { guidAutogeneratedColumn = GetGuidArgs(tree.Returning as DbNewInstanceExpression); } if (tree.SetClauses.Count != 0) { var first = true; // (c1, c2, c3, ...) commandText.Append("("); if (!string.IsNullOrEmpty(guidAutogeneratedColumn)) { commandText.Append(SQLiteProviderManifestHelper.QuoteIdentifier(guidAutogeneratedColumn)); first = false; } foreach (DbSetClause setClause in tree.SetClauses) { if (first) { first = false; } else { commandText.Append(", "); } setClause.Property.Accept(translator); } commandText.AppendLine(")"); // values c1, c2, ... first = true; commandText.Append("values ("); if (!string.IsNullOrEmpty(guidAutogeneratedColumn)) { _lastGuid = Guid.NewGuid(); commandText.Append(string.Format("{{{0}}}", _lastGuid)); first = false; } foreach (DbSetClause setClause in tree.SetClauses) { if (first) { first = false; } else { commandText.Append(", "); } setClause.Value.Accept(translator); translator.RegisterMemberValue(setClause.Property, setClause.Value); } commandText.AppendLine(");"); } else { commandText.AppendLine(" default values;"); } // generate returning sql GenerateReturningSql(commandText, tree, translator, tree.Returning); parameters = translator.Parameters; return(commandText.ToString()); }
/// <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 IdentityValue = @@Identity /// /// </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 (returning == null) { 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 "); var table = ((DbScanExpression)tree.Target.Expression).Target; var identity = false; var first = true; foreach (var keyMember in table.ElementType.KeyMembers) { if (first) { first = false; } else { commandText.Append(" and "); } commandText.Append(SQLiteProviderManifestHelper.QuoteIdentifier(keyMember.Name)); 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 @@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)); } if (keyMember.TypeUsage.EdmType.Name.ToLower() == "guid") // We can't retrieve the latest inserted guid from Access //commandText.Append("@@guid"); { commandText.AppendFormat("{{{0}}}", _lastGuid); } else { commandText.Append("@@identity"); } identity = true; } } }