Class: MicroSql::Table
- Inherits:
-
Object
- Object
- MicroSql::Table
- Defined in:
- lib/micro_sql/table.rb
Overview
– A basic table ———————————————————-
Direct Known Subclasses
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#initialize(db, sql) ⇒ Table
constructor
A new instance of Table.
- #insert(*values) ⇒ Object
- #primary_key ⇒ Object
Constructor Details
#initialize(db, sql) ⇒ Table
Returns a new instance of Table.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/micro_sql/table.rb', line 6 def initialize(db, sql) @db = db if sql !~ /\s/ # no space: sql must be the table_name @table_name = sql raise "No such table: '#{@table_name}'" unless exists? elsif sql =~ /\s*CREATE TABLE\s+([^\( ]+)/ @table_name = $1 raise ArgumentError, "Cannot determine table_name from SQL: #{sql}" unless table_name build(sql) unless exists? end end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
4 5 6 |
# File 'lib/micro_sql/table.rb', line 4 def db @db end |
Instance Method Details
#insert(*values) ⇒ Object
20 21 22 |
# File 'lib/micro_sql/table.rb', line 20 def insert(*values) db.insert table_name, *values end |
#primary_key ⇒ Object
24 25 26 |
# File 'lib/micro_sql/table.rb', line 24 def primary_key @primary_key ||= db.primary_key(table_name) end |