lib/og/backend.rb in og-0.7.0 vs lib/og/backend.rb in og-0.8.0
- old
+ new
@@ -6,37 +6,58 @@
require "yaml"
require "og/connection"
-module Og
+class Og
-# = OgUtils
+# = Backend
#
-# A collection of useful utilities.
+# Abstract backend. A backend communicates with the RDBMS.
+# This is the base class for the various backend implementations.
#
-module Utils
+class Backend
+
+ # The actual connection to the database
+ attr_accessor :conn
+ # Intitialize the connection to the RDBMS.
+ #
+ def initialize(config)
+ raise "Not implemented"
+ end
+
+ # Close the connection to the RDBMS.
+ #
+ def close()
+ @conn.close()
+ end
+
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ # Utilities
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
# Encode the name of the klass as an sql safe string.
- # The Module separators are replaced with _ and NOT stripped out so
- # that we can convert back to the original notation if needed.
- # The leading module if available is removed.
+ # The Module separators are replaced with _ and NOT stripped
+ # out so that we can convert back to the original notation if
+ # needed. The leading module if available is removed.
#
def self.encode(klass)
"#{klass.name.gsub(/^.*::/, "")}".gsub(/::/, "_").downcase
end
- # The name of the SQL table where objects of this class are stored.
+ # The name of the SQL table where objects of this class
+ # are stored.
#
def self.table(klass)
- "_#{$og_table_prefix}#{encode(klass)}"
+ "_#{Og.table_prefix}#{encode(klass)}"
end
# The name of the join table for the two given classes.
#
def self.join_table(klass1, klass2)
- "_#{$og_table_prefix}j_#{Og::Utils.encode(klass1)}_#{Og::Utils.encode(klass2)}"
+ "_#{Og.table_prefix}j_#{encode(klass1)}_#{encode(klass2)}"
end
# Returns the props that will be included in the insert query.
# For some backends the oid should be stripped.
#
@@ -121,12 +142,12 @@
end
}
end
# Precompile the code to read objects of the given class
- # from the backend. In order to allow for changing field/attribute
- # orders we have to use a field mapping hash.
+ # from the backend. In order to allow for changing
+ # field/attribute orders we have to use a field mapping hash.
#
def self.eval_og_deserialize(klass, og)
calc_field_index(klass, og)
props = klass.__props
@@ -134,55 +155,35 @@
props.each do |p|
if idx = og.managed_classes[klass].field_index[p.name]
# more fault tolerant if a new field is added and it
# doesnt exist in the database.
- code << "@#{p.name} = #{Og::Utils.read_prop(p, idx)}"
+ code << "@#{p.name} = #{read_prop(p, idx)}"
end
end
klass.class_eval %{
def og_deserialize(res, tuple = nil)
#{code.join('; ')}
end
}
end
-end
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ # Connection methods.
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-# = Backend
-#
-# Abstract backend. A backend communicates with the RDBMS.
-# This is the base class for the various backend implementations.
-#
-class Backend
-
- # The actual connection to the database
- attr_accessor :conn
-
- # Intitialize the connection to the RDBMS.
- #
- def initialize(config)
- raise "Not implemented"
- end
-
- # Close the connection to the RDBMS.
- #
- def close()
- @conn.close()
- end
-
# Create the database.
#
def self.create_db(database, user = nil, password = nil)
- $log.info "Creating database '#{database}'."
+ Logger.info "Creating database '#{database}'."
end
# Drop the database.
#
def self.drop_db(database, user = nil, password = nil)
- $log.info "Dropping database '#{database}'."
+ Logger.info "Dropping database '#{database}'."
end
# Execute an SQL query and return the result.
#
def query(sql)
@@ -193,19 +194,19 @@
#
def exec(sql)
raise "Not implemented"
end
- # Execute an SQL query and return the result. Wrapped in a rescue
- # block.
+ # Execute an SQL query and return the result. Wrapped in a
+ # rescue block.
#
def safe_query(sql)
raise "Not implemented"
end
- # Execute an SQL query, no result returned. Wrapped in a rescue
- # block.
+ # Execute an SQL query, no result returned. Wrapped in a
+ # rescue block.
#
def safe_exec(sql)
raise "Not implemented"
end
@@ -233,13 +234,13 @@
exec "ROLLBACK"
end
# Create the fields that correpsond to the klass properties.
# The generated fields array is used in create_table.
- # If the property has an :sql metadata this overrides the default mapping.
- # If the property has an :extra_sql metadata the extra sql is appended
- # after the default mapping.
+ # If the property has an :sql metadata this overrides the
+ # default mapping. If the property has an :extra_sql metadata
+ # the extra sql is appended after the default mapping.
#
def create_fields(klass, typemap)
fields = []
klass.__props.each do |p|
@@ -295,6 +296,6 @@
raise "Not implemented"
end
end
-end # module
+end # namespace