lib/og/adapters/mysql.rb in nitro-0.11.0 vs lib/og/adapters/mysql.rb in nitro-0.12.0
- old
+ new
@@ -1,8 +1,8 @@
# * George Moschovitis <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
-# $Id: mysql.rb 266 2005-02-28 14:50:48Z gmosx $
+# $Id: mysql.rb 270 2005-03-07 17:52:16Z gmosx $
require 'mysql'
require 'og/adapter'
require 'og/connection'
@@ -17,10 +17,11 @@
class MysqlAdapter < Adapter
def initialize
super
@typemap.update(TrueClass => 'tinyint')
+ @typecast.update(TrueClass => "#\{:s: ? \"1\" : 'NULL' \}")
end
def self.escape(str)
return nil unless str
return Mysql.quote(str)
@@ -116,10 +117,47 @@
ensure
res.free if res
end
+ def create_fields(klass)
+ fields = []
+
+ klass.__props.each do |p|
+ klass.sql_index(p.symbol) if p.meta[:sql_index]
+
+ field = "#{p.symbol}"
+
+ if p.meta and p.meta[:sql]
+ field << " #{p.meta[:sql]}"
+ else
+ field << " #{@typemap[p.klass]}"
+
+ 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
+
+ # set unique
+ # FIXME: correctly handle UNIQUE constrain.
+ # field << " UNIQUE" if p.meta[:unique]
+
+ # attach extra sql
+ if extra_sql = p.meta[:extra_sql]
+ field << " #{extra_sql}"
+ end
+ end
+ end
+
+ fields << field
+ end
+
+ return fields
+ end
+
def create_table(klass, db)
conn = db.get_connection
fields = create_fields(klass)
@@ -163,14 +201,14 @@
# 'many_to_many' relations.
if klass.__meta and joins = klass.__meta[:sql_join]
for data in joins
# the class to join to and some options.
- join_class, options = *data
+ join_name, join_class, options = *data
# gmosx: dont use DBTABLE here, perhaps the join class
# is not managed yet.
- join_table = "#{self.class.join_table(klass, join_class)}"
+ join_table = "#{self.class.join_table(klass, join_class, join_name)}"
join_src = "#{self.class.encode(klass)}_oid"
join_dst = "#{self.class.encode(join_class)}_oid"
begin
conn.store.query("CREATE TABLE #{join_table} ( key1 integer NOT NULL, key2 integer NOT NULL )")
conn.store.query("CREATE INDEX #{join_table}_key1_idx ON #{join_table} (key1)")