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 ff57af63
authored
Jul 27, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
postgres' pools will now work correctly.
1 parent
db577ddb
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
41 deletions
lib/dialects/postgres/connector-manager.js
lib/dialects/postgres/query.js
lib/dialects/postgres/connector-manager.js
View file @
ff57af6
...
...
@@ -9,8 +9,9 @@ module.exports = (function() {
this
.
client
=
null
this
.
config
=
config
||
{}
this
.
config
.
port
=
this
.
config
.
port
||
5432
this
.
pooling
=
(
!!
this
.
config
.
poolCfg
&&
(
this
.
config
.
poolCfg
.
maxConnections
>
0
))
this
.
pooling
=
(
!!
this
.
config
.
pool
&&
(
this
.
config
.
pool
.
maxConnections
>
0
))
this
.
pg
=
this
.
config
.
native
?
require
(
pgModule
).
native
:
require
(
pgModule
)
this
.
poolIdentifier
=
null
// Better support for BigInts
// https://github.com/brianc/node-postgres/issues/166#issuecomment-9514935
...
...
@@ -18,52 +19,60 @@ module.exports = (function() {
// set pooling parameters if specified
if
(
this
.
pooling
)
{
this
.
pg
.
defaults
.
poolSize
=
this
.
config
.
pool
Cfg
.
maxConnections
this
.
pg
.
defaults
.
poolIdleTimeout
=
this
.
config
.
pool
Cfg
.
maxIdleTime
this
.
pg
.
defaults
.
poolSize
=
this
.
config
.
pool
.
maxConnections
this
.
pg
.
defaults
.
poolIdleTimeout
=
this
.
config
.
pool
.
maxIdleTime
}
this
.
disconnectTimeoutId
=
null
this
.
pendingQueries
=
0
this
.
maxConcurrentQueries
=
(
this
.
config
.
maxConcurrentQueries
||
50
)
process
.
on
(
'exit'
,
function
()
{
this
.
disconnect
()
}.
bind
(
this
))
}
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
"../connector-manager"
).
prototype
)
var
isConnecting
=
false
var
isConnected
=
false
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
ConnectorManager
.
prototype
.
endQuery
=
function
()
{
var
self
=
this
if
(
this
.
client
===
null
)
{
this
.
connect
()
if
(
!
self
.
pooling
&&
self
.
pendingQueries
===
0
)
{
setTimeout
(
function
()
{
self
.
pendingQueries
===
0
&&
self
.
disconnect
.
call
(
self
)
},
100
)
}
var
query
=
new
Query
(
this
.
client
,
this
.
sequelize
,
callee
,
options
||
{})
self
.
pendingQueries
+=
1
return
query
.
run
(
sql
)
.
success
(
function
()
{
self
.
endQuery
.
call
(
self
)
})
.
error
(
function
()
{
self
.
endQuery
.
call
(
self
)
})
}
ConnectorManager
.
prototype
.
endQuery
=
function
(
)
{
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
var
self
=
this
self
.
pendingQueries
-=
1
if
(
self
.
pendingQueries
==
0
)
{
setTimeout
(
function
()
{
self
.
pendingQueries
==
0
&&
self
.
disconnect
.
call
(
self
)
},
100
)
}
self
.
pendingQueries
++
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
connect
()
.
on
(
'error'
,
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
on
(
'success'
,
function
(
done
)
{
var
query
=
new
Query
(
self
.
client
,
self
.
sequelize
,
callee
,
options
||
{})
done
=
done
||
null
query
.
run
(
sql
,
done
)
.
success
(
function
(
results
)
{
emitter
.
emit
(
'success'
,
results
);
self
.
endQuery
.
call
(
self
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
);
self
.
endQuery
.
call
(
self
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
})
}).
run
().
complete
(
function
()
{
self
.
pendingQueries
--
})
}
ConnectorManager
.
prototype
.
connect
=
function
()
{
ConnectorManager
.
prototype
.
connect
=
function
(
callback
)
{
var
self
=
this
var
emitter
=
new
(
require
(
'events'
).
EventEmitter
)()
// in case database is slow to connect, prevent orphaning the client
if
(
this
.
isConnecting
)
{
return
emitter
.
emit
(
'success'
)
return
emitter
}
this
.
isConnecting
=
true
...
...
@@ -71,36 +80,44 @@ module.exports = (function() {
var
uri
=
this
.
sequelize
.
getQueryInterface
().
QueryGenerator
.
databaseConnectionUri
(
this
.
config
)
var
connectCallback
=
function
(
err
,
client
)
{
var
connectCallback
=
function
(
err
,
client
,
done
)
{
self
.
isConnecting
=
false
if
(
!!
err
)
{
// release the pool immediately, very important.
done
&&
done
(
err
)
if
(
err
.
code
)
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
emitter
.
emit
(
'error'
,
'Failed to authenticate for PostgresSQL. Please double check your settings.'
)
emitter
.
emit
(
'error'
,
new
Error
(
"Failed to authenticate for PostgresSQL. Please double check your settings."
)
)
break
case
'ENOTFOUND'
:
case
'EHOSTUNREACH'
:
case
'EINVAL'
:
emitter
.
emit
(
'error'
,
'Failed to find PostgresSQL server. Please double check your settings.'
)
emitter
.
emit
(
'error'
,
new
Error
(
"Failed to find PostgresSQL server. Please double check your settings."
)
)
break
default
:
emitter
.
emit
(
'error'
,
err
)
break
}
}
}
else
if
(
client
)
{
client
.
query
(
"SET TIME ZONE 'UTC'"
)
.
on
(
'end'
,
function
()
{
client
.
query
(
"SET TIME ZONE 'UTC'"
).
on
(
'end'
,
function
()
{
self
.
isConnected
=
true
this
.
client
=
client
self
.
client
=
client
emitter
.
emit
(
'success'
,
done
)
});
}
else
{
this
.
client
=
null
self
.
client
=
null
emitter
.
emit
(
'success'
,
done
)
}
}
if
(
this
.
pooling
)
{
// acquire client from pool
this
.
pg
.
connect
(
uri
,
connectCallback
)
this
.
poolIdentifier
=
this
.
pg
.
pools
.
getOrCreate
(
this
.
sequelize
.
config
)
this
.
poolIdentifier
.
connect
(
connectCallback
)
}
else
{
//create one-off client
this
.
client
=
new
this
.
pg
.
Client
(
uri
)
...
...
@@ -111,8 +128,14 @@ module.exports = (function() {
}
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
var
self
=
this
if
(
this
.
client
)
this
.
client
.
end
()
if
(
this
.
poolIdentifier
)
{
this
.
poolIdentifier
.
destroyAllNow
()
}
if
(
this
.
client
)
{
this
.
client
.
end
()
}
this
.
client
=
null
this
.
isConnecting
=
false
this
.
isConnected
=
false
...
...
lib/dialects/postgres/query.js
View file @
ff57af6
...
...
@@ -18,8 +18,9 @@ module.exports = (function() {
}
Utils
.
inherit
(
Query
,
AbstractQuery
)
Query
.
prototype
.
run
=
function
(
sql
)
{
Query
.
prototype
.
run
=
function
(
sql
,
done
)
{
this
.
sql
=
sql
var
self
=
this
if
(
this
.
options
.
logging
!==
false
)
{
this
.
options
.
logging
(
'Executing: '
+
this
.
sql
)
...
...
@@ -39,13 +40,14 @@ module.exports = (function() {
}.
bind
(
this
))
query
.
on
(
'end'
,
function
()
{
done
&&
done
()
this
.
emit
(
'sql'
,
this
.
sql
)
if
(
receivedError
)
{
return
}
onSuccess
.
call
(
this
,
rows
)
onSuccess
.
call
(
this
,
rows
,
sql
)
}.
bind
(
this
))
return
this
...
...
@@ -55,11 +57,11 @@ module.exports = (function() {
return
'id'
}
var
onSuccess
=
function
(
rows
)
{
var
onSuccess
=
function
(
rows
,
sql
)
{
var
results
=
[]
,
self
=
this
,
isTableNameQuery
=
(
this
.
sql
.
indexOf
(
'SELECT table_name FROM information_schema.tables'
)
===
0
)
,
isRelNameQuery
=
(
this
.
sql
.
indexOf
(
'SELECT relname FROM pg_class WHERE oid IN'
)
===
0
)
,
isTableNameQuery
=
(
sql
.
indexOf
(
'SELECT table_name FROM information_schema.tables'
)
===
0
)
,
isRelNameQuery
=
(
sql
.
indexOf
(
'SELECT relname FROM pg_class WHERE oid IN'
)
===
0
)
if
(
isTableNameQuery
||
isRelNameQuery
)
{
if
(
isRelNameQuery
)
{
...
...
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