Sha256: 048385afb961a6400182c6290a8080e5cb9db3e271bf24144f840d08d7b895b2

Contents?: true

Size: 1.24 KB

Versions: 13

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

# Boxcars is a framework for running a series of tools to get an answer to a question.
module Boxcars
  # A Boxcar that interprets a prompt and executes SQL code using Active Record to get answers
  class SQLActiveRecord < SQLBase
    # @param connection [ActiveRecord::Connection] The SQL connection to use for this boxcar.
    # @param tables [Array<String>] The tables to use for this boxcar. Will use all if nil.
    # @param except_tables [Array<String>] The tables to exclude from this boxcar. Will exclude none if nil.
    # @param kwargs [Hash] Any other keyword arguments to pass to the parent class. This can include
    #   :name, :description, :prompt, :top_k, :stop, and :engine
    def initialize(connection: nil, tables: nil, except_tables: nil, **kwargs)
      connection ||= ::ActiveRecord::Base.connection
      super
    end

    private

    def table_schema(table)
      ["CREATE TABLE #{table} (",
       connection&.columns(table)&.map { |c| " #{c.name} #{c.sql_type} #{c.null ? "NULL" : "NOT NULL"}" }&.join(",\n"),
       ");"].join("\n")
    end

    def dialect
      connection.class.name.split("::").last.sub("Adapter", "")
    end

    def get_output(code)
      connection&.exec_query(code)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
boxcars-0.7.3 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.7.2 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.7.1 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.9 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.8 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.7 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.6 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.5 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.4 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.3 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.2 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.6.1 lib/boxcars/boxcar/sql_active_record.rb
boxcars-0.5.1 lib/boxcars/boxcar/sql_active_record.rb