lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb in knjrbfw-0.0.23 vs lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb in knjrbfw-0.0.24

- old
+ new

@@ -1,14 +1,13 @@ require "#{$knjpath}wref" class KnjDB_mysql::Tables - attr_reader :db, :driver, :list + attr_reader :db, :list def initialize(args) @args = args @db = @args[:db] - @driver = @args[:driver] @subtype = @db.opts[:subtype] @list_mutex = Mutex.new @list = Knj::Wref_map.new @list_should_be_reloaded = true end @@ -21,34 +20,37 @@ def [](table_name) table_name = table_name.to_s begin return @list[table_name] - rescue WeakRef::RefError + rescue Knj::Wref::Recycled #ignore. end - self.list do |table_obj| + self.list(:name => table_name) do |table_obj| return table_obj if table_obj.name == table_name end raise Knj::Errors::NotFound.new("Table was not found: #{table_name}.") end def list(args = {}) ret = {} unless block_given? + sql = "SHOW TABLE STATUS" + if args[:name] + sql << " WHERE `Name` = '#{@db.esc(args[:name])}'" + end + @list_mutex.synchronize do - @db.q("SHOW TABLE STATUS") do |d_tables| + @db.q(sql) do |d_tables| obj = @list.get!(d_tables[:Name]) if !obj obj = KnjDB_mysql::Tables::Table.new( :db => @db, - :driver => @driver, - :data => d_tables, - :tables => self + :data => d_tables ) @list[d_tables[:Name]] = obj end if block_given? @@ -77,36 +79,49 @@ first = false if first col_data.delete("after") if col_data["after"] sql << @db.cols.data_sql(col_data) end - sql << ")" - - @db.query(sql) - if data["indexes"] - table_obj = self[name] - table_obj.create_indexes(data["indexes"]) + sql << ", " + sql << KnjDB_mysql::Tables::Table.create_indexes(data["indexes"], { + :db => @db, + :return_sql => true, + :create => false, + :on_table => false, + :table_name => name + }) end + + sql << ")" + @db.query(sql) end end class KnjDB_mysql::Tables::Table attr_accessor :list def initialize(args) @args = args @db = args[:db] - @driver = args[:driver] @data = args[:data] @subtype = @db.opts[:subtype] @list = Knj::Wref_map.new @indexes_list = Knj::Wref_map.new raise "Could not figure out name from: '#{@data}'." if !@data[:Name] end + def reload + @data = @db.q("SHOW TABLE STATUS WHERE `Name` = '#{@db.esc(self.name)}'").fetch + end + + #Used to validate in Knj::Wrap_map. + def __object_unique_id__ + return @data[:Name] + end + def name return @data[:Name] end def drop @@ -117,16 +132,20 @@ def optimize @db.query("OPTIMIZE TABLE `#{self.name}`") return self end + def rows_count + return @data[:Rows].to_i + end + def column(name) name = name.to_s begin return @list[name] - rescue WeakRef::RefError + rescue Knj::Wref::Recycled #ignore. end self.columns do |col| return col if col.name == name @@ -145,11 +164,10 @@ if !obj obj = KnjDB_mysql::Columns::Column.new( :table_name => self.name, :db => @db, - :driver => @driver, :data => d_cols ) @list[d_cols[:Field]] = obj end @@ -178,11 +196,10 @@ if !obj obj = KnjDB_mysql::Indexes::Index.new( :table_name => self.name, :db => @db, - :driver => @driver, :data => d_indexes ) obj.columns << d_indexes[:Column_name] @indexes_list[d_indexes[:Key_name]] = obj end @@ -204,11 +221,11 @@ def index(name) name = name.to_s begin return @indexes_list[name] - rescue WeakRef::RefError + rescue Knj::Wref::Recycled #ignore. end self.indexes do |index| return index if index.name == name @@ -222,41 +239,81 @@ sql = "ALTER TABLE `#{self.name}` ADD COLUMN #{@db.cols.data_sql(col_data)};" @db.query(sql) end end - def create_indexes(index_arr) + def create_indexes(index_arr, args = {}) + return KnjDB_mysql::Tables::Table.create_indexes(index_arr, args.merge(:table_name => self.name, :db => @db)) + end + + def self.create_indexes(index_arr, args = {}) + db = args[:db] + + if args[:return_sql] + sql = "" + first = true + end + index_arr.each do |index_data| + if !args[:return_sql] + sql = "" + end + + if args[:create] or !args.key?(:create) + sql << "CREATE" + end + if index_data.is_a?(String) index_data = {"name" => index_data, "columns" => [index_data]} end raise "No name was given." if !index_data.key?("name") or index_data["name"].strip.length <= 0 raise "No columns was given on index: '#{index_data["name"]}'." if !index_data["columns"] or index_data["columns"].empty? - sql = "CREATE" + if args[:return_sql] + if first + first = false + else + sql << ", " + end + end + sql << " UNIQUE" if index_data["unique"] - sql << " INDEX #{@db.escape_col}#{@db.esc_col(index_data["name"])}#{@db.escape_col} ON #{@db.escape_table}#{@db.esc_table(self.name)}#{@db.escape_table} (" + sql << " INDEX #{db.escape_col}#{db.esc_col(index_data["name"])}#{db.escape_col}" + if args[:on_table] or !args.key?(:on_table) + sql << " ON #{db.escape_table}#{db.esc_table(args[:table_name])}#{db.escape_table}" + end + + sql << " (" + first = true index_data["columns"].each do |col_name| sql << ", " if !first first = false if first - sql << "#{@db.escape_col}#{@db.esc_col(col_name)}#{@db.escape_col}" + sql << "#{db.escape_col}#{db.esc_col(col_name)}#{db.escape_col}" end sql << ")" - @db.query(sql) + if !args[:return_sql] + db.query(sql) + end end + + if args[:return_sql] + return sql + else + return nil + end end def rename(newname) oldname = self.name @db.query("ALTER TABLE `#{oldname}` RENAME TO `#{newname}`") - @args[:tables].list[newname] = self - @args[:tables].list.delete(oldname) + @db.tables.list[newname] = self + @db.tables.list.delete(oldname) @data[:Name] = newname end def truncate @db.query("TRUNCATE `#{self.name}`") \ No newline at end of file