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 151fa019
authored
Aug 02, 2014
by
Sascha Depold
Committed by
Matt Broadstone
Dec 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix dropTableQuery
1 parent
8f4f2f12
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
248 additions
and
10 deletions
.jshintrc
lib/dialects/mssql/query-generator.js
lib/dialects/mssql/query.js
lib/query-interface.js
test/query-interface.test.js
.jshintrc
View file @
151fa01
...
...
@@ -35,5 +35,8 @@
"beforeEach",
"after",
"afterEach"
]
],
"globals":{
"Promise": true
}
}
lib/dialects/mssql/query-generator.js
View file @
151fa01
'use strict'
;
var
Utils
=
require
(
'../../utils'
);
var
Utils
=
require
(
'../../utils'
)
,
DataTypes
=
require
(
'../../data-types'
)
,
util
=
require
(
'util'
);
//drop table Group
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
...
...
@@ -12,13 +17,18 @@ module.exports = (function() {
},
showTablesQuery
:
function
()
{
return
'SELECT
*
FROM sys.Tables'
;
return
'SELECT
name
FROM sys.Tables'
;
},
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{};
var
query
=
"IF OBJECT_ID('dbo.<%= tableName %>', 'U') IS NOT NULL DROP TABLE dbo.<%= tableName %>"
;
var
query
=
"IF (EXISTS ("
+
"SELECT * FROM INFORMATION_SCHEMA.TABLES"
+
" WHERE TABLE_NAME='<%= tableName %>'))"
+
" BEGIN"
+
" DROP TABLE \"<%= tableName %>\""
+
" END"
;
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
...
...
@@ -26,13 +36,19 @@ module.exports = (function() {
},
createTableQuery
:
function
(
tableName
,
attributes
,
options
)
{
var
query
=
"IF OBJECT_ID('<%= unquotedTable %>', N'U') IS NULL CREATE TABLE <%= table %> (<%= attributes%>)"
,
attrStr
=
[]
var
attrStr
=
[]
,
self
=
this
,
primaryKeys
=
Utils
.
_
.
keys
(
Utils
.
_
.
pick
(
attributes
,
function
(
dataType
){
return
dataType
.
indexOf
(
'PRIMARY KEY'
)
>=
0
;
}));
var
query
=
"IF (NOT EXISTS ("
+
"SELECT * FROM INFORMATION_SCHEMA.TABLES"
+
" WHERE TABLE_NAME='<%= tableName %>'))"
+
" BEGIN"
+
" CREATE TABLE <%= tableName %> (<%= attributes%>)"
+
" END"
;
for
(
var
attr
in
attributes
)
{
if
(
attributes
.
hasOwnProperty
(
attr
))
{
var
dataType
=
attributes
[
attr
];
...
...
@@ -51,11 +67,211 @@ module.exports = (function() {
var
values
=
{
unquotedTable
:
tableName
,
table
:
self
.
quote
(
tableName
),
table
Name
:
self
.
quote
(
tableName
),
attributes
:
attrStr
.
join
(
", "
)
};
return
Utils
.
_
.
template
(
query
)(
values
).
trim
()
+
";"
;
},
getDataType
:
function
(
type
){
switch
(
type
){
case
'TINYINT(1)'
:
return
'BIT'
;
default
:
return
type
;
}
},
showIndexQuery
:
function
(
tableName
,
options
)
{
var
sql
=
"SELECT"
+
" TableName = t.name,"
+
" IndexName = ind.name,"
+
" IndexId = ind.index_id,"
+
" ColumnId = ic.index_column_id,"
+
" ColumnName = col.name"
+
" FROM"
+
" sys.indexes ind"
+
" INNER JOIN "
+
" sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id"
+
" INNER JOIN"
+
" sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id"
+
" INNER JOIN "
+
" sys.tables t ON ind.object_id = t.object_id"
+
" WHERE t.name = '<%= tableName %>'<%= options %>"
;
return
Utils
.
_
.
template
(
sql
)({
tableName
:
tableName
,
options
:
(
options
||
{}).
database
?
' FROM `'
+
options
.
database
+
'`'
:
''
});
},
addIndexQuery
:
function
(
tableName
,
attributes
,
options
,
rawTablename
)
{
options
=
options
||
{};
var
transformedAttributes
=
attributes
.
map
(
function
(
attribute
)
{
if
(
typeof
attribute
===
'string'
)
{
return
this
.
quoteIdentifier
(
attribute
);
}
else
{
var
result
=
''
;
if
(
!
attribute
.
attribute
)
{
throw
new
Error
(
'The following index attribute has no attribute: '
+
util
.
inspect
(
attribute
));
}
result
+=
this
.
quoteIdentifier
(
attribute
.
attribute
);
if
(
this
.
_dialect
.
supports
.
index
.
collate
&&
attribute
.
collate
)
{
result
+=
' COLLATE '
+
this
.
quoteIdentifier
(
attribute
.
collate
);
}
if
(
this
.
_dialect
.
supports
.
index
.
length
&&
attribute
.
length
)
{
result
+=
'('
+
attribute
.
length
+
')'
;
}
if
(
attribute
.
order
)
{
result
+=
' '
+
attribute
.
order
;
}
return
result
;
}
}.
bind
(
this
));
var
onlyAttributeNames
=
attributes
.
map
(
function
(
attribute
)
{
return
(
typeof
attribute
===
'string'
)
?
attribute
:
attribute
.
attribute
;
}.
bind
(
this
));
options
=
Utils
.
_
.
defaults
(
options
,
{
type
:
''
,
indicesType
:
options
.
type
||
''
,
indexType
:
options
.
method
||
undefined
,
indexName
:
options
.
name
||
Utils
.
inflection
.
underscore
(
rawTablename
+
'_'
+
onlyAttributeNames
.
join
(
'_'
)),
parser
:
null
});
if
(
options
.
indicesType
.
toLowerCase
()
===
'unique'
)
{
options
.
unique
=
true
;
delete
options
.
indicesType
;
}
if
(
!
this
.
_dialect
.
supports
.
index
.
type
)
{
delete
options
.
indicesType
;
}
return
Utils
.
_
.
compact
([
'CREATE'
,
options
.
unique
?
'UNIQUE'
:
''
,
options
.
indicesType
,
'INDEX'
,
this
.
_dialect
.
supports
.
index
.
concurrently
&&
options
.
concurrently
?
'CONCURRENTLY'
:
undefined
,
this
.
quoteIdentifiers
(
options
.
indexName
),
this
.
_dialect
.
supports
.
index
.
using
===
1
&&
options
.
indexType
?
'USING '
+
options
.
indexType
:
''
,
'ON'
,
this
.
quoteIdentifiers
(
tableName
),
this
.
_dialect
.
supports
.
index
.
using
===
2
&&
options
.
indexType
?
'USING '
+
options
.
indexType
:
''
,
'('
+
transformedAttributes
.
join
(
', '
)
+
')'
,
(
this
.
_dialect
.
supports
.
index
.
parser
&&
options
.
parser
?
'WITH PARSER '
+
options
.
parser
:
undefined
)
]).
join
(
' '
);
},
attributesToSQL
:
function
(
attributes
)
{
var
result
=
{};
for
(
var
name
in
attributes
)
{
var
dataType
=
attributes
[
name
];
if
(
Utils
.
_
.
isPlainObject
(
dataType
))
{
var
template
;
if
(
dataType
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
))
{
template
=
'ENUM('
+
Utils
.
_
.
map
(
dataType
.
values
,
function
(
value
)
{
return
this
.
escape
(
value
);
}.
bind
(
this
)).
join
(
', '
)
+
')'
;
}
else
{
throw
new
Error
(
'Values for ENUM haven\'t been defined.'
);
}
}
else
{
template
=
this
.
getDataType
(
dataType
.
type
.
toString
());
}
if
(
dataType
.
hasOwnProperty
(
'allowNull'
)
&&
(
!
dataType
.
allowNull
))
{
template
+=
' NOT NULL'
;
}
if
(
dataType
.
autoIncrement
)
{
template
+=
' auto_increment'
;
}
// Blobs/texts cannot have a defaultValue
if
(
dataType
.
type
!==
'TEXT'
&&
dataType
.
type
.
_binary
!==
true
&&
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
template
+=
' DEFAULT '
+
this
.
escape
(
dataType
.
defaultValue
);
}
if
(
dataType
.
unique
===
true
)
{
template
+=
' UNIQUE'
;
}
if
(
dataType
.
primaryKey
)
{
template
+=
' PRIMARY KEY'
;
}
if
(
dataType
.
comment
&&
Utils
.
_
.
isString
(
dataType
.
comment
)
&&
dataType
.
comment
.
length
)
{
template
+=
' COMMENT '
+
this
.
escape
(
dataType
.
comment
);
}
if
(
dataType
.
references
)
{
template
+=
' REFERENCES '
+
this
.
quoteTable
(
dataType
.
references
);
if
(
dataType
.
referencesKey
)
{
template
+=
' ('
+
this
.
quoteIdentifier
(
dataType
.
referencesKey
)
+
')'
;
}
else
{
template
+=
' ('
+
this
.
quoteIdentifier
(
'id'
)
+
')'
;
}
if
(
dataType
.
onDelete
)
{
template
+=
' ON DELETE '
+
dataType
.
onDelete
.
toUpperCase
();
}
if
(
dataType
.
onUpdate
)
{
template
+=
' ON UPDATE '
+
dataType
.
onUpdate
.
toUpperCase
();
}
}
result
[
name
]
=
template
;
}
else
{
result
[
name
]
=
dataType
;
}
}
console
.
log
(
'results'
,
result
);
return
result
;
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
* @param {String} tableName The name of the table.
* @param {String} schemaName The name of the schema.
* @return {String} The generated sql query.
*/
getForeignKeysQuery
:
function
(
tableName
,
schemaName
)
{
return
[
"SELECT"
,
"K_Table = FK.TABLE_NAME, FK_Column = CU.COLUMN_NAME, PK_Table = PK.TABLE_NAME, PK_Column = PT.COLUMN_NAME, Constraint_Name = C.CONSTRAINT_NAME"
,
"FROM"
,
"INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C"
,
"INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME"
,
"INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME"
,
"INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME"
,
"INNER JOIN ("
,
"SELECT"
,
"i1.TABLE_NAME,"
,
"i2.COLUMN_NAME"
,
"FROM"
,
"INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1"
,
"INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2"
,
"ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME"
,
"WHERE"
,
"i1.CONSTRAINT_TYPE = 'PRIMARY KEY'"
,
") PT ON PT.TABLE_NAME = PK.TABLE_NAME"
].
join
(
" "
);
}
};
...
...
lib/dialects/mssql/query.js
View file @
151fa01
...
...
@@ -31,7 +31,7 @@ module.exports = (function() {
}
var
promise
=
new
Utils
.
Promise
(
function
(
resolve
,
reject
)
{
console
.
log
(
self
.
sql
);
//
console.log(self.sql);
self
.
connection
...
...
lib/query-interface.js
View file @
151fa01
...
...
@@ -225,6 +225,7 @@ module.exports = (function() {
var
skip
=
options
.
skip
||
[];
return
self
.
showAllTables
().
then
(
function
(
tableNames
)
{
//console.log(tableNames);
if
(
self
.
sequelize
.
options
.
dialect
===
'sqlite'
)
{
return
self
.
sequelize
.
query
(
'PRAGMA foreign_keys;'
).
then
(
function
(
result
)
{
var
foreignKeysAreEnabled
=
result
.
foreign_keys
===
1
;
...
...
@@ -284,6 +285,7 @@ module.exports = (function() {
QueryInterface
.
prototype
.
showAllTables
=
function
(
options
)
{
var
self
=
this
;
options
=
Utils
.
_
.
extend
({
transaction
:
null
,
raw
:
true
...
...
@@ -292,7 +294,20 @@ module.exports = (function() {
var
showTablesSql
=
self
.
QueryGenerator
.
showTablesQuery
();
return
self
.
sequelize
.
query
(
showTablesSql
,
null
,
options
).
then
(
function
(
tableNames
)
{
return
Utils
.
_
.
flatten
(
tableNames
);
tableNames
=
Utils
.
_
.
flatten
(
tableNames
);
if
(
self
.
sequelize
.
options
.
dialect
===
'mssql'
)
{
tableNames
=
tableNames
.
map
(
function
(
smth
)
{
if
(
Utils
.
_
.
isObject
(
smth
))
{
return
Utils
.
_
.
values
(
smth
)[
0
];
}
else
{
return
smth
;
}
});
}
//console.log('done yeah', arguments);
return
tableNames
;
});
};
...
...
test/query-interface.test.js
View file @
151fa01
...
...
@@ -88,7 +88,11 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
self
.
queryInterface
.
showIndex
(
'Group'
).
complete
(
function
(
err
,
indexes
)
{
expect
(
err
).
to
.
be
.
null
var
indexColumns
=
_
.
uniq
(
indexes
.
map
(
function
(
index
)
{
return
index
.
name
}))
var
indexColumns
=
_
.
uniq
(
indexes
.
map
(
function
(
index
)
{
console
.
log
(
'index:'
,
index
)
return
index
.
name
}))
expect
(
indexColumns
).
to
.
include
(
'group_username_is_admin'
)
self
.
queryInterface
.
removeIndex
(
'Group'
,
[
'username'
,
'isAdmin'
]).
complete
(
function
(
err
)
{
...
...
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