lib/baza/table.rb in baza-0.0.20 vs lib/baza/table.rb in baza-0.0.21

- old
+ new

@@ -1,6 +1,7 @@ class Baza::Table + include Comparable include Baza::DatabaseModelFunctionality attr_reader :db def to_s @@ -22,19 +23,45 @@ end end end def row(id) - data = rows({id: id}, limit: 1).fetch - raise Baza::Errors::RowNotFound unless data - - Baza::Row.new( - db: db, - table: name, - data: data - ) + row = rows({id: id}, limit: 1).first + raise Baza::Errors::RowNotFound unless row + row end def to_param name + end + + def insert(data, args = {}) + @db.insert(name, data, args) + end + + def upsert_duplicate_key(data, terms = {}, args = {}) + @db.upsert_duplicate_key(name, data, terms, args) + end + + def rows_count + sql = "SELECT COUNT(*) AS count FROM #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}" + @db.query(sql).fetch.fetch(:count).to_i + end + + def truncate + @db.query("TRUNCATE #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}") + self + end + + def <=>(other) + return false unless other.is_a?(Baza::Table) + return false unless other.db.opts.fetch(:db) == db.opts.fetch(:db) + other.name <=> name + end + + def create_columns(col_arr) + col_arr.each do |col_data| + sql = "ALTER TABLE #{db.sep_col}#{name}#{db.sep_col} ADD COLUMN #{@db.columns.data_sql(col_data)};" + @db.query(sql) + end end end