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 a7fa3161
authored
Oct 15, 2018
by
Wei-Liang Liou
Committed by
Sushant
Oct 15, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(query-interface): createDatabase / dropDatabase support (#10027)
1 parent
cf6c05e0
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
172 additions
and
0 deletions
lib/dialects/mssql/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/query-interface.js
test/unit/dialects/mssql/query-generator.test.js
test/unit/dialects/mysql/query-generator.test.js
test/unit/dialects/postgres/query-generator.test.js
lib/dialects/mssql/query-generator.js
View file @
a7fa316
...
@@ -15,6 +15,35 @@ const throwMethodUndefined = function(methodName) {
...
@@ -15,6 +15,35 @@ const throwMethodUndefined = function(methodName) {
};
};
class
MSSQLQueryGenerator
extends
AbstractQueryGenerator
{
class
MSSQLQueryGenerator
extends
AbstractQueryGenerator
{
createDatabaseQuery
(
databaseName
,
options
)
{
options
=
_
.
extend
({
collate
:
null
},
options
||
{});
const
query
=
[
'IF NOT EXISTS (SELECT * FROM sys.databases WHERE name ='
,
wrapSingleQuote
(
databaseName
),
')'
,
'BEGIN'
,
'CREATE DATABASE'
,
this
.
quoteIdentifier
(
databaseName
),
'<%= collation %>;'
,
'END;'
].
join
(
' '
);
const
values
=
{
collation
:
options
.
collate
?
'COLLATE '
+
this
.
escape
(
options
.
collate
)
:
''
};
return
_
.
template
(
query
,
this
.
_templateSettings
)(
values
).
trim
();
}
dropDatabaseQuery
(
databaseName
)
{
return
[
'IF EXISTS (SELECT * FROM sys.databases WHERE name ='
,
wrapSingleQuote
(
databaseName
),
')'
,
'BEGIN'
,
'DROP DATABASE'
,
this
.
quoteIdentifier
(
databaseName
),
';'
,
'END;'
].
join
(
' '
);
}
createSchema
(
schema
)
{
createSchema
(
schema
)
{
return
[
return
[
'IF NOT EXISTS (SELECT schema_name'
,
'IF NOT EXISTS (SELECT schema_name'
,
...
...
lib/dialects/mysql/query-generator.js
View file @
a7fa316
...
@@ -16,6 +16,33 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
...
@@ -16,6 +16,33 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
});
});
}
}
createDatabaseQuery
(
databaseName
,
options
)
{
options
=
_
.
extend
({
charset
:
null
,
collate
:
null
},
options
||
{});
const
query
=
'CREATE DATABASE IF NOT EXISTS <%= database %><%= charset %><%= collation %>'
;
const
values
=
{
database
:
this
.
quoteIdentifier
(
databaseName
),
charset
:
options
.
charset
?
' DEFAULT CHARSET SET = '
+
this
.
escape
(
options
.
charset
)
:
''
,
collation
:
options
.
collate
?
' DEFAULT COLLATE = '
+
this
.
escape
(
options
.
collate
)
:
''
};
return
_
.
template
(
query
,
this
.
_templateSettings
)(
values
).
trim
()
+
';'
;
}
dropDatabaseQuery
(
databaseName
)
{
const
query
=
'DROP DATABASE IF EXISTS <%= database %>'
;
const
values
=
{
database
:
this
.
quoteIdentifier
(
databaseName
)
};
return
_
.
template
(
query
,
this
.
_templateSettings
)(
values
).
trim
()
+
';'
;
}
createSchema
()
{
createSchema
()
{
return
'SHOW TABLES'
;
return
'SHOW TABLES'
;
}
}
...
...
lib/dialects/postgres/query-generator.js
View file @
a7fa316
...
@@ -12,6 +12,25 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
...
@@ -12,6 +12,25 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
return
`SET search_path to
${
searchPath
}
;`
;
return
`SET search_path to
${
searchPath
}
;`
;
}
}
createDatabaseQuery
(
databaseName
,
options
)
{
options
=
_
.
extend
({
encoding
:
null
,
collate
:
null
},
options
||
{});
const
values
=
{
database
:
this
.
quoteTable
(
databaseName
),
encoding
:
options
.
encoding
?
' ENCODING = '
+
this
.
escape
(
options
.
encoding
)
:
''
,
collation
:
options
.
collate
?
' LC_COLLATE = '
+
this
.
escape
(
options
.
collate
)
:
''
};
return
`CREATE DATABASE IF NOT EXISTS
${
values
.
database
}${
values
.
encoding
}${
values
.
collation
}
;`
;
}
dropDatabaseQuery
(
databaseName
)
{
return
`DROP DATABASE IF EXISTS
${
this
.
quoteTable
(
databaseName
)}
CASCADE;`
;
}
createSchema
(
schema
)
{
createSchema
(
schema
)
{
const
databaseVersion
=
_
.
get
(
this
,
'sequelize.options.databaseVersion'
,
0
);
const
databaseVersion
=
_
.
get
(
this
,
'sequelize.options.databaseVersion'
,
0
);
...
...
lib/query-interface.js
View file @
a7fa316
...
@@ -25,6 +25,37 @@ class QueryInterface {
...
@@ -25,6 +25,37 @@ class QueryInterface {
}
}
/**
/**
* Creates a database
*
* @param {string} schema Schema name to create
* @param {Object} [options] Query options
* @param {string} [options.charset] Database default character set, MYSQL only
* @param {string} [options.encoding] Database default character set, PostgreSQL only
* @param {string} [options.collate] Database default collation
*
* @returns {Promise}
*/
createDatabase
(
schema
,
options
)
{
options
=
options
||
{};
const
sql
=
this
.
QueryGenerator
.
createDatabaseQuery
(
schema
,
options
);
return
this
.
sequelize
.
query
(
sql
,
options
);
}
/**
* Drops a database
*
* @param {string} schema Schema name to drop
* @param {Object} [options] Query options
*
* @returns {Promise}
*/
dropDatabase
(
schema
,
options
)
{
options
=
options
||
{};
const
sql
=
this
.
QueryGenerator
.
dropDatabaseQuery
(
schema
);
return
this
.
sequelize
.
query
(
sql
,
options
);
}
/**
* Creates a schema
* Creates a schema
*
*
* @param {string} schema Schema name to create
* @param {string} schema Schema name to create
...
...
test/unit/dialects/mssql/query-generator.test.js
View file @
a7fa316
...
@@ -15,6 +15,24 @@ if (current.dialect.name === 'mssql') {
...
@@ -15,6 +15,24 @@ if (current.dialect.name === 'mssql') {
});
});
});
});
it
(
'createDatabaseQuery'
,
function
()
{
expectsql
(
this
.
queryGenerator
.
createDatabaseQuery
(
'myDatabase'
),
{
mssql
:
"IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'myDatabase' ) BEGIN CREATE DATABASE [myDatabase] ; END;"
});
});
it
(
'createDatabaseQuery with collate'
,
function
()
{
expectsql
(
this
.
queryGenerator
.
createDatabaseQuery
(
'myDatabase'
,
{
collate
:
'Latin1_General_CS_AS_KS_WS'
}),
{
mssql
:
"IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'myDatabase' ) BEGIN CREATE DATABASE [myDatabase] COLLATE N'Latin1_General_CS_AS_KS_WS'; END;"
});
});
it
(
'dropDatabaseQuery'
,
function
()
{
expectsql
(
this
.
queryGenerator
.
dropDatabaseQuery
(
'myDatabase'
),
{
mssql
:
"IF EXISTS (SELECT * FROM sys.databases WHERE name = 'myDatabase' ) BEGIN DROP DATABASE [myDatabase] ; END;"
});
});
it
(
'createTableQuery'
,
function
()
{
it
(
'createTableQuery'
,
function
()
{
expectsql
(
this
.
queryGenerator
.
createTableQuery
(
'myTable'
,
{
int
:
'INTEGER'
},
{}),
{
expectsql
(
this
.
queryGenerator
.
createTableQuery
(
'myTable'
,
{
int
:
'INTEGER'
},
{}),
{
mssql
:
"IF OBJECT_ID('[myTable]', 'U') IS NULL CREATE TABLE [myTable] ([int] INTEGER);"
mssql
:
"IF OBJECT_ID('[myTable]', 'U') IS NULL CREATE TABLE [myTable] ([int] INTEGER);"
...
...
test/unit/dialects/mysql/query-generator.test.js
View file @
a7fa316
...
@@ -11,6 +11,30 @@ const chai = require('chai'),
...
@@ -11,6 +11,30 @@ const chai = require('chai'),
if
(
dialect
===
'mysql'
)
{
if
(
dialect
===
'mysql'
)
{
describe
(
'[MYSQL Specific] QueryGenerator'
,
()
=>
{
describe
(
'[MYSQL Specific] QueryGenerator'
,
()
=>
{
const
suites
=
{
const
suites
=
{
createDatabaseQuery
:
[
{
arguments
:
[
'myDatabase'
],
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase`;'
},
{
arguments
:
[
'myDatabase'
,
{
charset
:
'utf8mb4'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT CHARSET SET = \'utf8mb4\';'
},
{
arguments
:
[
'myDatabase'
,
{
collate
:
'utf8mb4_unicode_ci'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT COLLATE = \'utf8mb4_unicode_ci\';'
},
{
arguments
:
[
'myDatabase'
,
{
charset
:
'utf8mb4'
,
collate
:
'utf8mb4_unicode_ci'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS `myDatabase` DEFAULT CHARSET SET = \'utf8mb4\' DEFAULT COLLATE = \'utf8mb4_unicode_ci\';'
}
],
dropDatabaseQuery
:
[
{
arguments
:
[
'myDatabase'
],
expectation
:
'DROP DATABASE IF EXISTS `myDatabase`;'
}
],
arithmeticQuery
:
[
arithmeticQuery
:
[
{
{
title
:
'Should use the plus operator'
,
title
:
'Should use the plus operator'
,
...
...
test/unit/dialects/postgres/query-generator.test.js
View file @
a7fa316
...
@@ -14,6 +14,30 @@ const chai = require('chai'),
...
@@ -14,6 +14,30 @@ const chai = require('chai'),
if
(
dialect
.
match
(
/^postgres/
))
{
if
(
dialect
.
match
(
/^postgres/
))
{
describe
(
'[POSTGRES Specific] QueryGenerator'
,
()
=>
{
describe
(
'[POSTGRES Specific] QueryGenerator'
,
()
=>
{
const
suites
=
{
const
suites
=
{
createDatabaseQuery
:
[
{
arguments
:
[
'myDatabase'
],
expectation
:
'CREATE DATABASE IF NOT EXISTS "myDatabase";'
},
{
arguments
:
[
'myDatabase'
,
{
encoding
:
'UTF8'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS "myDatabase" ENCODING = \'UTF8\';'
},
{
arguments
:
[
'myDatabase'
,
{
collate
:
'en_US.UTF-8'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS "myDatabase" LC_COLLATE = \'en_US.UTF-8\';'
},
{
arguments
:
[
'myDatabase'
,
{
encoding
:
'UTF8'
,
collate
:
'en_US.UTF-8'
}],
expectation
:
'CREATE DATABASE IF NOT EXISTS "myDatabase" ENCODING = \'UTF8\' LC_COLLATE = \'en_US.UTF-8\';'
}
],
dropDatabaseQuery
:
[
{
arguments
:
[
'myDatabase'
],
expectation
:
'DROP DATABASE IF EXISTS "myDatabase" CASCADE;'
}
],
arithmeticQuery
:
[
arithmeticQuery
:
[
{
{
title
:
'Should use the plus operator'
,
title
:
'Should use the plus operator'
,
...
...
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