private SqlCodeExpr ParseOnPartitionSchemeName(IParser parser) { if (!parser.MatchToken(SqlToken.ON)) { return(null); } var filegroupName = parser.ConsumeObjectId(); if (parser.MatchToken(SqlToken.LParen)) { var column = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.RParen); return(new OnSqlCodeExpr { Name = new PartitionSchemeNameSqlCodeExpr { Name = filegroupName, Column = column } }); } return(new OnSqlCodeExpr { Name = filegroupName }); }
private CreateIndexSqlCodeExpr CreateIndex(TextSpan indexSpan, IParser parser) { var indexName = parser.ConsumeObjectId(); parser.Scanner.Consume(SqlToken.ON); if (!parser.TryConsumeObjectId(out var tableName)) { tableName = parser.Consume(SqlToken.TempTable); } var onColumnsList = new List <SqlCodeExpr>(); parser.Scanner.Consume(SqlToken.LParen); do { var columnName = parser.ConsumeObjectId(); onColumnsList.Add(columnName); } while (parser.Scanner.Match(SqlToken.Comma)); parser.Scanner.Consume(SqlToken.RParen); return(new CreateIndexSqlCodeExpr { IndexName = indexName, TableName = tableName, OnColumns = onColumnsList }); }
private IExpression CreatePartitionScheme(TextSpan token, IParser parser) { parser.Scanner.Consume(SqlToken.Scheme); var schemeName = parser.ConsumeObjectId(); parser.Scanner.Consume(SqlToken.As); parser.Scanner.Consume(SqlToken.Partition); var funcName = parser.ConsumeObjectId(); parser.Scanner.TryConsumeString(SqlToken.ALL, out var allToken); parser.Scanner.Consume(SqlToken.To); var groupNameList = new List <SqlCodeExpr>(); parser.Scanner.Consume(SqlToken.LParen); do { groupNameList.Add(parser.ConsumePrimary()); } while (parser.Scanner.Match(SqlToken.Comma)); parser.Scanner.Consume(SqlToken.RParen); return(new CreatePartitionSchemeSqlCodeExpr { SchemeName = schemeName, FuncName = funcName, AllToken = allToken, GroupNameList = groupNameList }); }
public IExpression Parse(TextSpan token, IParser parser) { var tokenStr = parser.Scanner.GetSpanString(token); var variableExpr = new VariableSqlCodeExpr { Name = tokenStr }; if (parser.MatchToken(SqlToken.Dot)) { var nodesExpr = parser.PrefixParse(SqlToken.NODES) as SqlCodeExpr; parser.ConsumeToken(SqlToken.As); var aliasName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.LParen); var columnName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.RParen); return(new VariableNodesSqlCodeExpr { Variable = variableExpr, NodesExpr = nodesExpr, AliasName = aliasName, ColumnName = columnName }); } return(variableExpr); }
private SqlCodeExpr CreateSynonym(TextSpan synonymSpan, IParser parser) { var synonymName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.FOR); var objectId = parser.ConsumeObjectId(); return(new CreateSynonymSqlCodeExpr { Name = synonymName, ObjectId = objectId }); }
private MergeUpdateSqlCodeExpr ParseMergeUpdate(IParser parser) { if (!parser.Scanner.Match(SqlToken.Update)) { return(null); } parser.ConsumeToken(SqlToken.Set); var updateSetList = new List <AssignSqlCodeExpr>(); do { var leftExpr = parser.ConsumeObjectId(true); var oper = parser.ConsumeTokenString(); var rightExpr = parser.ParseExpIgnoreComment(); updateSetList.Add(new AssignSqlCodeExpr { Left = leftExpr, Oper = oper, Right = rightExpr }); } while (parser.Scanner.Match(SqlToken.Comma)); return(new MergeUpdateSqlCodeExpr { SetList = updateSetList }); }
private SqlCodeExpr CreateClusteredIndex(TextSpan clusteredSpan, IParser parser) { parser.Scanner.Consume(SqlToken.Index); var indexName = parser.ConsumeObjectId(); parser.Scanner.Consume(SqlToken.ON); if (!parser.TryConsumeObjectId(out var tableName)) { tableName = parser.Consume(SqlToken.TempTable); } parser.Scanner.Consume(SqlToken.LParen); var onColumnsList = parser.ParseOrderItemList(); parser.Scanner.Consume(SqlToken.RParen); var withExpr = parser.ParseConstraintWithOptions(); var onPartitionSchemeNameExpr = ParseOnPartitionSchemeName(parser); return(new CreateClusteredIndexSqlCodeExpr { IndexName = indexName, TableName = tableName, OnColumns = onColumnsList, WithExpr = withExpr, OnPartitionSchemeNameExpr = onPartitionSchemeNameExpr }); }
private DbccSqlCodeExpr ParseDbccCheckIdent(IParser parser) { if (!parser.MatchToken(SqlToken.CHECKIDENT)) { return(null); } parser.ConsumeToken(SqlToken.LParen); var tableName = parser.ConsumeObjectId(); var optionParametersList = ParseSeqOptionWithComma( parser, parser1 => parser1.ConsumeToTokenValueAny(SqlToken.NORESEED, SqlToken.RESEED), parser1 => parser1.ParseExpIgnoreComment() ); parser.ConsumeToken(SqlToken.RParen); var parametersList = new List <SqlCodeExpr>(); parametersList.Add(tableName); parametersList.AddRange(optionParametersList); return(new DbccSqlCodeExpr { Name = "CHECKIDENT", ParametersList = parametersList }); }
private IntoSqlCodeExpr ParseIntoExpr(IParser parser) { if (!parser.Scanner.Match(SqlToken.Into)) { return(null); } var intoTable = parser.ConsumeTableName(int.MaxValue); var columnList = new List <SqlCodeExpr>(); if (parser.MatchToken(SqlToken.LParen)) { do { var column = parser.ConsumeObjectId(true); columnList.Add(column); } while (parser.Scanner.Match(SqlToken.Comma)); parser.ConsumeToken(SqlToken.RParen); } return(new IntoSqlCodeExpr { IntoTable = intoTable, ColumnList = columnList }); }
private SqlCodeExpr AlterIndex(TextSpan indexSpan, IParser parser) { var indexName = parser.ParseExpIgnoreComment(int.MaxValue); parser.ConsumeToken(SqlToken.ON); var tableName = parser.ConsumeObjectId(); if (parser.MatchToken(SqlToken.REORGANIZE)) { return(new AlterIndexReogranizeSqlCodeExpr { TableName = tableName }); } parser.ConsumeToken(SqlToken.Rebuild); var withExpr = parser.ParseConstraintWithOptions(); return(new AlterIndexSqlCodeExpr { IndexName = indexName, TableName = tableName, WithExpr = withExpr }); }
private IExpression AlterDatabase(TextSpan databaseSpan, IParser parser) { SqlCodeExpr databaseName = null; if (parser.MatchToken(SqlToken.CURRENT)) { databaseName = new ObjectIdSqlCodeExpr { ObjectName = "CURRENT" }; } else { databaseName = parser.ConsumeObjectId(); } var actionExpr = parser.ConsumeAny( ParseSetOnOff, ParseAddFileGroup); var isSemicolon = parser.MatchToken(SqlToken.Semicolon); return(new AlterDatabaseSqlCodeExpr { DatabaseName = databaseName, ActionExpr = actionExpr, IsSemicolon = isSemicolon, }); }
private IExpression CreateFunction(TextSpan token, IParser parser) { var nameExpr = parser.ConsumeObjectId(); parser.Scanner.Consume(SqlToken.LParen); var arguments = parser.ConsumeArgumentList(); parser.Scanner.Consume(SqlToken.RParen); parser.Scanner.Consume(SqlToken.Returns); parser.Scanner.TryConsumeVariable(out var returnVariableExpr); var returnTypeExpr = parser.ConsumeDataType(); parser.Scanner.Consume(SqlToken.As); var body = parser.ConsumeBeginBody(); return(new CreateFunctionSqlCodeExpr { Name = nameExpr, Arguments = arguments, ReturnVariable = returnVariableExpr, ReturnType = returnTypeExpr, Body = body }); }
private IExpression AlterTable(TextSpan tableSpan, IParser parser) { var tableName = parser.ConsumeObjectId(); //var alterAction = parser.ConsumeTokenStringAny(SqlToken.ADD, SqlToken.Set, SqlToken.Drop, SqlToken.NOCHECK, SqlToken.CHECK); var alterActions = parser.ConsumeTokenListAny(new[] { SqlToken.ADD }, new[] { SqlToken.Set }, new[] { SqlToken.Drop }, new[] { SqlToken.NOCHECK }, new [] { SqlToken.CHECK }, new [] { SqlToken.With, SqlToken.NOCHECK }); var alterAction = string.Join(" ", alterActions.Select(x => parser.Scanner.GetSpanString(x))); var optionList = parser.ParseAll( ParseLRParenOptionList, SqlParserExtension.ParseClustered, SqlParserExtension.ParsePrimaryKey, SqlParserExtension.ParseConstraint, SqlParserExtension.ParseDefault, ParseFor, ParseColumnDataTypeList ); return(new AlterTableSqlCodeExpr { TableName = tableName, AlterAction = alterAction, OptionList = optionList }); }
private CreateRoleSqlCodeExpr ParseCreateRole(IParser parser) { if (!parser.MatchToken(SqlToken.ROLE)) { return(null); } var roleName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.AUTHORIZATION); var schemeName = parser.ConsumeObjectId(); return(new CreateRoleSqlCodeExpr { RoleName = roleName, SchemaName = schemeName }); }
private SqlCodeExpr DropIndex(TextSpan indexSpan, IParser parser) { var indexName = parser.ConsumeObjectId(); return(new DropIndexSqlCodeExpr { IndexName = indexName }); }
public IExpression Parse(TextSpan token, IParser parser) { var permissionPrincipal = new[] { SqlToken.CONNECT, SqlToken.Delete, SqlToken.Insert, SqlToken.Select, SqlToken.Update, SqlToken.Execute, SqlToken.Exec, SqlToken.Alter }; var permissionList = parser.Scanner.ConsumeToStringListByDelimiter(SqlToken.Comma, permissionPrincipal) .ToList(); if (permissionList.Count == 0) { var permissionPrincipalStr = string.Join(",", permissionPrincipal); ThrowHelper.ThrowParseException(parser, $"Expect one of {permissionPrincipalStr}."); } SqlCodeExpr onObjectId = null; if (parser.Scanner.Match(SqlToken.ON)) { //onObjectId = parser.ConsumeAny(SqlToken.Object, SqlToken.SqlIdentifier, SqlToken.Identifier) as SqlCodeExpr; onObjectId = parser.ParseExpIgnoreComment(); } parser.Scanner.Consume(SqlToken.To); var targetList = parser.ConsumeByDelimiter(SqlToken.Comma, () => { return(parser.ParseExp() as SqlCodeExpr); }).ToList(); SqlCodeExpr asDbo = null; if (parser.Scanner.Match(SqlToken.As)) { asDbo = parser.ConsumeObjectId(); } var isSemicolon = parser.MatchToken(SqlToken.Semicolon); return(new GrantSqlCodeExpr { PermissionList = permissionList, OnObjectId = onObjectId, TargetList = targetList, AsDbo = asDbo, IsSemicolon = isSemicolon }); }
public IExpression Parse(TextSpan token, IParser parser) { parser.Scanner.Consume(SqlToken.TABLE); var tableName = parser.ConsumeObjectId(); return(new TruncateTableSqlCodeExpr() { TableName = tableName, }); }
private SqlCodeExpr DropTrigger(TextSpan triggerSpan, IParser parser) { var name = parser.ConsumeObjectId(); return(new DropSqlCodeExpr { TargetId = "TRIGGER", ObjectId = name }); }
private CreateUserSqlCodeExpr ParseCreateUser(IParser parser) { if (!parser.MatchToken(SqlToken.USER)) { return(null); } var userName = parser.ConsumeObjectId(); SqlCodeExpr loginExpr = null; if (parser.MatchTokenList(SqlToken.FOR, SqlToken.LOGIN)) { loginExpr = new ForLoginSqlCodeExpr { LoginName = parser.ConsumeObjectId() }; } if (parser.MatchTokenList(SqlToken.WITHOUT, SqlToken.LOGIN)) { loginExpr = new WithoutLoginSqlCodeExpr(); } SqlCodeExpr withExpr = null; if (parser.MatchTokenList(SqlToken.With, SqlToken.DEFAULT_SCHEMA, SqlToken.Equal)) { var schemaName = parser.ConsumeObjectId(); withExpr = new WithDefaultSchemaSqlCodeExpr { SchemaName = schemaName }; } return(new CreateUserSqlCodeExpr { UserName = userName, LoginName = loginExpr, WithExpr = withExpr }); }
public IExpression Parse(TextSpan token, IParser parser) { parser.Scanner.Consume(SqlToken.LParen); PartitionBySqlCodeExpr partitionBy = null; if (parser.Scanner.Match(SqlToken.Partition)) { var partitionColumnList = new List <SqlCodeExpr>(); parser.Scanner.Consume(SqlToken.By); do { partitionColumnList.Add(parser.ConsumeObjectId()); } while (parser.Scanner.Match(SqlToken.Comma)); partitionBy = new PartitionBySqlCodeExpr() { ColumnList = partitionColumnList }; } OrderBySqlCodeExpr orderBy = null; if (parser.MatchTokenList(SqlToken.Order, SqlToken.By)) { var orderColumnList = new List <SortSqlCodeExpr>(); do { //var name = parser.ConsumeObjectId(); var name = parser.ParseExpIgnoreComment(); parser.Scanner.TryConsumeAny(out var sortTokenSpan, SqlToken.Asc, SqlToken.Desc); var sortToken = parser.Scanner.GetSpanString(sortTokenSpan); orderColumnList.Add(new SortSqlCodeExpr { Name = name, SortToken = sortToken }); } while (parser.Scanner.Match(SqlToken.Comma)); orderBy = new OrderBySqlCodeExpr { ColumnList = orderColumnList }; } parser.Scanner.Consume(SqlToken.RParen); return(new OverSqlCodeExpr { PartitionBy = partitionBy, OrderBy = orderBy }); }
private DropRoleSqlCodeExpr DropRole(TextSpan roleSpan, IParser parser) { var ifExists = parser.MatchTokenList(SqlToken.If, SqlToken.Exists); var name = parser.ConsumeObjectId(); return(new DropRoleSqlCodeExpr { IfExists = ifExists, RoleName = name, }); }
private IExpression ParseCreateView(TextSpan viewSpan, IParser parser) { var viewName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.As); var expr = parser.ParseExpIgnoreComment(); return(new CreateViewSqlCodeExpr { Name = viewName, Expr = expr }); }
private static SqlCodeExpr ParseAddFileGroup(IParser parser) { if (!parser.MatchTokenList(SqlToken.ADD, SqlToken.FILEGROUP)) { return(null); } var fileGroupName = parser.ConsumeObjectId(); return(new AddFileGroupSqlCodeExpr { FileGroupName = fileGroupName }); }
private CreateNonclusteredIndexSqlCodeExpr CreateNonClusteredIndex(TextSpan nonClusteredSpan, IParser parser) { parser.ConsumeToken(SqlToken.Index); var indexName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.ON); var tableName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.LParen); var columnList = parser.ParseOrderItemList(); parser.ConsumeToken(SqlToken.RParen); SqlCodeExpr whereExpr = null; if (parser.MatchToken(SqlToken.Where)) { parser.ConsumeToken(SqlToken.LParen); whereExpr = parser.ParseExpIgnoreComment(); parser.ConsumeToken(SqlToken.RParen); } var withExpr = parser.ParseConstraintWithOptions(); var onPrimary = parser.ParseOnPrimary(); var isSemicolon = parser.MatchToken(SqlToken.Semicolon); return(new CreateNonclusteredIndexSqlCodeExpr { IndexName = indexName, TableName = tableName, ColumnList = columnList, WhereExpr = whereExpr, WithExpr = withExpr, OnPrimary = onPrimary, IsSemicolon = isSemicolon, }); }
public IExpression Parse(TextSpan token, IParser parser) { var pivotStr = parser.Scanner.GetSpanString(token); parser.Scanner.Consume(SqlToken.LParen); var aggregated = parser.ParseExpIgnoreComment(); parser.Scanner.Consume(SqlToken.FOR); var column = parser.ConsumeObjectId(); parser.Scanner.Consume(SqlToken.In); parser.Scanner.Consume(SqlToken.LParen); var pivotedColumnsList = new List <SqlCodeExpr>(); do { var pivotedColumn = parser.ParseExpIgnoreComment(); pivotedColumnsList.Add(pivotedColumn as SqlCodeExpr); } while (parser.Scanner.Match(SqlToken.Comma)); parser.Scanner.Consume(SqlToken.RParen); parser.Scanner.Consume(SqlToken.RParen); parser.Scanner.Match(SqlToken.As); var aliasName = parser.ConsumeObjectId(); return(new PivotSqlCodeExpr { Token = pivotStr.ToUpper(), Aggregated = aggregated, Column = column, PivotedColumns = pivotedColumnsList, AliasName = aliasName }); }
public IExpression Parse(TextSpan token, IParser parser) { if (parser.Scanner.Match(SqlToken.ColonColon)) { var id = parser.ConsumeObjectId(); return(new TypeIdSqlCodeExpr { Id = id }); } token.Type = SqlToken.Identifier.ToString(); return(parser.PrefixParse(token)); }
private SqlCodeExpr ParseFor(IParser parser) { if (!parser.MatchToken(SqlToken.FOR)) { return(null); } var objectId = parser.ConsumeObjectId(); return(new ForSqlCodeExpr { ObjectId = objectId }); }
private SqlCodeExpr ParseWithTable(TextSpan withSpan, IParser parser) { var items = new List <WithItemSqlCodeExpr>(); do { var table = parser.ConsumeObjectId(); var columns = new List <SqlCodeExpr>(); if (parser.Scanner.Match(SqlToken.LParen)) { do { var column = parser.ConsumeObjectId(); columns.Add(column); } while (parser.Scanner.Match(SqlToken.Comma)); parser.Scanner.Consume(SqlToken.RParen); } parser.Scanner.Consume(SqlToken.As); parser.Scanner.Consume(SqlToken.LParen); var innerExpr = parser.ParseExpIgnoreComment(); parser.Scanner.Consume(SqlToken.RParen); items.Add(new WithItemSqlCodeExpr { Table = table, Columns = columns, InnerExpr = innerExpr }); } while (parser.Scanner.Match(SqlToken.Comma)); return(new WithTableSqlCodeExpr { Items = items }); }
public IExpression Parse(TextSpan token, IParser parser) { if (parser.Scanner.Match(SqlToken.ColonColon)) { var id = parser.ConsumeObjectId(); return(new ObjectSqlCodeExpr { Id = id }); } var helpMessage = parser.Scanner.GetHelpMessage(); throw new ParseException($"Parse OBJECT:: fail.\r\n{helpMessage}"); }
private IExpression ParseCreateType(TextSpan typeSpan, IParser parser) { var typeName = parser.ConsumeObjectId(); parser.ConsumeToken(SqlToken.As); var tableSpan = parser.ConsumeToken(SqlToken.TABLE); var typeExpr = parser.ConsumeTableDataTypeList(); return(new CreateTypeSqlCodeExpr { Name = typeName, TypeExpr = typeExpr, }); }