Class: AutoREST::MySQLDB
- Defined in:
- lib/autorest/db/mysql.rb
Overview
MySQL adapter for AutoREST.
Uses the ‘mysql2` gem to connect and interact with a MySQL database. Automatically detects tables and primary key columns.
Instance Method Summary collapse
-
#escape(input) ⇒ String
Escapes identifiers or values for safe usage in queries.
-
#exec_sql(sql) ⇒ Array<Hash>
Executes a raw SQL query.
-
#initialize(host, port, user, passwd, dbname) ⇒ MySQLDB
constructor
A new instance of MySQLDB.
-
#prepare ⇒ void
Loads table metadata including columns and primary keys.
Methods inherited from DBAdapter
#close, #columns, #del_row, #insert, #row, #rows, #set_access_tables, #tables, #update
Constructor Details
#initialize(host, port, user, passwd, dbname) ⇒ MySQLDB
Returns a new instance of MySQLDB.
25 26 27 28 |
# File 'lib/autorest/db/mysql.rb', line 25 def initialize(host, port, user, passwd, dbname) conn = Mysql2::Client.new(host: host, port: port, username: user, password: passwd, database: dbname) super(:mysql, dbname, conn) end |
Instance Method Details
#escape(input) ⇒ String
Escapes identifiers or values for safe usage in queries.
54 55 56 |
# File 'lib/autorest/db/mysql.rb', line 54 def escape(input) @db_conn.escape(input) end |
#exec_sql(sql) ⇒ Array<Hash>
Executes a raw SQL query.
47 48 49 |
# File 'lib/autorest/db/mysql.rb', line 47 def exec_sql(sql) @db_conn.query(sql).to_a end |
#prepare ⇒ void
This method returns an undefined value.
Loads table metadata including columns and primary keys.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/autorest/db/mysql.rb', line 32 def prepare @tables = {} @db_conn.query("show tables").each do |t| tname = t["Tables_in_#{@dbname}"] row_details = @db_conn.query("desc #{tname}") @tables[tname] = {} row_details.each do |row| @tables[tname][row["Field"]] = {type: row["Type"], pk: row["Key"] == "PRI"} end end end |