Highcrest::Persistence (Module)

In: highcrest/persistence.rb
highcrest/persistence/base_oracle.rb
highcrest/persistence/oci8.rb
highcrest/persistence/oracle.rb
highcrest/persistence/oracle_odbc.rb

Methods

append_features   column_info   dbh   dbh   dbh   dbh   delete!   generate_id!   insert!   replace!   table_name   update!  

Public Class methods

Mixes various class methods into the class_that_includes_persistence

[Source]

# File highcrest/persistence.rb, line 84
    def Persistence.append_features(any_persistent_class)
        super any_persistent_class
        class << any_persistent_class

            # Returns a database handle
            def dbh()
                instance.dbh
            end

            # Fetches an object by primary key
            def read(*key)
                instance.select! *key
            end

            # Fetches an array of database rows
            def search(conditions)
                instance.search conditions
            end

            # Fetches an array of objects
            def select_all(conditions)
                instance.select_all conditions
            end

            # Deletes matching objects
            def delete_all(conditions)
                instance.delete_all conditions
            end


            # Registers an attribute to contain an array of dependent child objects,
            # and modifies the class's CRUD methods to persist that array
            def dependent_array(attribute_symbol, type)
                @array_types ||= Hash.new
                @array_types[attribute_symbol.id2name] = type
                self.module_eval "def \#{ attribute_symbol.id2name }\n@\#{ attribute_symbol.id2name } ||= Array.new\nend\n\ndef \#{ attribute_symbol.id2name }=(array)\n@\#{ attribute_symbol.id2name } = array\nend\n\nalias_method :__\#{ :select_where!.to_i }__, :select_where!\ndef select_where!(where_sql, *key)\n__\#{ :select_where!.to_i }__ where_sql, *key\nread_array_\#{ attribute_symbol.id2name }\nself\nend\n\nalias_method :__\#{ :insert!.to_i }__, :insert!\ndef insert!\n__\#{ :insert!.to_i }__\nwrite_array_\#{ attribute_symbol.id2name }\nself\nend\n\nalias_method :__\#{ :update!.to_i }__, :update!\ndef update!\ndelete_array_\#{ attribute_symbol.id2name }\n__\#{ :update!.to_i }__\nwrite_array_\#{ attribute_symbol.id2name }\nself\nend\n\nalias_method :__\#{ :delete!.to_i }__, :delete!\ndef delete!\ndelete_array_\#{ attribute_symbol.id2name }\n__\#{ :delete!.to_i }__\nend\n"
            end

            attr_accessor :table_name, :column_names, :key_column_names,
                :lob_column_names, :writable_column_names, :optimistic_column_names,
                :lob_column_initializers, :lob_column_resetters,
                :column_info, :column_types, :where_key_clause, :select_sql,
                :delete_sql, :insert_sql, :update_sql, :search_sql,
                :array_types #:nodoc:

            private
            def instance
                @instance ||= new
            end
        end
    end

Public Instance methods

Returns the database handle used by this class - by default a lazy static connection to scott/tiger

[Source]

# File highcrest/persistence/oracle_odbc.rb, line 8
    def dbh()
        @@dbh ||= temp = DBI.connect('DBI:ODBC:ORCL', 'scott', 'tiger')
    end

Returns the database handle used by this class - by default a lazy static connection to scott/tiger

[Source]

# File highcrest/persistence/oracle.rb, line 8
    def dbh()
        @@dbh ||= begin
            temp = DBI.connect('DBI:Oracle:', 'scott', 'tiger')
            temp.execute "alter session set nls_date_format='YYYY/MM/DD HH24:MI:SS'"
            temp
        end
    end

Returns a database handle

[Source]

# File highcrest/persistence.rb, line 176
    def dbh()
        raise 'Method not implemented'
    end

Returns the database handle used by this class - by default a lazy static connection to scott/tiger

[Source]

# File highcrest/persistence/oci8.rb, line 8
    def dbh()
        @@dbh ||= begin
            temp = DBI.connect('DBI:OCI8:', 'scott', 'tiger')
            temp.execute "alter session set nls_date_format='YYYY/MM/DD HH24:MI:SS'"
            temp
        end
    end

Deletes this record, and clears the optimistic cache

  foo = Foo.read 'key'
  foo.delete!         # foo deleted
  foo.replace!        # foo re-inserted

[Source]

# File highcrest/persistence.rb, line 230
    def delete!()
        rows = self.class.dbh.do delete_sql + where_key_sql + and_unchanged_sql,
                *(key_column_values + optimistic_column_values)
        raise RecordNotDeletedError.new if rows == 0
        discard_optimistic_cache!
    end

Inserts the database record, and replaces the optimistic cache with the inserted values

  foo = Foo.new
  foo.id = 'key'      # assign key explicitly
  foo.insert!         # then insert

[Source]

# File highcrest/persistence.rb, line 204
    def insert!()
        rows = self.class.dbh.do insert_sql, *writable_column_values
        reset_optimistic_cache!
        write_lobs
        self
    end

Inserts or updates depending on the presence/absence of optimistic cache. When inserting, generates new ids first

  foo = Foo.new
  foo.replace!        # generates id, then inserts
  foo.replace!        # updates

[Source]

# File highcrest/persistence.rb, line 195
    def replace!()
        if @optimistic_values.nil? then generate_id!.insert! else update! end
    end

Updates the database record, and replaces the optimistic cache with the updated values

  foo = Foo.read 'key'
  foo.attribute = 'new value'
  foo.update!             # or foo.replace!

[Source]

# File highcrest/persistence.rb, line 216
    def update!()
        rows = self.class.dbh.do update_sql + where_key_sql + and_unchanged_sql,
                *(writable_column_values + key_column_values + optimistic_column_values)
        raise RecordNotUpdatedError.new if rows == 0
        reset_optimistic_cache!
        write_lobs
        self
    end

Protected Instance methods

Gets the database column descriptions for the named table. You may have to redefine this method if your DBD driver does not fully implement the columns method.

[Source]

# File highcrest/persistence.rb, line 393
    def column_info() #:doc:
        self.class.dbh.columns table_name
    end

Replaces current key columns with newly generated values using new_id

[Source]

# File highcrest/persistence.rb, line 507
    def generate_id!() #:doc:
        key_column_names.each { |name| send name.downcase + "=", new_id(name) }
        self
    end

Name of the table used to store the object. Unless redefined, this is the last part of the classname.

[Source]

# File highcrest/persistence.rb, line 573
    def table_name() #:doc:
        self.class.table_name ||= self.class.to_s.downcase.split("::").reverse[0]
    end

[Validate]