Module: Zena::DbHelper::AbstractDb
- Included in:
- Mysql, Postgresql, Sqlite3
- Defined in:
- lib/zena/db_helper/abstract_db.rb
Instance Method Summary collapse
- #adapter ⇒ Object
- #add_column(*args) ⇒ Object
- #add_unique_key(table, keys) ⇒ Object
- #change_column(*args) ⇒ Object
- #change_engine(table, engine) ⇒ Object
- #connection ⇒ Object
-
#date_condition(date_cond, field, ref_date) ⇒ Object
This is used by zafu and it’s a mess.
-
#delete(table, opts) ⇒ Object
‘DELETE’ depending on a two table query.
- #execute(*args) ⇒ Object
-
#fetch_attribute(sql) ⇒ Object
Fetch a single row of raw data from db.
- #fetch_attributes(attributes, table_name, sql) ⇒ Object
- #fetch_ids(sql, attr_name = 'id') ⇒ Object
-
#insensitive_find(klass, count, attributes) ⇒ Object
Case insensitive find.
-
#insert_dummy_ids ⇒ Object
Insert a dummy (empty) link to use when mixing queries (QueryNode) with links and without.
-
#insert_many(table, columns, values) ⇒ Object
Insert a list of values (multicolumn insert).
-
#migrated_once? ⇒ Boolean
Return true if we can load models because the database has the basic tables.
- #next_zip(site_id) ⇒ Object
-
#prepare_connection ⇒ Object
Fixes #98.
-
#quote(value) ⇒ Object
Quote value in SQL.
- #quote_column_name(column_name) ⇒ Object
- #quote_date(date) ⇒ Object
- #select_all(sql_or_array) ⇒ Object
-
#set_attribute(obj, key, value) ⇒ Object
Set a single attribute directly in the database.
-
#sql_function(function, *args) ⇒ Object
Return a string matching the SQLiss function.
- #table_options ⇒ Object
- #update(*args) ⇒ Object
- #update_value(name, opts) ⇒ Object
Instance Method Details
#adapter ⇒ Object
37 38 39 |
# File 'lib/zena/db_helper/abstract_db.rb', line 37 def adapter @adapter ||= self.to_s.downcase.split('::').last end |
#add_column(*args) ⇒ Object
4 5 6 |
# File 'lib/zena/db_helper/abstract_db.rb', line 4 def add_column(*args) connection.add_column(*args) end |
#add_unique_key(table, keys) ⇒ Object
69 70 71 |
# File 'lib/zena/db_helper/abstract_db.rb', line 69 def add_unique_key(table, keys) raise Exception.new("Database Adapter #{adapter.inspect} not supported yet (you can probably fix this).") end |
#change_column(*args) ⇒ Object
8 9 10 |
# File 'lib/zena/db_helper/abstract_db.rb', line 8 def change_column(*args) connection.change_column(*args) end |
#change_engine(table, engine) ⇒ Object
65 66 67 |
# File 'lib/zena/db_helper/abstract_db.rb', line 65 def change_engine(table, engine) # do nothing end |
#connection ⇒ Object
49 50 51 |
# File 'lib/zena/db_helper/abstract_db.rb', line 49 def connection ActiveRecord::Base.connection end |
#date_condition(date_cond, field, ref_date) ⇒ Object
This is used by zafu and it’s a mess. ref_date can be a string (‘2005-05-03’) or ruby (‘Time.now’). It should not come uncleaned from evil web.
142 143 144 |
# File 'lib/zena/db_helper/abstract_db.rb', line 142 def date_condition(date_cond, field, ref_date) raise Exception.new("Database Adapter #{adapter.inspect} not supported yet (you can probably fix this).") end |
#delete(table, opts) ⇒ Object
‘DELETE’ depending on a two table query.
74 75 76 |
# File 'lib/zena/db_helper/abstract_db.rb', line 74 def delete(table, opts) raise Exception.new("Database Adapter #{adapter.inspect} not supported yet (you can probably fix this).") end |
#execute(*args) ⇒ Object
41 42 43 |
# File 'lib/zena/db_helper/abstract_db.rb', line 41 def execute(*args) connection.execute(*args) end |
#fetch_attribute(sql) ⇒ Object
Fetch a single row of raw data from db
93 94 95 |
# File 'lib/zena/db_helper/abstract_db.rb', line 93 def fetch_attribute(sql) raise Exception.new("Database Adapter #{adapter.inspect} not supported yet (you can probably fix this).") end |
#fetch_attributes(attributes, table_name, sql) ⇒ Object
103 104 105 106 |
# File 'lib/zena/db_helper/abstract_db.rb', line 103 def fetch_attributes(attributes, table_name, sql) sql = "SELECT #{attributes.map{|a| connection.quote_column_name(a)}.join(',')} FROM #{table_name} WHERE #{sql}" connection.select_all(sql) end |
#fetch_ids(sql, attr_name = 'id') ⇒ Object
97 98 99 100 101 |
# File 'lib/zena/db_helper/abstract_db.rb', line 97 def fetch_ids(sql, attr_name='id') connection.select_all(sql, "#{name} Load").map! do |record| record[attr_name].to_i end end |
#insensitive_find(klass, count, attributes) ⇒ Object
Case insensitive find.
25 26 27 |
# File 'lib/zena/db_helper/abstract_db.rb', line 25 def insensitive_find(klass, count, attributes) klass.find(count, :conditions => attributes) end |
#insert_dummy_ids ⇒ Object
Insert a dummy (empty) link to use when mixing queries (QueryNode) with links and without.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/zena/db_helper/abstract_db.rb', line 147 def insert_dummy_ids tables = [] connection.tables.each do |table| if table =~ /^idx_/ || table == 'links' if table =~ /^idx_nodes/ next if fetch_attribute("SELECT node_id FROM #{table} WHERE node_id = 0") else next if fetch_attribute("SELECT id FROM #{table} WHERE id = 0") end # index table klass = Class.new(ActiveRecord::Base) do set_table_name table end dummy_hash = {} klass.columns.each do |col| if !col.null if col.name =~ /_id$/ dummy_hash[col.name] = 0 elsif col.name != 'id' dummy_hash[col.name] = '' end end end if dummy = klass.create(dummy_hash) tables << table if klass.column_names.include?('id') connection.execute "UPDATE #{table} SET id = 0 WHERE id = #{dummy.id}" end else raise "Could not create dummy record for table #{table}" end else next end end tables end |
#insert_many(table, columns, values) ⇒ Object
Insert a list of values (multicolumn insert). The values should be properly escaped before being passed to this method.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/zena/db_helper/abstract_db.rb', line 80 def insert_many(table, columns, values) return if values.empty? values = values.compact.uniq.map do |list| list.map {|e| quote(e)} end columns = columns.map{|c| quote_column_name(c)}.join(',') values = values.map {|value| "(#{value.join(',')})"}.join(', ') execute "INSERT INTO #{table} (#{columns}) VALUES #{values}" end |
#migrated_once? ⇒ Boolean
Return true if we can load models because the database has the basic tables.
195 196 197 |
# File 'lib/zena/db_helper/abstract_db.rb', line 195 def migrated_once? connection.tables.include?('nodes') end |
#next_zip(site_id) ⇒ Object
116 117 118 |
# File 'lib/zena/db_helper/abstract_db.rb', line 116 def next_zip(site_id) raise Exception.new("Database Adapter #{adapter.inspect} not supported yet (you can probably fix this).") end |
#prepare_connection ⇒ Object
Fixes #98
190 191 192 |
# File 'lib/zena/db_helper/abstract_db.rb', line 190 def prepare_connection # do nothing by default ? end |
#quote(value) ⇒ Object
Quote value in SQL.
20 21 22 |
# File 'lib/zena/db_helper/abstract_db.rb', line 20 def quote(value) connection.quote(value) end |
#quote_column_name(column_name) ⇒ Object
53 54 55 |
# File 'lib/zena/db_helper/abstract_db.rb', line 53 def quote_column_name(column_name) connection.quote_column_name(column_name) end |
#quote_date(date) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/zena/db_helper/abstract_db.rb', line 29 def quote_date(date) if date.kind_of?(Time) "'#{date.strftime('%Y-%m-%d %H:%M:%S')}'" else "''" end end |
#select_all(sql_or_array) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/zena/db_helper/abstract_db.rb', line 108 def select_all(sql_or_array) if sql_or_array.kind_of?(String) connection.select_all(sql_or_array) else connection.select_all(Node.send(:sanitize_sql, sql_or_array)) end end |
#set_attribute(obj, key, value) ⇒ Object
Set a single attribute directly in the database.
13 14 15 16 17 |
# File 'lib/zena/db_helper/abstract_db.rb', line 13 def set_attribute(obj, key, value) obj.write_attribute(key, value) execute "UPDATE #{obj.class.table_name} SET #{key}=#{quote(value)} WHERE id=#{obj[:id]}" obj.send(:changed_attributes).delete(key.to_s) end |
#sql_function(function, *args) ⇒ Object
Return a string matching the SQLiss function.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/zena/db_helper/abstract_db.rb', line 121 def sql_function(function, *args) return args.first unless function arg = args.first case function when 'count' "COUNT(#{arg})" when 'min' "MIN(#{arg})" when 'max' "MAX(#{arg})" when 'sum' "SUM(#{arg})" when 'coalesce' "COALESCE(#{args.join(',')})" else super end end |
#table_options ⇒ Object
57 58 59 |
# File 'lib/zena/db_helper/abstract_db.rb', line 57 def '' end |
#update(*args) ⇒ Object
45 46 47 |
# File 'lib/zena/db_helper/abstract_db.rb', line 45 def update(*args) connection.update(*args) end |
#update_value(name, opts) ⇒ Object
61 62 63 |
# File 'lib/zena/db_helper/abstract_db.rb', line 61 def update_value(name, opts) raise Exception.new("Database Adapter #{adapter.inspect} not supported yet (you can probably fix this).") end |