lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb in knjrbfw-0.0.9 vs lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb in knjrbfw-0.0.10
- old
+ new
@@ -1,7 +1,7 @@
class KnjDB_mysql::Tables
- attr_reader :db, :driver
+ attr_reader :db, :driver, :list
attr_accessor :list_should_be_reloaded
def initialize(args)
@args = args
@db = @args[:db]
@@ -10,10 +10,14 @@
@list_mutex = Mutex.new
@list = Knj::Wref_map.new
@list_should_be_reloaded = true
end
+ def clean
+ @list.clean
+ end
+
#Returns a table by the given table-name.
def [](table_name)
table_name = table_name.to_s
begin
@@ -28,37 +32,14 @@
raise Knj::Errors::NotFound.new("Table was not found: #{table_name}.")
end
def list(args = {})
- ret = {}
+ ret = {} unless block_given?
@list_mutex.synchronize do
@db.q("SHOW TABLE STATUS") do |d_tables|
- if @subtype == "java"
- d_tables = {
- :Name => d_tables[:TABLE_NAME],
- :Engine => d_tables[:ENGINE],
- :Version => d_tables[:VERSION],
- :Row_format => d_tables[:ROW_FORMAT],
- :Rows => d_tables[:TABLE_ROWS],
- :Avg_row_length => d_tables[:AVG_ROW_LENGTH],
- :Data_length => d_tables[:DATA_LENGTH],
- :Max_data_length => d_tables[:MAX_DATA_LENGTH],
- :Index_length => d_tables[:INDEX_LENGTH],
- :Data_free => d_tables[:DATA_FREE],
- :Auto_increment => d_tables[:AUTO_INCREMENT],
- :Create_time => d_tables[:CREATE_TIME],
- :Update_time => d_tables[:UPDATE_TIME],
- :Check_time => d_tables[:CHECK_TIME],
- :Collation => d_tables[:TABLE_COLLATION],
- :Checksum => d_tables[:CHECKSUM],
- :Create_options => d_tables[:CREATE_OPTIONS],
- :Comment => d_tables[:TABLE_COMMENT]
- }
- end
-
obj = @list.get!(d_tables[:Name])
if !obj
obj = KnjDB_mysql::Tables::Table.new(
:db => @db,
@@ -75,11 +56,15 @@
ret[d_tables[:Name]] = obj
end
end
end
- return ret
+ if block_given?
+ return nil
+ else
+ return ret
+ end
end
def create(name, data)
raise "No columns was given for '#{name}'." if !data["columns"] or data["columns"].empty?
@@ -115,11 +100,11 @@
@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 keys: '#{@data.keys.sort.join(", ")}'." if !@data[:Name]
+ raise "Could not figure out name from: '#{@data}'." if !@data[:Name]
end
def name
return @data[:Name]
end
@@ -154,24 +139,10 @@
@db.cols
ret = {}
sql = "SHOW FULL COLUMNS FROM `#{self.name}`"
@db.q(sql) do |d_cols|
- if @subtype == "java"
- d_cols = {
- :Field => d_cols[:COLUMN_NAME],
- :Type => d_cols[:COLUMN_TYPE],
- :Collation => d_cols[:COLLATION_NAME],
- :Null => d_cols[:IS_NULLABLE],
- :Key => d_cols[:COLUMN_KEY],
- :Default => d_cols[:COLUMN_DEFAULT],
- :Extra => d_cols[:EXTRA],
- :Privileges => d_cols[:PRIVILEGES],
- :Comment => d_cols[:COLUMN_COMMENT]
- }
- end
-
obj = @list.get!(d_cols[:Field])
if !obj
obj = KnjDB_mysql::Columns::Column.new(
:table_name => self.name,
@@ -187,37 +158,18 @@
else
ret[d_cols[:Field]] = obj
end
end
- raise "No block was given." if !block_given?
-
return ret
end
def indexes
@db.indexes
ret = {}
@db.q("SHOW INDEX FROM `#{self.name}`") do |d_indexes|
- if @subtype == "java"
- d_indexes = {
- :Table => d_indexes[:TABLE_NAME],
- :Non_unique => d_indexes[:NON_UNIQUE],
- :Key_name => d_indexes[:INDEX_NAME],
- :Seq_in_index => d_indexes[:SEQ_IN_INDEX],
- :Column_name => d_indexes[:COLUMN_NAME],
- :Collation => d_indexes[:COLLATION],
- :Cardinality => d_indexes[:CARDINALITY],
- :Sub_part => d_indexes[:SUB_PART],
- :Packed => d_indexes[:PACKED],
- :Null => d_indexes[:NULLABLE],
- :Index_type => d_indexes[:INDEX_TYPE],
- :Comment => d_indexes[:COMMENT]
- }
- end
-
next if d_indexes[:Key_name] == "PRIMARY"
obj = @indexes_list.get!(d_indexes[:Key_name])
if !obj
@@ -235,11 +187,9 @@
yield(obj)
else
ret[d_indexes[:Key_name]] = obj
end
end
-
- raise "No block was given." if !block_given?
return ret
end
def index(name)
\ No newline at end of file