Class: Baza::Driver::Pg::Tables
- Defined in:
- lib/baza/driver/pg/tables.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
- #[](table_name) ⇒ Object
- #create(table_name, columns:, indexes: nil, return_sql: false, temp: false) ⇒ Object
-
#initialize(args) ⇒ Tables
constructor
A new instance of Tables.
- #list(args = {}) ⇒ Object
Methods inherited from Tables
Constructor Details
#initialize(args) ⇒ Tables
Returns a new instance of Tables.
4 5 6 7 |
# File 'lib/baza/driver/pg/tables.rb', line 4 def initialize(args) @args = args @db = @args.fetch(:db) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
2 3 4 |
# File 'lib/baza/driver/pg/tables.rb', line 2 def db @db end |
Instance Method Details
#[](table_name) ⇒ Object
9 10 11 12 13 |
# File 'lib/baza/driver/pg/tables.rb', line 9 def [](table_name) table = list(name: table_name).first raise Baza::Errors::TableNotFound unless table table end |
#create(table_name, columns:, indexes: nil, return_sql: false, temp: false) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/baza/driver/pg/tables.rb', line 42 def create(table_name, columns:, indexes: nil, return_sql: false, temp: false) table_name = table_name.to_s raise "Invalid table name: #{table_name}" if table_name.strip.empty? raise "No columns was given for '#{table_name}'." if !columns || columns.empty? create_table_sql = "CREATE" create_table_sql << " TEMPORARY" if temp create_table_sql << " TABLE #{db.quote_table(table_name)} (" first = true columns.each do |col_data| create_table_sql << ", " unless first first = false if first col_data.delete(:after) if col_data[:after] create_table_sql << db.columns.data_sql(col_data) end columns.each do |col_data| # rubocop :disable Style/CombinableLoops next unless col_data.key?(:foreign_key) create_table_sql << "," create_table_sql << " CONSTRAINT #{col_data.fetch(:foreign_key).fetch(:name)}" if col_data.fetch(:foreign_key)[:name] create_table_sql << " FOREIGN KEY (#{col_data.fetch(:name)}) " \ "REFERENCES #{col_data.fetch(:foreign_key).fetch(:to).fetch(0)} (#{col_data.fetch(:foreign_key).fetch(:to).fetch(1)})" end create_table_sql << ")" sqls = [create_table_sql] if indexes && !indexes.empty? sqls += db.indexes.create_index(indexes, table_name: table_name, return_sql: true) end return sqls if return_sql db.transaction do sqls.each do |sql| db.query(sql) end end end |
#list(args = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/baza/driver/pg/tables.rb', line 15 def list(args = {}) tables_list = [] unless block_given? where_args = { table_catalog: @db.opts[:db], table_schema: "public" } where_args[:table_name] = args.fetch(:name) if args[:name] @db.select([:information_schema, :tables], where_args, orderby: :table_name) do |table_data| table = Baza::Driver::Pg::Table.new( driver: @db.driver, data: table_data ) next if table.native? if tables_list tables_list << table else yield table end end tables_list end |