Class: OnlyofficeMysqlHelper::MySQLHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/onlyoffice_mysql_helper/mysql_helper.rb

Overview

Class for using mysql

Constant Summary collapse

SQL_SERVER_ADDRESS_LOCAL =

Returns default sql address.

Returns:

  • (String)

    default sql address

'127.0.0.1'
SQL_SERVER_USER_LOCAL =

Returns default sql user.

Returns:

  • (String)

    default sql user

'root'
SQL_SERVER_PASSWORD_LOCAL =

Returns default sql password.

Returns:

  • (String)

    default sql password

''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address: SQL_SERVER_ADDRESS_LOCAL, database: 'performance_test', user: SQL_SERVER_USER_LOCAL, password: SQL_SERVER_PASSWORD_LOCAL) ⇒ MySQLHelper

Returns a new instance of MySQLHelper.



17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#databaseString

Returns database name.

Returns:

  • (String)

    database name



8
9
10
# File 'lib/onlyoffice_mysql_helper/mysql_helper.rb', line 8

def database
  @database
end

Instance Method Details

#add_record(table_name, hash) ⇒ nil

Add hash record to table

Parameters:

  • table_name (String)

    to add hash

  • hash (Hash)

    to add

Returns:

  • (nil)


34
35
36
37
38
39
# 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') ⇒ nil

Create specific table if not exists

Parameters:

  • name (String)

    of table

  • columns (String) (defaults to: 'id INT PRIMARY KEY AUTO_INCREMENT')

    of table

Returns:

  • (nil)


45
46
47
# 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) ⇒ nil

Delete record by condition

Parameters:

  • table_name (String)

    of table

  • condition (String)

    to delete

Returns:

  • (nil)


71
72
73
# 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) ⇒ nil

Drop whole table

Parameters:

  • table_name (String)

    of table

Returns:

  • (nil)


78
79
80
# 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 = '') ⇒ Object

Select table records

Parameters:

  • table_name (String)

    of table

  • condition (String) (defaults to: '')

    to filter

Returns:

  • (Object)

    result of select



53
54
55
56
57
58
59
# 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

#tablesObject

List all tables in base

Returns:

  • (Object)

    table list



63
64
65
# File 'lib/onlyoffice_mysql_helper/mysql_helper.rb', line 63

def tables
  send_query { 'SHOW TABLES;' }
end