lib/og/adapters/mysql.rb in og-0.10.0 vs lib/og/adapters/mysql.rb in og-0.11.0

- old
+ new

@@ -1,26 +1,26 @@ # * George Moschovitis <gm@navel.gr> # (c) 2004-2005 Navel, all rights reserved. -# $Id: mysql.rb 259 2005-02-15 08:54:54Z gmosx $ +# $Id: mysql.rb 266 2005-02-28 14:50:48Z gmosx $ require 'mysql' require 'og/adapter' require 'og/connection' require 'glue/attribute' -class Og +module Og # The MySQL adapter. This adapter communicates with # an MySQL rdbms. For extra documentation see # lib/og/adapter.rb class MysqlAdapter < Adapter def initialize super - @typemap.update({TrueClass => 'tinyint'}) + @typemap.update(TrueClass => 'tinyint') end def self.escape(str) return nil unless str return Mysql.quote(str) @@ -40,18 +40,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 "'#\{#{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} ? 1 : 0 \}" else + # gmosx: keep the '' for nil symbols. return %|#\{@#{p.symbol} ? "'#\{#{self.class}.escape(@#{p.symbol}.to_yaml)\}'" : "''"\}| end end def read_prop(p, idx) @@ -80,10 +81,14 @@ def drop_db(database, user = nil, password = nil) `mysqladmin -f --user=#{user} --password=#{password} drop #{database}` super end + def props_for_insert(klass) + klass.__props.reject { |p| :oid == p.symbol } + end + def insert_code(klass, db, pre_cb, post_cb) props = props_for_insert(klass) values = props.collect { |p| write_prop(p) }.join(',') sql = "INSERT INTO #{klass::DBTABLE} (#{props.collect {|p| p.name}.join(',')}) VALUES (#{values})" @@ -96,10 +101,10 @@ #{post_cb} } end def new_connection(db) - return Og::MysqlConnection.new(db) + return MysqlConnection.new(db) end def calc_field_index(klass, db) res = db.query "SELECT * FROM #{klass::DBTABLE} LIMIT 1" meta = db.managed_classes[klass]