Class: MicroSql

Inherits:
Object
  • Object
show all
Defined in:
lib/micro_sql.rb,
lib/micro_sql.rb

Direct Known Subclasses

PgAdapter, SqliteAdapter

Defined Under Namespace

Classes: Error, KeyValueTable, PgAdapter, RollbackException, SqliteAdapter, Table

Instance Method Summary collapse

Instance Method Details

#ask(sql, *args) ⇒ Object



36
37
38
39
# File 'lib/micro_sql.rb', line 36

def ask(sql, *args)
  results = execute :ask, sql, *args
  format_results_for_ask(results)
end

#exec(sql, *args) ⇒ Object



45
46
47
# File 'lib/micro_sql.rb', line 45

def exec(sql, *args)
  execute :prepare, sql, *args
end

#exec!(sql, *args) ⇒ Object



41
42
43
# File 'lib/micro_sql.rb', line 41

def exec!(sql, *args)
  execute :no_prepare, sql, *args
end

#insert(table, *values) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/micro_sql.rb', line 80

def insert(table, *values)
  hashes, arrays = values.partition do |value| value.is_a?(Hash) end

  id1 = insert_array_values(table, arrays)
  id2 = insert_hash_values(table, hashes)
  
  id1 || id2
end

#key_value_table(name) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/micro_sql.rb', line 141

def key_value_table(name)
  @key_value_tables ||= Hash.new { |hash, key|
    hash[key] = MicroSql::KeyValueTable.new(self, key)
  }

  @key_value_tables[name] 
end

#rollback!Object

Raises:



74
75
76
# File 'lib/micro_sql.rb', line 74

def rollback!
  raise RollbackException
end

#table(sql_or_name) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/micro_sql.rb', line 123

def table(sql_or_name)
  @tables ||= {}

  return table_from_name(sql_or_name) if sql_or_name !~ /\s/

  table = Table.new(self, sql_or_name)
  @tables[table.table_name] = table 
end

#tablesObject



119
120
121
# File 'lib/micro_sql.rb', line 119

def tables
  raise "Implementation missing: #{self.class}#tables"
end

#transaction(&block) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/micro_sql.rb', line 64

def transaction(&block)
  r = nil
  @impl.transaction do
    r = yield
  end
  r
rescue RollbackException
  nil
end