Class: SQLite3::Database

Inherits:
Object show all
Defined in:
lib/sqlite3/database_extension.rb

Instance Method Summary collapse

Instance Method Details

#ask(sql, *args) ⇒ Object



61
62
63
# File 'lib/sqlite3/database_extension.rb', line 61

def ask(sql, *args)
  query(sql).ask *prepare_arguments(args)
end

#exec(sql, *args) ⇒ Object

execute multiple SQL statements at once.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sqlite3/database_extension.rb', line 11

def exec(sql, *args)
  raise "Cannot run nondestructive query: #{sql}" if readonly?

  args = prepare_arguments(args)

  while sql =~ /\S/ do
    statement = prepare(sql)

    sql = statement.remainder
    if statement.active?
      statement.execute!(*args)
    end
  end

rescue
  STDERR.puts "#{sql}: #{$!}"
  raise
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sqlite3/database_extension.rb', line 70

def print(sql, *args)
  require "pp"

  results = select sql, *args
  log_sql = sql.gsub(/\n/, " ").gsub(/\s+/, " ")
  puts "=" * log_sql.length
  puts log_sql
  puts "-" * log_sql.length

  results.each do |result|
    pp result.to_a
  end
  puts "=" * log_sql.length
end

#readonly=(readonly) ⇒ Object



6
7
8
# File 'lib/sqlite3/database_extension.rb', line 6

def readonly=(readonly)
  @readonly = readonly
end

#readonly?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/sqlite3/database_extension.rb', line 2

def readonly?
  @readonly
end

#run(sql, *args) ⇒ Object



57
58
59
# File 'lib/sqlite3/database_extension.rb', line 57

def run(sql, *args)
  query(sql).run *prepare_arguments(args)
end

#select(sql, *args) ⇒ Object

run a select like query. Returns an array of records.



66
67
68
# File 'lib/sqlite3/database_extension.rb', line 66

def select(sql, *args)
  query(sql).select *prepare_arguments(args)
end