Module: Sqlglot::Native Private

Extended by:
FFI::Library
Defined in:
lib/sqlglot/native.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Low-level FFI bindings to libsqlglot_rust.

This module is not intended for direct use – see the public API on Sqlglot instead. All returned C strings are freed via sqlglot_free in ensure blocks so callers never need to manage memory.

Constant Summary collapse

LIB_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Locate the shared library. Search order:

1. SQLGLOT_LIB_PATH environment variable (explicit override)
2. lib/sqlglot/ inside the gem (where extconf.rb copies the build)
3. ext/sqlglot_rust/sql-glot-rust/target/release/ (dev builds)
4. System library path (LD_LIBRARY_PATH / DYLD_LIBRARY_PATH)
"sqlglot_rust"
SOEXT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

“so” on Linux, “dylib” on macOS

FFI::Platform::LIBSUFFIX

Class Method Summary collapse

Class Method Details

.find_libraryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sqlglot/native.rb', line 26

def self.find_library
  # 1. Explicit override
  if (env_path = ENV["SQLGLOT_LIB_PATH"])
    return env_path if File.exist?(env_path)
  end

  gem_root = File.expand_path("../..", __dir__)

  candidates = [
    # 2. Installed location (lib/sqlglot/)
    File.join(gem_root, "lib", "sqlglot", "lib#{LIB_NAME}.#{SOEXT}"),
    # 3. Dev build location
    File.join(gem_root, "ext", "sqlglot_rust", "sql-glot-rust",
              "target", "release", "lib#{LIB_NAME}.#{SOEXT}"),
  ]

  candidates.each { |path| return path if File.exist?(path) }

  # 4. System path fallback (FFI will search LD_LIBRARY_PATH etc.)
  LIB_NAME
end

.generate(ast_json, dialect) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate SQL from a JSON AST string.

Parameters:

  • ast_json (String)
  • dialect (String, nil)

Returns:

  • (String)

    generated SQL



124
125
126
127
128
129
# File 'lib/sqlglot/native.rb', line 124

def self.generate(ast_json, dialect)
  ptr = sqlglot_generate(ast_json, dialect)
  read_and_free(ptr,
                error_class: GenerateError,
                message: "Failed to generate SQL from AST")
end

.parse(sql, dialect) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse SQL and return the JSON AST string.

Parameters:

  • sql (String)
  • dialect (String, nil)

Returns:

  • (String)

    JSON string



99
100
101
102
103
104
# File 'lib/sqlglot/native.rb', line 99

def self.parse(sql, dialect)
  ptr = sqlglot_parse(sql, dialect)
  read_and_free(ptr,
                error_class: ParseError,
                message: "Failed to parse SQL: #{sql.inspect}")
end

.read_and_free(ptr, error_class:, message:) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read a C string returned by the FFI, free it, and return a Ruby String. Raises the given error class if the pointer is NULL.

Parameters:

  • ptr (FFI::Pointer)
  • error_class (Class<Sqlglot::Error>)
  • message (String)

Returns:

  • (String)


84
85
86
87
88
89
90
91
92
# File 'lib/sqlglot/native.rb', line 84

def self.read_and_free(ptr, error_class:, message:)
  if ptr.null?
    raise error_class, message
  end

  ptr.read_string.force_encoding("UTF-8")
ensure
  sqlglot_free(ptr) if ptr && !ptr.null?
end

.transpile(sql, from_dialect, to_dialect) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transpile SQL from one dialect to another.

Parameters:

  • sql (String)
  • from_dialect (String, nil)
  • to_dialect (String, nil)

Returns:

  • (String)

    transpiled SQL



112
113
114
115
116
117
# File 'lib/sqlglot/native.rb', line 112

def self.transpile(sql, from_dialect, to_dialect)
  ptr = sqlglot_transpile(sql, from_dialect, to_dialect)
  read_and_free(ptr,
                error_class: TranspileError,
                message: "Failed to transpile SQL: #{sql.inspect}")
end

.versionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the library version string.

Returns:

  • (String)


134
135
136
# File 'lib/sqlglot/native.rb', line 134

def self.version
  sqlglot_version()
end