csharp Dotnet TSQL脚本解析器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Dotnet TSQL脚本解析器相关的知识,希望对你有一定的参考价值。
namespace parser_console
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = System.ConsoleColor.Green;
Tsql.Parser.Parse(Utils.Data.script);
Console.ReadKey();
}
}
}
namespace Utils
{
using System.IO;
static public class Tools
{
static public Stream StringIO(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
static public class Data
{
static public string script = @"
IF OBJECT_D ( 'SP_TAHMIN', 'P' ) IS NOT NULL
DROP PROCEDURE SP_TAHMIN;
GO
IF OBJECT_ID ( 'V_PMUM', 'V' ) IS NOT NULL
DROP VIEW V_PMUM;
GO
CREATE VIEW [dbo].[V_HOURLY]
AS
SELECT DISTINCT
DATEPART(HOUR, TARIHZAMAN)[H],
TARIHZAMAN
--DATEADD(HOUR, DATEPART(HOUR, TARIHZAMAN), TARIHZAMAN)[TARIHSAAT]
,[ENERJIMERKEZI]
,ISNULL(CAST( TRY_PARSE( [SAATLIKURETIM] AS float USING 'en-US') AS VARCHAR), '0.0') [SAATLIKURETIM]
--,cast(KAPASITEFAKTORU AS varchar)[KAPASITEFAKTORU]
,CAST(TRY_PARSE( KAPASITEFAKTORU AS float) AS varchar)[KAPASITEFAKTORU]
FROM
HOURLYGENERATION
--WHERE ENERJIMERKEZI = REPLACE (
-- (SELECT ENERJIMERKEZI FROM PLANT WHERE ID = 4),
-- CHAR(13) + CHAR(10), '')
GO
CREATE PROC SP_HOURLY
@H INT,
@TARIH DATETIME,
@MERKEZ VARCHAR(MAX)
AS
SELECT * FROM sys.tables
GO
CREATE PROCEDURE SP_HOURLY
@H INT,
@TARIH DATETIME,
@MERKEZ VARCHAR(MAX)
AS
SELECT * FROM sys.tables
";
}
}
namespace Tsql
{
using Microsoft.SqlServer.TransactSql.ScriptDom;
using System.Collections.Generic;
using System.IO;
using System.Xml.Schema;
static public class Parser
{
static public void FormatToken(TSqlParserToken token)
{
//var tokentype = Microsoft.SqlServer.TransactSql.ScriptDom.TSqlTokenType
var template = "{0}:{1} {2} {3} {4}";
var result = string.Format(
template,
token.Line,
token.Column,
token.Text,
token.TokenType,
token.IsKeyword());
System.Console.WriteLine(result);
}
static public void Parse(string script)
{
var parser = new TSql120Parser(true);
IList<ParseError> errors;
var result = parser.Parse(new StringReader(script), out errors);
foreach (var token in result.ScriptTokenStream)
{
FormatToken(token);
}
}
}
}
以上是关于csharp Dotnet TSQL脚本解析器的主要内容,如果未能解决你的问题,请参考以下文章