Class: MIDB::API::Dbengine
- Inherits:
-
Object
- Object
- MIDB::API::Dbengine
- 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(cnf, db) ⇒ Dbengine
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(cnf, db) ⇒ Dbengine
Constructor - initializes the attributes with the configuration from ServerController
28 29 30 31 32 33 34 35 |
# File 'lib/midb/dbengine_model.rb', line 28 def initialize(cnf, db) @engine = cnf["dbengine"] @host = cnf["dbhost"] @port = cnf["dbport"] @uname = cnf["dbuser"] @pwd = cnf["dbpassword"] @db = 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/midb/dbengine_model.rb', line 40 def connect() begin # 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 rescue MIDB::Interface::Errors.exception(:database_error, $!) return false end end |
#extract(result, field) ⇒ String, Fixnum
Extract a field from a query, because different engines return different types (see #query)
82 83 84 85 86 87 88 89 90 |
# File 'lib/midb/dbengine_model.rb', line 82 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)
97 98 99 100 101 102 103 |
# File 'lib/midb/dbengine_model.rb', line 97 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.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/midb/dbengine_model.rb', line 63 def query(res, query) begin if @engine == :sqlite3 return res.execute(query) elsif @engine == :mysql return res.query(query) end rescue MIDB::Interface::Errors.exception(:query_error, $!) return false end end |