Sha256: 98d9e834614b2567b164ef0433fc245b6b29410b6afcc631de20c3ea65887c66

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

require 'alfa/support'
require 'alfa/models/base_sql'

module Alfa
  module Models
    class MySQL < Alfa::Models::BaseSQL
      include Alfa::ClassInheritance
      inheritable_attributes :connection, :table

      @connection = nil
      @table = nil
      @pk = :id

      class << self

        def register_database database
          raise 'Expected database to be instance of Alfa::Database::MySQL' unless database.ancestors.include? Alfa::Database::MySQL
          @connection = database
        end

        #@return Array || nil
        def all
          @connection.query("SELECT * FROM `#{self.table}`")
        end

        def find filter = {}

        end

        def find_first filter = {}
        end

        def count filter = {}
        end

        def exists? pk
        end

        def create data = {}
          #data.symbolize_keys!
          @connection.query("INSERT INTO `#{self.table}` SET #{self.prepare_set_string(data)}")
        end

        def delete pk
        end

        def update data = {}
        end

        def table
          @table.to_s
        end

        def prepare_set_string data = {}
          data.map{|field, value| "`#{@connection.escape(field)}`='#{@connection.escape(value)}'"}.join(', ')
        end

      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alfa-0.0.5.pre lib/alfa/models/mysql.rb
alfa-0.0.4.pre lib/alfa/models/mysql.rb
alfa-0.0.2.pre lib/alfa/models/mysql.rb