lib/og/adapter.rb in nitro-0.10.0 vs lib/og/adapter.rb in nitro-0.11.0
- old
+ new
@@ -1,15 +1,15 @@
# * George Moschovitis <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
-# $Id: adapter.rb 255 2005-02-10 12:45:32Z gmosx $
+# $Id: adapter.rb 266 2005-02-28 14:50:48Z gmosx $
require 'yaml'
require 'singleton'
require 'og/connection'
-class Og
+module Og
# An adapter communicates with the backend datastore.
# The adapters for all supported datastores extend this
# class. Typically, an RDBMS is used to implement a
# datastore.
@@ -72,10 +72,11 @@
# Parse sql datetime
# TODO: Optimize this
def self.parse_timestamp(str)
+ return nil unless str
return Time.parse(str)
end
# Input YYYY-mm-dd
# TODO: Optimize this
@@ -106,11 +107,11 @@
def new_connection(db)
return Og::Connection.new(db)
end
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # :section: OR mapping methods and utilities.
+ # :section: O->R mapping methods and 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
@@ -119,43 +120,60 @@
def self.encode(klass)
"#{klass.name.gsub(/^.*::/, "")}".gsub(/::/, "_").downcase
end
# The name of the SQL table where objects of this class
- # are stored.
+ # are stored. A prefix is needed to avoid colision with
+ # reserved prefices (for example User maps to user which
+ # is reserved in postgresql). The prefix should start
+ # with an alphanumeric character to be compatible with
+ # all RDBMS (most notable Oracle).
+ #
+ # You may want to override this method to map an existing
+ # database schema using Og.
def self.table(klass)
- "_#{Og.table_prefix}#{encode(klass)}"
+ "og_#{Og.table_prefix}#{encode(klass)}"
end
# The name of the join table for the two given classes.
+ # A prefix is needed to avoid colision with reserved
+ # prefices (for example User maps to user which
+ # is reserved in postgresql). The prefix should start
+ # with an alphanumeric character to be compatible with
+ # all RDBMS (most notable Oracle).
+ #
+ # You may want to override this method to map an existing
+ # database schema using Og.
def self.join_table(klass1, klass2)
- "_#{Og.table_prefix}j_#{encode(klass1)}_#{encode(klass2)}"
+ "og_#{Og.table_prefix}j_#{encode(klass1)}_#{encode(klass2)}"
end
-
+
# Return an sql string evaluator for the property.
# No need to optimize this, used only to precalculate code.
# YAML is used to store general Ruby objects to be more
# portable.
- #
+ #--
# FIXME: add extra handling for float.
+ #++
def write_prop(p)
if p.klass.ancestors.include?(Integer)
return "#\{@#{p.symbol} || 'NULL'\}"
elsif p.klass.ancestors.include?(Float)
return "#\{@#{p.symbol} || 'NULL'\}"
elsif p.klass.ancestors.include?(String)
- return "'#\{#{self.class}.escape(@#{p.symbol})\}'"
+ return %|#\{@#{p.symbol} ? "'#\{#{self.class}.escape(@#{p.symbol})\}'" : 'NULL'\}|
elsif p.klass.ancestors.include?(Time)
return %|#\{@#{p.symbol} ? "'#\{#{self.class}.timestamp(@#{p.symbol})\}'" : 'NULL'\}|
elsif p.klass.ancestors.include?(Date)
return %|#\{@#{p.symbol} ? "'#\{#{self.class}.date(@#{p.symbol})\}'" : 'NULL'\}|
elsif p.klass.ancestors.include?(TrueClass)
return "#\{@#{p.symbol} ? \"'t'\" : 'NULL' \}"
else
+ # gmosx: keep the '' for nil symbols.
return %|#\{@#{p.symbol} ? "'#\{#{self.class}.escape(@#{p.symbol}.to_yaml)\}'" : "''"\}|
end
end
# Return an evaluator for reading the property.
@@ -195,13 +213,22 @@
if p.meta and p.meta[:sql]
field << " #{p.meta[:sql]}"
else
field << " #{@typemap[p.klass]}"
- # attach extra sql
- if p.meta and extra_sql = p.meta[:extra_sql]
- field << " #{extra_sql}"
+
+ if p.meta
+ # set default value (gmosx: not that useful in the
+ # current implementation).
+ if default = p.meta[:default]
+ field << " DEFAULT #{default.inspect} NOT NULL"
+ end
+
+ # attach extra sql
+ if extra_sql = p.meta[:extra_sql]
+ field << " #{extra_sql}"
+ end
end
end
fields << field
end
@@ -233,10 +260,10 @@
# Generate the mapping of the database fields to the
# object properties.
def calc_field_index(klass, og)
- raise 'Not implemented!'
+ # Implement if needed.
end
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# :section: Managed object enchant methods
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -