protected override void BuildStatement(SqlCodeObjectBuilder builder) { var objName = Sql.ObjectName.Parse(ObjectName); foreach (var grantee in Grantees) { var grantPrivilege = Security.Privileges.None; var columns = new string[0]; foreach (var privilegeNode in Privileges) { var privilege = ParsePrivilege(privilegeNode.Privilege); grantPrivilege |= privilege; if (privilegeNode.Columns != null) { var privCols = new List <string>(columns); foreach (var privColumn in privilegeNode.Columns) { if (!privCols.Contains(privColumn)) { privCols.Add(privColumn); } } columns = privCols.ToArray(); } } builder.AddObject(new GrantPrivilegesStatement(grantee, grantPrivilege, WithGrant, objName, columns)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var tableName = ObjectName.Parse(TableName); if (ValuesInsert != null) { var valueInsert = ValuesInsert; var values = valueInsert.Values.Select(x => x.Values.Select(ExpressionBuilder.Build) .ToArray()) .ToList(); builder.AddObject(new InsertStatement(tableName, ColumnNames, values)); } else if (SetInsert != null) { var assignments = SetInsert.Assignments; var columnNames = new List<string>(); var values = new List<SqlExpression>(); foreach (var assignment in assignments) { var columnName = assignment.ColumnName; var value = ExpressionBuilder.Build(assignment.Value); columnNames.Add(columnName); values.Add(value); } builder.AddObject(new InsertStatement(tableName, columnNames.ToArray(), new[] {values.ToArray()})); } else if (QueryInsert != null) { var queryInsert = QueryInsert; var queryExpression = ExpressionBuilder.Build(queryInsert.QueryExpression) as SqlQueryExpression; if (queryExpression == null) throw new SqlParseException(); builder.AddObject(new InsertSelectStatement(tableName, ColumnNames, queryExpression)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var queryExpression = (SqlQueryExpression)ExpressionBuilder.Build(QueryExpression); var statement = new CreateViewStatement(ViewName.Name, ColumnNames, queryExpression); statement.ReplaceIfExists = ReplaceIfExists; builder.AddObject(statement); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (VariableName.Equals(IsolationLevelVariable)) { var isolationLevel = ParseIsolationLevel(Value); builder.AddObject(new SetIsolationLevelStatement(isolationLevel)); } else if (VariableName.Equals(AccessVariable)) { bool status; if (String.Equals(Value, "READ ONLY", StringComparison.OrdinalIgnoreCase)) { status = true; } else if (String.Equals(Value, "READ WRITE", StringComparison.OrdinalIgnoreCase)) { status = false; } else { throw new SqlParseException("Invalid access type"); } builder.AddObject(new SetReadOnlyStatement(status)); } }
public SqlCompileResult Compile(SqlCompileContext context) { if (context == null) throw new ArgumentNullException("context"); var compileResult = new SqlCompileResult(context); try { var sqlSource = context.SourceText; var result = SqlParsers.Default.Parse(sqlSource); foreach (var error in result.Errors) { var location = new SourceLocation(error.Line, error.Column); compileResult.Messages.Add(new SqlCompileMessage(CompileMessageLevel.Error, error.Message, location)); } ITypeResolver typeResolver = null; if (context.Context != null) typeResolver = context.Context.TypeResolver(); var builder = new SqlCodeObjectBuilder(typeResolver); var objects = builder.Build(result.RootNode); foreach (var statement in objects) { compileResult.Statements.Add(statement); } } catch (SqlParseException ex) { compileResult.Messages.Add(new SqlCompileMessage(CompileMessageLevel.Error, ex.Message)); } catch (Exception ex) { compileResult.Messages.Add(new SqlCompileMessage(CompileMessageLevel.Error, ex.Message)); } return compileResult; }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var objName = Sql.ObjectName.Parse(ObjectName); foreach (var grantee in Grantees) { var grantPrivilege = Security.Privileges.None; var columns = new string[0]; foreach (var privilegeNode in Privileges) { var privilege = ParsePrivilege(privilegeNode.Privilege); grantPrivilege |= privilege; if (privilegeNode.Columns != null) { var privCols = new List<string>(columns); foreach (var privColumn in privilegeNode.Columns) { if (!privCols.Contains(privColumn)) privCols.Add(privColumn); } columns = privCols.ToArray(); } } builder.AddObject(new GrantPrivilegesStatement(grantee, grantPrivilege, WithGrant, objName, columns)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (CreateTable != null) { var statements = new List <ISqlCodeObject>(); CreateTable.Build(builder.TypeResolver, builder); foreach (var statement in statements) { if (statement is CreateTableStatement) { ((CreateTableStatement)statement).IfNotExists = true; } } foreach (var statement in statements) { builder.AddObject(statement); } } else if (Actions != null) { foreach (var action in Actions) { BuildAction(builder.TypeResolver, ObjectName.Parse(TableName), action, builder); } } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { foreach (var tableName in TableNames) { var name = ObjectName.Parse(tableName); builder.AddObject(new DropTableStatement(name, IfExists)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { foreach (var viewName in ViewNames) { var name = ObjectName.Parse(viewName); builder.AddObject(new DropViewStatement(name, IfExists)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { foreach (var grantee in Grantees) { foreach (var role in Roles) { builder.AddObject(new GrantRoleStatement(grantee, role, WithAdmin)); } } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { SqlExpression exp = null; if (WhenExpression != null) exp = ExpressionBuilder.Build(WhenExpression); builder.AddObject(new LoopControlStatement(LoopControlType.Exit, Label, exp)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (String.IsNullOrEmpty(TypeName)) throw new InvalidOperationException(); var typeName = ObjectName.Parse(TypeName); builder.AddObject(new DropTypeStatement(typeName)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (SimpleUpdate != null) { BuildSimpleUpdate(builder, SimpleUpdate); } else if (QueryUpdate != null) { BuildQueryUpdate(builder, QueryUpdate); } }
private void BuildSimpleUpdate(SqlCodeObjectBuilder builder, SimpleUpdateNode node) { var whereExpression = ExpressionBuilder.Build(node.WhereExpression); var assignments = UpdateAssignments(node.Columns); var statement = new UpdateStatement(node.TableName, whereExpression, assignments); statement.Limit = node.Limit; builder.AddObject(statement); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (UserNames == null) throw new InvalidOperationException("None user was set to delete."); foreach (var userName in UserNames) { builder.AddObject(new DropUserStatement(userName)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var args = new List<SqlExpression>(); if (Arguments != null) { args = Arguments.Select(ExpressionBuilder.Build).ToList(); } builder.AddObject(new OpenStatement(CursorName, args.ToArray())); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (Identificator is IdentifiedByPasswordNode) { var passwordNode = (IdentifiedByPasswordNode)Identificator; var password = ExpressionBuilder.Build(passwordNode.Password); builder.AddObject(new CreateUserStatement(UserName, password)); } else { throw new NotSupportedException(); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (CallbackTrigger) { var tableName = ObjectName.Parse(TableName); builder.AddObject(new DropCallbackTriggersStatement(tableName)); } else { var triggerName = ObjectName.Parse(TriggerName); builder.AddObject(new DropTriggerStatement(triggerName)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var varType = DataTypeBuilder.Build(builder.TypeResolver, Type); var statement = new DeclareVariableStatement(VariableName, varType); if (DefaultExpression != null) statement.DefaultExpression = ExpressionBuilder.Build(DefaultExpression); statement.IsConstant = IsConstant; statement.IsNotNull = IsConstant || IsNotNull; builder.AddObject(statement); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var args = new List <SqlExpression>(); if (Arguments != null) { args = Arguments.Select(ExpressionBuilder.Build).ToList(); } builder.AddObject(new OpenStatement(CursorName, args.ToArray())); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (String.IsNullOrEmpty(TypeName)) { throw new InvalidOperationException(); } var typeName = ObjectName.Parse(TypeName); builder.AddObject(new DropTypeStatement(typeName)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var varRefExp = ExpressionBuilder.Build(VariableReference); var valueExp = ExpressionBuilder.Build(ValueExpression); if (!(varRefExp is SqlVariableReferenceExpression) && !(varRefExp is SqlReferenceExpression)) throw new NotSupportedException("Only simple references are supported now."); builder.AddObject(new AssignVariableStatement(varRefExp, valueExp)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var tableName = ObjectName.Parse(TableName); if (!FromCursor) { var whereExp = ExpressionBuilder.Build(WhereExpression); builder.AddObject(new DeleteStatement(tableName, whereExp, Limit)); } else { builder.AddObject(new DeleteCurrentStatement(tableName, CursorName)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { SqlExpression exp = null; if (WhenExpression != null) { exp = ExpressionBuilder.Build(WhenExpression); } builder.AddObject(new LoopControlStatement(LoopControlType.Continue, Label, exp)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var target = ParseTarget(Target); var statement = new ShowStatement(target); if (!String.IsNullOrEmpty(TableName)) { statement.TableName = ObjectName.Parse(TableName); } builder.AddObject(statement); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (UserNames == null) { throw new InvalidOperationException("None user was set to delete."); } foreach (var userName in UserNames) { builder.AddObject(new DropUserStatement(userName)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var typeName = ObjectName.Parse(TypeName); var members = Attributes.Select(x => { var type = DataTypeBuilder.Build(builder.TypeResolver, x.Type); return(new UserTypeMember(x.Name, type)); }); builder.AddObject(new CreateTypeStatement(typeName, members) { ReplaceIfExists = ReplaceIfExists }); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var varRefExp = ExpressionBuilder.Build(VariableReference); var valueExp = ExpressionBuilder.Build(ValueExpression); if (!(varRefExp is SqlVariableReferenceExpression) && !(varRefExp is SqlReferenceExpression)) { throw new NotSupportedException("Only simple references are supported now."); } builder.AddObject(new AssignVariableStatement(varRefExp, valueExp)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var queryExpression = (SqlQueryExpression) ExpressionBuilder.Build(QueryExpression); if (QueryExpression.IntoClause != null) { var refExp = ExpressionBuilder.Build(QueryExpression.IntoClause); builder.AddObject(new SelectIntoStatement(queryExpression, refExp)); } else { var orderBy = BuildOrderBy(OrderBy); var statement = new SelectStatement(queryExpression, orderBy); statement.Limit = BuildLimit(Limit); builder.AddObject(statement); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { FetchDirection direction; if (!TryParseDirection(Direction, out direction)) throw new InvalidOperationException(); var statement = new FetchStatement(CursorName, direction); if (Into != null) statement.IntoReference = ExpressionBuilder.Build(Into); if (Position != null) statement.PositionExpression = ExpressionBuilder.Build(Position); builder.AddObject(statement); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var varType = DataTypeBuilder.Build(builder.TypeResolver, Type); var statement = new DeclareVariableStatement(VariableName, varType); if (DefaultExpression != null) { statement.DefaultExpression = ExpressionBuilder.Build(DefaultExpression); } statement.IsConstant = IsConstant; statement.IsNotNull = IsConstant || IsNotNull; builder.AddObject(statement); }
public void Build(ITypeResolver typeResolver, SqlCodeObjectBuilder builder) { string idColumn = null; var tableName = TableName; var constraints = new List <SqlTableConstraint>(); var columns = new List <SqlTableColumn>(); foreach (var column in Columns) { if (column.IsIdentity) { if (!String.IsNullOrEmpty(idColumn)) { throw new InvalidOperationException(String.Format("Table {0} defines already {1} as identity column.", TableName, idColumn)); } if (column.Default != null) { throw new InvalidOperationException(String.Format("The identity column {0} cannot have a DEFAULT constraint.", idColumn)); } idColumn = column.ColumnName; } var columnInfo = column.BuildColumn(typeResolver, tableName.Name, constraints); columns.Add(columnInfo); } //TODO: Optimization: merge same constraints builder.AddObject(MakeCreateTable(tableName.Name, columns, IfNotExists, Temporary)); foreach (var constraint in Constraints) { var constraintInfo = constraint.BuildConstraint(); builder.AddObject(new AlterTableStatement(ObjectName.Parse(tableName.Name), new AddConstraintAction(constraintInfo))); } foreach (var constraint in constraints) { builder.AddObject(MakeAlterTableAddConstraint(tableName.Name, constraint)); } }
private static void BuildAction(ITypeResolver typeResolver, ObjectName tableName, IAlterActionNode action, SqlCodeObjectBuilder builder) { if (action is AddColumnNode) { var column = ((AddColumnNode) action).Column; var constraints = new List<SqlTableConstraint>(); var columnInfo = column.BuildColumn(typeResolver, tableName.FullName, constraints); builder.AddObject(new AlterTableStatement(tableName, new AddColumnAction(columnInfo))); foreach (var constraint in constraints) { builder.AddObject(new AlterTableStatement(tableName, new AddConstraintAction(constraint))); } } else if (action is AddConstraintNode) { var constraint = ((AddConstraintNode) action).Constraint; var constraintInfo = constraint.BuildConstraint(); builder.AddObject(new AlterTableStatement(tableName, new AddConstraintAction(constraintInfo))); } else if (action is DropColumnNode) { var columnName = ((DropColumnNode) action).ColumnName; builder.AddObject(new AlterTableStatement(tableName, new DropColumnAction(columnName))); } else if (action is DropConstraintNode) { var constraintName = ((DropConstraintNode) action).ConstraintName; builder.AddObject(new AlterTableStatement(tableName, new DropConstraintAction(constraintName))); } else if (action is SetDefaultNode) { var actionNode = ((SetDefaultNode) action); var columnName = actionNode.ColumnName; var expression = ExpressionBuilder.Build(actionNode.Expression); builder.AddObject(new AlterTableStatement(tableName, new SetDefaultAction(columnName, expression))); } else if (action is DropDefaultNode) { var columnName = ((DropDefaultNode) action).ColumnName; builder.AddObject(new AlterTableStatement(tableName, new DropDefaultAction(columnName))); } else if (action is AlterColumnNode) { var column = ((AlterColumnNode) action).Column; var constraints = new List<SqlTableConstraint>(); var columnInfo = column.BuildColumn(typeResolver, tableName.FullName, constraints); // CHECK: Here we do a drop and add column: is there a better way on the back-end? builder.AddObject(new AlterTableStatement(tableName, new DropColumnAction(columnInfo.ColumnName))); builder.AddObject(new AlterTableStatement(tableName, new AddColumnAction(columnInfo))); foreach (var constraint in constraints) { builder.AddObject(new AlterTableStatement(tableName, new AddConstraintAction(constraint))); } } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var queryExpression = (SqlQueryExpression)ExpressionBuilder.Build(QueryExpression); if (QueryExpression.IntoClause != null) { var refExp = ExpressionBuilder.Build(QueryExpression.IntoClause); builder.AddObject(new SelectIntoStatement(queryExpression, refExp)); } else { var orderBy = BuildOrderBy(OrderBy); var statement = new SelectStatement(queryExpression, orderBy); statement.Limit = BuildLimit(Limit); builder.AddObject(statement); } }
public SqlTableConstraint BuildConstraint() { if (String.Equals(ConstraintTypeNames.Check, ConstraintType, StringComparison.OrdinalIgnoreCase)) { var exp = ExpressionBuilder.Build(CheckExpression); return(new SqlTableConstraint(ConstraintName, Tables.ConstraintType.Check, Columns.ToArray()) { CheckExpression = exp }); } if (String.Equals(ConstraintTypeNames.PrimaryKey, ConstraintType, StringComparison.OrdinalIgnoreCase)) { return(SqlTableConstraint.PrimaryKey(ConstraintName, Columns.ToArray())); } if (String.Equals(ConstraintTypeNames.UniqueKey, ConstraintType, StringComparison.OrdinalIgnoreCase)) { return(SqlTableConstraint.UniqueKey(ConstraintName, Columns.ToArray())); } if (String.Equals(ConstraintTypeNames.ForeignKey, ConstraintType, StringComparison.OrdinalIgnoreCase)) { var fTable = ReferencedTableName.Name; var fColumns = ReferencedColumns; var onDelete = ForeignKeyAction.NoAction; var onUpdate = ForeignKeyAction.NoAction; if (!String.IsNullOrEmpty(OnDeleteAction)) { onDelete = SqlCodeObjectBuilder.GetForeignKeyAction(OnDeleteAction); } if (!String.IsNullOrEmpty(OnUpdateAction)) { onUpdate = SqlCodeObjectBuilder.GetForeignKeyAction(OnUpdateAction); } var fkey = SqlTableConstraint.ForeignKey(ConstraintName, Columns.ToArray(), fTable, fColumns.ToArray(), onDelete, onUpdate); return(fkey); } throw new NotSupportedException(); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { if (CreateTable != null) { var statements = new List<ISqlCodeObject>(); CreateTable.Build(builder.TypeResolver, builder); foreach (var statement in statements) { if (statement is CreateTableStatement) ((CreateTableStatement) statement).IfNotExists = true; } foreach (var statement in statements) { builder.AddObject(statement); } } else if (Actions != null) { foreach (var action in Actions) { BuildAction(builder.TypeResolver, ObjectName.Parse(TableName), action, builder); } } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var parameters = new List<CursorParameter>(); if (Parameters != null) { foreach (var parameterNode in Parameters) { var dataType = builder.BuildDataType(parameterNode.ParameterType); parameters.Add(new CursorParameter(parameterNode.ParameterName, dataType)); } } var flags = new CursorFlags(); if (Insensitive) flags |= CursorFlags.Insensitive; if (Scroll) flags |= CursorFlags.Scroll; var queryExpression = (SqlQueryExpression) ExpressionBuilder.Build(QueryExpression); builder.AddObject(new DeclareCursorStatement(CursorName, parameters.ToArray(), flags, queryExpression)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var seqName = ObjectName.Parse(SequenceName); var statement = new CreateSequenceStatement(seqName); if (IncrementBy != null) statement.IncrementBy = ExpressionBuilder.Build(IncrementBy); if (Cache != null) statement.Cache = ExpressionBuilder.Build(Cache); if (StartWith != null) statement.StartWith = ExpressionBuilder.Build(StartWith); if (MinValue != null) statement.MinValue = ExpressionBuilder.Build(MinValue); if (MaxValue != null) statement.MaxValue = ExpressionBuilder.Build(MaxValue); statement.Cycle = Cycle; builder.AddObject(statement); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { var tableName = ObjectName.Parse(TableName); if (ValuesInsert != null) { var valueInsert = ValuesInsert; var values = valueInsert.Values.Select(x => x.Values.Select(ExpressionBuilder.Build) .ToArray()) .ToList(); builder.AddObject(new InsertStatement(tableName, ColumnNames, values)); } else if (SetInsert != null) { var assignments = SetInsert.Assignments; var columnNames = new List <string>(); var values = new List <SqlExpression>(); foreach (var assignment in assignments) { var columnName = assignment.ColumnName; var value = ExpressionBuilder.Build(assignment.Value); columnNames.Add(columnName); values.Add(value); } builder.AddObject(new InsertStatement(tableName, columnNames.ToArray(), new[] { values.ToArray() })); } else if (QueryInsert != null) { var queryInsert = QueryInsert; var queryExpression = ExpressionBuilder.Build(queryInsert.QueryExpression) as SqlQueryExpression; if (queryExpression == null) { throw new SqlParseException(); } builder.AddObject(new InsertSelectStatement(tableName, ColumnNames, queryExpression)); } }
private void BuildBlock(SqlCodeObjectBuilder builder) { var block = new PlSqlBlock {Label = Label}; if (Declarations != null) { foreach (var declaration in Declarations) { var declBuilder = new SqlCodeObjectBuilder(builder.TypeResolver); var results = declBuilder.Build(declaration); foreach (var result in results) { // TODO: } } } if (CodeBlock != null) { foreach (var statement in CodeBlock.Statements) { } } builder.AddObject(block); }
public void Build(ITypeResolver typeResolver, SqlCodeObjectBuilder builder) { string idColumn = null; var tableName = TableName; var constraints = new List<SqlTableConstraint>(); var columns = new List<SqlTableColumn>(); foreach (var column in Columns) { if (column.IsIdentity) { if (!String.IsNullOrEmpty(idColumn)) throw new InvalidOperationException(String.Format("Table {0} defines already {1} as identity column.", TableName, idColumn)); if (column.Default != null) throw new InvalidOperationException(String.Format("The identity column {0} cannot have a DEFAULT constraint.", idColumn)); idColumn = column.ColumnName; } var columnInfo = column.BuildColumn(typeResolver, tableName.Name, constraints); columns.Add(columnInfo); } //TODO: Optimization: merge same constraints builder.AddObject(MakeCreateTable(tableName.Name, columns, IfNotExists, Temporary)); foreach (var constraint in Constraints) { var constraintInfo = constraint.BuildConstraint(); builder.AddObject(new AlterTableStatement(ObjectName.Parse(tableName.Name), new AddConstraintAction(constraintInfo))); } foreach (var constraint in constraints) { builder.AddObject(MakeAlterTableAddConstraint(tableName.Name, constraint)); } }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { FetchDirection direction; if (!TryParseDirection(Direction, out direction)) { throw new InvalidOperationException(); } var statement = new FetchStatement(CursorName, direction); if (Into != null) { statement.IntoReference = ExpressionBuilder.Build(Into); } if (Position != null) { statement.PositionExpression = ExpressionBuilder.Build(Position); } builder.AddObject(statement); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { throw new NotImplementedException(); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { builder.AddObject(new CloseStatement(CursorName)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { builder.AddObject(new DeclareExceptionStatement(Name)); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { Build(builder.TypeResolver, builder); }
protected override void BuildStatement(SqlCodeObjectBuilder builder) { builder.AddObject(new RollbackStatement()); }
protected abstract void BuildStatement(SqlCodeObjectBuilder builder);
protected override void BuildStatement(SqlCodeObjectBuilder builder) { builder.AddObject(new CommitStatement()); }