Class: Ezframe::DB

Inherits:
Object show all
Defined in:
lib/ezframe/database.rb

Defined Under Namespace

Classes: Cache, JointHash

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.poolObject

Returns the value of attribute pool.



5
6
7
# File 'lib/ezframe/database.rb', line 5

def pool
  @pool
end

.sequelObject

Returns the value of attribute sequel.



5
6
7
# File 'lib/ezframe/database.rb', line 5

def sequel
  @sequel
end

Class Method Details

.connect(dbfile = nil) ⇒ Object



21
22
23
24
25
# 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) ⇒ Object

テーブル生成



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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) ⇒ Object



53
54
55
# File 'lib/ezframe/database.rb', line 53

def dataset(table_name)
  @sequel[table_name.to_sym]
end

.delete(dataset, id) ⇒ Object



157
158
159
# File 'lib/ezframe/database.rb', line 157

def delete(dataset, id)
  dataset.where(id: id).update({ deleted_at: Time.now })
end

.disconnectObject



27
28
29
# File 'lib/ezframe/database.rb', line 27

def disconnect
  @sequel.disconnect
end

.exec(sql, first: nil) ⇒ Object



39
40
41
42
43
44
45
46
# 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_connObject



31
32
33
34
35
36
37
# 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 = {}) ⇒ Object

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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 = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



148
149
150
# File 'lib/ezframe/database.rb', line 148

def insert(table_name, val_h)
  dataset(table_name).insert(val_h)
end

.run(sql) ⇒ Object



48
49
50
51
# File 'lib/ezframe/database.rb', line 48

def run(sql)
  conn = get_conn
  conn.run(sql)
end

.update(dataset, id, val_h) ⇒ Object



152
153
154
155
# 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