Momomoto::Table

From Pentabarf

Jump to: navigation, search

With the Momomoto::Table class you can access tables and views in the database.

To create a class to access the table person you would do the following:

class Person < Momomoto::Table; end

The class name is used as tablename. Metadata like primary keys are fetched from the database.

Contents

Simple Queries

To retrieve all rows of the table the select method is used:

rows = Person.select

This method returns an array of Person::Row instances. Momomoto defines accessors for each column of the table.

puts rows[0].first_name

Queries with constraints

Generic operators for all datatypes

symbol description symbol description symbol description
 :le less or equal  :lt less than  :ge greater or equal
 :gt greater than  :eq equal  :ne not equal

Operators for text datatypes

symbol description
 :like LIKE operator in SQL
  • _ is placeholder for a single character
  •  % is placeholder for multiple characters
 :ilike ILIKE operator in SQL

identical to :like but case insensitive

retrieve rows where the person_id equals 3

rows = Person.select(:person_id=>3)

retrieve rows where the person_id is greater than 10

rows = Person.select(:person_id=>{:gt=>10})

retrieve rows where the person_id is greater than 10 and less than 20

rows = Person.select(:person_id=>{:gt=>10,:lt=>20})

Custom setter and getter methods

To define custom setter/getter methods for the row class you should define those methods on the Methods Module in the namespace of the table class. The methods defined in this module will be included in the row class. To set/get values directly on the row you can use the set_column and get_column method.

class Person < Momomoto::Table
  module Methods
    def nickname=( value )
      set_column( :nickname, value.downcase )
    end
  end
end
Personal tools