class OnlyofficeMysqlHelper::MySQLHelper

Class for using mysql

Constants

SQL_SERVER_ADDRESS_LOCAL

@return [String] default sql address

SQL_SERVER_PASSWORD_LOCAL

@return [String] default sql password

SQL_SERVER_USER_LOCAL

@return [String] default sql user

Attributes

database[RW]

@return [String] database name

Public Class Methods

new(address: SQL_SERVER_ADDRESS_LOCAL, database: 'performance_test', user: SQL_SERVER_USER_LOCAL, password: SQL_SERVER_PASSWORD_LOCAL) click to toggle source
# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 17
def initialize(address: SQL_SERVER_ADDRESS_LOCAL,
               database: 'performance_test',
               user: SQL_SERVER_USER_LOCAL,
               password: SQL_SERVER_PASSWORD_LOCAL)
  port = ENV['DB_PORT'] || 3306
  @connection = Mysql2::Client.new(host: address,
                                   port: port,
                                   username: user,
                                   password: password,
                                   database: database)
  @database = database
end

Public Instance Methods

add_record(table_name, hash) click to toggle source

Add hash record to table @param [String] table_name to add hash @param [Hash] hash to add @return [nil]

# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 34
def add_record(table_name, hash)
  send_query do
    "INSERT INTO `#{table_name}` (`id`, #{from_query_keys(hash)}) "\
    "VALUES (NULL,#{from_query_values(hash)});"
  end
end
create_table(name, columns = 'id INT PRIMARY KEY AUTO_INCREMENT') click to toggle source

Create specific table if not exists @param [String] name of table @param [String] columns of table @return [nil]

# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 45
def create_table(name, columns = 'id INT PRIMARY KEY AUTO_INCREMENT')
  send_query { "CREATE TABLE IF NOT EXISTS `#{name}`(#{columns});" }
end
delete_record(table_name, condition) click to toggle source

Delete record by condition @param [String] table_name of table @param [String] condition to delete @return [nil]

# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 71
def delete_record(table_name, condition)
  send_query { "DELETE FROM `#{table_name}` WHERE #{condition};" }
end
drop_table(table_name) click to toggle source

Drop whole table @param [String] table_name of table @return [nil]

# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 78
def drop_table(table_name)
  send_query { "DROP TABLE `#{table_name}`;" }
end
select_records(table_name, condition = '') click to toggle source

Select table records @param [String] table_name of table @param [String] condition to filter @return [Object] result of select

# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 53
def select_records(table_name, condition = '')
  send_query do
    query = "SELECT * FROM `#{table_name}`"
    query += " #{condition}" unless condition == ''
    "#{query};"
  end
end
tables() click to toggle source

List all tables in base @return [Object] table list

# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 63
def tables
  send_query { 'SHOW TABLES;' }
end

Private Instance Methods

from_query_keys(hash) click to toggle source
# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 90
def from_query_keys(hash)
  query_keys = ''
  hash.each_key do |key_value|
    query_keys += "`#{key_value}`,"
  end
  query_keys.chop!
end
from_query_values(hash) click to toggle source
# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 98
def from_query_values(hash)
  query_values = ''
  hash.each_value do |value|
    query_values += "'#{value.to_s.delete("'")}',"
  end
  query_values.chop!
end
send_query() { || ... } click to toggle source
# File lib/onlyoffice_mysql_helper/mysql_helper.rb, line 84
def send_query
  yield
  OnlyofficeLoggerHelper.log("Query: #{yield}")
  @connection.query(yield)
end