Class: MIDB::DbengineModel
- Inherits:
-
Object
- Object
- MIDB::DbengineModel
- Defined in:
- lib/midb/dbengine_model.rb
Overview
This class handles engine-dependent database operations
Instance Attribute Summary collapse
-
#db ⇒ String
Name of the database.
-
#engine ⇒ Symbol
The database engine being used.
-
#host ⇒ String
Host name where the database is located.
-
#port ⇒ Fixnum
Port for the database.
-
#pwd ⇒ String
Password for the database.
-
#uname ⇒ String
Username for the database.
Instance Method Summary collapse
-
#connect ⇒ SQLite3::Database, Mysql2::Client
Connect to the specified database.
-
#extract(result, field) ⇒ String, Fixnum
Extract a field from a query, because different engines return different types (see #query).
-
#initialize ⇒ DbengineModel
constructor
Constructor - initializes the attributes with the configuration from ServerController.
-
#length(result) ⇒ Fixnum
Returns the length of a result, because different engines return diferent types (see #query).
-
#query(res, query) ⇒ Array, Hash
Perform a query to the database.
Constructor Details
#initialize ⇒ DbengineModel
Constructor - initializes the attributes with the configuration from ServerController
25 26 27 28 29 30 31 32 |
# File 'lib/midb/dbengine_model.rb', line 25 def initialize() @engine = MIDB::ServerController.config["dbengine"] @host = MIDB::ServerController.config["dbhost"] @port = MIDB::ServerController.config["dbport"] @uname = MIDB::ServerController.config["dbuser"] @pwd = MIDB::ServerController.config["dbpassword"] @db = MIDB::ServerController.db end |
Instance Attribute Details
#db ⇒ String
Returns Name of the database.
22 |
# File 'lib/midb/dbengine_model.rb', line 22 attr_accessor :engine, :host, :uname, :pwd, :port, :db |
#engine ⇒ Symbol
Returns The database engine being used.
22 23 24 |
# File 'lib/midb/dbengine_model.rb', line 22 def engine @engine end |
#host ⇒ String
Returns Host name where the database is located.
22 |
# File 'lib/midb/dbengine_model.rb', line 22 attr_accessor :engine, :host, :uname, :pwd, :port, :db |
#port ⇒ Fixnum
Returns Port for the database.
22 |
# File 'lib/midb/dbengine_model.rb', line 22 attr_accessor :engine, :host, :uname, :pwd, :port, :db |
#pwd ⇒ String
Returns Password for the database.
22 |
# File 'lib/midb/dbengine_model.rb', line 22 attr_accessor :engine, :host, :uname, :pwd, :port, :db |
#uname ⇒ String
Returns Username for the database.
22 |
# File 'lib/midb/dbengine_model.rb', line 22 attr_accessor :engine, :host, :uname, :pwd, :port, :db |
Instance Method Details
#connect ⇒ SQLite3::Database, Mysql2::Client
Connect to the specified database
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/midb/dbengine_model.rb', line 37 def connect() # Connect to an SQLite3 database if @engine == :sqlite3 sq = SQLite3::Database.open("./db/#{@db}.db") sq.results_as_hash = true return sq # Connect to a MySQL database elsif @engine == :mysql return Mysql2::Client.new(:host => @host, :username => @uname, :password => @pwd, :database => @db) end end |
#extract(result, field) ⇒ String, Fixnum
Extract a field from a query, because different engines return different types (see #query)
69 70 71 72 73 74 75 76 77 |
# File 'lib/midb/dbengine_model.rb', line 69 def extract(result, field) if @engine == :sqlite3 return result[0][field] || result[field] elsif @engine == :mysql result.each do |row| return row[field] end end end |
#length(result) ⇒ Fixnum
Returns the length of a result, because different engines return diferent types (see #query)
84 85 86 87 88 89 90 |
# File 'lib/midb/dbengine_model.rb', line 84 def length(result) if @engine == :sqlite3 return result.length elsif @engine == :mysql return result.count end end |
#query(res, query) ⇒ Array, Hash
Perform a query to the database.
55 56 57 58 59 60 61 |
# File 'lib/midb/dbengine_model.rb', line 55 def query(res, query) if @engine == :sqlite3 return res.execute(query) elsif @engine == :mysql return res.query(query) end end |