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 d25638c4
authored
Jun 12, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(connections): remove obsolete files
1 parent
af790ca8
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
0 additions
and
1134 deletions
lib/dialects/abstract/connector-manager.js
lib/dialects/mariadb/connector-manager.js
lib/dialects/mysql/connector-manager.js
lib/dialects/postgres/connector-manager.js
lib/dialects/sqlite/connector-manager.js
lib/dialects/abstract/connector-manager.js
deleted
100644 → 0
View file @
af790ca
'use strict'
;
module
.
exports
=
(
function
()
{
var
ConnectorManager
=
function
(
sequelize
,
config
)
{
throw
new
Error
(
'Define the constructor!'
);
};
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
throw
new
Error
(
'Define the query method!'
);
};
ConnectorManager
.
prototype
.
afterTransactionSetup
=
function
(
callback
)
{
callback
();
};
ConnectorManager
.
prototype
.
connect
=
function
()
{
throw
new
Error
(
'Define the connect method!'
);
};
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
throw
new
Error
(
'Define the disconnect method!'
);
};
ConnectorManager
.
prototype
.
reconnect
=
function
()
{
this
.
disconnect
();
this
.
connect
();
};
ConnectorManager
.
prototype
.
cleanup
=
function
()
{
if
(
this
.
onProcessExit
)
{
process
.
removeListener
(
'exit'
,
this
.
onProcessExit
);
}
};
return
ConnectorManager
;
})();
lib/dialects/mariadb/connector-manager.js
deleted
100644 → 0
View file @
af790ca
'use strict'
;
var
mariadb
,
Pooling
=
require
(
'generic-pool'
)
,
Query
=
require
(
'./query'
)
,
Utils
=
require
(
'../../utils'
)
,
without
=
function
(
arr
,
elem
)
{
return
arr
.
filter
(
function
(
e
)
{
return
e
!==
elem
;
});
};
module
.
exports
=
(
function
()
{
var
ConnectorManager
=
function
(
sequelize
,
config
)
{
try
{
if
(
config
.
dialectModulePath
)
{
mariadb
=
require
(
config
.
dialectModulePath
);
}
else
{
mariadb
=
require
(
'mariasql'
);
}
}
catch
(
err
)
{
console
.
log
(
'You need to install mariasql package manually'
);
}
this
.
sequelize
=
sequelize
;
this
.
client
=
null
;
this
.
config
=
config
||
{};
this
.
config
.
port
=
this
.
config
.
port
||
3306
;
this
.
disconnectTimeoutId
=
null
;
this
.
queue
=
[];
this
.
activeQueue
=
[];
this
.
maxConcurrentQueries
=
(
this
.
config
.
maxConcurrentQueries
||
50
);
this
.
poolCfg
=
Utils
.
_
.
defaults
(
this
.
config
.
pool
,
{
maxConnections
:
10
,
minConnections
:
0
,
maxIdleTime
:
1000
,
handleDisconnects
:
false
,
validate
:
function
(
client
)
{
return
client
&&
client
.
connected
;
}
});
this
.
pendingQueries
=
0
;
this
.
useReplicaton
=
!!
config
.
replication
;
this
.
useQueue
=
config
.
queue
!==
undefined
?
config
.
queue
:
true
;
var
self
=
this
;
if
(
this
.
useReplicaton
)
{
var
reads
=
0
,
writes
=
0
;
// Init configs with options from config if not present
for
(
var
i
in
config
.
replication
.
read
)
{
config
.
replication
.
read
[
i
]
=
Utils
.
_
.
defaults
(
config
.
replication
.
read
[
i
],
{
host
:
this
.
config
.
host
,
port
:
this
.
config
.
port
,
username
:
this
.
config
.
username
,
password
:
this
.
config
.
password
,
database
:
this
.
config
.
database
});
}
config
.
replication
.
write
=
Utils
.
_
.
defaults
(
config
.
replication
.
write
,
{
host
:
this
.
config
.
host
,
port
:
this
.
config
.
port
,
username
:
this
.
config
.
username
,
password
:
this
.
config
.
password
,
database
:
this
.
config
.
database
});
// I'll make my own pool, with blackjack and hookers!
this
.
pool
=
{
release
:
function
(
client
)
{
if
(
client
.
queryType
===
'read'
)
{
return
this
.
read
.
release
(
client
);
}
else
{
return
this
.
write
.
release
(
client
);
}
},
acquire
:
function
(
callback
,
priority
,
queryType
)
{
if
(
queryType
===
'SELECT'
)
{
this
.
read
.
acquire
(
callback
,
priority
);
}
else
{
this
.
write
.
acquire
(
callback
,
priority
);
}
},
drain
:
function
()
{
this
.
read
.
drain
();
this
.
write
.
drain
();
},
read
:
Pooling
.
Pool
({
name
:
'sequelize-read'
,
create
:
function
(
done
)
{
if
(
reads
>=
self
.
config
.
replication
.
read
.
length
)
{
reads
=
0
;
}
var
config
=
self
.
config
.
replication
.
read
[
reads
++
];
connect
.
call
(
self
,
function
(
err
,
connection
)
{
if
(
connection
)
{
connection
.
queryType
=
'read'
;
}
done
(
err
,
connection
);
},
config
);
},
destroy
:
function
(
client
)
{
disconnect
.
call
(
self
,
client
);
},
validate
:
self
.
poolCfg
.
validate
,
max
:
self
.
poolCfg
.
maxConnections
,
min
:
self
.
poolCfg
.
minConnections
,
idleTimeoutMillis
:
self
.
poolCfg
.
maxIdleTime
}),
write
:
Pooling
.
Pool
({
name
:
'sequelize-write'
,
create
:
function
(
done
)
{
connect
.
call
(
self
,
function
(
err
,
connection
)
{
if
(
connection
)
{
connection
.
queryType
=
'write'
;
}
done
(
err
,
connection
);
},
self
.
config
.
replication
.
write
);
},
destroy
:
function
(
client
)
{
disconnect
.
call
(
self
,
client
);
},
validate
:
self
.
poolCfg
.
validate
,
max
:
self
.
poolCfg
.
maxConnections
,
min
:
self
.
poolCfg
.
minConnections
,
idleTimeoutMillis
:
self
.
poolCfg
.
maxIdleTime
})
};
}
else
if
(
this
.
poolCfg
)
{
//the user has requested pooling, so create our connection pool
this
.
pool
=
Pooling
.
Pool
({
name
:
'sequelize-mariadb'
,
create
:
function
(
done
)
{
connect
.
call
(
self
,
done
);
},
destroy
:
function
(
client
)
{
disconnect
.
call
(
self
,
client
);
},
max
:
self
.
poolCfg
.
maxConnections
,
min
:
self
.
poolCfg
.
minConnections
,
validate
:
self
.
poolCfg
.
validate
,
idleTimeoutMillis
:
self
.
poolCfg
.
maxIdleTime
});
}
this
.
onProcessExit
=
function
()
{
//be nice & close our connections on exit
if
(
self
.
pool
)
{
self
.
pool
.
drain
();
}
else
if
(
self
.
client
)
{
disconnect
(
self
.
client
);
}
return
;
}.
bind
(
this
);
process
.
on
(
'exit'
,
this
.
onProcessExit
);
};
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
'../abstract/connector-manager'
).
prototype
);
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
var
self
=
this
;
options
=
options
||
{};
if
(
this
.
useQueue
)
{
// If queueing we'll let the execQueueItem method handle connecting
var
queueItem
=
{
query
:
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
),
sql
:
sql
};
queueItem
.
query
.
options
.
uuid
=
this
.
config
.
uuid
;
enqueue
.
call
(
this
,
queueItem
,
options
);
return
queueItem
.
query
.
promise
.
finally
(
function
()
{
afterQuery
.
call
(
self
,
queueItem
);
});
}
var
query
=
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
);
this
.
pendingQueries
++
;
query
.
options
.
uuid
=
this
.
config
.
uuid
;
return
this
.
getConnection
(
options
).
then
(
function
(
client
)
{
query
.
client
=
client
;
return
query
.
run
(
sql
).
finally
(
function
()
{
self
.
pendingQueries
--
;
if
(
self
.
pool
)
{
self
.
pool
.
release
(
query
.
client
);
}
else
{
if
(
self
.
pendingQueries
===
0
)
{
setTimeout
(
function
()
{
if
(
self
.
pendingQueries
===
0
)
{
self
.
disconnect
.
call
(
self
);
}
},
100
);
}
}
});
});
};
ConnectorManager
.
prototype
.
getConnection
=
function
(
options
)
{
var
self
=
this
;
options
=
options
||
{};
return
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
if
(
!
self
.
pool
)
{
// Regular client caching
if
(
self
.
client
)
{
return
resolve
(
self
.
client
);
}
else
{
// Cache for concurrent queries
if
(
self
.
_getConnection
)
{
return
resolve
(
self
.
_getConnection
);
}
// Set cache and acquire connection
self
.
_getConnection
=
this
;
connect
.
call
(
self
,
function
(
err
,
client
)
{
if
(
err
)
{
return
reject
(
err
);
}
// Unset caching, should now be caught by the self.client check
self
.
_getConnection
=
null
;
self
.
client
=
client
;
resolve
(
client
);
});
}
}
else
{
// Acquire from pool
self
.
pool
.
acquire
(
function
(
err
,
client
)
{
if
(
err
)
{
return
reject
(
err
);
}
resolve
(
client
);
},
options
.
priority
,
options
.
type
);
}
});
};
ConnectorManager
.
prototype
.
connect
=
function
()
{
var
self
=
this
;
// in case database is slow to connect, prevent orphaning the client
if
(
this
.
isConnecting
||
this
.
pool
)
{
return
;
}
connect
.
call
(
self
,
function
(
err
,
client
)
{
self
.
client
=
client
;
return
;
});
return
;
};
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
if
(
this
.
client
)
{
disconnect
.
call
(
this
,
this
.
client
);
}
return
;
};
// private
var
disconnect
=
function
(
client
)
{
if
(
!
client
)
{
return
;
// TODO possible orphaning of clients?
}
var
self
=
this
;
if
(
!
this
.
useQueue
)
{
this
.
client
=
null
;
client
.
end
();
return
;
}
var
intervalObj
=
null
;
var
cleanup
=
function
()
{
// make sure to let queued items be finish before calling end
if
(
self
&&
self
.
hasQueuedItems
)
{
return
;
}
client
.
end
();
if
(
self
&&
self
.
client
)
{
self
.
client
=
null
;
}
clearInterval
(
intervalObj
);
};
intervalObj
=
setInterval
(
cleanup
,
10
);
cleanup
();
};
var
connect
=
function
(
done
,
config
)
{
config
=
config
||
this
.
config
;
var
self
=
this
,
client
;
this
.
isConnecting
=
true
;
var
connectionConfig
=
{
host
:
config
.
host
,
port
:
config
.
port
,
user
:
config
.
username
,
password
:
config
.
password
,
db
:
config
.
database
,
metadata
:
true
};
if
(
config
.
dialectOptions
)
{
Object
.
keys
(
config
.
dialectOptions
).
forEach
(
function
(
key
)
{
connectionConfig
[
key
]
=
config
.
dialectOptions
[
key
];
});
}
if
(
connectionConfig
.
unixSocket
)
{
delete
connectionConfig
.
host
;
delete
connectionConfig
.
port
;
}
client
=
new
mariadb
();
client
.
connect
(
connectionConfig
);
client
.
on
(
'error'
,
function
(
err
)
{
self
.
isConnecting
=
false
;
done
(
err
);
})
.
on
(
'connect'
,
function
()
{
client
.
query
(
"SET time_zone = '+0:00'"
).
on
(
'result'
,
function
(
res
)
{
res
.
on
(
'end'
,
function
()
{
client
.
setMaxListeners
(
self
.
maxConcurrentQueries
);
self
.
isConnecting
=
false
;
if
(
config
.
pool
.
handleDisconnects
)
{
handleDisconnect
(
self
.
pool
,
client
);
}
done
(
null
,
client
);
});
});
})
.
on
(
'close'
,
function
()
{
disconnect
.
call
(
self
,
client
);
});
};
var
handleDisconnect
=
function
(
pool
,
client
)
{
client
.
on
(
'error'
,
function
(
err
)
{
if
(
err
.
code
!==
'PROTOCOL_CONNECTION_LOST'
)
{
throw
err
;
}
pool
.
destroy
(
client
);
});
};
var
enqueue
=
function
(
queueItem
,
options
)
{
options
=
options
||
{};
if
(
this
.
activeQueue
.
length
<
this
.
maxConcurrentQueries
)
{
this
.
activeQueue
.
push
(
queueItem
);
if
(
this
.
pool
)
{
var
self
=
this
;
this
.
pool
.
acquire
(
function
(
err
,
client
)
{
if
(
err
)
{
queueItem
.
query
.
reject
(
err
);
return
;
}
//we set the client here, asynchronously, when getting a pooled connection
//allowing the ConnectorManager.query method to remain synchronous
queueItem
.
query
.
client
=
client
;
queueItem
.
client
=
client
;
execQueueItem
.
call
(
self
,
queueItem
);
return
;
},
undefined
,
options
.
type
);
}
else
{
execQueueItem
.
call
(
this
,
queueItem
);
}
}
else
{
this
.
queue
.
push
(
queueItem
);
}
};
var
dequeue
=
function
(
queueItem
)
{
//return the item's connection to the pool
if
(
this
.
pool
)
{
this
.
pool
.
release
(
queueItem
.
client
);
}
this
.
activeQueue
=
without
(
this
.
activeQueue
,
queueItem
);
};
var
transferQueuedItems
=
function
(
count
)
{
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
var
queueItem
=
this
.
queue
.
shift
();
if
(
queueItem
)
{
enqueue
.
call
(
this
,
queueItem
);
}
}
};
var
afterQuery
=
function
(
queueItem
)
{
dequeue
.
call
(
this
,
queueItem
);
transferQueuedItems
.
call
(
this
,
this
.
maxConcurrentQueries
-
this
.
activeQueue
.
length
);
disconnectIfNoConnections
.
call
(
this
);
};
var
execQueueItem
=
function
(
queueItem
)
{
queueItem
.
query
.
run
(
queueItem
.
sql
,
queueItem
.
client
);
};
ConnectorManager
.
prototype
.
__defineGetter__
(
'hasQueuedItems'
,
function
()
{
return
(
this
.
queue
.
length
>
0
)
||
(
this
.
activeQueue
.
length
>
0
)
||
(
this
.
client
&&
this
.
client
.
_queue
&&
(
this
.
client
.
_queue
.
length
>
0
));
});
// legacy
ConnectorManager
.
prototype
.
__defineGetter__
(
'hasNoConnections'
,
function
()
{
return
!
this
.
hasQueuedItems
;
});
ConnectorManager
.
prototype
.
__defineGetter__
(
'isConnected'
,
function
()
{
return
this
.
client
!==
null
;
});
var
disconnectIfNoConnections
=
function
()
{
var
self
=
this
;
this
.
disconnectTimeoutId
&&
clearTimeout
(
this
.
disconnectTimeoutId
);
this
.
disconnectTimeoutId
=
setTimeout
(
function
()
{
self
.
isConnected
&&
!
self
.
hasQueuedItems
&&
self
.
disconnect
();
},
100
);
};
return
ConnectorManager
;
})();
lib/dialects/mysql/connector-manager.js
deleted
100644 → 0
View file @
af790ca
'use strict'
;
var
mysql
,
Pooling
=
require
(
'generic-pool'
)
,
Query
=
require
(
'./query'
)
,
Utils
=
require
(
'../../utils'
)
,
without
=
function
(
arr
,
elem
)
{
return
arr
.
filter
(
function
(
e
)
{
return
e
.
query
.
uuid
!==
elem
.
query
.
uuid
;
});
};
module
.
exports
=
(
function
()
{
var
ConnectorManager
=
function
(
sequelize
,
config
)
{
try
{
if
(
config
.
dialectModulePath
)
{
mysql
=
require
(
config
.
dialectModulePath
);
}
else
{
mysql
=
require
(
'mysql'
);
}
}
catch
(
err
)
{
throw
new
Error
(
'Please install mysql package manually'
);
}
this
.
sequelize
=
sequelize
;
this
.
client
=
null
;
this
.
config
=
config
||
{};
this
.
config
.
port
=
this
.
config
.
port
||
3306
;
this
.
disconnectTimeoutId
=
null
;
this
.
queue
=
[];
this
.
activeQueue
=
[];
this
.
maxConcurrentQueries
=
(
this
.
config
.
maxConcurrentQueries
||
50
);
this
.
poolCfg
=
Utils
.
_
.
defaults
(
this
.
config
.
pool
,
{
maxConnections
:
10
,
minConnections
:
0
,
maxIdleTime
:
1000
,
handleDisconnects
:
false
,
validate
:
validateConnection
});
this
.
pendingQueries
=
0
;
this
.
useReplicaton
=
!!
config
.
replication
;
this
.
useQueue
=
config
.
queue
!==
undefined
?
config
.
queue
:
true
;
var
self
=
this
;
if
(
this
.
useReplicaton
)
{
var
reads
=
0
,
writes
=
0
;
// Init configs with options from config if not present
for
(
var
i
in
config
.
replication
.
read
)
{
config
.
replication
.
read
[
i
]
=
Utils
.
_
.
defaults
(
config
.
replication
.
read
[
i
],
{
host
:
this
.
config
.
host
,
port
:
this
.
config
.
port
,
username
:
this
.
config
.
username
,
password
:
this
.
config
.
password
,
database
:
this
.
config
.
database
});
}
config
.
replication
.
write
=
Utils
.
_
.
defaults
(
config
.
replication
.
write
,
{
host
:
this
.
config
.
host
,
port
:
this
.
config
.
port
,
username
:
this
.
config
.
username
,
password
:
this
.
config
.
password
,
database
:
this
.
config
.
database
});
// I'll make my own pool, with blackjack and hookers!
this
.
pool
=
{
release
:
function
(
client
)
{
if
(
client
.
queryType
===
'read'
)
{
return
this
.
read
.
release
(
client
);
}
else
{
return
this
.
write
.
release
(
client
);
}
},
acquire
:
function
(
callback
,
priority
,
queryType
)
{
if
(
queryType
===
'SELECT'
)
{
this
.
read
.
acquire
(
callback
,
priority
);
}
else
{
this
.
write
.
acquire
(
callback
,
priority
);
}
},
drain
:
function
()
{
this
.
read
.
drain
();
this
.
write
.
drain
();
},
read
:
Pooling
.
Pool
({
name
:
'sequelize-read'
,
create
:
function
(
done
)
{
if
(
reads
>=
self
.
config
.
replication
.
read
.
length
)
{
reads
=
0
;
}
var
config
=
self
.
config
.
replication
.
read
[
reads
++
];
connect
.
call
(
self
,
function
(
err
,
connection
)
{
if
(
connection
)
{
connection
.
queryType
=
'read'
;
}
done
(
err
,
connection
);
},
config
);
},
destroy
:
function
(
client
)
{
disconnect
.
call
(
self
,
client
);
},
validate
:
self
.
poolCfg
.
validate
,
max
:
self
.
poolCfg
.
maxConnections
,
min
:
self
.
poolCfg
.
minConnections
,
idleTimeoutMillis
:
self
.
poolCfg
.
maxIdleTime
}),
write
:
Pooling
.
Pool
({
name
:
'sequelize-write'
,
create
:
function
(
done
)
{
connect
.
call
(
self
,
function
(
err
,
connection
)
{
if
(
connection
)
{
connection
.
queryType
=
'write'
;
}
done
(
err
,
connection
);
},
self
.
config
.
replication
.
write
);
},
destroy
:
function
(
client
)
{
disconnect
.
call
(
self
,
client
);
},
validate
:
self
.
poolCfg
.
validate
,
max
:
self
.
poolCfg
.
maxConnections
,
min
:
self
.
poolCfg
.
minConnections
,
idleTimeoutMillis
:
self
.
poolCfg
.
maxIdleTime
})
};
}
else
if
(
this
.
poolCfg
)
{
//the user has requested pooling, so create our connection pool
this
.
pool
=
Pooling
.
Pool
({
name
:
'sequelize-mysql'
,
create
:
function
(
done
)
{
connect
.
call
(
self
,
function
(
err
,
connection
)
{
// This has to be nested for some reason, else the error won't propagate correctly
done
(
err
,
connection
);
});
},
destroy
:
function
(
client
)
{
disconnect
.
call
(
self
,
client
);
},
max
:
self
.
poolCfg
.
maxConnections
,
min
:
self
.
poolCfg
.
minConnections
,
validate
:
self
.
poolCfg
.
validate
,
idleTimeoutMillis
:
self
.
poolCfg
.
maxIdleTime
});
}
this
.
onProcessExit
=
function
()
{
//be nice & close our connections on exit
if
(
self
.
pool
)
{
self
.
pool
.
drain
();
}
else
if
(
self
.
client
)
{
disconnect
(
self
.
client
);
}
return
;
}.
bind
(
this
);
process
.
on
(
'exit'
,
this
.
onProcessExit
);
};
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
'../abstract/connector-manager'
).
prototype
);
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
var
self
=
this
;
options
=
options
||
{};
if
(
this
.
useQueue
)
{
// If queueing we'll let the execQueueItem method handle connecting
var
queueItem
=
{
query
:
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
),
sql
:
sql
};
queueItem
.
query
.
options
.
uuid
=
this
.
config
.
uuid
;
enqueue
.
call
(
this
,
queueItem
,
options
);
return
queueItem
.
query
.
promise
.
finally
(
function
()
{
afterQuery
.
call
(
self
,
queueItem
);
});
}
var
query
=
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
);
this
.
pendingQueries
++
;
query
.
options
.
uuid
=
this
.
config
.
uuid
;
return
this
.
getConnection
(
options
).
then
(
function
(
client
)
{
query
.
client
=
client
;
return
query
.
run
(
sql
).
finally
(
function
()
{
self
.
pendingQueries
--
;
if
(
self
.
pool
)
{
self
.
pool
.
release
(
query
.
client
);
}
else
{
if
(
self
.
pendingQueries
===
0
)
{
setTimeout
(
function
()
{
if
(
self
.
pendingQueries
===
0
)
{
self
.
disconnect
.
call
(
self
);
}
},
100
);
}
}
});
});
};
ConnectorManager
.
prototype
.
getConnection
=
function
(
options
)
{
var
self
=
this
;
options
=
options
||
{};
return
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
if
(
!
self
.
pool
)
{
// Regular client caching
if
(
self
.
client
)
{
return
resolve
(
self
.
client
);
}
else
{
// Cache for concurrent queries
if
(
self
.
_getConnection
)
{
return
resolve
(
self
.
_getConnection
);
}
// Set cache and acquire connection
self
.
_getConnection
=
this
;
connect
.
call
(
self
,
function
(
err
,
client
)
{
if
(
err
)
{
return
reject
(
err
);
}
// Unset caching, should now be caught by the self.client check
self
.
_getConnection
=
null
;
self
.
client
=
client
;
resolve
(
client
);
});
}
}
if
(
self
.
pool
)
{
// Acquire from pool
self
.
pool
.
acquire
(
function
(
err
,
client
)
{
if
(
err
)
{
return
reject
(
err
);
}
resolve
(
client
);
},
options
.
priority
,
options
.
type
);
}
});
};
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
if
(
this
.
client
)
{
disconnect
.
call
(
this
,
this
.
client
);
}
return
;
};
// private
var
disconnect
=
function
(
client
)
{
var
self
=
this
;
this
.
client
=
null
;
if
(
!
client
)
{
return
;
// TODO possible orphaning of clients?
}
client
.
end
(
function
()
{
if
(
!
self
.
useQueue
)
{
return
client
.
destroy
();
}
var
intervalObj
=
null
;
var
cleanup
=
function
()
{
// make sure to let client finish before calling destroy
if
(
client
.
_queue
&&
(
client
.
_queue
.
length
>
0
))
{
return
;
}
// needed to prevent mysql connection leak
client
.
destroy
();
clearInterval
(
intervalObj
);
};
intervalObj
=
setInterval
(
cleanup
,
10
);
cleanup
();
return
;
});
};
var
connect
=
function
(
done
,
config
)
{
config
=
config
||
this
.
config
;
var
connectionConfig
=
{
host
:
config
.
host
,
port
:
config
.
port
,
user
:
config
.
username
,
password
:
config
.
password
,
database
:
config
.
database
,
timezone
:
'Z'
};
if
(
config
.
dialectOptions
)
{
Object
.
keys
(
config
.
dialectOptions
).
forEach
(
function
(
key
)
{
connectionConfig
[
key
]
=
config
.
dialectOptions
[
key
];
});
}
var
connection
=
mysql
.
createConnection
(
connectionConfig
);
connection
.
connect
(
function
(
err
)
{
if
(
err
)
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
case
'ER_ACCESS_D2ENIED_ERROR'
:
done
(
'Failed to authenticate for MySQL. Please double check your settings.'
);
break
;
case
'ENOTFOUND'
:
case
'EHOSTUNREACH'
:
case
'EINVAL'
:
done
(
'Failed to find MySQL server. Please double check your settings.'
);
break
;
default
:
done
(
err
);
break
;
}
return
;
}
done
(
null
,
connection
);
});
connection
.
query
(
"SET time_zone = '+0:00'"
);
// client.setMaxListeners(self.maxConcurrentQueries)
this
.
isConnecting
=
false
;
if
(
config
.
pool
!==
null
&&
config
.
pool
.
handleDisconnects
)
{
handleDisconnect
(
this
.
pool
,
connection
);
}
};
var
handleDisconnect
=
function
(
pool
,
client
)
{
client
.
on
(
'error'
,
function
(
err
)
{
if
(
err
.
code
!==
'PROTOCOL_CONNECTION_LOST'
)
{
throw
err
;
}
pool
.
destroy
(
client
);
});
};
var
validateConnection
=
function
(
client
)
{
return
client
&&
client
.
state
!==
'disconnected'
;
};
var
enqueue
=
function
(
queueItem
,
options
)
{
options
=
options
||
{};
if
(
this
.
activeQueue
.
length
<
this
.
maxConcurrentQueries
)
{
this
.
activeQueue
.
push
(
queueItem
);
execQueueItem
.
call
(
this
,
queueItem
);
}
else
{
this
.
queue
.
push
(
queueItem
);
}
};
var
dequeue
=
function
(
queueItem
)
{
//return the item's connection to the pool
if
(
this
.
pool
)
{
this
.
pool
.
release
(
queueItem
.
client
);
}
this
.
activeQueue
=
without
(
this
.
activeQueue
,
queueItem
);
};
var
transferQueuedItems
=
function
(
count
)
{
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
var
queueItem
=
this
.
queue
.
shift
();
if
(
queueItem
)
{
enqueue
.
call
(
this
,
queueItem
);
}
}
};
var
afterQuery
=
function
(
queueItem
)
{
dequeue
.
call
(
this
,
queueItem
);
transferQueuedItems
.
call
(
this
,
this
.
maxConcurrentQueries
-
this
.
activeQueue
.
length
);
disconnectIfNoConnections
.
call
(
this
);
};
var
execQueueItem
=
function
(
queueItem
)
{
this
.
getConnection
({
priority
:
queueItem
.
query
.
options
.
priority
,
type
:
queueItem
.
query
.
options
.
type
}).
then
(
function
(
connection
)
{
queueItem
.
query
.
client
=
connection
;
queueItem
.
client
=
connection
;
queueItem
.
query
.
run
(
queueItem
.
sql
);
},
function
(
err
)
{
queueItem
.
query
.
reject
(
err
);
});
};
ConnectorManager
.
prototype
.
__defineGetter__
(
'hasQueuedItems'
,
function
()
{
return
(
this
.
queue
.
length
>
0
)
||
(
this
.
activeQueue
.
length
>
0
)
||
(
this
.
client
&&
this
.
client
.
_queue
&&
(
this
.
client
.
_queue
.
length
>
0
));
});
// legacy
ConnectorManager
.
prototype
.
__defineGetter__
(
'hasNoConnections'
,
function
()
{
return
!
this
.
hasQueuedItems
;
});
ConnectorManager
.
prototype
.
__defineGetter__
(
'isConnected'
,
function
()
{
return
this
.
client
!==
null
;
});
var
disconnectIfNoConnections
=
function
()
{
var
self
=
this
;
this
.
disconnectTimeoutId
&&
clearTimeout
(
this
.
disconnectTimeoutId
);
this
.
disconnectTimeoutId
=
setTimeout
(
function
()
{
self
.
isConnected
&&
!
self
.
hasQueuedItems
&&
self
.
disconnect
();
},
100
);
};
return
ConnectorManager
;
})();
lib/dialects/postgres/connector-manager.js
deleted
100644 → 0
View file @
af790ca
'use strict'
;
var
Query
=
require
(
'./query'
)
,
Utils
=
require
(
'../../utils'
);
module
.
exports
=
(
function
()
{
var
ConnectorManager
=
function
(
sequelize
,
config
)
{
var
pgModule
=
config
.
dialectModulePath
||
'pg'
;
this
.
sequelize
=
sequelize
;
this
.
client
=
null
;
this
.
config
=
config
||
{};
this
.
config
.
port
=
this
.
config
.
port
||
5432
;
this
.
pooling
=
(
!!
this
.
config
.
pool
&&
(
this
.
config
.
pool
.
maxConnections
>
0
));
this
.
pg
=
this
.
config
.
native
?
require
(
pgModule
).
native
:
require
(
pgModule
);
// Better support for BigInts
// https://github.com/brianc/node-postgres/issues/166#issuecomment-9514935
this
.
pg
.
types
.
setTypeParser
(
20
,
String
);
this
.
disconnectTimeoutId
=
null
;
this
.
pendingQueries
=
0
;
this
.
clientDrained
=
true
;
this
.
maxConcurrentQueries
=
(
this
.
config
.
maxConcurrentQueries
||
50
);
this
.
ConnectionParameters
=
require
(
pgModule
+
'/lib/connection-parameters'
);
this
.
onProcessExit
=
function
()
{
this
.
disconnect
();
}.
bind
(
this
);
process
.
on
(
'exit'
,
this
.
onProcessExit
);
};
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
'../abstract/connector-manager'
).
prototype
);
ConnectorManager
.
prototype
.
endQuery
=
function
()
{
var
self
=
this
;
self
.
pendingQueries
--
;
if
(
!
self
.
pooling
&&
self
.
pendingQueries
===
0
)
{
setTimeout
(
function
()
{
self
.
pendingQueries
===
0
&&
self
.
disconnect
.
call
(
self
);
},
100
);
}
};
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
var
self
=
this
;
self
.
pendingQueries
++
;
self
.
clientDrained
=
false
;
return
self
.
connect
().
then
(
function
(
done
)
{
var
query
=
new
Query
(
self
.
client
,
self
.
sequelize
,
callee
,
options
||
{});
// We return the query regardless of error or success in the query
return
query
.
run
(
sql
).
finally
(
function
()
{
self
.
endQuery
.
call
(
self
);
done
&&
done
();
});
});
};
ConnectorManager
.
prototype
.
afterTransactionSetup
=
function
(
callback
)
{
this
.
setTimezone
(
this
.
client
,
'UTC'
,
callback
);
};
ConnectorManager
.
prototype
.
connect
=
function
(
callback
)
{
var
self
=
this
;
return
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
// in case database is slow to connect, prevent orphaning the client
// TODO: We really need some sort of queue/flush/drain mechanism
if
(
this
.
isConnecting
&&
!
this
.
pooling
&&
this
.
client
===
null
)
{
return
resolve
();
}
this
.
isConnecting
=
true
;
this
.
isConnected
=
false
;
var
uri
=
this
.
sequelize
.
getQueryInterface
().
QueryGenerator
.
databaseConnectionUri
(
this
.
config
)
,
config
=
new
this
.
ConnectionParameters
(
uri
);
// set pooling parameters if specified
if
(
this
.
pooling
)
{
config
.
poolSize
=
this
.
config
.
pool
.
maxConnections
||
10
;
config
.
poolIdleTimeout
=
this
.
config
.
pool
.
maxIdleTime
||
30000
;
config
.
reapIntervalMillis
=
this
.
config
.
pool
.
reapInterval
||
1000
;
config
.
uuid
=
this
.
config
.
uuid
;
}
var
connectCallback
=
function
(
err
,
client
,
done
)
{
var
timezoneCallback
=
function
()
{
self
.
isConnected
=
true
;
self
.
client
=
client
;
resolve
(
done
);
};
self
.
isConnecting
=
false
;
if
(
!!
err
)
{
// release the pool immediately, very important.
done
&&
done
(
err
);
self
.
client
=
null
;
if
(
err
.
code
)
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
reject
(
new
Error
(
'Failed to authenticate for PostgresSQL. Please double check your settings.'
));
break
;
case
'ENOTFOUND'
:
case
'EHOSTUNREACH'
:
case
'EINVAL'
:
reject
(
new
Error
(
'Failed to find PostgresSQL server. Please double check your settings.'
));
break
;
default
:
reject
(
err
);
break
;
}
}
else
{
reject
(
new
Error
(
err
.
message
));
}
}
else
if
(
client
)
{
if
(
self
.
config
.
keepDefaultTimezone
)
{
timezoneCallback
();
}
else
{
self
.
setTimezone
(
client
,
'UTC'
,
timezoneCallback
);
}
}
else
if
(
self
.
config
.
native
)
{
if
(
self
.
config
.
keepDefaultTimezone
)
{
timezoneCallback
();
}
else
{
self
.
setTimezone
(
self
.
client
,
'UTC'
,
timezoneCallback
);
}
}
else
{
done
&&
done
();
self
.
client
=
null
;
resolve
();
}
};
if
(
this
.
pooling
)
{
// acquire client from pool
this
.
pg
.
connect
(
config
,
connectCallback
);
}
else
{
if
(
!!
this
.
client
)
{
connectCallback
(
null
,
this
.
client
);
}
else
{
//create one-off client
var
responded
=
false
;
this
.
client
=
new
this
.
pg
.
Client
(
config
);
this
.
client
.
connect
(
function
(
err
,
client
,
done
)
{
responded
=
true
;
connectCallback
(
err
,
client
||
self
.
client
,
done
);
});
// If we didn't ever hear from the client.connect() callback the connection timeout, node-postgres does not treat this as an error since no active query was ever emitted
this
.
client
.
on
(
'end'
,
function
()
{
if
(
!
responded
)
{
connectCallback
(
new
Error
(
'Connection timed out'
));
}
});
// Closes a client correctly even if we have backed up queries
// https://github.com/brianc/node-postgres/pull/346
this
.
client
.
on
(
'drain'
,
function
()
{
self
.
clientDrained
=
true
;
});
}
}
}.
bind
(
this
));
};
ConnectorManager
.
prototype
.
setTimezone
=
function
(
client
,
timezone
,
callback
)
{
client
.
query
(
"SET TIME ZONE '"
+
(
timezone
||
'UTC'
)
+
"'"
).
on
(
'error'
,
function
(
err
)
{
callback
(
err
);
}).
on
(
'end'
,
function
()
{
callback
();
});
};
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
if
(
this
.
client
)
{
if
(
this
.
clientDrained
)
{
this
.
client
.
end
();
}
this
.
client
=
null
;
}
this
.
isConnecting
=
false
;
this
.
isConnected
=
false
;
};
return
ConnectorManager
;
})();
lib/dialects/sqlite/connector-manager.js
deleted
100644 → 0
View file @
af790ca
'use strict'
;
var
sqlite3
,
Utils
=
require
(
'../../utils'
)
,
Query
=
require
(
'./query'
);
module
.
exports
=
(
function
()
{
var
ConnectorManager
=
function
(
sequelize
,
config
)
{
this
.
sequelize
=
sequelize
;
this
.
config
=
config
;
if
(
config
.
dialectModulePath
)
{
sqlite3
=
require
(
config
.
dialectModulePath
).
verbose
();
}
else
{
sqlite3
=
require
(
'sqlite3'
).
verbose
();
}
};
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
'../abstract/connector-manager'
).
prototype
);
ConnectorManager
.
prototype
.
connect
=
function
()
{
var
emitter
=
new
(
require
(
'events'
).
EventEmitter
)()
,
self
=
this
,
db
;
this
.
database
=
db
=
new
sqlite3
.
Database
(
self
.
sequelize
.
options
.
storage
||
':memory:'
,
function
(
err
)
{
if
(
err
)
{
if
(
err
.
code
===
'SQLITE_CANTOPEN'
)
{
emitter
.
emit
(
'error'
,
'Failed to find SQL server. Please double check your settings.'
);
}
}
if
(
!
err
&&
self
.
sequelize
.
options
.
foreignKeys
!==
false
)
{
// Make it possible to define and use foreign key constraints unless
// explicitly disallowed. It's still opt-in per relation
db
.
run
(
'PRAGMA FOREIGN_KEYS=ON'
);
}
});
};
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
if
(
!
this
.
database
)
{
this
.
connect
();
}
return
new
Query
(
this
.
database
,
this
.
sequelize
,
callee
,
options
).
run
(
sql
);
};
return
ConnectorManager
;
})();
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