Class: KnjDB_sqlite3::Tables
- Defined in:
- lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb
Defined Under Namespace
Classes: Table
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
Instance Method Summary collapse
- #[](table_name) ⇒ Object
- #create(name, data, args = nil) ⇒ Object
-
#initialize(args) ⇒ Tables
constructor
A new instance of Tables.
- #list ⇒ Object
Constructor Details
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
2 3 4 |
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 2 def db @db end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
2 3 4 |
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 2 def driver @driver end |
Instance Method Details
#[](table_name) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 12 def [](table_name) table_name = table_name.to_s begin ret = @list[table_name] return ret rescue Wref::Recycled #ignore. end self.list do |table_obj| return table_obj if table_obj.name.to_s == table_name.to_s end raise Errno::ENOENT, "Table was not found: #{table_name}." end |
#create(name, data, args = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 61 def create(name, data, args = nil) sql = "CREATE TABLE `#{name}` (" first = true data["columns"].each do |col_data| sql << ", " if !first first = false if first sql << @db.cols.data_sql(col_data) end sql << ")" if args and args[:return_sql] ret = [sql] else @db.query(sql) end if data.key?("indexes") and data["indexes"] table_obj = self[name] if args and args[:return_sql] ret += table_obj.create_indexes(data["indexes"], :return_sql => true) else table_obj.create_indexes(data["indexes"]) end end if args and args[:return_sql] return ret else return nil end end |
#list ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb', line 29 def list ret = {} unless block_given? @list_mutex.synchronize do q_tables = @db.select("sqlite_master", {"type" => "table"}, {"orderby" => "name"}) do |d_tables| next if d_tables[:name] == "sqlite_sequence" obj = @list.get!(d_tables[:name]) if !obj obj = KnjDB_sqlite3::Tables::Table.new( :db => @db, :data => d_tables ) @list[d_tables[:name]] = obj end if block_given? yield(obj) else ret[d_tables[:name]] = obj end end end if block_given? return nil else return ret end end |