Module parsesql

The parsesql module implements a high performance SQL file parser. It parses PostgreSQL syntax and the SQL ANSI standard.


hashes, strutils, lexbase, streams


SqlLexer = object of BaseLexer
  filename: string
the parser object.
SqlNodeKind = enum
  nkNone, nkIdent, nkStringLit, nkBitStringLit, nkHexStringLit, nkIntegerLit,
  nkNumericLit, nkPrimaryKey, nkForeignKey, nkNotNull, nkNull, nkStmtList, nkDot,
  nkDotDot, nkPrefix, nkInfix, nkCall, nkColumnReference, nkReferences, nkDefault,
  nkCheck, nkConstraint, nkUnique, nkIdentity, nkColumnDef, ## name, datatype, constraints
  nkInsert, nkUpdate, nkDelete, nkSelect, nkSelectDistinct, nkSelectColumns, nkAsgn,
  nkFrom, nkGroup, nkHaving, nkOrder, nkDesc, nkUnion, nkIntersect, nkExcept,
  nkColumnList, nkValueList, nkWhere, nkCreateTable, nkCreateTableIfNotExists,
  nkCreateType, nkCreateTypeIfNotExists, nkCreateIndex, nkCreateIndexIfNotExists,
kind of SQL abstract syntax tree
SqlParseError = object of ValueError
Invalid SQL encountered
SqlNode = ref SqlNodeObj
an SQL abstract syntax tree node
SqlNodeObj = object
  case kind*: SqlNodeKind       ## kind of syntax tree
  of nkIdent, nkStringLit, nkBitStringLit, nkHexStringLit, nkIntegerLit, nkNumericLit:
      strVal*: string          ## AST leaf: the identifier, numeric literal
                    ## string literal, etc.
      sons*: seq[SqlNode]      ## the node's children
an SQL abstract syntax tree node
SqlParser = object of SqlLexer
  tok: Token
SQL parser object


proc len(n: SqlNode): int {.raises: [], tags: [].}
proc `[]`(n: SqlNode; i: int): SqlNode {.raises: [], tags: [].}
proc add(father, n: SqlNode) {.raises: [], tags: [].}
proc parseSQL(input: Stream; filename: string): SqlNode {.
    raises: [Exception, Exception, ValueError, SqlParseError],
    tags: [ReadIOEffect, RootEffect].}
parses the SQL from input into an AST and returns the AST. filename is only used for error messages. Syntax errors raise an EInvalidSql exception.
proc renderSQL(n: SqlNode): string {.raises: [Exception], tags: [RootEffect].}
Converts an SQL abstract syntax tree to its string representation.
proc `$`(n: SqlNode): string {.raises: [Exception], tags: [RootEffect].}
an alias for renderSQL.

© 2006–2017 Andreas Rumpf
Licensed under the MIT License.