class Ezframe::DB

Attributes

pool[RW]
sequel[RW]

Public Class Methods

connect(dbfile = nil) click to toggle source
# File lib/ezframe/database.rb, line 21
def connect(dbfile = nil)
  dbfile ||= @dbfile
  @sequel = Sequel.connect(dbfile, EzLogs: [EzLog])
  return @sequel
end
create_table(table_name, dbtype_h) click to toggle source

テーブル生成

# File lib/ezframe/database.rb, line 120
def create_table(table_name, dbtype_h)
  %w[id created_at updated_at deleted_at].each do |key|
    dbtype_h.delete(key.to_sym)
  end
  # puts "create_table: #{table_name}"
  if @dbfile.index("postgres")
    @sequel.create_table(table_name) do
      primary_key :id, identity: true
      dbtype_h.each do |key, dbtype|
        column(key, dbtype)
      end
      column(:created_at, :timestamp, default: Sequel::CURRENT_TIMESTAMP)
      column(:updated_at, :timestamp)
      column(:deleted_at, :timestamp)
    end
  else
    @sequel.create_table(table_name) do
      primary_key :id, auto_increment: true
      dbtype_h.each do |key, dbtype|
        column(key, dbtype)
      end
      column(:created_at, :timestamp, default: Sequel::CURRENT_TIMESTAMP)
      column(:updated_at, :timestamp)
      column(:deleted_at, :timestamp)
    end
  end
end
dataset(table_name) click to toggle source
# File lib/ezframe/database.rb, line 53
def dataset(table_name)
  @sequel[table_name.to_sym]
end
delete(dataset, id) click to toggle source
# File lib/ezframe/database.rb, line 157
def delete(dataset, id)
  dataset.where(id: id).update({ deleted_at: Time.now })
end
disconnect() click to toggle source
# File lib/ezframe/database.rb, line 27
def disconnect
  @sequel.disconnect
end
exec(sql, first: nil) click to toggle source
# File lib/ezframe/database.rb, line 39
def exec(sql, first: nil)
  conn = get_conn
  if first
    return conn[sql].first
  else
    return conn[sql].all
  end
end
get_conn() click to toggle source
# File lib/ezframe/database.rb, line 31
def get_conn
  if @pool
    @pool.hold {|conn| return conn }
  else
    @sequel
  end
end
get_join_table(structure, opts = {}) click to toggle source

テーブルを連結して、全てのデータを返す。

# File lib/ezframe/database.rb, line 77
def get_join_table(structure, opts = {})
  col_h = {}
  reverse_col_h = {}
  query_a = []
  table_a = []
  prefix="_x_"
  structure[:column_list].each_with_index do |k, i|
    key = "#{prefix}#{i+1}"
    col_h[k.to_sym] = key.to_sym
    reverse_col_h[key.to_sym] = k
    query_a.push "#{k} AS #{key}"
  end
  tables = structure[:tables].clone
  join_cond = structure[:join_condition]
  tb = tables.shift
  table_part = [ tb ]
  tables.each do |table|
    cond = join_cond[table.to_sym]
    if cond
      table_part.push " LEFT JOIN #{table} ON #{cond}"
    else
      table_part.push " LEFT JOIN #{table} ON #{tb}.#{table} = #{table}.id"
    end
  end
  sql = "SELECT #{query_a.join(', ')} FROM #{table_part.join(' ')}"
  sql += " WHERE #{opts[:where]}" if opts[:where]
  sql += " ORDER BY #{opts[:order]}" if opts[:order]
  sql += " LIMIT #{opts[:limit]}" if opts[:limit]
  data_a = self.exec(sql)
  res_a = []
  data_a.each do |data|
    new_data = JointHash.new(tb)
    data.each do |k, v|
      orig_key = reverse_col_h[k.to_sym]
      next unless orig_key
      new_data[orig_key] = v
    end
    res_a.push(new_data)
  end
  return res_a
end
init(dbfile = nil, opts = {}) click to toggle source
# File lib/ezframe/database.rb, line 7
def init(dbfile = nil, opts = {})
  @dbfile = dbfile || ENV["EZFRAME_DB"] || Config[:database]
  unless @dbfile
    raise "database settings error: dbfile=#{Config[:database]}"
  end
  #if Config[:use_connection_pool] || opts[:use_connection_pool]
    #@pool = Sequel::ConnectionPool.new(max_connections: 10) do
    #  Sequel.connect(@dbfile, loggers: [EzLog])
    #end
  #else
  connect(@dbfile)
  #end
end
insert(table_name, val_h) click to toggle source
# File lib/ezframe/database.rb, line 148
def insert(table_name, val_h)
  dataset(table_name).insert(val_h)
end
run(sql) click to toggle source
# File lib/ezframe/database.rb, line 48
def run(sql)
  conn = get_conn
  conn.run(sql)
end
update(dataset, id, val_h) click to toggle source
# File lib/ezframe/database.rb, line 152
def update(dataset, id, val_h)
  val_h.update({ updated_at: Time.now })
  dataset.where(id: id).update(val_h)
end