internal static void Run(SyntaxNodeAnalysisContext context, BinaryExpressionSyntax token) { //Check binaryexpression case string id = token.ToFullString(); if (string.IsNullOrWhiteSpace(id)) { return; } if (id.Contains("+") == false) { return; } //Split string string[] list = id.Split('+'); string sql = BuildSqlStringFromList(list, context, id); try { AzureML(sql); sendData(getJson); while (getResult == "") { } Deserialize(getResult); getResult = ""; } catch { } //Report warning union query injection if (reportWarningUnionquery != "0") { Diagnostics.UnionQueryBinary.Run(context); } //Report warning illegal query injection if (reportWarningIllegalquery != "0") { Diagnostics.IllegalBinary.Run(context); } //Report warning piggybacked query injection if (reportWarningPiggybackedquery != "0") { Diagnostics.PiggybackedBinary.Run(context); } //Check string if (string.IsNullOrWhiteSpace(sql)) { return; } //Check Error List <string> errors = SqlParser.Parse(sql); if (errors.Count == 0) { return; } //Report Error string errorText = String.Join("\r\n", errors); var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation(), errorText); context.ReportDiagnostic(diagnostic); }
public void ShouldParseJoinClause(string sql, string expectedSql) { var result = SqlParser.TryParse(sql, _defaultDialect, _defaultTablePrefix, null, out var rawQuery, out var messages); Assert.True(result); Assert.Equal(expectedSql, FormatSql(rawQuery)); }
public void ShouldParseComments(string sql, string expectedSql, object[] expectedParameters = null) { var result = SqlParser.TryParse(sql, _defaultDialect, _defaultTablePrefix, out var rawQuery, out var rawParameters, out var messages); Assert.True(result); Assert.Equal(expectedSql, FormatSql(rawQuery)); }
protected SqlCommaSeparatedTokenList(SqlParserState state, out AmpElement error) : base(Enumerable.Empty <TElement>()) { List <AmpElement <SqlKind> > items = new List <AmpElement <SqlKind> >(); while (true) { if (SqlParser.TryParse <TElement>(state, out var v)) { items.Add(v); } else { error = state.Error; return; } if (state.IsKind(SqlKind.CommaToken)) { items.Add(state.CurrentToken); state.Read(); continue; } else { break; } } SetItems(items); error = null; }
protected SqlFromClause(SqlParserState state, out AmpElement error) { Kind = SqlKind.FromClause; if (state.IsKind(SqlKind.FromToken)) { FromToken = state.CurrentToken; state.Read(); } else { error = SqlParseError.Construct(state); return; } if (SqlParser.TryParse <SqlCommaSeparatedTokenList <SqlSelectSource> >(state, out var sources)) { Sources = sources; } else { error = state.Error; return; } error = null; }
internal static void Run(SyntaxNodeAnalysisContext context, InterpolatedStringExpressionSyntax token) { //Check string string id = token.ToFullString(); if (string.IsNullOrWhiteSpace(id)) { return; } //Send to class Helper string sql = Helper.BuildSqlStringFromIdString(context, id); if (string.IsNullOrWhiteSpace(sql)) { return; } //Send to Class Parser List <string> errors = SqlParser.Parse(sql); if (errors.Count == 0) { return; } //Report Error string errorText = String.Join("\r\n", errors); var diagnostic = Diagnostic.Create(Rule, context.Node.GetLocation(), errorText); context.ReportDiagnostic(diagnostic); }
private TableSourceExpression ExpressionTestHelper(string query) { var p = new SqlParser(); var ss = (SelectStatement)p.Execute(new SelectStatement(), query); return(ss.FindDescendantRecursive <TableSourceExpression>()); }
public void BiggerCaseAndGroupBy() { string sql = @"SELECT SUM( (CASE WHEN [t9].[IsUnviewed] = 1 THEN 14 ELSE 15 END)) AS [UnreadCount], COUNT(*) AS [TotalCount] FROM ( SELECT 1 AS [value], [t0].[MailboxFolderID], [t1].[PItemStatusEnumID], [t1].[CurrentOpTypeEnumID], [t1].[CurrentOpExtractStatusEnumID], [t1].[ID], [t0].[SystemFolderEnumID], [t1].[PurgeScansStatusEnumID], [t1].[IsScanOnly], [t0].[MailboxID], [t0].[IsUnviewed] FROM [dbo].[VItems] AS [t0] LEFT OUTER JOIN ([dbo].[PItems] AS [t1] INNER JOIN ([dbo].[PItemLocations] AS [t2] INNER JOIN ([dbo].[Locations] AS [t3] INNER JOIN [dbo].[Facilities] AS [t4] ON [t4].[ID] = [t3].[FacilityID]) ON [t3].[LocationID] = [t2].[LocationID]) ON [t2].[ID] = [t1].[PItemLocationID] LEFT OUTER JOIN [dbo].[Scans] AS [t5] ON [t5].[ID] = [t1].[PendingScanID]) ON [t1].[VItemID] = [t0].[ID] LEFT OUTER JOIN ([dbo].[VItemMailAttributes] AS [t6] LEFT OUTER JOIN [dbo].[VItemAddresses] AS [t7] ON [t7].[ID] = [t6].[SenderAddressID]) ON [t6].[VItemID] = [t0].[ID] INNER JOIN [dbo].[Mailboxes] AS [t8] ON [t8].[ID] = [t0].[MailboxID] ) AS [t9] WHERE ([t9].[MailboxFolderID] IS NULL) AND ((([t9].[PItemStatusEnumID] = 1) AND (([t9].[CurrentOpTypeEnumID] IS NULL) OR (NOT (([t9].[CurrentOpTypeEnumID]) IN (24, 34, 64, 65, 32))) OR ([t9].[CurrentOpExtractStatusEnumID] = 5))) OR ((EXISTS( SELECT NULL AS [EMPTY] FROM [dbo].[Scans] AS [t10] WHERE ([t10].[ScanStatusEnumID] = 4) AND ([t10].[PItemID] = [t9].[ID]) )) AND ([t9].[SystemFolderEnumID] = 0) AND ([t9].[PurgeScansStatusEnumID] = 10) AND ([t9].[IsScanOnly] = 1))) AND ([t9].[MailboxID] IN (11, 12, 13)) GROUP BY [t9].[value]"; SqlParser sp = new SqlParser(); var parsedSql = sp.Parse(sql); Assert.Equal(2, parsedSql.Select.Fields.Count); Assert.Equal(1, parsedSql.Select.Tables.Count); Assert.Equal(0, parsedSql.Select.Joins.Count); Assert.Equal(3, parsedSql.Select.Wheres.Count); }
/// <summary> /// Ejecuta un script SQL interpretándolo antes /// </summary> private void ExecuteScriptSqlParsed(ProviderModel provider, string fileName, SentenceExecuteScript sentence) { List <SqlSectionModel> sections = new SqlParser().TokenizeByFile(fileName, MapVariables(GetVariables(), sentence.Mapping), out string error); // Si no hay ningún error, ejecuta el script if (string.IsNullOrWhiteSpace(error)) { // Recorre las seccionaes foreach (SqlSectionModel section in sections) { if (string.IsNullOrWhiteSpace(error) && section.Type == SqlSectionModel.SectionType.Sql && !string.IsNullOrWhiteSpace(section.Content)) { try { provider.Execute(CreateDataProviderCommand(section.Content, sentence.Timeout)); } catch (Exception exception) { error = $"Error when execute script {System.IO.Path.GetFileName(fileName)}. {exception.Message}"; } } } } // Añade el error if (!string.IsNullOrWhiteSpace(error)) { AddError(error); } }
public void SqlParsingTest(string inputSql, string expectedOperation, string expectedTable) { var parsed = SqlParser.GetParsedDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, inputSql); Assert.AreEqual(expectedOperation.ToLower(), parsed.Operation, string.Format("Expected operation {0} but was {1}", expectedOperation, parsed.Operation)); Assert.AreEqual(expectedTable?.ToLower(), parsed.Model, string.Format("Expected table {0} but was {1}", expectedTable, parsed.Model)); }
/// <summary> /// ExecutePaginated sql file most simple /// </summary> static void ExecuteSimple(IExecutor executor) { // create parameter object instance for 2-way-sql var condition = new SqlCondition { MiddleNames = new List <string> { "A", "J", "M" } }; var watch = new Stopwatch(); watch.Start(); // create SqlParse instance var parser = new SqlParser(FilePath, condition); // parse sql file var result = parser.Parse(); watch.Stop(); Console.WriteLine($"time\t{watch.Elapsed}"); watch.Reset(); watch.Start(); Debug.WriteLine($"+++>{result.DebugSql}"); executor.Execute(ConnectionString, result); watch.Stop(); Console.WriteLine($"time\t{watch.Elapsed}"); }
public void FindParameters_NoParameter() { SqlParser sqlParser = new SqlParser(); var paramters = sqlParser.FindParameters("SELECT * FROM Users"); Assert.AreEqual(0, paramters.Count); }
private static void ParseUsingMangedParser(SqlQuerySpec sqlQuerySpec) { if (!SqlParser.TryParse(sqlQuerySpec.QueryText, out SqlQuery sqlQuery)) { throw new InvalidOperationException("FAILED TO PARSE QUERY."); } }
public static TObject Excute <TObject>(IContext context, Func <MySqlCommand, TObject> doExcute, string querySql = "") { var queryConfig = context.GetConfig(); using (var connection = new MySqlConnection(SqlEnginerConfig.GetConnection(queryConfig.Connection))) { connection.Open(); var realSql = SqlParser.Convert(context, string.IsNullOrEmpty(querySql) ? queryConfig.Sql : querySql); using (var cmd = connection.CreateCommand()) { cmd.CommandText = realSql.Item1; if (realSql.Item2 == null || !realSql.Item2.Any()) { return(doExcute(cmd)); } foreach (var ps in realSql.Item2) { cmd.Parameters.AddWithValue(ps.Key, ps.Value); } return(doExcute(cmd)); } } }
public void Example_DatabaseStatementParser() { DatabaseStatementParser dbParser = null; var cachedThroughput = ThroughputExerciser.Create() .UsingThreads(10) .ForDuration(1000) // 1 second .DoThisToSetup(() => { dbParser = new DatabaseStatementParser(Mock.Create <ICacheStatsReporter>()); }) .DoThisUnitOfWork((threadId, uowIdLocal, uowIdGlobal) => { var stmt = dbParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, $"SELECT * FROM dbo.User WHERE UserID = {uowIdLocal}"); }) .ExecAll(); var notCachedThroughput = ThroughputExerciser.Create() .UsingThreads(10) .ForDuration(1000) // 1 second .DoThisToSetup(() => { dbParser = new DatabaseStatementParser(Mock.Create <ICacheStatsReporter>()); }) .DoThisUnitOfWork((threadId, uowIdLocal, uowIdGlobal) => { var stmt = SqlParser.GetParsedDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, $"SELECT * FROM dbo.User WHERE UserID = {uowIdLocal}"); }) .ExecAll(); //Caching makes things better Assert.Greater(cachedThroughput.CountUnitsOfWorkPerformed, notCachedThroughput.CountUnitsOfWorkPerformed); //Chaching makes things 50% better //Assert.Greater(cachedThroughput.CountUnitsOfWorkPerformed, notCachedThroughput.CountUnitsOfWorkPerformed * 1.5); }
protected SqlWhereClause(SqlParserState state, out AmpElement error) { Kind = SqlKind.WhereClause; if (state.IsKind(SqlKind.WhereToken)) { WhereToken = state.CurrentToken; state.Read(); } else { error = SqlParseError.Construct(state); return; } if (SqlParser.TryParse <SqlExpression>(state, out var expr)) { Expression = expr; } else { error = state.Error; return; } error = null; }
protected SqlSelectColumn(SqlParserState state, out AmpElement error) { Kind = SqlKind.SelectColumnSyntax; if (SqlParser.TryParse <SqlExpression>(state, out var sqlExpression)) { Expression = sqlExpression; } else { error = state.Error; return; } if (state.IsKind(SqlKind.AsToken)) { AsToken = state.CurrentToken; state.Read(); } if (state.IsKind(SqlKind.IdentifierToken) || state.IsKind(SqlKind.QuotedIdentifierToken)) { NameToken = state.CurrentToken; state.Read(); } else if (AsToken != null) { error = SqlParseError.Construct(state); return; } error = null; }
private List <FileRow> ParseDataContent(bool skipFirstLine) { switch (SourceType) { case SourceType.Excel: case SourceType.Text: if (!File.Exists(txtFilePath.Text)) { throw new Exception("Specified file does not exist"); } break; } switch (SourceType) { case SourceType.Excel: return(ExcelParser.GetValues(txtFilePath.Text, cboSheetName.Text, skipFirstLine)); case SourceType.Text: return(TextParser.GetValues(txtFilePath.Text, GetDelimiter(), skipFirstLine)); case SourceType.Sql: return(SqlParser.GetValues(cboConnectionSource.SelectedValue.ToString(), cboTableNameSource.SelectedValue.ToString(), txtFilter.Text)); default: throw new Exception($"SourceType={SourceType} is not handled"); } }
/// <summary> /// SPHRSBILLING.REPORT_TYPE /// </summary> public ReportTypePackage(ReportTypeTable table) { Table = table; ReportedFields = new ObservableCollection <ReportPair>(); AvailableFields = new ObservableCollection <string>(); Parser = new SqlParser(); }
public void TestAllSqlFiles() { var sqlFolder = @"D:\VDisk\MyGitHub\SQL"; var p = new SqlParser(); var fileCount = 0; foreach (var sqlFile in ReadSqlFiles(sqlFolder)) { var sql = File.ReadAllText(sqlFile); try { _outputHelper.WriteLine($"parsing='{sqlFile}'"); p.ParseAll(sql).ToList(); } catch { _outputHelper.WriteLine($"parsedCount={fileCount}"); _outputHelper.WriteLine($"'{sqlFile}'"); throw; } fileCount++; } _outputHelper.WriteLine($"Total parsed Count={fileCount}"); _outputHelper.WriteLine($"=== END ==="); }
public void ShouldDefineDefaultParametersValue() { var parameters = new Dictionary<string, object>(); var result = SqlParser.TryParse("select a where a = @b:10", _defaultDialect, _defaultTablePrefix, parameters, out var rawQuery, out var messages); Assert.True(result); Assert.Equal(10, parameters["b"]); }
/// <summary> /// Obtiene el datareader de una consulta /// </summary> internal async Task <DbDataReader> ExecuteReaderAsync(IDbProvider provider, string query, Models.ArgumentListModel arguments, TimeSpan timeout, CancellationToken cancellationToken) { List <SqlSectionModel> scripts = new SqlParser().Tokenize(query, arguments.Constants.ToDictionary(), out string error); if (!string.IsNullOrWhiteSpace(error)) { throw new Exception(error); } else { ParametersDbCollection parametersDb = ConvertParameters(provider, arguments.Parameters); // Obtiene el datareader foreach (SqlSectionModel script in scripts) { if (script.Type == SqlSectionModel.SectionType.Sql) { string sql = provider.SqlHelper.ConvertSqlNoParameters(script.Content, parametersDb).TrimIgnoreNull(); if (!string.IsNullOrWhiteSpace(sql)) { return(await provider.ExecuteReaderAsync(sql, null, CommandType.Text, timeout, cancellationToken)); } } } // Si ha llegado hasta aquí es porque no ha leído nada return(null); } }
public void CreateStatementsTests(SqlTestData.ExpectedTable expectedTable) { Assert.True(SqlParser.TryParse(expectedTable.Sql, out SqlTableDefinition definition)); Assert.NotNull(definition); SqlTestData.Compare(definition, expectedTable); }
public void add_mul() { var sql = "1 + 2 * 3"; var expr = new SqlParser().ParseArithmeticPartial(sql); new OperandExpr { Left = new IntegerExpr { Value = 1 }, Oper = "+", Right = new OperandExpr { Left = new IntegerExpr { Value = 2 }, Oper = "*", Right = new IntegerExpr { Value = 3 } } }.ToExpectedObject().ShouldEqual(expr); }
public ReportTypePackage(ReportTypePackage copyMe) { if (copyMe == null) { return; } Parser = new SqlParser(); ReportedFields = new ObservableCollection <ReportPair>(); AvailableFields = new ObservableCollection <string>(); foreach (ReportPair pair in copyMe.ReportedFields) { ReportedFields.Add(new ReportPair(pair)); } foreach (string avail in copyMe.AvailableFields) { AvailableFields.Add(avail.ToString()); } Id = copyMe.Id; MustEncrypt = copyMe.MustEncrypt; AllowDynamicMod = copyMe.AllowDynamicMod; DataDumpOnly = copyMe.DataDumpOnly; Description = copyMe.Description.ToString(); ShortName = copyMe.ShortName.ToString(); Sql = copyMe.Sql.ToString(); ReConstructPackageListFromSql(null); //ReConstructSQLFromGridView(); Table = copyMe.Table; //OrderByClause = copyMe.OrderByClause; originalSql = copyMe.originalSql; }
public void add_mul_add() { var sql = "1 + 2 * 3 + 4"; var expr = new SqlParser().ParseArithmeticPartial(sql); "1 + 2 * 3 + 4".ToExpectedObject().ShouldEqual(expr.ToString()); }
internal static void Run(SyntaxNodeAnalysisContext context, BinaryExpressionSyntax token) { string id = token.ToFullString(); if (string.IsNullOrWhiteSpace(id)) { return; } if (id.Contains("+") == false) { return; } string[] list = id.Split('+'); string sql = BuildSqlStringFromList(list, context, id); if (string.IsNullOrWhiteSpace(sql)) { return; } List <string> errors = SqlParser.Parse(sql); if (errors.Count == 0) { return; } string errorText = String.Join("\r\n", errors); var diagnostic = Diagnostic.Create(RuleParam1, context.Node.GetLocation(), errorText); context.ReportDiagnostic(diagnostic); }
private void GetSearchCondition(string query, out SelectStatement select, out SearchCondition where) { var p = new SqlParser(); select = (SelectStatement)p.Execute(new SelectStatement(), query); where = select.FindDescendantRecursive <WhereClause>().FindDescendant <SearchCondition>(); }
internal static void Run(SyntaxNodeAnalysisContext context, LiteralExpressionSyntax literalExpression) { if (literalExpression == null) { return; } if (literalExpression.IsKind(SyntaxKind.StringLiteralExpression) && literalExpression.Token.IsKind(SyntaxKind.StringLiteralToken)) { var sql = literalExpression.Token.ValueText; if (string.IsNullOrWhiteSpace(sql)) { return; } List <string> errors = SqlParser.Parse(sql); if (errors.Count == 0) { return; } string errorText = String.Join("\r\n", errors); var diagnostic = Diagnostic.Create(Rule, literalExpression.GetLocation(), errorText); context.ReportDiagnostic(diagnostic); } }
public void BigHonkinSubquery() { string sql = @"SELECT SUM((CASE WHEN a.type = 1 THEN 'a' ELSE 'b' END)) AS [UnreadCount], COUNT(*) AS [TotalCount] FROM ( SELECT 1 AS [value], [t0].[field1], [t1].[field2], [t1].[field3] FROM [dbo].[table1] AS [t0] LEFT OUTER JOIN ([dbo].[table2] AS [t1] INNER JOIN ([dbo].[table3] AS [t2] INNER JOIN ([dbo].[table4] AS [t3] INNER JOIN [dbo].[table5] AS [t4] ON [t4].[ID] = [t3].[OtherID]) ON [t3].[ID] = [t2].[OtherID]) ON [t2].[ID] = [t1].[OtherID] )) AS [t9] WHERE ([t9].[OtherID] IS NULL) GROUP BY [t9].[value]"; SqlParser sp = new SqlParser(); var parsedSql = sp.Parse(sql); Assert.Equal(2, parsedSql.Select.Fields.Count); Assert.Equal("[unreadcount]", parsedSql.Select.Fields[0].Alias); Assert.Equal("[totalcount]", parsedSql.Select.Fields[1].Alias); Assert.Equal(1, parsedSql.Select.Tables.Count); Assert.Equal(0, parsedSql.Select.Joins.Count); Assert.Equal(1, parsedSql.Select.Wheres.Count); Assert.Equal(1, parsedSql.Select.GroupBys.Count); }
private TableHint[] TableHintTestHelper(string query) { var p = new SqlParser(); var ss = (SelectStatement)p.Execute(new SelectStatement(), query); var hl = ss.FindDescendantRecursive<TableHintList>(); return hl.EnumerateDescendantsRecursive<TableHint>().ToArray(); }
private void GeneratePartitions(int partitionCount, SqlParser.TableStatistics stat) { SqlQueryPartition qp = null; int s = stat.KeyValue.Count / partitionCount; for (int i = 0; i < partitionCount; i++) { qp = new SqlQueryPartition(this, this.Context); qp.ID = Partitions.Count; qp.PartitioningKeyTo = stat.KeyValue[(i + 1) * s]; if (i == 0) { qp.PartitioningKeyFrom = double.NegativeInfinity; } else { qp.PartitioningKeyFrom = Partitions[i - 1].PartitioningKeyTo; } AppendPartition(qp); } Partitions[Partitions.Count - 1].PartitioningKeyTo = double.PositiveInfinity; }
private SelectStatement CreateSelect(string query) { SqlParser p = new SqlParser(); var select = (SelectStatement)p.Execute(new SelectStatement(), query); SqlNameResolver nr = new SqlNameResolver(); nr.DefaultTableDatasetName = Jhu.Graywulf.Test.Constants.TestDatasetName; nr.DefaultFunctionDatasetName = Jhu.Graywulf.Test.Constants.CodeDatasetName; nr.SchemaManager = CreateSchemaManager(); nr.Execute(select); return select; }
private TableSourceExpression ExpressionTestHelper(string query) { var p = new SqlParser(); var ss = (SelectStatement)p.Execute(new SelectStatement(), query); return ss.FindDescendantRecursive<TableSourceExpression>(); }
private SelectStatement CreateSelect(string query) { var p = new SqlParser(); return (SelectStatement)p.Execute(new SelectStatement(), query); }
private SearchCondition GetSearchCondition(string sql) { SqlParser p = new SqlParser(); var select = (SelectStatement)p.Execute(new SelectStatement(), sql); var where = select.FindDescendantRecursive<WhereClause>(); return where.FindDescendant<SearchCondition>(); }
private Jhu.Graywulf.SqlParser.SelectStatement ExpressionTestHelper(string query) { var p = new SqlParser(); return (Jhu.Graywulf.SqlParser.SelectStatement)p.Execute(new Jhu.Graywulf.SqlParser.SelectStatement(), query); }
private QuerySpecification Parse(string query) { var p = new SqlParser(); var ss = (SelectStatement)p.Execute(query); var qs = (QuerySpecification)ss.EnumerateQuerySpecifications().First(); ResolveNames(qs); return qs; }
private void GetSearchCondition(string query, out SelectStatement select, out SearchCondition where) { var p = new SqlParser(); select = (SelectStatement)p.Execute(new SelectStatement(), query); where = select.FindDescendantRecursive<WhereClause>().FindDescendant<SearchCondition>(); }
private Jhu.Graywulf.SqlParser.Expression ExpressionTestHelper(string query) { var p = new SqlParser(); return (Jhu.Graywulf.SqlParser.Expression)p.Execute(new Jhu.Graywulf.SqlParser.Expression(), query); }
private Jhu.Graywulf.SqlParser.TableOrViewName TablenameTestHelper(string query) { var p = new SqlParser(); return (Jhu.Graywulf.SqlParser.TableOrViewName)p.Execute(new Jhu.Graywulf.SqlParser.TableOrViewName(), query); }