Sha256: bc59a863c5778e5e84f67a0a88c7c2547dab9e0d86e7c01e86d2cf3364c17e1e

Contents?: true

Size: 975 Bytes

Versions: 1

Compression:

Stored size: 975 Bytes

Contents

class MicroSql::SqliteAdapter < MicroSql
  def initialize(url)
    require "sqlite3"

    uri = URI.parse(url)
    @impl = SQLite3::Database.new(uri.path)
    exec! "PRAGMA synchronous = OFF"
  end
  
  def execute_batch(sql)
    @impl.execute_batch(sql)
  end

  def tables
    exec("SELECT name FROM sqlite_master WHERE type=?", "table").map(&:first)
  end
  
  private
  
  def execute(prepare, sql, *args)
    prepared_query = prepare(sql)
    execute_prepared_query sql, prepared_query, *args
  ensure
    unprepare(sql) if prepare == :no_prepare
  end
  
  def execute_prepared_query(sql, query, *args)
    results = query.execute!(*args)
    case sql
    when /^\s*INSERT/i then @impl.last_insert_row_id
    when /^\s*UPDATE/i then @impl.changes
    when /^\s*DELETE/i then @impl.changes
    else                    results
    end
  rescue SQLite3::ConstraintException
    raise Error, $!.message
  end

  def prepare_query(key, sql)
    @impl.prepare(sql)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
micro_sql-0.4.3 lib/micro_sql/sqlite_adapter.rb