Action SQL Parser
For iOS, tvOS and macOS development with Swift and XcodeAvailable exclusively as part of the Action Data framework.
Action SQL Parser
The ADSQLParser
provides the ability to parse text containing one or more SQL commands into an Action Data SQL Document Object Model (DOM) and is used to provide SQL support for data sources that don’t support SQL natively (such as CloudKit and JSON).
The ADSQLParser
uses SQLite’s SQL syntax and currently support a subset of the full SQL language. For example:
let sql = """
CREATE TABLE IF NOT EXISTS parts
(
part_id INTEGER PRIMARY KEY,
stock INTEGER DEFAULT 0 NOT NULL,
description TEXT CHECK( description != '' ) -- empty strings not allowed
);
"""
let instructions = try ADSQLParser.parse(sql)
print(instructions)
ADSQLParser
is composed of the following pieces:
Shared Elements
Contains elements shared across all SQL Parser DOM items such as ADSQLColumnConstraint
and ADSQLOrderByClause
.
Instructions
Contains the Object Models used to hold the individual SQL commands parsed from the original text stream using a ADSQLParser
such as ADSQLCreateTableInstruction
and ADSQLSelectInstruction
.
Expressions
Contains the Object Models used to hold the individual expressions parsed from a SQL command by a ADSQLParser
. Expressions represent items such as the result columns for SQL SELECT commands, the comparisons in a WHERE clause or elements from a ORDER BY clause.
Protocols
Contains the protocols used to pass Instructions and Expressions throughout the SQL Parsersystem.
Enumerations
Contains the Enumerations used throughout the SQL Parser system to define things such as SQL Keywords, Function Names, Column Data Types and Parser Error Codes.
ADSQLParser
Parses a SQL statement into an Action Data SQL Document Object Model (DOM). This parser currently supports a subset of SQL commands as defined by SQLite.
The static Parse
method of the ADSQLParser
is called to convert a string containing one or more valid SQL commands into a ADSQLInstruction
array. Each ADSQLInstruction
instance in the array represents an instruction parsed from the source text. For example:
let sql = "SELECT * FROM parts WHERE part_id = 10"
let instructions = try ADSQLParser.parse(sql)
print(instructions)
If the ADSQLParser
is unable to parse the given source text, it will throw a ADSQLParseError
with the details of the issue encountered.