Module: Sqlglot

Defined in:
lib/sqlglot.rb,
lib/sqlglot/error.rb,
lib/sqlglot/query.rb,
lib/sqlglot/native.rb,
lib/sqlglot/dialect.rb,
lib/sqlglot/railtie.rb,
lib/sqlglot/version.rb,
lib/sqlglot/ast_walker.rb

Overview

Sqlglot wraps the sql-glot-rust library, providing SQL parsing, transpilation across 30+ dialects, and query metadata extraction.

Examples:

Transpile SQL between dialects

Sqlglot.transpile("SELECT NOW()", from: :postgres, to: :bigquery)
# => "SELECT CURRENT_TIMESTAMP()"

Parse SQL into an AST Hash

ast = Sqlglot.parse("SELECT a FROM t", dialect: :mysql)
# => {"Select" => {"columns" => [...], ...}}

Extract query metadata

q = Sqlglot::Query.new("SELECT a FROM users AS u JOIN orders AS o ON u.id = o.user_id")
q.tables       # => ["users", "orders"]
q.columns_dict # => {select: ["a"], join: ["users.id", "orders.user_id"]}

Defined Under Namespace

Modules: AstWalker, Dialect, Native Classes: Configuration, Error, GenerateError, LibraryNotFoundError, ParseError, Query, Railtie, TranspileError

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Returns:



43
44
45
# File 'lib/sqlglot.rb', line 43

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields the configuration object for modification.

Examples:

Sqlglot.configure do |config|
  config.default_dialect = :postgres
end

Yields:



53
54
55
# File 'lib/sqlglot.rb', line 53

def configure
  yield(configuration)
end

.generate(ast, dialect: nil) ⇒ String

Generate SQL from an AST Hash for a given dialect.

Parameters:

  • ast (Hash)

    an AST previously returned by parse

  • dialect (Symbol, String, nil) (defaults to: nil)

    target dialect

Returns:

  • (String)

    the generated SQL

Raises:



90
91
92
93
94
# File 'lib/sqlglot.rb', line 90

def generate(ast, dialect: nil)
  dialect_str = resolve_dialect(dialect)
  ast_json = JSON.generate(ast)
  Native.generate(ast_json, dialect_str)
end

.parse(sql, dialect: nil) ⇒ Hash

Parse a SQL string into a Ruby Hash representing the AST.

Parameters:

  • sql (String)

    the SQL to parse

  • dialect (Symbol, String, nil) (defaults to: nil)

    source dialect (default: configured or ANSI)

Returns:

  • (Hash)

    the deserialized AST

Raises:



65
66
67
68
69
# File 'lib/sqlglot.rb', line 65

def parse(sql, dialect: nil)
  dialect_str = resolve_dialect(dialect)
  json = Native.parse(sql, dialect_str)
  JSON.parse(json)
end

.transpile(sql, from: nil, to: nil) ⇒ String

Transpile SQL from one dialect to another.

Parameters:

  • sql (String)

    the SQL to transpile

  • from (Symbol, String, nil) (defaults to: nil)

    source dialect

  • to (Symbol, String, nil) (defaults to: nil)

    target dialect

Returns:

  • (String)

    the transpiled SQL

Raises:



78
79
80
81
82
# File 'lib/sqlglot.rb', line 78

def transpile(sql, from: nil, to: nil)
  from_str = resolve_dialect(from)
  to_str   = resolve_dialect(to)
  Native.transpile(sql, from_str, to_str)
end

.versionString

Return the version of the underlying sql-glot-rust library.

Returns:

  • (String)

    e.g. “0.10.0”



99
100
101
# File 'lib/sqlglot.rb', line 99

def version
  Native.version
end