Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an ActiveRecord adapter #64

Open
noteflakes opened this issue Jan 18, 2024 · 2 comments
Open

Implement an ActiveRecord adapter #64

noteflakes opened this issue Jan 18, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@noteflakes
Copy link
Contributor

'nough said.

@noteflakes noteflakes added the enhancement New feature or request label Jan 18, 2024
@noteflakes noteflakes self-assigned this Jan 18, 2024
@fractaledmind
Copy link

I started experimenting in the Rails codebase to see what I needed to patch/alias from Extralite to have the default Rails SQLite adapter work with Extralite instead. I haven't finished that project, but I was at the point where I was running the tests. Here is the patch I was using:

module Extralite
  class Database
    alias_method :original_execute, :execute
    alias_method :execute, :query
    alias_method :execute_batch2, :query
    alias_method :readonly?, :read_only?

    def transaction(mode = :immediate)
      mode = @default_transaction_mode if mode.nil?
      execute "begin #{mode} transaction"

      if block_given?
        abort = false
        begin
          yield self
        rescue
          abort = true
          raise
        ensure
          abort ? rollback : commit
        end
      end

      true
    end

    def commit = execute("commit transaction")
    def rollback = execute("rollback transaction")
    def encoding = "UTF-8"
    def busy_timeout(timeout) = self.busy_timeout=(timeout)
  end

  class Query
    alias_method :original_to_a, :to_a
    alias_method :to_a, :to_a_ary
    alias_method :reset!, :reset
    def bind_params(bind_vars) = bind(*bind_vars)

    alias_method :original_columns, :columns
    def columns = original_columns.map(&:to_s)
  end
end

On the Rails side, I only needed to patch the SQLite3Adapter.new_client method:

def new_client(config)
  ::Extralite::Database.new(config[:database].to_s, config)
  # ...
end

I'm not sure if you want to go in this direction (proving a compatibility layer so that the default Rails adapter can be used as is), but if so, this is a healthy starting point.

@martijn10kb
Copy link

@fractaledmind I kinda found that you have been more then just experimenting.. And I saw that you have a gem with this PR: fractaledmind/activerecord-enhancedsqlite3-adapter#2 Will there ever be a future where this will be finished nicely, with that PR integrated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants