Module: Mysql2Model::Container::ClassMethods

Defined in:
lib/mysql2_model/container.rb

Instance Method Summary collapse

Instance Method Details

#clientObject

Your models repository client via the cache



101
102
103
# File 'lib/mysql2_model/container.rb', line 101

def client
  Mysql2Model::Client[default_repository_name]
end

#default_repository_nameObject

This method is abstract.
TODO:

Should probably rename this to just default_repository since Array != Symbol or just “repository”; “default_repository_name” came from Datamapper

Define which repository or repositories act as the destination(s) of your model.

Examples:

Single Repository

class MyClass
  include Mysql2Model::Container
  self.default_repository_name
    :infrastructure
  end
end

Multiple Repositories

class MyClass
  include Mysql2Model::Container
  self.default_repository_name
    [:db1,:db2,:db3]
  end
end


96
97
98
# File 'lib/mysql2_model/container.rb', line 96

def default_repository_name
  :default
end

#query(statement = '', *args) ⇒ Object Also known as: execute

Flavor the behavior by returning the resultset as instances of self instead of Mysql2::Result



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/mysql2_model/container.rb', line 144

def query(statement='',*args)
  statement = yield if block_given?
  with_composed_sql(statement,*args) do |composed_sql|
    response = client.query(composed_sql)
    if response.respond_to?(:map)
      response.map do |row|
        # @todo Patch Mysql2 to support loading the primitives directly into custom class :as => CustomClass, and remove this map
        # @todo This is defeating Mysql2's lazy loading, but it's good for proof-of-concept
        self.new(row)
      end
    else
      response
    end
  end
end

#value(statement = '', *args) ⇒ Object, Array

TODO:

Remove the block pattern, we will need to utilize the block for the evented query pattern

Useful with queries that only return one result, like a COUNT.

Returns:

  • (Object)

    only the first value from the row when there is only one row

  • (Array)

    that contains the first value from each row if multiple rows are returned; as in a COUNT against multiple repos



132
133
134
135
136
137
138
139
140
141
# File 'lib/mysql2_model/container.rb', line 132

def value(statement='',*args)
  statement = yield if block_given?
  with_composed_sql(statement,*args) do |composed_sql|
    if (rv = client.query(composed_sql)).count > 1
      rv.map {|row| row.first.last }
    else
      rv.first.first.last
    end
  end
end

#value_sum(statement = '', *args) ⇒ Object

TODO:

considering renaming value_sum => value, value => value_for_each, but what happens if value is a String instead of Numeric?

TODO:

Remove the block pattern, we will need to utilize the block for the evented query pattern

Returns the sum of adding the first value from each of the rows primarily useful with count queries running against multiple repos.

Returns:

  • the sum of adding the first value from each of the rows primarily useful with count queries running against multiple repos



119
120
121
122
123
124
125
126
# File 'lib/mysql2_model/container.rb', line 119

def value_sum(statement='',*args)
  statement = yield if block_given?
  with_composed_sql(statement,*args) do |sql|
    client.query(sql).inject(0) { |sum,row|
      sum += row.first.last
    }
  end
end

#with_composed_sql(statement = '', *args) {|composed_sql| ... } ⇒ Object

Parameters:

  • statement (String) (defaults to: '')

    Uncomposed MySQL Statement

  • args (Array)

    arguements for composure

Yield Parameters:

  • composed_sql (String)

    Composed MySQL Statement

Yield Returns:

  • the return of the block



109
110
111
112
113
# File 'lib/mysql2_model/container.rb', line 109

def with_composed_sql(statement='',*args)
  composed_sql = compose_sql(statement,*args).strip
  log.info("SQL:[#{composed_sql}]")
  yield composed_sql
end