lib/og/store/mysql.rb in og-0.24.0 vs lib/og/store/mysql.rb in og-0.25.0

- old
+ new

@@ -3,12 +3,12 @@ rescue Object => ex Logger.error 'Ruby-Mysql bindings are not installed!' Logger.error 'Trying to use the pure-Ruby binding included in Og' begin # Attempt to use the included pure ruby version. - require 'vendor/mysql' - require 'vendor/mysql411' + require 'og/vendor/mysql' + require 'og/vendor/mysql411' rescue Object => ex Logger.error ex end end @@ -41,34 +41,34 @@ def each_row each do |row| yield(row, 0) end end - + def first_value val = fetch_row[0] free return val end - + alias_method :close, :free def fields fetch_fields.map { |f| f.name } - end + end end module Og module MysqlUtils include SqlUtils - + def escape(str) return nil unless str return Mysql.quote(str) end - + def quote(val) case val when Fixnum, Integer, Float val ? val.to_s : 'NULL' when String @@ -77,15 +77,15 @@ val ? "'#{timestamp(val)}'" : 'NULL' when Date val ? "'#{date(val)}'" : 'NULL' when TrueClass val ? "'1'" : 'NULL' - else + else # gmosx: keep the '' for nil symbols. val ? escape(val.to_yaml) : '' - end - end + end + end end # A Store that persists objects into a MySQL database. # To read documentation about the methods, consult # the documentation for SqlStore and Store. @@ -127,11 +127,11 @@ # * :address, the addres where the server is listening. # * :socket, is useful when the pure ruby driver is used. # this is the location of mysql.sock. For Ubuntu/Debian # this is '/var/run/mysqld/mysqld.sock'. You can find # the location for your system in my.cnf - + def initialize(options) super @typemap.update(TrueClass => 'tinyint') @@ -144,16 +144,16 @@ options[:socket] ) # You should set recconect to true to avoid MySQL has # gone away errors. - + if @conn.respond_to? :reconnect options[:reconnect] = true unless options.has_key?(:reconnect) @conn.reconnect = options[:reconnect] end - + rescue => ex if ex.errno == 1049 # database does not exist. Logger.info "Database '#{options[:name]}' not found!" self.class.create(options) retry @@ -164,19 +164,19 @@ def close @conn.close super end - def enchant(klass, manager) - if klass.ann.this.primary_key.symbol == :oid + def enchant(klass, manager) + if klass.ann.self.primary_key.symbol == :oid unless klass.properties.include? :oid klass.property :oid, Fixnum, :sql => 'integer AUTO_INCREMENT PRIMARY KEY' end end super end - + def query(sql) Logger.debug sql if $DBG @conn.query_with_result = true return @conn.query(sql) rescue => ex @@ -189,24 +189,24 @@ @conn.query(sql) rescue => ex handle_sql_exception(ex, sql) end - def start + def start # nop # FIXME: InnoDB supports transactions. end - + # Commit a transaction. - + def commit # nop, not supported? # FIXME: InnoDB supports transactions. end - + # Rollback a transaction. - + def rollback # nop, not supported? # FIXME: InnoDB supports transactions. end @@ -221,42 +221,42 @@ # rp: fixes problems when user doesn't have # write access to db. # THINK, should a method more like this be # used instead of catching database exceptions # for 'table exists'? - + return if @conn.list_tables.include?(klass::OGTABLE) - - + + fields = fields_for_class(klass) sql = "CREATE TABLE #{klass::OGTABLE} (#{fields.join(', ')}" - + # Create table constraints. - - if constraints = klass.ann.this[:sql_constraint] + + if constraints = klass.ann.self[:sql_constraint] sql << ", #{constraints.join(', ')}" end - + if table_type = @options[:table_type] sql << ") TYPE = #{table_type};" else sql << ");" end - + # Create indices. - - if indices = klass.ann.this[:index] + + if indices = klass.ann.self[:index] for data in indices idx, options = *data idx = idx.to_s pre_sql, post_sql = options[:pre], options[:post] idxname = idx.gsub(/ /, "").gsub(/,/, "_").gsub(/\(.*\)/, "") sql << " CREATE #{pre_sql} INDEX #{klass::OGTABLE}_#{idxname}_idx #{post_sql} ON #{klass::OGTABLE} (#{idx});" end end - + @conn.query_with_result = false begin @conn.query(sql) Logger.info "Created table '#{klass::OGTABLE}'." @@ -266,15 +266,15 @@ return else raise end end - + # Create join tables if needed. Join tables are used in # 'many_to_many' relations. - - if join_tables = klass.ann.this[:join_tables] + + if join_tables = klass.ann.self[:join_tables] for info in join_tables begin create_join_table_sql(info).each do |sql| @conn.query sql end @@ -315,16 +315,16 @@ return %|#\{@#{p} ? "'#\{#{self.class}.timestamp(@#{p})\}'" : 'NULL'\}| elsif p.klass.ancestors.include?(Date) return %|#\{@#{p} ? "'#\{#{self.class}.date(@#{p})\}'" : 'NULL'\}| elsif p.klass.ancestors.include?(TrueClass) return "#\{@#{p} ? \"'1'\" : 'NULL' \}" - else + else # gmosx: keep the '' for nil symbols. return %|#\{@#{p} ? "'#\{#{self.class}.escape(@#{p}.to_yaml)\}'" : "''"\}| end - end - + end + def read_prop(p, col) if p.klass.ancestors.include?(Integer) return "#{self.class}.parse_int(res[#{col} + offset])" elsif p.klass.ancestors.include?(Float) return "#{self.class}.parse_float(res[#{col} + offset])" @@ -334,32 +334,32 @@ return "#{self.class}.parse_timestamp(res[#{col} + offset])" elsif p.klass.ancestors.include?(Date) return "#{self.class}.parse_date(res[#{col} + offset])" elsif p.klass.ancestors.include?(TrueClass) return "('1' == res[#{col} + offset])" - else + else return "YAML.load(res[#{col} + offset])" - end + end end def eval_og_insert(klass) props = klass.properties.values values = props.collect { |p| write_prop(p) }.join(',') if klass.schema_inheritance? props << Property.new(:symbol => :ogtype, :klass => String) values << ", '#{klass}'" end - + sql = "INSERT INTO #{klass::OGTABLE} (#{props.collect {|p| field_for_property(p)}.join(',')}) VALUES (#{values})" klass.class_eval %{ def og_insert(store) - #{Aspects.gen_advice_code(:og_insert, klass.advices, :pre) if klass.respond_to?(:advices)} + #{Glue::Aspects.gen_advice_code(:og_insert, klass.advices, :pre) if klass.respond_to?(:advices)} store.conn.query_with_result = false store.conn.query "#{sql}" @#{klass.pk_symbol} = store.conn.insert_id - #{Aspects.gen_advice_code(:og_insert, klass.advices, :post) if klass.respond_to?(:advices)} + #{Glue::Aspects.gen_advice_code(:og_insert, klass.advices, :post) if klass.respond_to?(:advices)} end } end end