index.js
2.44 KB
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
// MySQL2 Client
// -------
import inherits from 'inherits';
import Client_MySQL from '../mysql';
import Promise from 'bluebird';
import * as helpers from '../../helpers';
import { pick, map, assign } from 'lodash'
import Transaction from './transaction';
const configOptions = [
'isServer',
'stream',
'host',
'port',
'localAddress',
'socketPath',
'user',
'password',
'passwordSha1',
'database',
'connectTimeout',
'insecureAuth',
'supportBigNumbers',
'bigNumberStrings',
'decimalNumbers',
'dateStrings',
'debug',
'trace',
'stringifyObjects',
'timezone',
'flags',
'queryFormat',
'pool',
'ssl',
'multipleStatements',
'namedPlaceholders',
'typeCast',
'charsetNumber',
'compress'
];
// Always initialize with the "QueryBuilder" and "QueryCompiler"
// objects, which extend the base 'lib/query/builder' and
// 'lib/query/compiler', respectively.
function Client_MySQL2(config) {
Client_MySQL.call(this, config)
}
inherits(Client_MySQL2, Client_MySQL)
assign(Client_MySQL2.prototype, {
// The "dialect", for reference elsewhere.
driverName: 'mysql2',
transaction() {
return new Transaction(this, ...arguments)
},
_driver() {
return require('mysql2')
},
validateConnection() {
return true
},
// Get a raw connection, called by the `pool` whenever a new
// connection needs to be added to the pool.
acquireRawConnection() {
const connection = this.driver.createConnection(pick(this.connectionSettings, configOptions))
return new Promise((resolver, rejecter) => {
connection.connect((err) => {
if (err) {
return rejecter(err)
}
connection.on('error', err => {
connection.__knex__disposed = err
})
resolver(connection)
})
})
},
processResponse(obj, runner) {
const { response } = obj
const { method } = obj
const rows = response[0]
const fields = response[1]
if (obj.output) return obj.output.call(runner, rows, fields)
switch (method) {
case 'select':
case 'pluck':
case 'first': {
const resp = helpers.skim(rows)
if (method === 'pluck') return map(resp, obj.pluck)
return method === 'first' ? resp[0] : resp
}
case 'insert':
return [rows.insertId]
case 'del':
case 'update':
case 'counter':
return rows.affectedRows
default:
return response
}
}
})
export default Client_MySQL2;