private void TranslateCommandTree(DbCommandTree commandTree, DbCommand command) { SqlBaseGenerator sqlGenerator = null; DbQueryCommandTree select; DbInsertCommandTree insert; DbUpdateCommandTree update; DbDeleteCommandTree delete; if ((select = commandTree as DbQueryCommandTree) != null) { sqlGenerator = new SqlSelectGenerator(select); } else if ((insert = commandTree as DbInsertCommandTree) != null) { sqlGenerator = new SqlInsertGenerator(insert); } else if ((update = commandTree as DbUpdateCommandTree) != null) { sqlGenerator = new SqlUpdateGenerator(update); } else if ((delete = commandTree as DbDeleteCommandTree) != null) { sqlGenerator = new SqlDeleteGenerator(delete); } else { // TODO: get a message (unsupported DbCommandTree type) throw new ArgumentException(); } sqlGenerator.BuildCommand(command); }
internal override void WriteSql(StringBuilder sqlText) { if (_variable != null) { _variable.WriteSql(sqlText); sqlText.Append("."); } sqlText.Append(SqlBaseGenerator.QuoteIdentifier(_property.Name)); base.WriteSql(sqlText); }
internal override void WriteSql(StringBuilder sqlText) { _column.WriteSql(sqlText); ColumnReferenceExpression column = _column as ColumnReferenceExpression; if (column == null || column.Name != _columnName) { sqlText.Append(" AS "); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(_columnName)); } base.WriteSql(sqlText); }
internal override void WriteSql(StringBuilder sqlText) { sqlText.Append("SELECT "); if (Distinct) { sqlText.Append("DISTINCT "); } if (Projection != null) { Projection.WriteSql(sqlText); } else { if (ColumnsToProject.Count == 0) { sqlText.Append("1"); // Could be arbitrary, let's pick 1 } else { var first = true; foreach (var column in ColumnsToProject) { if (!first) { sqlText.Append(", "); } else { first = false; } sqlText.Append(SqlBaseGenerator.QuoteIdentifier(column.Key.Item1)); sqlText.Append("."); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(column.Key.Item2)); if (column.Key.Item2 != column.Value) { sqlText.Append(" AS "); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(column.Value)); } } } } sqlText.Append(" FROM "); From.WriteSql(sqlText); Where?.WriteSql(sqlText); GroupBy?.WriteSql(sqlText); OrderBy?.WriteSql(sqlText); Skip?.WriteSql(sqlText); Limit?.WriteSql(sqlText); base.WriteSql(sqlText); }
internal void AppendReturning(DbNewInstanceExpression expression) { Append(" RETURNING ");//Don't put () around columns it will probably have unwanted effect bool first = true; foreach (var returingProperty in expression.Arguments) { if (!first) { Append(","); } Append(SqlBaseGenerator.QuoteIdentifier((returingProperty as DbPropertyExpression).Property.Name)); first = false; } }
internal override void WriteSql(StringBuilder sqlText) { bool wrap = !(_from is LiteralExpression || _from is ScanExpression); if (wrap) { sqlText.Append("("); } _from.WriteSql(sqlText); if (wrap) { sqlText.Append(")"); } sqlText.Append(" AS "); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(_name)); base.WriteSql(sqlText); }
internal override void WriteSql(StringBuilder sqlText) { var from = _from as InputExpression; if (from != null) { var input = from; if (!ForceSubquery && input.Projection == null && input.Where == null && input.Distinct == false && input.OrderBy == null && input.Skip == null && input.Limit == null) { // There is no point of writing // (SELECT ? FROM <from> AS <name>) AS <name> // so just write <from> AS <name> // <name> is always the same for both nodes // However, PostgreSQL needs a subquery in case we are in the right hand side of an Apply expression if (((FromExpression)input.From).Name != Name) { throw new ArgumentException(); } input.From.WriteSql(sqlText); } else { sqlText.Append("("); input.WriteSql(sqlText); sqlText.Append(") AS "); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(Name)); } } else { var wrap = !(_from is LiteralExpression || _from is ScanExpression || _from is FunctionExpression); if (wrap) { sqlText.Append("("); } _from.WriteSql(sqlText); if (wrap) { sqlText.Append(")"); } sqlText.Append(" AS "); sqlText.Append(SqlBaseGenerator.QuoteIdentifier(Name)); } base.WriteSql(sqlText); }
internal override void WriteSql(StringBuilder sqlText) { if (_variableSubstitution.ContainsKey(_name)) { sqlText.Append(SqlBaseGenerator.QuoteIdentifier(_variableSubstitution[_name])); } else { // TODO: come up with a better solution // need some way of removing extra levels of dots if (_name.Contains(".")) { sqlText.Append(SqlBaseGenerator.QuoteIdentifier(_name.Substring(_name.LastIndexOf('.') + 1))); } else { sqlText.Append(SqlBaseGenerator.QuoteIdentifier(_name)); } } base.WriteSql(sqlText); }
internal override IEnumerable <ColumnExpression> GetProjectedColumns() { Dictionary <string, string> emptySubstitution = new Dictionary <string, string>(); if (_from is ScanExpression) { ScanExpression scan = (ScanExpression)_from; foreach (var property in scan.Target.ElementType.Members.OfType <EdmProperty>()) { yield return(new ColumnExpression(new PropertyExpression(new VariableReferenceExpression(Name, emptySubstitution), property), property.Name, property.TypeUsage)); } } else { foreach (var column in _from.GetProjectedColumns()) { string columnRef = string.Format("{0}.{1}", SqlBaseGenerator.QuoteIdentifier(Name), column.Name); yield return(new ColumnExpression(new LiteralExpression(columnRef), column.Name, column.ColumnType)); } } }
internal void TranslateCommandTree(Version serverVersion, DbCommandTree commandTree, DbCommand command, bool createParametersForNonSelect = true) { SqlBaseGenerator sqlGenerator = null; DbQueryCommandTree select; DbInsertCommandTree insert; DbUpdateCommandTree update; DbDeleteCommandTree delete; if ((select = commandTree as DbQueryCommandTree) != null) { sqlGenerator = new SqlSelectGenerator(select); } else if ((insert = commandTree as DbInsertCommandTree) != null) { sqlGenerator = new SqlInsertGenerator(insert); } else if ((update = commandTree as DbUpdateCommandTree) != null) { sqlGenerator = new SqlUpdateGenerator(update); } else if ((delete = commandTree as DbDeleteCommandTree) != null) { sqlGenerator = new SqlDeleteGenerator(delete); } else { // TODO: get a message (unsupported DbCommandTree type) throw new ArgumentException(); } sqlGenerator._createParametersForConstants = select != null ? false : createParametersForNonSelect; sqlGenerator._command = (NpgsqlCommand)command; sqlGenerator.Version = serverVersion; sqlGenerator.BuildCommand(command); }
internal override void WriteSql(StringBuilder sqlText) { _column.WriteSql(sqlText); sqlText.Append(" AS " + SqlBaseGenerator.QuoteIdentifier(_columnName)); base.WriteSql(sqlText); }
internal override void WriteSql(StringBuilder sqlText) { sqlText.Append(SqlBaseGenerator.QuoteIdentifier(_property.Name)); base.WriteSql(sqlText); }