sql-parser

parsing SQL with python to find specific statements

吃可爱长大的小学妹 提交于 2021-02-10 20:31:42
问题 I am using sqlparse to try and find specific statements. code: import sqlparse text = 'SELECT * FROM dbo.table' parse = sqlparse.parse(text) pizza = [t for t in parse[0].tokens if t.ttype in (sqlparse.tokens.Keyword)] I get [<DML 'SELECT' at 0x9f12318>, <Keyword 'FROM' at 0x9f12548>] but what I really want is to return the table name: dbo.table how can I do that? My end goal is to parse through folders of sql scripts and find references and other details. 回答1: If you print each of the tokens

Apache Calcite to Find Selected Columns in an SQL String

拟墨画扇 提交于 2020-03-23 12:11:37
问题 I have a use case where I want to know the columns which have been selected in an SQL string.For instance, if the SQL is like this: SELECT name, age*5 as intelligence FROM bla WHERE bla=bla Then, after parsing the above String, I just want the output to be: name, intelligence . Firstly, is it possible through Calcite? Any other option is also welcome. PS: I want to know this before actually running the query on database. 回答1: This is definitely doable with Calcite. You'll want to start by

Adding a User-Defined Function to Calcite

微笑、不失礼 提交于 2020-01-24 03:15:09
问题 I need to add a user-defined function to Calcite that takes an integer as a parameter and returns an integer. public class SquareFunction { public int eval(int a) { return a*a; } } and the relevant code that creates a schema and adds the function is SchemaPlus rootSchema = Frameworks.createRootSchema(false); rootSchema.add("SQUARE_FUNC", ScalarFunctionImpl.create(SquareFunction.class,"eval"); But a simple SQL like select SQUARE_FUNC(1) from test; fails during the validation with the following

Parse CASE WHEN statements with sqlparse

我怕爱的太早我们不能终老 提交于 2019-12-13 03:20:03
问题 I have the following SQL query and would like to parse it using sqlparse import sqlparse query = """ select SUM(case when(A.dt_unix<=86400 and B.flag="V") then 1 end) as TEST_COLUMN_1, SUM(case when(A.Amt - B.Amt > 0 and B.Cat1 = "A" and (B.Cat2 = "M" or B.Cat3 = "C" or B.Cat4 = "B") and B.Cat5 is NULL) then 1 end) as TEST_COLUMN_2 from test_table A left join test_table_2 as B on A.ID=B.ID where A.DT >B.DT group by A.ID """ query_tokens = sqlparse.parse(query)[0].tokens print(query_tokens)

RegEx to parse stored procedure and object names from DDL in a .sql file C#

十年热恋 提交于 2019-12-12 02:09:07
问题 I have a .sql file which may contain DDL definitions of several stored procedures, alter statements for Tables, Triggers, Views etc It could have statements like these : CREATE PROC / CREATE PROCEDURE ALTER PROC / ALTER PROCEDURE DROP PROC / DROP PROCEDURE CREATE TABLE/TRIGGER/VIEW ALTER TABLE/TRIGGER/VIEW DROP TABLE/TRIGGER/VIEW etc What is the best way to parse the .sql file and just get the list of objects(Proc/Table/View names) and the action that is being taken on them (ALTER/CREATE/DROP

How to extract all table names and aliases from Tsql select statements in .net

六眼飞鱼酱① 提交于 2019-12-11 17:06:06
问题 All of my tenanted tables in sql server have a field customer_id. I'm attempting to write an interceptor for NPoco that will extract all table and alias names from the query. match it to an exception list of tables that are not tenanted, and modify the select to check the tenant key of all tenanted tables in the where clause. I'm having a really hard time finding a sql parser that can extract table names and aliases from a sql statement. A good solution would parse the following statement.

Error on parsing T-SQL using TSql120Parser

杀马特。学长 韩版系。学妹 提交于 2019-12-11 09:13:13
问题 Hello stackoverflowers, I've got a problem parsing T-SQL statements using TSql120Parser from Microsoft.SqlServer.TransactSql.ScriptDom . My goal is to simply parse a T-SQL select statement and get it's tokens. This is the essential part of the problem: using (var reader = new StringReader(query)) { IList<ParseError> errors; var parser = new TSql120Parser(true); var fragment = parser.Parse(reader, out errors); parameters.AddRange( fragment.ScriptTokenStream .Where(token => token.TokenType ==

How to parse / tokenize an SQL statement in Node.js [closed]

≡放荡痞女 提交于 2019-12-03 05:51:00
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . I'm looking for a way to parse / tokenize SQL statement within a Node.js application, in order to: Tokenize all the "basics" SQL keywords defined in the ISO/IEC 9075 standard or here. Validate the SQL syntax. Find out what the query is gonna do (e.g. read or write?). Do you have any

How to parse / tokenize an SQL statement in Node.js [closed]

点点圈 提交于 2019-12-02 20:31:06
I'm looking for a way to parse / tokenize SQL statement within a Node.js application, in order to: Tokenize all the "basics" SQL keywords defined in the ISO/IEC 9075 standard or here . Validate the SQL syntax. Find out what the query is gonna do (e.g. read or write?). Do you have any solution or advises peeps? Linked: Any Javascript/Jquery Library To validate SQL statment? I've done research and I found out some ways to do it: Using existing node.js libraries I did a Google search and I didn't found a consensual and popular library to use. I found those ones: simple-sql-parser (22 stars on

How do I install Microsoft.SqlServer.Management.SqlParser?

匆匆过客 提交于 2019-12-01 18:04:05
My c# installer project came up with a load of warnings when I rebuilt it on a new machine. The warnings were of the ilk that they were looking for version 11.0.0.0 of a dll where only version 10.0.0.0 existed. This I found was due to the version of SQL Server installed (SQL Server 2008 R2). Most of the dll's were specifically due to the version of the Share Management Objects package not being version 11.0.0.0. So I installed SqlSysClrTypes.msi (x86) that is required for Shared Management Objects. I then installed ShareManagementObjects.msi (x86). I got them from here . Some of the errors