Source Edit

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

Unstable API.

Imports

strutils, lexbase, decode_helpers, streams

Types

  1. SqlLexer = object of BaseLexer

the parser object. Source Edit

  1. SqlNode = ref SqlNodeObj

an SQL abstract syntax tree node Source Edit

  1. SqlNodeKind = enum
  2. nkNone, nkIdent, nkQuotedIdent, nkStringLit, nkBitStringLit, nkHexStringLit,
  3. nkIntegerLit, nkNumericLit, nkPrimaryKey, nkForeignKey, nkNotNull, nkNull,
  4. nkStmtList, nkDot, nkDotDot, nkPrefix, nkInfix, nkCall, nkPrGroup,
  5. nkColumnReference, nkReferences, nkDefault, nkCheck, nkConstraint, nkUnique,
  6. nkIdentity, nkColumnDef, ## name, datatype, constraints
  7. nkInsert, nkUpdate, nkDelete, nkSelect, nkSelectDistinct, nkSelectColumns,
  8. nkSelectPair, nkAsgn, nkFrom, nkFromItemPair, nkGroup, nkLimit, nkOffset,
  9. nkHaving, nkOrder, nkJoin, nkDesc, nkUnion, nkIntersect, nkExcept,
  10. nkColumnList, nkValueList, nkWhere, nkCreateTable, nkCreateTableIfNotExists,
  11. nkCreateType, nkCreateTypeIfNotExists, nkCreateIndex,
  12. nkCreateIndexIfNotExists, nkEnumDef

kind of SQL abstract syntax tree Source Edit

  1. SqlNodeObj = object
  2. case kind*: SqlNodeKind ## kind of syntax tree
  3. of LiteralNodes:
  4. strVal*: string ## AST leaf: the identifier, numeric literal
  5. ## string literal, etc.
  6. else:
  7. sons*: seq[SqlNode] ## the node's children

an SQL abstract syntax tree node Source Edit

  1. SqlParseError = object of ValueError

Invalid SQL encountered Source Edit

  1. SqlParser = object of SqlLexer

SQL parser object Source Edit

Procs

  1. proc `$`(n: SqlNode): string {....raises: [Exception], tags: [RootEffect],
  2. forbids: [].}

an alias for renderSql. Source Edit

  1. proc `[]`(n: SqlNode; i: BackwardsIndex): SqlNode {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc `[]`(n: SqlNode; i: int): SqlNode {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc add(father, n: SqlNode) {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc len(n: SqlNode): int {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc newNode(k: SqlNodeKind): SqlNode {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc newNode(k: SqlNodeKind; s: string): SqlNode {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

  1. proc newNode(k: SqlNodeKind; sons: seq[SqlNode]): SqlNode {....raises: [],
  2. tags: [], forbids: [].}

Source Edit

  1. proc parseSql(input: Stream; filename: string): SqlNode {....raises: [IOError,
  2. OSError, IOError, OSError, ValueError, SqlParseError, Exception],
  3. tags: [ReadIOEffect, RootEffect, WriteIOEffect], forbids: [].}

parses the SQL from input into an AST and returns the AST. filename is only used for error messages. Syntax errors raise an SqlParseError exception. Source Edit

  1. proc parseSql(input: string; filename = ""): SqlNode {.
  2. ...raises: [IOError, OSError, ValueError, SqlParseError, Exception],
  3. tags: [ReadIOEffect, RootEffect, WriteIOEffect], forbids: [].}

parses the SQL from input into an AST and returns the AST. filename is only used for error messages. Syntax errors raise an SqlParseError exception. Source Edit

  1. proc renderSql(n: SqlNode; upperCase = false): string {....raises: [Exception],
  2. tags: [RootEffect], forbids: [].}

Converts an SQL abstract syntax tree to its string representation. Source Edit

  1. proc treeRepr(s: SqlNode): string {....raises: [], tags: [], forbids: [].}

Source Edit