C #에서 SQL 코드 구문 분석
C #을 사용하여 SQL 코드를 구문 분석하고 싶습니다.
특히, SQL 코드를 구문 분석하고 트리 또는 다른 구조를 생성 할 수있는 무료 파서가 있습니까? 또한 중첩 된 구조에 대한 적절한 트리를 생성해야합니다.
또한이 트리의 노드가 나타내는 어떤 종류의 문을 반환해야합니다.
예를 들어, 노드에 루프 조건이 포함되어 있으면 이것이 노드의 "루프 유형"임을 반환해야합니다.
아니면 C #에서 코드를 구문 분석하고 원하는 유형의 트리를 생성 할 수있는 방법이 있습니까?
Microsoft EF ( Entity Framework )를 사용합니다.
표현식 트리를 작성하는 "Entity SQL"파서가 있습니다.
using System.Data.EntityClient;
...
EntityConnection conn = new EntityConnection(myContext.Connection.ConnectionString);
conn.Open();
EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = @"Select t.MyValue From MyEntities.MyTable As t";
var queryExpression = cmd.Expression;
....
conn.Close();
또는 이와 비슷한 것을 MSDN에서 확인하십시오.
그리고 그것은 모두 Ballmers tick에 있습니다 :-)
The Code Project, SQL Parser 에도 하나가 있습니다 .
행운을 빕니다.
특히 Transact-SQL (Microsoft SQL Server)의 경우 SQL Server에 포함되어 있으며 자유롭게 배포 할 수있는 어셈블리 인 Microsoft.SqlServer.Management.SqlParser.dll 에서 사용할 수 있는 Microsoft.SqlServer.Management.SqlParser.Parser
네임 스페이스를 사용할 수 있습니다.
다음은 T-SQL을 문자열로 토큰 시퀀스로 구문 분석하는 방법의 예입니다.
IEnumerable<TokenInfo> ParseSql(string sql)
{
ParseOptions parseOptions = new ParseOptions();
Scanner scanner = new Scanner(parseOptions);
int state = 0,
start,
end,
lastTokenEnd = -1,
token;
bool isPairMatch, isExecAutoParamHelp;
List<TokenInfo> tokens = new List<TokenInfo>();
scanner.SetSource(sql, 0);
while ((token = scanner.GetNext(ref state, out start, out end, out isPairMatch, out isExecAutoParamHelp)) != (int)Tokens.EOF)
{
TokenInfo tokenInfo =
new TokenInfo()
{
Start = start,
End = end,
IsPairMatch = isPairMatch,
IsExecAutoParamHelp = isExecAutoParamHelp,
Sql = sql.Substring(start, end - start + 1),
Token = (Tokens)token,
};
tokens.Add(tokenInfo);
lastTokenEnd = end;
}
return tokens;
}
점을 유의 TokenInfo
클래스는 위에서 언급 된 특성을 가진 단순한 클래스입니다.
Tokens
이 열거 형 :
와 같은 상수를 포함하는 TOKEN_BEGIN
, TOKEN_COMMIT
, TOKEN_EXISTS
등
Scott Hanselman은 최근 샘플 SQL 파서를 포함하는 Irony 프로젝트 를 소개 했습니다 .
You may take a look at a commerical component: general sql parser at http://www.sqlparser.com It supports SQL syntax of Oracle, T-SQL, DB2 and MySQL.
Try ANTLR - There are a bunch of SQL grammars on there.
VSTS 2008 Database Edition GDR includes assemblies that handle SQL parsing and script generation that you can reference from your project. Database Edition uses the parser to parse the script files to represent in-memory model of your database and then uses the script generator to generate SQL scripts from the model. I think there are just two assemblies you need to have and reference in your project. If you don't have the database edition, you may install the trial version to get the assemblies or there might be another way to have them without installing the database edition. Check out the following link. Data Dude:Getting to the Crown Jewels .
Try GOLD Parser, it's a powerful and easy to learn BNF engine. You can search the grammars already made for what you want (ie: SQL ANSI 89 Grammar).
I started using this for HQL parsing (the NHibernate query language, very similar to SQL), and it's awesome.
UPDATE: Now the NH dev team has done the HQL parsing using ANTLR (which is harder to use, but more powerful AFAIK).
As Diego suggested, grammars are the way to go IMHO. I've tried Coco/r before, but that is too simple for complex SQL. There's ANTLR with a number of grammars ready.
Someone even tried to build a SQL engine, check the code if there's something for you in SharpHSQL - An SQL engine written in C#.
참고URL : https://stackoverflow.com/questions/589096/parsing-sql-code-in-c-sharp
'Nice programing' 카테고리의 다른 글
배열의 내용을 가로로 인쇄하는 방법은 무엇입니까? (0) | 2020.11.16 |
---|---|
Windows 7에 Windows Phone 8 SDK를 설치하는 방법 (0) | 2020.11.15 |
null을 사전의 키로 사용할 수없는 이유 (0) | 2020.11.15 |
JavaScript 또는 jquery를 사용하여 HTML 페이지를 PDF로 저장할 수 있습니까? (0) | 2020.11.15 |
T-SQL을 사용한 퍼지 매칭 (0) | 2020.11.15 |