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 1c5aac4c
authored
May 07, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored .query for all dialects to return promises
1 parent
bbd668eb
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
206 additions
and
188 deletions
lib/dialects/mariadb/connector-manager.js
lib/dialects/mariadb/query.js
lib/dialects/mysql/connector-manager.js
lib/dialects/mysql/query.js
lib/dialects/sqlite/query.js
lib/dialects/mariadb/connector-manager.js
View file @
1c5aac4
...
@@ -159,54 +159,88 @@ module.exports = (function() {
...
@@ -159,54 +159,88 @@ module.exports = (function() {
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
"../abstract/connector-manager"
).
prototype
)
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
"../abstract/connector-manager"
).
prototype
)
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
if
(
!
this
.
isConnected
&&
!
this
.
pool
)
{
var
self
=
this
this
.
connect
()
}
options
=
options
||
{
}
if
(
this
.
useQueue
)
{
if
(
this
.
useQueue
)
{
// If queueing we'll let the execQueueItem method handle connecting
var
queueItem
=
{
var
queueItem
=
{
query
:
new
Query
(
this
.
client
,
this
.
sequelize
,
callee
,
options
||
{}),
query
:
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
),
client
:
this
.
client
,
sql
:
sql
sql
:
sql
}
}
;
queueItem
.
query
.
options
.
uuid
=
this
.
config
.
uuid
enqueue
.
call
(
this
,
queueItem
,
options
)
enqueue
.
call
(
this
,
queueItem
,
options
)
return
queueItem
.
query
return
queueItem
.
query
.
promise
.
finally
(
function
()
{
afterQuery
.
call
(
self
,
queueItem
)
})
}
}
var
self
=
this
var
query
=
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
);
,
query
=
new
Query
(
this
.
client
,
this
.
sequelize
,
callee
,
options
||
{})
this
.
pendingQueries
++
;
this
.
pendingQueries
++
query
.
options
.
uuid
=
this
.
config
.
uuid
query
.
done
(
function
()
{
return
this
.
getConnection
(
options
).
then
(
function
(
client
)
{
self
.
pendingQueries
--
query
.
client
=
client
return
query
.
run
(
sql
).
finally
(
function
()
{
self
.
pendingQueries
--
;
if
(
self
.
pool
)
{
if
(
self
.
pool
)
{
self
.
pool
.
release
(
query
.
client
)
self
.
pool
.
release
(
query
.
client
);
}
else
{
}
else
{
if
(
self
.
pendingQueries
===
0
)
{
if
(
self
.
pendingQueries
===
0
)
{
setTimeout
(
function
()
{
setTimeout
(
function
()
{
self
.
pendingQueries
===
0
&&
self
.
disconnect
.
call
(
self
)
if
(
self
.
pendingQueries
===
0
){
},
100
)
self
.
disconnect
.
call
(
self
);
}
},
100
);
}
}
}
}
});
})
})
};
ConnectorManager
.
prototype
.
getConnection
=
function
(
options
)
{
var
self
=
this
;
if
(
!
this
.
pool
)
{
options
=
options
||
{}
query
.
run
(
sql
)
return
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
if
(
!
self
.
pool
)
{
// Regular client caching
if
(
self
.
client
)
{
return
resolve
(
self
.
client
);
}
else
{
}
else
{
this
.
pool
.
acquire
(
function
(
err
,
client
)
{
// Cache for concurrent queries
if
(
err
)
{
if
(
self
.
_getConnection
)
{
return
query
.
emit
(
'error'
,
err
)
return
resolve
(
self
.
_getConnection
)
}
}
query
.
client
=
client
// Set cache and acquire connection
query
.
run
(
sql
)
self
.
_getConnection
=
this
;
return
connect
.
call
(
self
,
function
(
err
,
client
)
{
},
undefined
,
options
.
type
)
if
(
err
)
{
return
reject
(
err
);
}
}
return
query
// 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
()
{
ConnectorManager
.
prototype
.
connect
=
function
()
{
var
self
=
this
var
self
=
this
...
@@ -228,10 +262,12 @@ module.exports = (function() {
...
@@ -228,10 +262,12 @@ module.exports = (function() {
return
return
}
}
// private
// private
var
disconnect
=
function
(
client
)
{
var
disconnect
=
function
(
client
)
{
if
(
!
client
)
{
return
// TODO possible orphaning of clients?
}
var
self
=
this
var
self
=
this
if
(
!
this
.
useQueue
)
{
if
(
!
this
.
useQueue
)
{
this
.
client
=
null
this
.
client
=
null
...
@@ -324,7 +360,7 @@ module.exports = (function() {
...
@@ -324,7 +360,7 @@ module.exports = (function() {
var
self
=
this
var
self
=
this
this
.
pool
.
acquire
(
function
(
err
,
client
)
{
this
.
pool
.
acquire
(
function
(
err
,
client
)
{
if
(
err
)
{
if
(
err
)
{
queueItem
.
query
.
emit
(
'error'
,
err
)
queueItem
.
query
.
reject
(
err
)
return
return
}
}
//we set the client here, asynchronously, when getting a pooled connection
//we set the client here, asynchronously, when getting a pooled connection
...
@@ -367,12 +403,6 @@ module.exports = (function() {
...
@@ -367,12 +403,6 @@ module.exports = (function() {
var
execQueueItem
=
function
(
queueItem
)
{
var
execQueueItem
=
function
(
queueItem
)
{
var
self
=
this
queueItem
.
query
.
success
(
function
(){
afterQuery
.
call
(
self
,
queueItem
)
})
.
error
(
function
(){
afterQuery
.
call
(
self
,
queueItem
)
})
queueItem
.
query
.
run
(
queueItem
.
sql
,
queueItem
.
client
)
queueItem
.
query
.
run
(
queueItem
.
sql
,
queueItem
.
client
)
}
}
...
...
lib/dialects/mariadb/query.js
View file @
1c5aac4
...
@@ -12,7 +12,12 @@ module.exports = (function() {
...
@@ -12,7 +12,12 @@ module.exports = (function() {
raw
:
false
raw
:
false
},
options
||
{})
},
options
||
{})
var
self
=
this
this
.
checkLoggingOption
()
this
.
checkLoggingOption
()
this
.
promise
=
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
self
.
resolve
=
resolve
self
.
reject
=
reject
})
}
}
Utils
.
inherit
(
Query
,
AbstractQuery
)
Utils
.
inherit
(
Query
,
AbstractQuery
)
...
@@ -87,9 +92,9 @@ module.exports = (function() {
...
@@ -87,9 +92,9 @@ module.exports = (function() {
})
})
.
on
(
'error'
,
function
(
err
)
{
.
on
(
'error'
,
function
(
err
)
{
errorDetected
=
true
errorDetected
=
true
self
.
emit
(
'sql'
,
self
.
sql
)
self
.
promise
.
emit
(
'sql'
,
self
.
sql
)
err
.
sql
=
sql
err
.
sql
=
sql
self
.
emit
(
'error'
,
err
,
self
.
callee
)
self
.
reject
(
err
)
})
})
.
on
(
'end'
,
function
(
info
)
{
.
on
(
'end'
,
function
(
info
)
{
if
(
alreadyEnded
||
errorDetected
)
{
if
(
alreadyEnded
||
errorDetected
)
{
...
@@ -97,19 +102,18 @@ module.exports = (function() {
...
@@ -97,19 +102,18 @@ module.exports = (function() {
}
}
alreadyEnded
=
true
alreadyEnded
=
true
self
.
emit
(
'sql'
,
self
.
sql
)
self
.
promise
.
emit
(
'sql'
,
self
.
sql
)
// we need to figure out whether to send the result set
// we need to figure out whether to send the result set
// or info depending upon the type of query
// or info depending upon the type of query
if
(
/^call/
.
test
(
self
.
sql
.
toLowerCase
()))
{
if
(
/^call/
.
test
(
self
.
sql
.
toLowerCase
()))
{
self
.
emit
(
'success'
,
resultSet
)
self
.
resolve
(
resultSet
)
}
else
if
(
/^show/
.
test
(
self
.
sql
.
toLowerCase
())
||
}
else
if
(
/^show/
.
test
(
self
.
sql
.
toLowerCase
())
||
/^select/
.
test
(
self
.
sql
.
toLowerCase
())
||
/^select/
.
test
(
self
.
sql
.
toLowerCase
())
||
/^describe/
.
test
(
self
.
sql
.
toLowerCase
()))
{
/^describe/
.
test
(
self
.
sql
.
toLowerCase
()))
{
self
.
emit
(
'success'
,
self
.
formatResults
(
resultSet
))
self
.
resolve
(
self
.
formatResults
(
resultSet
))
}
else
{
}
else
{
self
.
emit
(
'success'
,
self
.
formatResults
(
info
))
self
.
resolve
(
self
.
formatResults
(
info
))
}
}
})
})
})
})
.
on
(
'error'
,
function
(
err
)
{
.
on
(
'error'
,
function
(
err
)
{
...
@@ -117,12 +121,12 @@ module.exports = (function() {
...
@@ -117,12 +121,12 @@ module.exports = (function() {
return
return
}
}
errorDetected
=
true
errorDetected
=
true
self
.
emit
(
'sql'
,
self
.
sql
)
self
.
promise
.
emit
(
'sql'
,
self
.
sql
)
self
.
emit
(
'error'
,
err
,
self
.
callee
)
self
.
reject
(
err
)
})
})
.
setMaxListeners
(
100
)
.
setMaxListeners
(
100
)
return
this
return
this
.
promise
}
}
return
Query
return
Query
...
...
lib/dialects/mysql/connector-manager.js
View file @
1c5aac4
...
@@ -2,7 +2,7 @@ var mysql
...
@@ -2,7 +2,7 @@ var mysql
,
Pooling
=
require
(
'generic-pool'
)
,
Pooling
=
require
(
'generic-pool'
)
,
Query
=
require
(
"./query"
)
,
Query
=
require
(
"./query"
)
,
Utils
=
require
(
"../../utils"
)
,
Utils
=
require
(
"../../utils"
)
,
without
=
function
(
arr
,
elem
)
{
return
arr
.
filter
(
function
(
e
)
{
return
e
!=
elem
})
}
,
without
=
function
(
arr
,
elem
)
{
return
arr
.
filter
(
function
(
e
)
{
return
e
.
query
.
uuid
!=
elem
.
query
.
uuid
})
}
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
ConnectorManager
=
function
(
sequelize
,
config
)
{
var
ConnectorManager
=
function
(
sequelize
,
config
)
{
...
@@ -160,23 +160,32 @@ module.exports = (function() {
...
@@ -160,23 +160,32 @@ module.exports = (function() {
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
"../abstract/connector-manager"
).
prototype
);
Utils
.
_
.
extend
(
ConnectorManager
.
prototype
,
require
(
"../abstract/connector-manager"
).
prototype
);
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
ConnectorManager
.
prototype
.
query
=
function
(
sql
,
callee
,
options
)
{
var
self
=
this
options
=
options
||
{}
if
(
this
.
useQueue
)
{
if
(
this
.
useQueue
)
{
// If queueing we'll let the execQueueItem method handle connecting
// If queueing we'll let the execQueueItem method handle connecting
var
queueItem
=
{
var
queueItem
=
{
query
:
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
||
{}
),
query
:
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
),
sql
:
sql
sql
:
sql
};
};
queueItem
.
query
.
options
.
uuid
=
this
.
config
.
uuid
queueItem
.
query
.
options
.
uuid
=
this
.
config
.
uuid
enqueue
.
call
(
this
,
queueItem
,
options
);
enqueue
.
call
(
this
,
queueItem
,
options
)
return
queueItem
.
query
;
return
queueItem
.
query
.
promise
.
finally
(
function
()
{
afterQuery
.
call
(
self
,
queueItem
)
})
}
}
var
self
=
this
,
query
=
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
||
{}
);
var
query
=
new
Query
(
null
,
this
.
sequelize
,
callee
,
options
);
this
.
pendingQueries
++
;
this
.
pendingQueries
++
;
query
.
options
.
uuid
=
this
.
config
.
uuid
query
.
options
.
uuid
=
this
.
config
.
uuid
query
.
done
(
function
()
{
return
this
.
getConnection
(
options
).
then
(
function
(
client
)
{
query
.
client
=
client
return
query
.
run
(
sql
).
finally
(
function
()
{
self
.
pendingQueries
--
;
self
.
pendingQueries
--
;
if
(
self
.
pool
)
{
if
(
self
.
pool
)
{
self
.
pool
.
release
(
query
.
client
);
self
.
pool
.
release
(
query
.
client
);
...
@@ -190,49 +199,36 @@ module.exports = (function() {
...
@@ -190,49 +199,36 @@ module.exports = (function() {
}
}
}
}
});
});
})
this
.
getConnection
(
options
,
function
(
err
,
client
)
{
if
(
err
)
{
return
query
.
emit
(
'error'
,
err
)
}
query
.
client
=
client
query
.
run
(
sql
)
});
return
query
;
};
};
ConnectorManager
.
prototype
.
getConnection
=
function
(
options
,
callback
)
{
ConnectorManager
.
prototype
.
getConnection
=
function
(
options
)
{
var
self
=
this
;
var
self
=
this
;
if
(
typeof
options
===
"function"
)
{
options
=
options
||
{}
callback
=
options
;
options
=
{};
}
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
if
(
!
self
.
pool
)
{
if
(
!
self
.
pool
)
{
// Regular client caching
// Regular client caching
if
(
self
.
client
)
{
if
(
self
.
client
)
{
return
emitter
.
emit
(
'success'
,
self
.
client
);
return
resolve
(
self
.
client
);
}
else
{
}
else
{
// Cache for concurrent queries
// Cache for concurrent queries
if
(
self
.
_getConnection
)
{
if
(
self
.
_getConnection
)
{
self
.
_getConnection
.
proxy
(
emitter
);
return
resolve
(
self
.
_getConnection
)
return
;
}
}
// Set cache and acquire connection
// Set cache and acquire connection
self
.
_getConnection
=
emitter
;
self
.
_getConnection
=
this
;
connect
.
call
(
self
,
function
(
err
,
client
)
{
connect
.
call
(
self
,
function
(
err
,
client
)
{
if
(
err
)
{
if
(
err
)
{
return
emitter
.
emit
(
'error'
,
err
);
return
reject
(
err
);
}
}
// Unset caching, should now be caught by the self.client check
// Unset caching, should now be caught by the self.client check
self
.
_getConnection
=
null
;
self
.
_getConnection
=
null
;
self
.
client
=
client
;
self
.
client
=
client
;
emitter
.
emit
(
'success'
,
client
);
resolve
(
client
);
});
});
}
}
}
}
...
@@ -240,12 +236,12 @@ module.exports = (function() {
...
@@ -240,12 +236,12 @@ module.exports = (function() {
// Acquire from pool
// Acquire from pool
self
.
pool
.
acquire
(
function
(
err
,
client
)
{
self
.
pool
.
acquire
(
function
(
err
,
client
)
{
if
(
err
)
{
if
(
err
)
{
return
emitter
.
emit
(
'error'
,
err
);
return
reject
(
err
);
}
}
emitter
.
emit
(
'success'
,
client
);
resolve
(
client
);
},
options
.
priority
,
options
.
type
);
},
options
.
priority
,
options
.
type
);
}
}
})
.
run
().
done
(
callback
);
})
};
};
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
ConnectorManager
.
prototype
.
disconnect
=
function
()
{
...
@@ -255,13 +251,15 @@ module.exports = (function() {
...
@@ -255,13 +251,15 @@ module.exports = (function() {
return
return
};
};
// private
// private
var
disconnect
=
function
(
client
)
{
var
disconnect
=
function
(
client
)
{
var
self
=
this
;
var
self
=
this
;
this
.
client
=
null
;
this
.
client
=
null
;
if
(
!
client
)
{
return
}
client
.
end
(
function
()
{
client
.
end
(
function
()
{
if
(
!
self
.
useQueue
)
{
if
(
!
self
.
useQueue
)
{
return
client
.
destroy
();
return
client
.
destroy
();
...
@@ -269,7 +267,6 @@ module.exports = (function() {
...
@@ -269,7 +267,6 @@ module.exports = (function() {
var
intervalObj
=
null
var
intervalObj
=
null
var
cleanup
=
function
()
{
var
cleanup
=
function
()
{
var
retryCt
=
0
// make sure to let client finish before calling destroy
// make sure to let client finish before calling destroy
if
(
client
.
_queue
&&
(
client
.
_queue
.
length
>
0
))
{
if
(
client
.
_queue
&&
(
client
.
_queue
.
length
>
0
))
{
return
return
...
@@ -287,7 +284,6 @@ module.exports = (function() {
...
@@ -287,7 +284,6 @@ module.exports = (function() {
var
connect
=
function
(
done
,
config
)
{
var
connect
=
function
(
done
,
config
)
{
config
=
config
||
this
.
config
config
=
config
||
this
.
config
var
emitter
=
new
(
require
(
'events'
).
EventEmitter
)()
var
connectionConfig
=
{
var
connectionConfig
=
{
host
:
config
.
host
,
host
:
config
.
host
,
port
:
config
.
port
,
port
:
config
.
port
,
...
@@ -309,22 +305,22 @@ module.exports = (function() {
...
@@ -309,22 +305,22 @@ module.exports = (function() {
switch
(
err
.
code
)
{
switch
(
err
.
code
)
{
case
'ECONNREFUSED'
:
case
'ECONNREFUSED'
:
case
'ER_ACCESS_D2ENIED_ERROR'
:
case
'ER_ACCESS_D2ENIED_ERROR'
:
emitter
.
emit
(
'error'
,
'Failed to authenticate for MySQL. Please double check your settings.'
)
done
(
'Failed to authenticate for MySQL. Please double check your settings.'
)
break
break
case
'ENOTFOUND'
:
case
'ENOTFOUND'
:
case
'EHOSTUNREACH'
:
case
'EHOSTUNREACH'
:
case
'EINVAL'
:
case
'EINVAL'
:
emitter
.
emit
(
'error'
,
'Failed to find MySQL server. Please double check your settings.'
)
done
(
'Failed to find MySQL server. Please double check your settings.'
)
break
break
default
:
default
:
emitter
.
emit
(
'error'
,
err
);
done
(
err
);
break
;
break
;
}
}
return
;
return
;
}
}
emitter
.
emit
(
'success'
,
connection
);
done
(
null
,
connection
);
})
})
connection
.
query
(
"SET time_zone = '+0:00'"
);
connection
.
query
(
"SET time_zone = '+0:00'"
);
...
@@ -333,13 +329,6 @@ module.exports = (function() {
...
@@ -333,13 +329,6 @@ module.exports = (function() {
if
(
config
.
pool
!==
null
&&
config
.
pool
.
handleDisconnects
)
{
if
(
config
.
pool
!==
null
&&
config
.
pool
.
handleDisconnects
)
{
handleDisconnect
(
this
.
pool
,
connection
)
handleDisconnect
(
this
.
pool
,
connection
)
}
}
emitter
.
on
(
'error'
,
function
(
err
)
{
done
(
err
);
});
emitter
.
on
(
'success'
,
function
(
connection
)
{
done
(
null
,
connection
);
});
}
}
var
handleDisconnect
=
function
(
pool
,
client
)
{
var
handleDisconnect
=
function
(
pool
,
client
)
{
...
@@ -388,26 +377,17 @@ module.exports = (function() {
...
@@ -388,26 +377,17 @@ module.exports = (function() {
disconnectIfNoConnections
.
call
(
this
)
disconnectIfNoConnections
.
call
(
this
)
}
}
var
execQueueItem
=
function
(
queueItem
)
{
var
execQueueItem
=
function
(
queueItem
)
{
var
self
=
this
this
.
getConnection
({
self
.
getConnection
({
priority
:
queueItem
.
query
.
options
.
priority
,
priority
:
queueItem
.
query
.
options
.
priority
,
type
:
queueItem
.
query
.
options
.
type
type
:
queueItem
.
query
.
options
.
type
},
function
(
err
,
connection
)
{
}).
then
(
function
(
connection
)
{
if
(
err
)
{
queueItem
.
query
.
emit
(
'error'
,
err
)
return
}
queueItem
.
query
.
client
=
connection
queueItem
.
query
.
client
=
connection
queueItem
.
client
=
connection
queueItem
.
client
=
connection
queueItem
.
query
.
success
(
function
(){
afterQuery
.
call
(
self
,
queueItem
)
})
.
error
(
function
(){
afterQuery
.
call
(
self
,
queueItem
)
})
queueItem
.
query
.
run
(
queueItem
.
sql
,
queueItem
.
client
)
queueItem
.
query
.
run
(
queueItem
.
sql
)
},
function
(
err
)
{
queueItem
.
query
.
reject
(
err
)
})
})
}
}
...
@@ -421,7 +401,7 @@ module.exports = (function() {
...
@@ -421,7 +401,7 @@ module.exports = (function() {
})
})
ConnectorManager
.
prototype
.
__defineGetter__
(
'isConnected'
,
function
()
{
ConnectorManager
.
prototype
.
__defineGetter__
(
'isConnected'
,
function
()
{
return
this
.
client
!=
null
return
this
.
client
!=
=
null
})
})
var
disconnectIfNoConnections
=
function
()
{
var
disconnectIfNoConnections
=
function
()
{
...
...
lib/dialects/mysql/query.js
View file @
1c5aac4
var
Utils
=
require
(
"../../utils"
)
var
Utils
=
require
(
"../../utils"
)
,
AbstractQuery
=
require
(
'../abstract/query'
)
,
AbstractQuery
=
require
(
'../abstract/query'
)
,
uuid
=
require
(
'node-uuid'
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
Query
=
function
(
client
,
sequelize
,
callee
,
options
)
{
var
Query
=
function
(
client
,
sequelize
,
callee
,
options
)
{
this
.
client
=
client
this
.
client
=
client
this
.
callee
=
callee
this
.
callee
=
callee
this
.
sequelize
=
sequelize
this
.
sequelize
=
sequelize
this
.
uuid
=
uuid
.
v4
()
this
.
options
=
Utils
.
_
.
extend
({
this
.
options
=
Utils
.
_
.
extend
({
logging
:
console
.
log
,
logging
:
console
.
log
,
plain
:
false
,
plain
:
false
,
raw
:
false
raw
:
false
},
options
||
{})
},
options
||
{})
var
self
=
this
this
.
checkLoggingOption
()
this
.
checkLoggingOption
()
this
.
promise
=
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
self
.
resolve
=
resolve
self
.
reject
=
reject
})
}
}
Utils
.
inherit
(
Query
,
AbstractQuery
)
Utils
.
inherit
(
Query
,
AbstractQuery
)
Query
.
prototype
.
run
=
function
(
sql
)
{
Query
.
prototype
.
run
=
function
(
sql
)
{
var
self
=
this
this
.
sql
=
sql
this
.
sql
=
sql
if
(
this
.
options
.
logging
!==
false
)
{
if
(
this
.
options
.
logging
!==
false
)
{
this
.
sequelize
.
log
(
'Executing ('
+
this
.
options
.
uuid
+
'): '
+
this
.
sql
)
this
.
sequelize
.
log
(
'Executing ('
+
this
.
options
.
uuid
+
'): '
+
this
.
sql
)
}
}
this
.
client
.
query
(
this
.
sql
,
function
(
err
,
results
,
fields
)
{
self
.
client
.
query
(
self
.
sql
,
function
(
err
,
results
,
fields
)
{
this
.
emit
(
'sql'
,
this
.
sql
,
this
.
options
.
uuid
)
self
.
promise
.
emit
(
'sql'
,
self
.
sql
,
self
.
options
.
uuid
)
if
(
err
)
{
if
(
err
)
{
err
.
sql
=
sql
err
.
sql
=
sql
this
.
emit
(
'error'
,
err
)
self
.
reject
(
err
)
}
else
{
}
else
{
this
.
emit
(
'success'
,
this
.
formatResults
(
results
))
self
.
resolve
(
self
.
formatResults
(
results
))
}
}
}.
bind
(
this
)).
setMaxListeners
(
100
)
}).
setMaxListeners
(
100
)
return
this
return
this
.
promise
}
}
return
Query
return
Query
...
...
lib/dialects/sqlite/query.js
View file @
1c5aac4
...
@@ -23,6 +23,7 @@ module.exports = (function() {
...
@@ -23,6 +23,7 @@ module.exports = (function() {
Query
.
prototype
.
run
=
function
(
sql
)
{
Query
.
prototype
.
run
=
function
(
sql
)
{
var
self
=
this
var
self
=
this
,
promise
this
.
sql
=
sql
this
.
sql
=
sql
...
@@ -30,90 +31,40 @@ module.exports = (function() {
...
@@ -30,90 +31,40 @@ module.exports = (function() {
this
.
sequelize
.
log
(
'Executing ('
+
this
.
options
.
uuid
+
'): '
+
this
.
sql
)
this
.
sequelize
.
log
(
'Executing ('
+
this
.
options
.
uuid
+
'): '
+
this
.
sql
)
}
}
return
new
Utils
.
Promise
(
function
(
resolve
)
{
var
columnTypes
=
{}
var
columnTypes
=
{}
this
.
database
.
serialize
(
function
()
{
promise
=
this
self
.
database
.
serialize
(
function
()
{
var
executeSql
=
function
()
{
var
executeSql
=
function
()
{
if
(
self
.
sql
.
indexOf
(
'-- '
)
===
0
)
{
if
(
self
.
sql
.
indexOf
(
'-- '
)
===
0
)
{
// the sql query starts with a comment. don't bother the server with that ...
// the sql query starts with a comment. don't bother the server with that ...
Utils
.
tick
(
function
()
{
promise
.
emit
(
'sql'
,
self
.
sql
,
self
.
options
.
uuid
)
self
.
emit
(
'sql'
,
self
.
sql
,
self
.
options
.
uuid
)
return
resolve
()
self
.
emit
(
'success'
,
null
)
})
}
else
{
}
else
{
resolve
(
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
self
.
database
[
getDatabaseMethod
.
call
(
self
)](
self
.
sql
,
function
(
err
,
results
)
{
self
.
database
[
getDatabaseMethod
.
call
(
self
)](
self
.
sql
,
function
(
err
,
results
)
{
// allow clients to listen to sql to do their own logging or whatnot
// allow clients to listen to sql to do their own logging or whatnot
self
.
emit
(
'sql'
,
self
.
sql
,
self
.
options
.
uuid
)
promise
.
emit
(
'sql'
,
self
.
sql
,
self
.
options
.
uuid
)
if
(
err
)
{
if
(
err
)
{
err
.
sql
=
self
.
sql
err
.
sql
=
self
.
sql
onFailure
.
call
(
self
,
err
)
reject
(
err
)
}
else
{
this
.
columnTypes
=
columnTypes
onSuccess
.
call
(
self
,
results
,
this
)
}
})
}
}
if
((
getDatabaseMethod
.
call
(
self
)
===
'all'
))
{
var
tableNames
=
[]
if
(
self
.
options
&&
self
.
options
.
tableNames
)
{
tableNames
=
self
.
options
.
tableNames
}
else
if
(
/FROM `
(
.*
?)
`/i
.
exec
(
self
.
sql
))
{
tableNames
.
push
(
/FROM `
(
.*
?)
`/i
.
exec
(
self
.
sql
)[
1
])
}
if
(
!
tableNames
.
length
)
{
executeSql
()
}
else
{
var
execute
=
Utils
.
_
.
after
(
tableNames
.
length
,
executeSql
)
tableNames
.
forEach
(
function
(
tableName
)
{
if
(
tableName
!==
'sqlite_master'
)
{
// get the column types
self
.
database
.
all
(
"PRAGMA table_info("
+
tableName
+
")"
,
function
(
err
,
results
)
{
if
(
!
err
)
{
for
(
var
i
=
0
,
l
=
results
.
length
;
i
<
l
;
i
++
)
{
columnTypes
[
tableName
+
'.'
+
results
[
i
].
name
]
=
columnTypes
[
results
[
i
].
name
]
=
results
[
i
].
type
}
}
execute
()
});
}
else
{
execute
()
}
})
}
}
else
{
executeSql
()
}
})
return
this
}
//private
var
getDatabaseMethod
=
function
()
{
if
(
this
.
send
(
'isInsertQuery'
)
||
this
.
send
(
'isUpdateQuery'
)
||
(
this
.
sql
.
toLowerCase
().
indexOf
(
'CREATE TEMPORARY TABLE'
.
toLowerCase
())
!==
-
1
)
||
this
.
options
.
type
===
QueryTypes
.
BULKDELETE
)
{
return
'run'
}
else
{
}
else
{
return
'all'
var
metaData
=
this
}
metaData
.
columnTypes
=
columnTypes
}
var
onSuccess
=
function
(
results
,
metaData
)
{
var
result
=
self
.
callee
var
result
=
this
.
callee
// add the inserted row id to the instance
// add the inserted row id to the instance
if
(
this
.
send
(
'isInsertQuery'
,
results
,
metaData
))
{
if
(
self
.
send
(
'isInsertQuery'
,
results
,
metaData
))
{
this
.
send
(
'handleInsertQuery'
,
results
,
metaData
)
self
.
send
(
'handleInsertQuery'
,
results
,
metaData
)
}
}
if
(
this
.
sql
.
indexOf
(
'sqlite_master'
)
!==
-
1
)
{
if
(
self
.
sql
.
indexOf
(
'sqlite_master'
)
!==
-
1
)
{
result
=
results
.
map
(
function
(
resultSet
)
{
return
resultSet
.
name
})
result
=
results
.
map
(
function
(
resultSet
)
{
return
resultSet
.
name
})
}
else
if
(
this
.
send
(
'isSelectQuery'
))
{
}
else
if
(
self
.
send
(
'isSelectQuery'
))
{
if
(
!
this
.
options
.
raw
)
{
if
(
!
self
.
options
.
raw
)
{
results
=
results
.
map
(
function
(
result
)
{
results
=
results
.
map
(
function
(
result
)
{
for
(
var
name
in
result
)
{
for
(
var
name
in
result
)
{
if
(
result
.
hasOwnProperty
(
name
)
&&
metaData
.
columnTypes
[
name
])
{
if
(
result
.
hasOwnProperty
(
name
)
&&
metaData
.
columnTypes
[
name
])
{
...
@@ -134,10 +85,10 @@ module.exports = (function() {
...
@@ -134,10 +85,10 @@ module.exports = (function() {
})
})
}
}
result
=
this
.
send
(
'handleSelectQuery'
,
results
)
result
=
self
.
send
(
'handleSelectQuery'
,
results
)
}
else
if
(
this
.
send
(
'isShowOrDescribeQuery'
))
{
}
else
if
(
self
.
send
(
'isShowOrDescribeQuery'
))
{
result
=
results
result
=
results
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA INDEX_LIST'
)
!==
-
1
)
{
}
else
if
(
self
.
sql
.
indexOf
(
'PRAGMA INDEX_LIST'
)
!==
-
1
)
{
// this is the sqlite way of getting the indexes of a table
// this is the sqlite way of getting the indexes of a table
result
=
results
.
map
(
function
(
result
)
{
result
=
results
.
map
(
function
(
result
)
{
return
{
return
{
...
@@ -146,7 +97,7 @@ module.exports = (function() {
...
@@ -146,7 +97,7 @@ module.exports = (function() {
unique
:
(
result
.
unique
===
0
)
unique
:
(
result
.
unique
===
0
)
}
}
})
})
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA TABLE_INFO'
)
!==
-
1
)
{
}
else
if
(
self
.
sql
.
indexOf
(
'PRAGMA TABLE_INFO'
)
!==
-
1
)
{
// this is the sqlite way of getting the metadata of a table
// this is the sqlite way of getting the metadata of a table
result
=
{}
result
=
{}
...
@@ -169,19 +120,62 @@ module.exports = (function() {
...
@@ -169,19 +120,62 @@ module.exports = (function() {
result
[
_result
.
name
].
defaultValue
=
result
[
_result
.
name
].
defaultValue
.
replace
(
/'/g
,
""
)
result
[
_result
.
name
].
defaultValue
=
result
[
_result
.
name
].
defaultValue
.
replace
(
/'/g
,
""
)
}
}
})
})
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA foreign_keys;'
)
!==
-
1
)
{
}
else
if
(
self
.
sql
.
indexOf
(
'PRAGMA foreign_keys;'
)
!==
-
1
)
{
result
=
results
[
0
]
result
=
results
[
0
]
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA foreign_keys'
)
!==
-
1
)
{
}
else
if
(
self
.
sql
.
indexOf
(
'PRAGMA foreign_keys'
)
!==
-
1
)
{
result
=
results
result
=
results
}
else
if
([
QueryTypes
.
BULKUPDATE
,
QueryTypes
.
BULKDELETE
].
indexOf
(
this
.
options
.
type
)
!==
-
1
)
{
}
else
if
([
QueryTypes
.
BULKUPDATE
,
QueryTypes
.
BULKDELETE
].
indexOf
(
self
.
options
.
type
)
!==
-
1
)
{
result
=
metaData
.
changes
result
=
metaData
.
changes
}
}
this
.
emit
(
'success'
,
result
)
resolve
(
result
)
}
})
}))
}
}
if
((
getDatabaseMethod
.
call
(
self
)
===
'all'
))
{
var
tableNames
=
[]
if
(
self
.
options
&&
self
.
options
.
tableNames
)
{
tableNames
=
self
.
options
.
tableNames
}
else
if
(
/FROM `
(
.*
?)
`/i
.
exec
(
self
.
sql
))
{
tableNames
.
push
(
/FROM `
(
.*
?)
`/i
.
exec
(
self
.
sql
)[
1
])
}
if
(
!
tableNames
.
length
)
{
return
executeSql
()
}
else
{
return
Utils
.
Promise
.
map
(
tableNames
,
function
(
tableName
)
{
if
(
tableName
!==
'sqlite_master'
)
{
return
new
Utils
.
Promise
(
function
(
resolve
)
{
// get the column types
self
.
database
.
all
(
"PRAGMA table_info("
+
tableName
+
")"
,
function
(
err
,
results
)
{
if
(
!
err
)
{
for
(
var
i
=
0
,
l
=
results
.
length
;
i
<
l
;
i
++
)
{
columnTypes
[
tableName
+
'.'
+
results
[
i
].
name
]
=
columnTypes
[
results
[
i
].
name
]
=
results
[
i
].
type
}
}
resolve
()
});
})
}
}).
then
(
executeSql
)
}
}
else
{
return
executeSql
()
}
})
})
}
}
var
onFailure
=
function
(
err
)
{
//private
this
.
emit
(
'error'
,
err
,
this
.
callee
)
var
getDatabaseMethod
=
function
()
{
if
(
this
.
send
(
'isInsertQuery'
)
||
this
.
send
(
'isUpdateQuery'
)
||
(
this
.
sql
.
toLowerCase
().
indexOf
(
'CREATE TEMPORARY TABLE'
.
toLowerCase
())
!==
-
1
)
||
this
.
options
.
type
===
QueryTypes
.
BULKDELETE
)
{
return
'run'
}
else
{
return
'all'
}
}
}
return
Query
return
Query
...
...
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