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 27c447c0
authored
Mar 01, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unified metadata receival
1 parent
e893107e
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
111 additions
and
28 deletions
lib/dialects/abstract/query.js
lib/dialects/postgres/query.js
lib/dialects/sqlite/query.js
lib/query-interface.js
spec/migrator.spec.js
spec/query-interface.spec.js
lib/dialects/abstract/query.js
View file @
27c447c
...
@@ -70,6 +70,27 @@ module.exports = (function() {
...
@@ -70,6 +70,27 @@ module.exports = (function() {
result
=
handleShowTableQuery
.
call
(
this
,
data
)
result
=
handleShowTableQuery
.
call
(
this
,
data
)
}
else
if
(
isShowOrDescribeQuery
.
call
(
this
))
{
}
else
if
(
isShowOrDescribeQuery
.
call
(
this
))
{
result
=
data
result
=
data
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
'describe'
)
===
0
)
{
result
=
result
.
map
(
function
(
result
)
{
return
{
attribute
:
result
.
Field
,
type
:
result
.
Type
.
toUpperCase
(),
allowNull
:
(
result
.
Null
===
'YES'
),
defaultValue
:
result
.
Default
}
})
}
else
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
'show index from'
)
===
0
)
{
result
=
Utils
.
_
.
uniq
(
result
.
map
(
function
(
result
)
{
return
{
name
:
result
.
Key_name
,
tableName
:
result
.
Table
,
unique
:
(
result
.
Non_unique
!==
1
)
}
}),
false
,
function
(
row
)
{
return
row
.
name
})
}
}
else
if
(
isCallQuery
.
call
(
this
))
{
}
else
if
(
isCallQuery
.
call
(
this
))
{
result
=
data
[
0
]
result
=
data
[
0
]
}
}
...
...
lib/dialects/postgres/query.js
View file @
27c447c
...
@@ -59,11 +59,34 @@ module.exports = (function() {
...
@@ -59,11 +59,34 @@ module.exports = (function() {
,
isRelNameQuery
=
(
this
.
sql
.
indexOf
(
'SELECT relname FROM pg_class WHERE oid IN'
)
===
0
)
,
isRelNameQuery
=
(
this
.
sql
.
indexOf
(
'SELECT relname FROM pg_class WHERE oid IN'
)
===
0
)
if
(
isTableNameQuery
||
isRelNameQuery
)
{
if
(
isTableNameQuery
||
isRelNameQuery
)
{
return
this
.
emit
(
'success'
,
rows
.
map
(
function
(
row
)
{
return
Utils
.
_
.
values
(
row
)
}))
if
(
isRelNameQuery
)
{
results
=
rows
.
map
(
function
(
row
)
{
return
{
name
:
row
.
relname
,
tableName
:
row
.
relname
.
split
(
'_'
)[
0
]
}
})
}
else
{
results
=
rows
.
map
(
function
(
row
)
{
return
Utils
.
_
.
values
(
row
)
})
}
return
this
.
emit
(
'success'
,
results
)
}
}
if
(
this
.
send
(
'isSelectQuery'
))
{
if
(
this
.
send
(
'isSelectQuery'
))
{
if
(
this
.
sql
.
toLowerCase
().
indexOf
(
'select column_name'
)
===
0
)
{
rows
=
rows
.
map
(
function
(
result
)
{
return
{
attribute
:
result
.
Field
,
type
:
result
.
Type
.
toUpperCase
(),
allowNull
:
(
result
.
Null
===
'YES'
),
defaultValue
:
result
.
Default
}
})
this
.
emit
(
'success'
,
rows
)
}
else
{
this
.
emit
(
'success'
,
this
.
send
(
'handleSelectQuery'
,
rows
))
this
.
emit
(
'success'
,
this
.
send
(
'handleSelectQuery'
,
rows
))
}
}
else
if
(
this
.
send
(
'isShowOrDescribeQuery'
))
{
}
else
if
(
this
.
send
(
'isShowOrDescribeQuery'
))
{
this
.
emit
(
'success'
,
results
)
this
.
emit
(
'success'
,
results
)
}
else
if
(
this
.
send
(
'isInsertQuery'
))
{
}
else
if
(
this
.
send
(
'isInsertQuery'
))
{
...
...
lib/dialects/sqlite/query.js
View file @
27c447c
...
@@ -103,10 +103,23 @@ module.exports = (function() {
...
@@ -103,10 +103,23 @@ module.exports = (function() {
result
=
results
result
=
results
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA INDEX_LIST'
)
!==
-
1
)
{
}
else
if
(
this
.
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
)
{
return
{
Key_name
:
result
.
name
}
})
result
=
results
.
map
(
function
(
result
)
{
return
{
name
:
result
.
name
,
tableName
:
result
.
name
.
split
(
'_'
)[
0
],
unique
:
(
result
.
unique
===
0
)
}
})
}
else
if
(
this
.
sql
.
indexOf
(
'PRAGMA TABLE_INFO'
)
!==
-
1
)
{
}
else
if
(
this
.
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
console
.
log
(
results
)
result
=
results
.
map
(
function
(
result
)
{
return
{
attribute
:
result
.
name
,
type
:
result
.
type
,
allowNull
:
(
result
.
notnull
===
0
),
defaultValue
:
result
.
dflt_value
}
})
}
}
this
.
emit
(
'success'
,
result
)
this
.
emit
(
'success'
,
result
)
...
...
lib/query-interface.js
View file @
27c447c
...
@@ -138,14 +138,15 @@ module.exports = (function() {
...
@@ -138,14 +138,15 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
describeTable
(
tableName
).
success
(
function
(
data
)
{
self
.
describeTable
(
tableName
).
success
(
function
(
data
)
{
data
=
data
.
filter
(
function
(
h
)
{
return
h
.
Field
==
attrNameBefore
})[
0
]
||
{}
data
=
data
.
filter
(
function
(
h
)
{
return
h
.
attribute
==
attrNameBefore
})[
0
]
||
{}
var
options
=
{}
var
options
=
{}
options
[
attrNameAfter
]
=
{
options
[
attrNameAfter
]
=
{
type
:
data
.
Type
,
attribute
:
data
.
attribute
,
allowNull
:
data
.
Null
==
'YES'
,
type
:
data
.
type
,
defaultValue
:
data
.
Default
allowNull
:
data
.
allowNull
,
defaultValue
:
data
.
defaultValue
}
}
var
sql
=
self
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
var
sql
=
self
.
QueryGenerator
.
renameColumnQuery
(
tableName
,
...
...
spec/migrator.spec.js
View file @
27c447c
...
@@ -121,9 +121,9 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
...
@@ -121,9 +121,9 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
})
})
})
})
;(
dialect
===
'sqlite'
?
itEventually
:
it
)
(
"executes migration #20111117063700 and correctly adds isBetaMember"
,
function
(
done
)
{
it
(
"executes migration #20111117063700 and correctly adds isBetaMember"
,
function
(
done
)
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'Person'
).
success
(
function
(
data
)
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'Person'
).
success
(
function
(
data
)
{
var
fields
=
data
.
map
(
function
(
d
)
{
return
d
.
Field
}).
sort
()
var
fields
=
data
.
map
(
function
(
d
)
{
return
d
.
attribute
}).
sort
()
expect
(
fields
).
toEqual
([
'isBetaMember'
,
'name'
])
expect
(
fields
).
toEqual
([
'isBetaMember'
,
'name'
])
done
()
done
()
})
})
...
@@ -190,7 +190,6 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
...
@@ -190,7 +190,6 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this
.
init
({
from
:
20111117063700
,
to
:
20111205162700
},
function
(
migrator
)
{
this
.
init
({
from
:
20111117063700
,
to
:
20111205162700
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
()
{
migrator
.
migrate
().
success
(
function
()
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
console
.
log
(
data
)
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'signature'
})[
0
]
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'signature'
})[
0
]
,
isAdmin
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'isAdmin'
})[
0
]
,
isAdmin
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'isAdmin'
})[
0
]
,
shopId
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'shopId'
})[
0
]
,
shopId
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'shopId'
})[
0
]
...
@@ -217,16 +216,16 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
...
@@ -217,16 +216,16 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this
.
init
({
to
:
20111206061400
},
function
(
migrator
)
{
this
.
init
({
to
:
20111206061400
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
migrator
.
migrate
().
success
(
function
(){
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'signature'
})[
0
]
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'signature'
})[
0
]
,
isAdmin
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'isAdmin'
})[
0
]
,
isAdmin
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'isAdmin'
})[
0
]
,
shopId
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'shopId'
})[
0
]
,
shopId
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'shopId'
})[
0
]
expect
(
signature
.
Field
).
toEqual
(
'signature'
)
expect
(
signature
.
attribute
).
toEqual
(
'signature'
)
expect
(
signature
.
Null
).
toEqual
(
'NO'
)
expect
(
signature
.
allowNull
).
toEqual
(
false
)
expect
(
isAdmin
.
Field
).
toEqual
(
'isAdmin'
)
expect
(
isAdmin
.
attribute
).
toEqual
(
'isAdmin'
)
expect
(
isAdmin
.
Null
).
toEqual
(
'NO'
)
expect
(
isAdmin
.
allowNull
).
toEqual
(
false
)
expect
(
isAdmin
.
Default
).
toEqual
(
'0'
)
expect
(
isAdmin
.
defaultValue
).
toEqual
(
'0'
)
expect
(
shopId
).
toBeFalsy
()
expect
(
shopId
).
toBeFalsy
()
...
@@ -242,12 +241,12 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
...
@@ -242,12 +241,12 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this
.
init
({
to
:
20111206063000
},
function
(
migrator
)
{
this
.
init
({
to
:
20111206063000
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
()
{
migrator
.
migrate
().
success
(
function
()
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'signature'
})[
0
]
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
==
'signature'
})[
0
]
expect
(
signature
.
Field
).
toEqual
(
'signature'
)
expect
(
signature
.
attribute
).
toEqual
(
'signature'
)
expect
(
signature
.
Type
).
toEqual
(
'varchar
(255)'
)
expect
(
signature
.
type
).
toEqual
(
'VARCHAR
(255)'
)
expect
(
signature
.
Null
).
toEqual
(
'NO'
)
expect
(
signature
.
allowNull
).
toEqual
(
false
)
expect
(
signature
.
Default
).
toEqual
(
'Signature'
)
expect
(
signature
.
defaultValue
).
toEqual
(
'Signature'
)
done
()
done
()
})
})
...
@@ -262,8 +261,8 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
...
@@ -262,8 +261,8 @@ describe(Helpers.getTestDialectTeaser("Migrator"), function() {
this
.
init
({
to
:
20111206163300
},
function
(
migrator
)
{
this
.
init
({
to
:
20111206163300
},
function
(
migrator
)
{
migrator
.
migrate
().
success
(
function
(){
migrator
.
migrate
().
success
(
function
(){
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
this
.
sequelize
.
getQueryInterface
().
describeTable
(
'User'
).
success
(
function
(
data
)
{
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'signature'
})[
0
]
var
signature
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
=
==
'signature'
})[
0
]
,
sig
=
data
.
filter
(
function
(
hash
){
return
hash
.
Field
==
'sig'
})[
0
]
,
sig
=
data
.
filter
(
function
(
hash
){
return
hash
.
attribute
=
==
'sig'
})[
0
]
expect
(
signature
).
toBeFalsy
()
expect
(
signature
).
toBeFalsy
()
expect
(
sig
).
toBeTruthy
()
expect
(
sig
).
toBeTruthy
()
...
...
spec/query-interface.spec.js
View file @
27c447c
...
@@ -54,7 +54,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
...
@@ -54,7 +54,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
})
})
})
})
describe
(
'
=>
indexes'
,
function
()
{
describe
(
'indexes'
,
function
()
{
before
(
function
(
done
)
{
before
(
function
(
done
)
{
this
.
interface
.
createTable
(
'User'
,
{
this
.
interface
.
createTable
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
,
username
:
Helpers
.
Sequelize
.
STRING
,
...
@@ -69,7 +69,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
...
@@ -69,7 +69,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
this
.
interface
.
showIndex
(
'User'
).
complete
(
function
(
err
,
indexes
)
{
this
.
interface
.
showIndex
(
'User'
).
complete
(
function
(
err
,
indexes
)
{
expect
(
err
).
toBeNull
()
expect
(
err
).
toBeNull
()
var
indexColumns
=
Helpers
.
Sequelize
.
Utils
.
_
.
uniq
(
indexes
.
map
(
function
(
index
)
{
return
index
.
Key_
name
}))
var
indexColumns
=
Helpers
.
Sequelize
.
Utils
.
_
.
uniq
(
indexes
.
map
(
function
(
index
)
{
return
index
.
name
}))
expect
(
indexColumns
).
toEqual
([
'user_username_is_admin'
])
expect
(
indexColumns
).
toEqual
([
'user_username_is_admin'
])
this
.
interface
.
removeIndex
(
'User'
,
[
'username'
,
'isAdmin'
]).
complete
(
function
(
err
)
{
this
.
interface
.
removeIndex
(
'User'
,
[
'username'
,
'isAdmin'
]).
complete
(
function
(
err
)
{
...
@@ -78,7 +78,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
...
@@ -78,7 +78,7 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
this
.
interface
.
showIndex
(
'User'
).
complete
(
function
(
err
,
indexes
)
{
this
.
interface
.
showIndex
(
'User'
).
complete
(
function
(
err
,
indexes
)
{
expect
(
err
).
toBeNull
()
expect
(
err
).
toBeNull
()
indexColumns
=
Helpers
.
Sequelize
.
Utils
.
_
.
uniq
(
indexes
.
map
(
function
(
index
)
{
return
index
.
Key_
name
}))
indexColumns
=
Helpers
.
Sequelize
.
Utils
.
_
.
uniq
(
indexes
.
map
(
function
(
index
)
{
return
index
.
name
}))
expect
(
indexColumns
).
toEqual
([])
expect
(
indexColumns
).
toEqual
([])
done
()
done
()
...
@@ -90,6 +90,32 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
...
@@ -90,6 +90,32 @@ describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
})
})
describe
(
'describeTable'
,
function
()
{
describe
(
'describeTable'
,
function
()
{
before
(
function
(
done
)
{
this
.
interface
.
createTable
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
,
isAdmin
:
Helpers
.
Sequelize
.
BOOLEAN
}).
success
(
done
)
})
it
(
'reads the metadata of the table'
,
function
(
done
)
{
this
.
interface
.
describeTable
(
'User'
).
complete
(
function
(
err
,
metadata
)
{
expect
(
err
).
toBeNull
()
var
username
=
metadata
.
filter
(
function
(
m
)
{
return
m
.
attribute
===
'username'
})[
0
]
var
isAdmin
=
metadata
.
filter
(
function
(
m
)
{
return
m
.
attribute
===
'isAdmin'
})[
0
]
expect
(
username
.
attribute
).
toEqual
(
'username'
)
expect
(
username
.
type
).
toEqual
(
dialect
===
'postgres'
?
'CHARACTER VARYING'
:
'VARCHAR(255)'
)
expect
(
username
.
allowNull
).
toBeTrue
()
expect
(
username
.
defaultValue
).
toBeNull
()
expect
(
isAdmin
.
attribute
).
toEqual
(
'isAdmin'
)
expect
(
isAdmin
.
type
).
toEqual
(
dialect
===
'postgres'
?
'BOOLEAN'
:
'TINYINT(1)'
)
expect
(
isAdmin
.
allowNull
).
toBeTrue
()
expect
(
isAdmin
.
defaultValue
).
toBeNull
()
done
()
})
})
})
})
})
})
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