-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbh.mysql.coffee
126 lines (98 loc) · 3.4 KB
/
dbh.mysql.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
## Include libraries
Q = require 'q'
mysql = require 'mysql'
## Include app logger
appLogger = require '../logger'
## Include abstract handler
AbstractDatabaseHandler = require './dbh.abstract'
## Handler for interacting with MySQL databases
##
class MysqlDatabaseHandler extends AbstractDatabaseHandler
log: null
config: null
connection: null
constructor: (dbConfig, logger=appLogger) ->
@config = dbConfig
@log = logger
connect: ->
deferred = Q.defer()
## Open connection to database
@connection = mysql.createConnection
host: @config.SQL_HOST
port: @config.SQL_PORT
user: @config.SQL_USER
password: @config.SQL_PASSWORD
database: @config.SQL_DATABASE_COMMON
charset: @config.SQL_CONNECTION_CHARSET or 'UTF8MB4_GENERAL_CI'
socketPath: @config.SQL_SOCKET_PATH
@connection.connect (err) =>
if err
@log.error(err, 'Database connection')
deferred.reject(err)
else
@log.debug 'Established database connection'
deferred.resolve()
return deferred.promise
disconnect: ->
deferred = Q.defer()
if @connection.state is 'disconnected'
@connection.destroy()
else
@connection.end (err) =>
if err
@log.error(err, 'Database disconnect')
else
@log.debug 'Closed database connection'
@connection.destroy()
deferred.resolve()
return deferred.promise
#
# Database query helper routines
#
toQuery: (wildValue) ->
return mysql.escape(wildValue)
sendQuery: (sqlQuery) ->
deferred = Q.defer()
@connection.query sqlQuery, (err, resultData, fieldsMetaData) =>
if err
@log.error(err, 'Database query')
deferred.reject(err)
else
deferred.resolve(resultData)
return deferred.promise
doTransaction: (transactionRoutineFunc) ->
deferred = Q.defer()
@connection.beginTransaction (err) =>
if err
@log.error(err, 'Database transaction')
deferred.reject(err)
else
routinePromise = transactionRoutineFunc()
deferred.resolve(routinePromise)
promise = deferred.promise
promise = promise.then =>
innerDeferred = Q.defer()
@connection.commit (err) =>
if err
@log.error(err, 'Database transaction commit')
innerDeferred.reject(err)
else
innerDeferred.resolve()
return innerDeferred.promise
promise.fail =>
@connection.rollback()
return promise
readSimpleData: (sqlQuery, rejectIfEmpty=false) ->
promise = @sendQuery(sqlQuery)
promise = promise.then (resultRows) =>
resultData = resultRows[0]
if rejectIfEmpty and not resultData?
err = new Error('Result is empty')
err.isDatabaseResult = true
throw err
return resultData
return promise
readMultipleData: (sqlQuery) ->
return @sendQuery(sqlQuery)
## Export class
module.exports = MysqlDatabaseHandler