1
|
|
|
require 'sqlite3' |
2
|
|
|
require 'mysql2' |
3
|
|
|
require 'midb/errors_view' |
4
|
|
|
module MIDB |
5
|
|
|
module API |
6
|
|
|
# @author unrar |
7
|
|
|
# This class handles engine-dependent database operations |
8
|
|
|
class Dbengine |
9
|
|
|
|
10
|
|
|
# @!attribute engine |
11
|
|
|
# @return [Symbol] The database engine being used |
12
|
|
|
# @!attribute host |
13
|
|
|
# @return [String] Host name where the database is located |
14
|
|
|
# @!attribute uname |
15
|
|
|
# @return [String] Username for the database |
16
|
|
|
# @!attribute pwd |
17
|
|
|
# @return [String] Password for the database. |
18
|
|
|
# @!attribute port |
19
|
|
|
# @return [Fixnum] Port for the database |
20
|
|
|
# @!attribute db |
21
|
|
|
# @return [String] Name of the database |
22
|
|
|
attr_accessor :engine, :host, :uname, :pwd, :port, :db |
23
|
|
|
|
24
|
|
|
# Constructor - initializes the attributes with the configuration from ServerController |
25
|
|
|
# |
26
|
|
|
# @param cnf [Array<String>] The configuration array. |
27
|
|
|
# @param db [String] Database name. |
28
|
|
|
def initialize(cnf, db) |
29
|
|
|
@engine = cnf["dbengine"] |
30
|
|
|
@host = cnf["dbhost"] |
31
|
|
|
@port = cnf["dbport"] |
32
|
|
|
@uname = cnf["dbuser"] |
33
|
|
|
@pwd = cnf["dbpassword"] |
34
|
|
|
@db = db |
35
|
|
|
end |
36
|
|
|
|
37
|
|
|
# Connect to the specified database |
38
|
|
|
# |
39
|
|
|
# @return [SQLite3::Database, Mysql2::Client] A resource referencing to the database |
40
|
|
|
def connect() |
41
|
|
|
begin |
42
|
|
|
# Connect to an SQLite3 database |
43
|
|
|
if @engine == :sqlite3 |
44
|
|
|
sq = SQLite3::Database.open("./db/#{@db}.db") |
45
|
|
|
sq.results_as_hash = true |
46
|
|
|
return sq |
47
|
|
|
# Connect to a MySQL database |
48
|
|
|
elsif @engine == :mysql |
49
|
|
|
return Mysql2::Client.new(:host => @host, :username => @uname, :password => @pwd, :database => @db) |
50
|
|
|
end |
51
|
|
|
rescue |
52
|
|
|
MIDB::Interface::Errors.exception(:database_error, $!) |
53
|
|
|
return false |
54
|
|
|
end |
55
|
|
|
end |
56
|
|
|
|
57
|
|
|
# Perform a query to the database. |
58
|
|
|
# |
59
|
|
|
# @param res [SQLite3::Database, Mysql2::Client] An existing database resource. |
60
|
|
|
# @param query [String] The SQL query to be ran. |
61
|
|
|
# |
62
|
|
|
# @return [Array, Hash] Returns an array of hashes for SQLite3 or a hash for MySQL |
63
|
|
|
def query(res, query) |
64
|
|
|
begin |
65
|
|
|
if @engine == :sqlite3 |
66
|
|
|
return res.execute(query) |
67
|
|
|
elsif @engine == :mysql |
68
|
|
|
return res.query(query) |
69
|
|
|
end |
70
|
|
|
rescue |
71
|
|
|
MIDB::Interface::Errors.exception(:query_error, $!) |
72
|
|
|
return false |
73
|
|
|
end |
74
|
|
|
end |
75
|
|
|
|
76
|
|
|
# Extract a field from a query, because different engines return different types (see #query) |
77
|
|
|
# |
78
|
|
|
# @param result [Array, Hash] The result of a query obtained via #query |
79
|
|
|
# @param field [String] The name of the field to be extracted. |
80
|
|
|
# |
81
|
|
|
# @return [String, Fixnum] The field extracted from a query |
82
|
|
|
def extract(result, field) |
83
|
|
|
if @engine == :sqlite3 |
84
|
|
|
return result[0][field] || result[field] |
85
|
|
|
elsif @engine == :mysql |
86
|
|
|
result.each do |row| |
87
|
|
|
return row[field] |
88
|
|
|
end |
89
|
|
|
end |
90
|
|
|
end |
91
|
|
|
|
92
|
|
|
# Returns the length of a result, because different engines return diferent types (see #query) |
93
|
|
|
# |
94
|
|
|
# @param result [Array, Hash] The result of a query obtained via #query |
95
|
|
|
# |
96
|
|
|
# @return [Fixnum] Length of the result. |
97
|
|
|
def length(result) |
98
|
|
|
if @engine == :sqlite3 |
99
|
|
|
return result.length |
100
|
|
|
elsif @engine == :mysql |
101
|
|
|
return result.count |
102
|
|
|
end |
103
|
|
|
end |
104
|
|
|
end |
105
|
|
|
end |
106
|
|
|
end |