lib/og/backends/mysql.rb in og-0.7.0 vs lib/og/backends/mysql.rb in og-0.8.0
- old
+ new
@@ -7,18 +7,53 @@
require "mysql"
require "og/backend"
-module Og
+class Og
-# = Utils
+# = MysqlBackend
#
-# A collection of useful utilities.
+# Implements a MySQL powered backend.
#
-module Utils
+class MysqlBackend < Og::Backend
+ # A mapping between Ruby and SQL types.
+ #
+ TYPEMAP = {
+ Integer => "integer",
+ Fixnum => "integer",
+ Float => "float",
+ String => "text",
+ Time => "timestamp",
+ Date => "date",
+ TrueClass => "boolean",
+ Object => "text",
+ Array => "text",
+ Hash => "text"
+ }
+
+ # Intitialize the connection to the RDBMS.
+ #
+ def initialize(config)
+ begin
+ @conn = Mysql.connect(config[:address], config[:user],
+ config[:password], config[:database])
+ rescue => ex
+ if ex.errno == 1049 # database does not exist.
+ Logger.info "Database '#{config[:database]}' not found!"
+ MysqlBackend.create_db(config[:database], config[:user], config[:password])
+ retry
+ end
+ raise
+ end
+ end
+
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ # Utilities
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
# Escape an SQL string
#
def self.escape(str)
return nil unless str
return Mysql.quote(str)
@@ -66,19 +101,19 @@
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 "'#\{Og::Utils.escape(@#{p.symbol})\}'"
+ return "'#\{Og::MysqlBackend.escape(@#{p.symbol})\}'"
elsif p.klass.ancestors.include?(Time)
- return %|#\{@#{p.symbol} ? "'#\{Og::Utils.timestamp(@#{p.symbol})\}'" : 'NULL'\}|
+ return %|#\{@#{p.symbol} ? "'#\{Og::MysqlBackend.timestamp(@#{p.symbol})\}'" : 'NULL'\}|
elsif p.klass.ancestors.include?(Date)
- return %|#\{@#{p.symbol} ? "'#\{Og::Utils.date(@#{p.symbol})\}'" : 'NULL'\}|
+ return %|#\{@#{p.symbol} ? "'#\{Og::MysqlBackend.date(@#{p.symbol})\}'" : 'NULL'\}|
elsif p.klass.ancestors.include?(TrueClass)
return "#\{@#{p.symbol} || 'NULL'\}"
else
- return %|#\{@#{p.symbol} ? "'#\{Og::Utils.escape(@#{p.symbol}.to_yaml)\}'" : "''"\}|
+ return %|#\{@#{p.symbol} ? "'#\{Og::MysqlBackend.escape(@#{p.symbol}.to_yaml)\}'" : "''"\}|
end
end
# Return an evaluator for reading the property.
# No need to optimize this, used only to precalculate code.
@@ -89,13 +124,13 @@
elsif p.klass.ancestors.include?(Float)
return "res[#{idx}].to_f()"
elsif p.klass.ancestors.include?(String)
return "res[#{idx}]"
elsif p.klass.ancestors.include?(Time)
- return "Og::Utils.parse_timestamp(res[#{idx}])"
+ return "Og::MysqlBackend.parse_timestamp(res[#{idx}])"
elsif p.klass.ancestors.include?(Date)
- return "Og::Utils.parse_date(res[#{idx}])"
+ return "Og::MysqlBackend.parse_date(res[#{idx}])"
elsif p.klass.ancestors.include?(TrueClass)
return "('true' == res[#{idx}])"
else
return "YAML::load(res[#{idx}])"
end
@@ -138,101 +173,67 @@
def self.eval_og_oid(klass)
klass.class_eval %{
prop_accessor :oid, Fixnum, :sql => "integer AUTO_INCREMENT PRIMARY KEY"
}
end
-end
-# = MysqlBackend
-#
-# Implements a MySQL powered backend.
-#
-class MysqlBackend < Og::Backend
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ # Connection methods.
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # A mapping between Ruby and SQL types.
- #
- TYPEMAP = {
- Integer => "integer",
- Fixnum => "integer",
- Float => "float",
- String => "text",
- Time => "timestamp",
- Date => "date",
- TrueClass => "boolean",
- Object => "text",
- Array => "text",
- Hash => "text"
- }
-
- # Intitialize the connection to the RDBMS.
- #
- def initialize(config)
- begin
- @conn = Mysql.connect(config[:address], config[:user],
- config[:password], config[:database])
- rescue => ex
- if ex.errno == 1049 # database does not exist.
- $log.info "Database '#{config[:database]}' not found!"
- MysqlBackend.create_db(config[:database], config[:user], config[:password])
- retry
- end
- raise
- end
- end
-
# Create the database.
#
def self.create_db(database, user = nil, password = nil)
- $log.info "Creating database '#{database}'."
+ Logger.info "Creating database '#{database}'."
`mysqladmin -f --user=#{user} --password=#{password} create #{database}`
end
# Drop the database.
#
def self.drop_db(database, user = nil, password = nil)
- $log.info "Dropping database '#{database}'."
+ Logger.info "Dropping database '#{database}'."
`mysqladmin -f --user=#{user} --password=#{password} drop #{database}`
end
# Execute an SQL query and return the result
#
def query(sql)
- $log.debug sql if $DBG
+ Logger.debug sql if $DBG
return @conn.query(sql)
end
# Execute an SQL query, no result returned.
#
def exec(sql)
- $log.debug sql if $DBG
+ Logger.debug sql if $DBG
@conn.query(sql)
end
# Execute an SQL query and return the result. Wrapped in a rescue
# block.
#
def safe_query(sql)
- $log.debug sql if $DBG
+ Logger.debug sql if $DBG
begin
return @conn.query(sql)
rescue => ex
- $log.error "DB error #{ex}, [#{sql}]"
- $log.error ex.backtrace
+ Logger.error "DB error #{ex}, [#{sql}]"
+ Logger.error ex.backtrace
return nil
end
end
# Execute an SQL query, no result returned. Wrapped in a rescue
# block.
#
def safe_exec(sql)
- $log.debug sql if $DBG
+ Logger.debug sql if $DBG
begin
@conn.query(sql)
rescue => ex
- $log.error "DB error #{ex}, [#{sql}]"
- $log.error ex.backtrace
+ Logger.error "DB error #{ex}, [#{sql}]"
+ Logger.error ex.backtrace
end
end
# Check if it is a valid resultset.
#
@@ -275,14 +276,14 @@
sql << ');'
begin
exec(sql)
- $log.info "Created table '#{klass::DBTABLE}'."
+ Logger.info "Created table '#{klass::DBTABLE}'."
rescue => ex
if ex.errno == 1050 # table already exists.
- $log.debug "Table already exists" if $DBG
+ Logger.debug "Table already exists" if $DBG
else
raise
end
end
@@ -306,19 +307,19 @@
# the class to join to and some options.
join_class, options = *data
# gmosx: dont use DBTABLE here, perhaps the join class
# is not managed yet.
- join_table = "#{Og::Utils.join_table(klass, join_class)}"
- join_src = "#{Og::Utils.encode(klass)}_oid"
- join_dst = "#{Og::Utils.encode(join_class)}_oid"
+ join_table = "#{self.class.join_table(klass, join_class)}"
+ join_src = "#{self.class.encode(klass)}_oid"
+ join_dst = "#{self.class.encode(join_class)}_oid"
begin
exec "CREATE TABLE #{join_table} ( key1 integer NOT NULL, key2 integer NOT NULL )"
exec "CREATE INDEX #{join_table}_key1_idx ON #{join_table} (key1)"
exec "CREATE INDEX #{join_table}_key2_idx ON #{join_table} (key2)"
rescue => ex
if ex.errno == 1050 # table already exists.
- $log.debug "Join table already exists" if $DBG
+ Logger.debug "Join table already exists" if $DBG
else
raise
end
end
end