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 fd22bf1c
authored
Oct 02, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Last fixes to mariadb, and fix import with relative paths on windows
1 parent
9a0c7d4b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
131 additions
and
63 deletions
lib/dialects/mariadb/connector-manager.js
lib/sequelize.js
test/config/config.js
test/support.js
lib/dialects/mariadb/connector-manager.js
View file @
fd22bf1
var
maria
sql
var
maria
db
,
Pooling
=
require
(
'generic-pool'
)
,
Query
=
require
(
"./query"
)
,
Utils
=
require
(
"../../utils"
)
,
without
=
function
(
arr
,
elem
)
{
return
arr
.
filter
(
function
(
e
)
{
return
e
!=
elem
})
}
try
{
mariasql
=
require
(
"mariasql"
)
}
catch
(
err
)
{
console
.
log
(
"You need to install mariasql package manually"
);
}
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
=
[]
...
...
@@ -19,7 +27,9 @@ module.exports = (function() {
this
.
poolCfg
=
Utils
.
_
.
defaults
(
this
.
config
.
pool
,
{
maxConnections
:
10
,
minConnections
:
0
,
maxIdleTime
:
1000
maxIdleTime
:
1000
,
handleDisconnects
:
false
,
validate
:
validateConnection
});
this
.
pendingQueries
=
0
;
this
.
useReplicaton
=
!!
config
.
replication
;
...
...
@@ -38,8 +48,7 @@ module.exports = (function() {
port
:
this
.
config
.
port
,
username
:
this
.
config
.
username
,
password
:
this
.
config
.
password
,
db
:
this
.
config
.
database
,
ssl
:
this
.
config
.
ssl
database
:
this
.
config
.
database
});
}
config
.
replication
.
write
=
Utils
.
_
.
defaults
(
config
.
replication
.
write
,
{
...
...
@@ -47,8 +56,7 @@ module.exports = (function() {
port
:
this
.
config
.
port
,
username
:
this
.
config
.
username
,
password
:
this
.
config
.
password
,
db
:
this
.
config
.
database
,
ssl
:
this
.
config
.
ssl
database
:
this
.
config
.
database
});
// I'll make my own pool, with blackjack and hookers!
...
...
@@ -74,17 +82,20 @@ module.exports = (function() {
read
:
Pooling
.
Pool
({
name
:
'sequelize-read'
,
create
:
function
(
done
)
{
if
(
reads
>=
self
.
config
.
replication
.
read
.
length
)
reads
=
0
;
if
(
reads
>=
self
.
config
.
replication
.
read
.
length
)
{
reads
=
0
}
var
config
=
self
.
config
.
replication
.
read
[
reads
++
];
connect
.
call
(
self
,
function
(
err
,
connection
)
{
connection
.
queryType
=
'read'
done
(
err
,
connection
)
done
(
null
,
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
...
...
@@ -94,12 +105,13 @@ module.exports = (function() {
create
:
function
(
done
)
{
connect
.
call
(
self
,
function
(
err
,
connection
)
{
connection
.
queryType
=
'write'
done
(
err
,
connection
)
done
(
null
,
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
...
...
@@ -117,6 +129,7 @@ module.exports = (function() {
},
max
:
self
.
poolCfg
.
maxConnections
,
min
:
self
.
poolCfg
.
minConnections
,
validate
:
self
.
poolCfg
.
validate
,
idleTimeoutMillis
:
self
.
poolCfg
.
maxIdleTime
})
}
...
...
@@ -145,6 +158,7 @@ module.exports = (function() {
if
(
this
.
useQueue
)
{
var
queueItem
=
{
query
:
new
Query
(
this
.
client
,
this
.
sequelize
,
callee
,
options
||
{}),
client
:
this
.
client
,
sql
:
sql
};
...
...
@@ -157,11 +171,12 @@ module.exports = (function() {
query
.
done
(
function
()
{
self
.
pendingQueries
--
;
if
(
self
.
pool
)
self
.
pool
.
release
(
query
.
client
);
else
{
if
(
self
.
pool
)
{
self
.
pool
.
release
(
query
.
client
);
}
else
{
if
(
self
.
pendingQueries
===
0
)
{
setTimeout
(
function
()
{
self
.
pendingQueries
===
0
&&
self
.
disconnect
.
call
(
self
)
;
self
.
pendingQueries
===
0
&&
self
.
disconnect
.
call
(
self
)
},
100
);
}
}
...
...
@@ -169,16 +184,16 @@ module.exports = (function() {
if
(
!
this
.
pool
)
{
query
.
run
(
sql
);
}
else
{
}
else
{
this
.
pool
.
acquire
(
function
(
err
,
client
)
{
if
(
err
)
return
query
.
emit
(
'error'
,
err
);
if
(
err
)
{
return
query
.
emit
(
'error'
,
err
)
}
query
.
client
=
client
;
query
.
run
(
sql
)
;
query
.
client
=
client
query
.
run
(
sql
)
return
;
},
undefined
,
options
.
type
)
;
},
undefined
,
options
.
type
)
}
return
query
;
...
...
@@ -198,8 +213,10 @@ module.exports = (function() {
};
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
if
(
this
.
client
)
disconnect
.
call
(
this
,
this
.
client
);
return
;
if
(
this
.
client
)
{
disconnect
.
call
(
this
,
this
.
client
)
}
return
};
...
...
@@ -207,49 +224,94 @@ module.exports = (function() {
var
disconnect
=
function
(
client
)
{
var
self
=
this
;
if
(
!
this
.
useQueue
)
{
this
.
client
=
null
;
}
if
(
client
.
connected
)
{
client
.
end
()
client
.
end
(
function
()
{
if
(
!
self
.
useQueue
)
{
return
client
.
destroy
();
}
var
intervalObj
=
null
var
cleanup
=
function
()
{
var
retryCt
=
0
// make sure to let client finish before calling destroy
if
(
self
&&
self
.
hasQueuedItems
)
{
return
}
// needed to prevent mysql connection leak
client
.
destroy
()
if
(
self
&&
self
.
client
)
{
self
.
client
=
null
self
.
isConnecting
=
false
}
clearInterval
(
intervalObj
)
}
intervalObj
=
setInterval
(
cleanup
,
10
)
cleanup
()
return
})
}
var
connect
=
function
(
done
,
config
)
{
config
=
config
||
this
.
config
var
connection
=
new
mariasql
()
var
emitter
=
new
(
require
(
'events'
).
EventEmitter
)
()
,
self
=
this
,
client
this
.
isConnecting
=
true
connection
.
connect
(
{
var
connectionConfig
=
{
host
:
config
.
host
,
port
:
config
.
port
,
user
:
config
.
username
,
password
:
config
.
password
,
db
:
config
.
database
,
ssl
:
config
.
ssl
||
undefined
,
metadata
:
true
// timezone: 'Z' // unsupported by mariasql
})
};
connection
.
on
(
'connect'
,
function
()
{
connection
.
query
(
"SET time_zone = '+0:00'"
);
connection
.
setMaxListeners
(
self
.
maxConcurrentQueries
)
this
.
isConnecting
=
false
if
(
config
.
dialectOptions
)
{
Object
.
keys
(
config
.
dialectOptions
).
forEach
(
function
(
key
)
{
connectionConfig
[
key
]
=
config
.
dialectOptions
[
key
];
});
}
done
(
null
,
connection
)
}).
on
(
'error'
,
function
(
err
)
{
this
.
isConnecting
=
false
client
=
new
mariadb
()
client
.
connect
(
connectionConfig
)
client
.
on
(
'error'
,
function
(
err
)
{
self
.
isConnecting
=
false
done
(
err
)
// disconnect.call(self, connection)
}).
on
(
'close'
,
function
()
{
disconnect
.
call
(
self
,
connection
)
})
.
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
validateConnection
=
function
(
client
)
{
return
client
&&
client
.
state
!==
'disconnected'
}
var
enqueue
=
function
(
queueItem
,
options
)
{
...
...
@@ -287,10 +349,6 @@ module.exports = (function() {
}
var
transferQueuedItems
=
function
(
count
)
{
// prevent possible overrun condition
if
(
count
>
this
.
queue
.
length
)
count
=
this
.
queue
.
length
for
(
var
i
=
0
;
i
<
count
;
i
++
)
{
var
queueItem
=
this
.
queue
.
shift
();
if
(
queueItem
)
{
...
...
@@ -300,8 +358,6 @@ module.exports = (function() {
}
var
afterQuery
=
function
(
queueItem
)
{
var
self
=
this
dequeue
.
call
(
this
,
queueItem
)
transferQueuedItems
.
call
(
this
,
this
.
maxConcurrentQueries
-
this
.
activeQueue
.
length
)
disconnectIfNoConnections
.
call
(
this
)
...
...
@@ -319,7 +375,7 @@ module.exports = (function() {
}
ConnectorManager
.
prototype
.
__defineGetter__
(
'hasQueuedItems'
,
function
()
{
return
(
this
.
queue
.
length
>
0
)
||
(
this
.
activeQueue
.
length
>
0
)
||
(
this
.
client
&&
this
.
client
.
_que
ries
&&
(
this
.
client
.
_queries
.
length
>
0
))
return
(
this
.
queue
.
length
>
0
)
||
(
this
.
activeQueue
.
length
>
0
)
||
(
this
.
client
&&
this
.
client
.
_que
ue
&&
(
this
.
client
.
_queue
.
length
>
0
))
})
// legacy
...
...
@@ -328,7 +384,7 @@ module.exports = (function() {
})
ConnectorManager
.
prototype
.
__defineGetter__
(
'isConnected'
,
function
()
{
return
this
.
client
!=
null
&&
this
.
client
.
connected
==
true
return
this
.
client
!=
null
})
var
disconnectIfNoConnections
=
function
()
{
...
...
lib/sequelize.js
View file @
fd22bf1
...
...
@@ -245,11 +245,10 @@ module.exports = (function() {
Sequelize
.
prototype
.
import
=
function
(
path
)
{
// is it a relative path?
if
(
url
.
parse
(
path
).
pathname
.
indexOf
(
'/'
)
!==
0
)
{
if
(
Path
.
normalize
(
path
).
indexOf
(
path
.
sep
)
!==
0
)
{
// make path relative to the caller
var
callerFilename
=
Utils
.
stack
()[
1
].
getFileName
()
,
callerMatch
=
callerFilename
.
match
(
/
(
.+
\/)
.+
?
$/
)
,
callerPath
=
callerMatch
[
1
]
,
callerPath
=
Path
.
dirname
(
callerFilename
)
path
=
Path
.
resolve
(
callerPath
,
path
)
}
...
...
test/config/config.js
View file @
fd22bf1
...
...
@@ -41,14 +41,14 @@ module.exports = {
},
mariadb
:
{
database
:
process
.
env
.
SEQ_
PG_DB
||
process
.
env
.
SEQ_DB
||
'sequelize_test'
,
username
:
process
.
env
.
SEQ_
PG_USER
||
process
.
env
.
SEQ_USER
||
"root"
,
password
:
process
.
env
.
SEQ_
PG_PW
||
process
.
env
.
SEQ_PW
||
null
,
host
:
process
.
env
.
SEQ_
PG_HOST
||
process
.
env
.
SEQ_HOST
||
'127.0.0.1'
,
port
:
process
.
env
.
SEQ_
PG_PORT
||
process
.
env
.
SEQ_PORT
||
3306
,
database
:
process
.
env
.
SEQ_
MYSQL_DB
||
process
.
env
.
SEQ_DB
||
'sequelize_test'
,
username
:
process
.
env
.
SEQ_
MYSQL_USER
||
process
.
env
.
SEQ_USER
||
"root"
,
password
:
process
.
env
.
SEQ_
MYSQL_PW
||
process
.
env
.
SEQ_PW
||
null
,
host
:
process
.
env
.
SEQ_
MYSQL_HOST
||
process
.
env
.
SEQ_HOST
||
'127.0.0.1'
,
port
:
process
.
env
.
SEQ_
MYSQL_PORT
||
process
.
env
.
SEQ_PORT
||
3306
,
pool
:
{
maxConnections
:
process
.
env
.
SEQ_
PG_POOL_MAX
||
process
.
env
.
SEQ_POOL_MAX
||
5
,
maxIdleTime
:
process
.
env
.
SEQ_
PG
_POOL_IDLE
||
process
.
env
.
SEQ_POOL_IDLE
||
3000
maxConnections
:
process
.
env
.
SEQ_
MYSQL_POOL_MAX
||
process
.
env
.
SEQ_POOL_MAX
||
1
,
maxIdleTime
:
process
.
env
.
SEQ_
MYSQL
_POOL_IDLE
||
process
.
env
.
SEQ_POOL_IDLE
||
3000
}
}
}
test/support.js
View file @
fd22bf1
...
...
@@ -63,14 +63,27 @@ var Support = {
},
clearDatabase
:
function
(
sequelize
,
callback
)
{
var
disablequery
=
sequelize
.
getQueryInterface
().
QueryGenerator
.
disableForeignKeyConstraintsQuery
()
,
dropAll
=
function
(
cb
)
{
return
function
()
{
sequelize
.
getQueryInterface
()
.
dropAllTables
()
.
success
(
function
()
{
sequelize
.
daoFactoryManager
.
daos
=
[]
callback
&&
callback
()
cb
&&
cb
()
})
.
error
(
function
(
err
)
{
console
.
log
(
err
)
})
.
error
(
function
(
err
)
{
console
.
log
(
"Error clearing database "
+
err
)
})
}
}
if
(
disablequery
)
{
sequelize
.
query
(
disablequery
).
success
(
dropAll
(
function
()
{
sequelize
.
query
(
sequelize
.
getQueryInterface
().
QueryGenerator
.
enableForeignKeyConstraintsQuery
()).
success
(
callback
)
}))
}
else
{
dropAll
(
callback
)
}
},
getSupportedDialects
:
function
()
{
...
...
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