Class: OnlyofficeMysqlHelper::MySQLHelper
- Inherits:
-
Object
- Object
- OnlyofficeMysqlHelper::MySQLHelper
- 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.
'127.0.0.1'
- SQL_SERVER_USER_LOCAL =
Returns default sql user.
'root'
- SQL_SERVER_PASSWORD_LOCAL =
Returns default sql password.
''
Instance Attribute Summary collapse
-
#database ⇒ String
Database name.
Instance Method Summary collapse
-
#add_record(table_name, hash) ⇒ nil
Add hash record to table.
-
#create_table(name, columns = 'id INT PRIMARY KEY AUTO_INCREMENT') ⇒ nil
Create specific table if not exists.
-
#delete_record(table_name, condition) ⇒ nil
Delete record by condition.
-
#drop_table(table_name) ⇒ nil
Drop whole table.
-
#initialize(address: SQL_SERVER_ADDRESS_LOCAL, database: 'performance_test', user: SQL_SERVER_USER_LOCAL, password: SQL_SERVER_PASSWORD_LOCAL) ⇒ MySQLHelper
constructor
A new instance of MySQLHelper.
-
#select_records(table_name, condition = '') ⇒ Object
Select table records.
-
#tables ⇒ Object
List all tables in base.
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
#database ⇒ String
Returns 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
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
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
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
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
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 |
#tables ⇒ Object
List all tables in base
63 64 65 |
# File 'lib/onlyoffice_mysql_helper/mysql_helper.rb', line 63 def tables send_query { 'SHOW TABLES;' } end |