class RailsAdminContent::Client

Constants

Syntax
Syntax_table

Public Class Methods

compose(params) click to toggle source
# File lib/rails_admin_content/client.rb, line 41
def self.compose(params)
  "SELECT * FROM #{params[:id]} WHERE #{params["field"].inject([]){|a, (key,value)| a << compose_key(params["calc"][key],key, value); a }.join('and')}"
end
compose_key(calc,key,value) click to toggle source
# File lib/rails_admin_content/client.rb, line 45
def self.compose_key(calc,key,value)
  case calc
  when '=', '>', '<'
    " `#{key}` #{calc} '#{value}' "
  when 'IN'
    " `#{key}` IN (#{value}) "
  when '≠'
    " `#{key}`  != '#{value}' "
  when 'LIKE'
    " `#{key}` LIKE '%#{value}%' "
  when '≥'
    " `#{key}`  >= '#{value}' "
  when '≤'
    " `#{key}`  <= '#{value}' "
  end
end
compose_update_sql(table_name,fields) click to toggle source
# File lib/rails_admin_content/client.rb, line 75
def self.compose_update_sql(table_name,fields)
  columns = desc_table(table_name)
  fields.inject([]) do |array, field|
    columns.each do |column|
      if field[1].present? && field[0] == column["Field"]
        value = case column["Type"].gsub(/\(.*?\)/,'')
        when 'int', 'decimal', 'tinyint' then field[1]
        when 'datetime' then "'#{field[1]}'"
        else
          "REPLACE(REPLACE(REPLACE('#{field[1]}', '&','&amp;'), '>', '&gt;'), '<', '&lt;')"
        end
        array << "#{table_name}.`#{field[0]}` = #{value}"
      end
    end
    array
  end.join(' , ')
end
conn() click to toggle source
# File lib/rails_admin_content/client.rb, line 18
def self.conn
  @client ||= new ActiveRecord::Base.connection_config
end
delete(table_name, ids) click to toggle source
# File lib/rails_admin_content/client.rb, line 63
def self.delete(table_name, ids)
  conn.origin_query("DELETE FROM #{table_name} WHERE id IN (#{ids.join(',')})") if ids.is_a?(Array)
end
desc_table(table_name) click to toggle source
# File lib/rails_admin_content/client.rb, line 93
def self.desc_table(table_name)
  conn.origin_query("DESC #{table_name}").each
end
get_tables() click to toggle source
# File lib/rails_admin_content/client.rb, line 37
def self.get_tables
  @tables ? @tables : @tables = conn.origin_query('SHOW TABLES')
end
insert(table_name, field) click to toggle source
# File lib/rails_admin_content/client.rb, line 67
def self.insert(table_name, field)
  conn.origin_query("INSERT INTO #{table_name} (#{field.keys.join(',')}) VALUES ('#{field.values.join("','")}')")
end
limit(query_sql, page=1, stint) click to toggle source
# File lib/rails_admin_content/client.rb, line 32
def self.limit(query_sql, page=1, stint)
  page = page == 1 ? page = 0 : page.to_i*stint.to_i-stint.to_i
  query_sql.downcase.match(Syntax).present? ? query_sql.downcase.gsub(Syntax, "LIMIT #{page},#{stint}") : "#{query_sql} LIMIT #{page},#{stint}"
end
modify_table_column_default_value(table_name, column, value, field_type) click to toggle source
# File lib/rails_admin_content/client.rb, line 101
def self.modify_table_column_default_value(table_name, column, value, field_type)
  default_value = case field_type.gsub(/\(.*?\)/,'')
  when "tinyint" then value.present?
  when "datetime" then value.to_time.to_s(:db)
  when "int" then value.to_i
  else
    value.to_s
  end
  conn.origin_query("ALTER TABLE #{table_name} ALTER COLUMN #{column} SET DEFAULT '#{default_value}'")
end
query(query_sql, page=nil, stint=nil) click to toggle source
# File lib/rails_admin_content/client.rb, line 24
def self.query(query_sql, page=nil, stint=nil)
  self.table_name = query_sql.downcase.match(Syntax_table)[1] if query_sql.downcase.match(Syntax_table)
  self.total_nums = conn.origin_query(query_sql).size
  query_sql = limit(query_sql, page, stint) if stint && page
  query_sql = query_sql.gsub(/;/, '')
  conn.origin_query(query_sql)
end
select_field(type) click to toggle source
# File lib/rails_admin_content/client.rb, line 8
def self.select_field(type)
  case type.gsub(/\(.*?\)/,'')
  when "tinyint" then [['等于', '='], ['不等于', '≠']]
  when "datetime" then [['等于', '='], ['不等于', '≠'], ['大于', '>'], ['小于','<']]
  when "int" then [['等于', '='], ['不等于', '≠'], ['大于', '>'], ['小于','<'], ['含有', 'IN']]
  else
    [['等于', '='], ['不等于', '≠'], ['包含', 'LIKE']]
  end
end
show_create_sql(table_name) click to toggle source
# File lib/rails_admin_content/client.rb, line 97
def self.show_create_sql(table_name)
  conn.origin_query("SHOW CREATE TABLE #{table_name}").each
end
update(table_name, id, fields) click to toggle source
# File lib/rails_admin_content/client.rb, line 71
def self.update(table_name, id, fields)
  conn.origin_query "UPDATE #{table_name} SET #{compose_update_sql(table_name, fields.to_a)}  WHERE id = #{id}"
end