NorKen Technologies, Inc.


Home Page of NorKen Technologies, Inc. Information About NorKen Technologies, Inc. Information About NorKen Technologies' Products Information About NorKen Technologies' Services Information About Ordering NorKen Technologies' Products How to Contact NorKen Technologies


FAQs



Free Trial

Order



Downloads

Grammars








































































ProGrammar Developers Toolkit

Parsing SQL with ProGrammar

The ProGrammar ActiveX control makes it easy to parse SQL statements from Visual Basic, Visual C++, Delphi, and other Windows-based applications.

The following example shows how to parse SQL from a Visual Basic program. While this example is intentionally very simple, it demonstrates the main steps involved in using ProGrammar in your applications.

The first step is to initialize the ProGrammar ActiveX control:

  ' Create an interface to the parser
Dim parser As Pgmr
Set parser = new Pgmr

The next step is loading the SQL grammar. The grammar is stored as a binary file named "SQL.GMR", which was generated by the ProGrammar IDE when the
SQL grammar definition was compiled.

  ' Load the SQL grammar
parser.SetGrammar ("SQL.GMR")

The following SQL grammars are currently available:

SQL A simple subset of the SQL language
SQL92 The full SQL92 grammar, based on ISO standard SQL2
TSQL A Beta version of the TSQL grammar

Next, the program sets the SQL text that will be parsed. The parser can load the text from a file or parse it directly from a memory buffer. In this example, the SQL text is contained in a file named "myfile.sql".

  ' Load the SQL file that will be parsed
parser.SetInputFilename ("myfile.sql")

The program then parses the file and checks for parse errors.

  ' Parse the SQL file
parser.Parse

' Show parse errors (if any)
numErrors = parser.GetNumErrors
For errorIndex = 0 To numErrors
   errorCode = parser.GetErrorCode(errorIndex)
   desc = parser.GetErrorDescription(errorIndex)
   ' TODO: display error to the user
Next errorIndex

When the Parse method is called, ProGrammar parses the input text using the SQL grammar and builds a parse tree data structure in memory. The program then calls ActiveX methods to retrieve relevant data items from the parse tree.

In this example, the program creates a list of all tables that are referenced by name in the SQL statements. In the SQL grammar definition, a table name is represented by a symbol named "table". Therefore, each reference to a table in the input text will have a corresponding node in the parse tree labeled "table", whose value is the name of the table.

  If parser.Status = pgStatusComplete Then

   ' Search the tree for all 'table' nodes
   searchID = parser.StartSearch ("/.*table", 0)

   Do
      ' Find the next node matching the search pattern
      tableNodeID = parser.FindNext (searchID)

      If tableNodeID <> 0 Then
         ' get the value of this node
         tableName = parser.GetValue (tableNodeID)

         ' TODO: Now, do something with the table name

      End If
   Loop Until tableNodeID = 0

   parser.EndSearch (searchID)
End If


Click here to return to the ProGrammar Homepage


Press Releases

What is a parser?

How do I build a parser using ProGrammar?

What grammars are available?

Which platforms are supported?

Feature Index

Overview of ProGrammar
ProGrammar IDE
Grammar Definition Language
Integration and Architecture
Example Grammars
Ready-to-use Parsers
Pricing and Licensing Terms






















For comments or questions about this site, please contact
webmaster@programmar.com
Copyright © 1998-2008 NorKen Technologies, Inc. All rights reserved.