Sha256: 6acee4553b80e701bf7a89548461e16169af69e354731e7a273652f7d6cfc4a7

Contents?: true

Size: 971 Bytes

Versions: 5

Compression:

Stored size: 971 Bytes

Contents

require "sqlite3"

class MicroSql::SqliteAdapter < MicroSql
  def initialize(url)
    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

5 entries across 5 versions & 1 rubygems

Version Path
micro_sql-0.4.2 lib/micro_sql/sqlite_adapter.rb
micro_sql-0.4.1 lib/micro_sql/sqlite_adapter.rb
micro_sql-0.3.2 lib/micro_sql/sqlite_adapter.rb
micro_sql-0.3.1 lib/micro_sql/sqlite_adapter.rb
micro_sql-0.3.0 lib/micro_sql/sqlite_adapter.rb