Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 39a1f11f
authored
Apr 20, 2020
by
Andy Edwards
Committed by
GitHub
Apr 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(dialects/mariadb): asyncify methods (#12131)
1 parent
1432cfd1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
91 deletions
lib/dialects/mariadb/connection-manager.js
lib/dialects/mariadb/query.js
lib/dialects/mariadb/connection-manager.js
View file @
39a1f11
...
...
@@ -2,7 +2,6 @@
const
AbstractConnectionManager
=
require
(
'../abstract/connection-manager'
);
const
SequelizeErrors
=
require
(
'../../errors'
);
const
Promise
=
require
(
'../../promise'
);
const
{
logger
}
=
require
(
'../../utils/logger'
);
const
DataTypes
=
require
(
'../../data-types'
).
mariadb
;
const
momentTz
=
require
(
'moment-timezone'
);
...
...
@@ -53,7 +52,7 @@ class ConnectionManager extends AbstractConnectionManager {
* @returns {Promise<Connection>}
* @private
*/
connect
(
config
)
{
async
connect
(
config
)
{
// Named timezone is not supported in mariadb, convert to offset
let
tzOffset
=
this
.
sequelize
.
options
.
timezone
;
tzOffset
=
/
\/
/
.
test
(
tzOffset
)
?
momentTz
.
tz
(
tzOffset
).
format
(
'Z'
)
...
...
@@ -89,50 +88,48 @@ class ConnectionManager extends AbstractConnectionManager {
}
}
return
this
.
lib
.
createConnection
(
connectionConfig
)
.
then
(
connection
=>
{
this
.
sequelize
.
options
.
databaseVersion
=
connection
.
serverVersion
();
debug
(
'connection acquired'
);
connection
.
on
(
'error'
,
error
=>
{
switch
(
error
.
code
)
{
case
'ESOCKET'
:
case
'ECONNRESET'
:
case
'EPIPE'
:
case
'PROTOCOL_CONNECTION_LOST'
:
this
.
pool
.
destroy
(
connection
);
}
});
return
connection
;
})
.
catch
(
err
=>
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
throw
new
SequelizeErrors
.
ConnectionRefusedError
(
err
);
case
'ER_ACCESS_DENIED_ERROR'
:
case
'ER_ACCESS_DENIED_NO_PASSWORD_ERROR'
:
throw
new
SequelizeErrors
.
AccessDeniedError
(
err
);
case
'ENOTFOUND'
:
throw
new
SequelizeErrors
.
HostNotFoundError
(
err
);
case
'EHOSTUNREACH'
:
case
'ENETUNREACH'
:
case
'EADDRNOTAVAIL'
:
throw
new
SequelizeErrors
.
HostNotReachableError
(
err
);
case
'EINVAL'
:
throw
new
SequelizeErrors
.
InvalidConnectionError
(
err
);
default
:
throw
new
SequelizeErrors
.
ConnectionError
(
err
);
try
{
const
connection
=
await
this
.
lib
.
createConnection
(
connectionConfig
);
this
.
sequelize
.
options
.
databaseVersion
=
connection
.
serverVersion
();
debug
(
'connection acquired'
);
connection
.
on
(
'error'
,
error
=>
{
switch
(
error
.
code
)
{
case
'ESOCKET'
:
case
'ECONNRESET'
:
case
'EPIPE'
:
case
'PROTOCOL_CONNECTION_LOST'
:
this
.
pool
.
destroy
(
connection
);
}
});
return
connection
;
}
catch
(
err
)
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
throw
new
SequelizeErrors
.
ConnectionRefusedError
(
err
);
case
'ER_ACCESS_DENIED_ERROR'
:
case
'ER_ACCESS_DENIED_NO_PASSWORD_ERROR'
:
throw
new
SequelizeErrors
.
AccessDeniedError
(
err
);
case
'ENOTFOUND'
:
throw
new
SequelizeErrors
.
HostNotFoundError
(
err
);
case
'EHOSTUNREACH'
:
case
'ENETUNREACH'
:
case
'EADDRNOTAVAIL'
:
throw
new
SequelizeErrors
.
HostNotReachableError
(
err
);
case
'EINVAL'
:
throw
new
SequelizeErrors
.
InvalidConnectionError
(
err
);
default
:
throw
new
SequelizeErrors
.
ConnectionError
(
err
);
}
}
}
disconnect
(
connection
)
{
async
disconnect
(
connection
)
{
// Don't disconnect connections with CLOSED state
if
(
!
connection
.
isValid
())
{
debug
(
'connection tried to disconnect but was already at CLOSED state'
);
return
Promise
.
resolve
()
;
return
;
}
//wrap native Promise into bluebird
return
Promise
.
resolve
(
connection
.
end
());
return
await
connection
.
end
();
}
validate
(
connection
)
{
...
...
lib/dialects/mariadb/query.js
View file @
39a1f11
...
...
@@ -4,7 +4,6 @@ const AbstractQuery = require('../abstract/query');
const
sequelizeErrors
=
require
(
'../../errors'
);
const
_
=
require
(
'lodash'
);
const
DataTypes
=
require
(
'../../data-types'
);
const
Promise
=
require
(
'../../promise'
);
const
{
logger
}
=
require
(
'../../utils/logger'
);
const
ER_DUP_ENTRY
=
1062
;
...
...
@@ -32,7 +31,7 @@ class Query extends AbstractQuery {
return
[
sql
,
bindParam
.
length
>
0
?
bindParam
:
undefined
];
}
run
(
sql
,
parameters
)
{
async
run
(
sql
,
parameters
)
{
this
.
sql
=
sql
;
const
{
connection
,
options
}
=
this
;
...
...
@@ -44,39 +43,33 @@ class Query extends AbstractQuery {
if
(
parameters
)
{
debug
(
'parameters(%j)'
,
parameters
);
}
return
Promise
.
resolve
(
connection
.
query
(
this
.
sql
,
parameters
)
.
then
(
results
=>
{
complete
();
// Log warnings if we've got them.
if
(
showWarnings
&&
results
&&
results
.
warningStatus
>
0
)
{
return
this
.
logWarnings
(
results
);
}
return
results
;
})
.
catch
(
err
=>
{
// MariaDB automatically rolls-back transactions in the event of a deadlock
if
(
options
.
transaction
&&
err
.
errno
===
1213
)
{
options
.
transaction
.
finished
=
'rollback'
;
}
let
results
;
complete
();
try
{
results
=
await
connection
.
query
(
this
.
sql
,
parameters
);
complete
();
err
.
sql
=
sql
;
err
.
parameters
=
parameters
;
throw
this
.
formatError
(
err
);
})
)
// Log warnings if we've got them.
.
then
(
results
=>
{
if
(
showWarnings
&&
results
&&
results
.
warningStatus
>
0
)
{
return
this
.
logWarnings
(
results
);
}
return
results
;
})
// Return formatted results...
.
then
(
results
=>
this
.
formatResults
(
results
));
if
(
showWarnings
&&
results
&&
results
.
warningStatus
>
0
)
{
await
this
.
logWarnings
(
results
);
}
}
catch
(
err
)
{
// MariaDB automatically rolls-back transactions in the event of a deadlock
if
(
options
.
transaction
&&
err
.
errno
===
1213
)
{
options
.
transaction
.
finished
=
'rollback'
;
}
complete
();
err
.
sql
=
sql
;
err
.
parameters
=
parameters
;
throw
this
.
formatError
(
err
);
}
if
(
showWarnings
&&
results
&&
results
.
warningStatus
>
0
)
{
await
this
.
logWarnings
(
results
);
}
return
this
.
formatResults
(
results
);
}
/**
...
...
@@ -196,32 +189,31 @@ class Query extends AbstractQuery {
}
}
logWarnings
(
results
)
{
return
this
.
run
(
'SHOW WARNINGS'
).
then
(
warningResults
=>
{
const
warningMessage
=
`MariaDB Warnings (
${
this
.
connection
.
uuid
||
'default'
}
): `
;
const
messages
=
[];
for
(
const
_warningRow
of
warningResults
)
{
if
(
_warningRow
===
undefined
||
typeof
_warningRow
[
Symbol
.
iterator
]
!==
'function'
)
{
continue
;
}
for
(
const
_warningResult
of
_warningRow
)
{
if
(
Object
.
prototype
.
hasOwnProperty
.
call
(
_warningResult
,
'Message'
))
{
messages
.
push
(
_warningResult
.
Message
);
}
else
{
for
(
const
_objectKey
of
_warningResult
.
keys
())
{
messages
.
push
(
[
_objectKey
,
_warningResult
[
_objectKey
]].
join
(
': '
));
}
async
logWarnings
(
results
)
{
const
warningResults
=
await
this
.
run
(
'SHOW WARNINGS'
);
const
warningMessage
=
`MariaDB Warnings (
${
this
.
connection
.
uuid
||
'default'
}
): `
;
const
messages
=
[];
for
(
const
_warningRow
of
warningResults
)
{
if
(
_warningRow
===
undefined
||
typeof
_warningRow
[
Symbol
.
iterator
]
!==
'function'
)
{
continue
;
}
for
(
const
_warningResult
of
_warningRow
)
{
if
(
Object
.
prototype
.
hasOwnProperty
.
call
(
_warningResult
,
'Message'
))
{
messages
.
push
(
_warningResult
.
Message
);
}
else
{
for
(
const
_objectKey
of
_warningResult
.
keys
())
{
messages
.
push
(
[
_objectKey
,
_warningResult
[
_objectKey
]].
join
(
': '
));
}
}
}
}
this
.
sequelize
.
log
(
warningMessage
+
messages
.
join
(
'; '
),
this
.
options
);
this
.
sequelize
.
log
(
warningMessage
+
messages
.
join
(
'; '
),
this
.
options
);
return
results
;
});
return
results
;
}
formatError
(
err
)
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment