Git Product home page Git Product logo

batch-parser's Introduction

SQL Batch Parser

This is a work-in-progress and not for production use.

Intent

This project tries to solve the problem of finding (batches of) SQL statements to solve problems like GO in a command in an embedded SQL script in the FluentMigrator project.

Structure

The main components are:

  • IRangeSearcher
  • ISpecialTokenSearcher

IRangeSearcher

Search for a range of things - like comments, quoted identifiers and SQL strings.

ISpecialTokenSearcher

Search for special tokens like GO, or ;. This allows splitting the SQL script into SQL statements and batches.

Example

// This is the variable with your SQL script
var sqlScript = "...";

// This is the found SQL text
string sqlText = null;

// The parser does all the work for us
var parser = new SqlServerBatchParser();

// Handle the GO token
parser.SpecialToken += (sender, evt) => {
    // Handle the special token (e.g. GO)
    if (string.IsNullOrEmpty(sqlText)) {
        // A GO was found, but no SQL statements
        return;
    }

    // Execute the sqlText
    // TODO...

    // Reset the variable to avoid
    // executing the same SQL code
    // twice when a second GO follows
    // without SQL text in between.
    sqlText = null;
};

// Store the found SQL text
parser.SqlText += (sender, evt) => {
    sqlText = evt.SqlText.Trim();
};

// Define the source to be used by the parser
using (var source = new TextReaderSource(new StringReader(sqlScript), takeOwnership: true))
{
    // This is where the hard stuff happens
    parser.Process(source, stripComments: true);
}

batch-parser's People

Contributors

fubar-coder avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.