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 9bbe34b0
authored
Jul 09, 2015
by
Mike Ringrose
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding MySQL support.
1 parent
56dbddde
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
236 additions
and
23 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/data-types.js
lib/dialects/mysql/index.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/index.js
lib/dialects/postgres/query-generator.js
lib/instance.js
test/integration/model/geometry.test.js
test/unit/sql/data-types.test.js
lib/dialects/abstract/query-generator.js
View file @
9bbe34b
...
@@ -991,18 +991,18 @@ var QueryGenerator = {
...
@@ -991,18 +991,18 @@ var QueryGenerator = {
return
self
.
handleSequelizeMethod
(
attr
);
return
self
.
handleSequelizeMethod
(
attr
);
}
}
if
(
mainModel
&&
mainModel
.
_isGeometryAttribute
(
attr
))
{
return
self
.
geometrySelect
(
attr
);
}
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
===
2
)
{
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
===
2
)
{
if
(
attr
[
0
].
_isSequelizeMethod
)
{
if
(
attr
[
0
].
_isSequelizeMethod
)
{
attr
[
0
]
=
self
.
handleSequelizeMethod
(
attr
[
0
]);
attr
[
0
]
=
self
.
handleSequelizeMethod
(
attr
[
0
]);
addTable
=
false
;
addTable
=
false
;
}
else
{
}
else
if
(
attr
[
0
].
indexOf
(
'('
)
===
-
1
&&
attr
[
0
].
indexOf
(
')'
)
===
-
1
)
{
if
(
mainModel
&&
mainModel
.
_isGeometryAttribute
(
attr
[
1
]))
{
attr
[
0
]
=
this
.
geometrySelect
(
attr
[
0
]);
}
else
if
(
attr
[
0
].
indexOf
(
'('
)
===
-
1
&&
attr
[
0
].
indexOf
(
')'
)
===
-
1
)
{
attr
[
0
]
=
self
.
quoteIdentifier
(
attr
[
0
]);
attr
[
0
]
=
self
.
quoteIdentifier
(
attr
[
0
]);
}
}
}
attr
=
[
attr
[
0
],
self
.
quoteIdentifier
(
attr
[
1
])].
join
(
' AS '
);
attr
=
[
attr
[
0
],
self
.
quoteIdentifier
(
attr
[
1
])].
join
(
' AS '
);
}
else
{
}
else
{
attr
=
attr
.
indexOf
(
Utils
.
TICK_CHAR
)
<
0
&&
attr
.
indexOf
(
'"'
)
<
0
?
self
.
quoteIdentifiers
(
attr
)
:
attr
;
attr
=
attr
.
indexOf
(
Utils
.
TICK_CHAR
)
<
0
&&
attr
.
indexOf
(
'"'
)
<
0
?
self
.
quoteIdentifiers
(
attr
)
:
attr
;
...
@@ -2149,7 +2149,7 @@ var QueryGenerator = {
...
@@ -2149,7 +2149,7 @@ var QueryGenerator = {
},
},
geometrySelect
:
function
(
column
)
{
geometrySelect
:
function
(
column
)
{
return
this
.
quoteIdentifiers
(
column
)
;
return
'AsText('
+
this
.
quoteIdentifiers
(
column
)
+
') AS '
+
column
;
}
}
};
};
...
...
lib/dialects/mysql/data-types.js
View file @
9bbe34b
...
@@ -16,8 +16,28 @@ UUID.prototype.toSql = function() {
...
@@ -16,8 +16,28 @@ UUID.prototype.toSql = function() {
return
'CHAR(36) BINARY'
;
return
'CHAR(36) BINARY'
;
};
};
var
SUPPORTED_GEOMETRY_TYPES
=
[
'POINT'
,
'LINESTRING'
,
'POLYGON'
];
var
GEOMETRY
=
function
()
{
if
(
!
(
this
instanceof
GEOMETRY
))
return
new
GEOMETRY
();
BaseTypes
.
GEOMETRY
.
apply
(
this
,
arguments
);
if
(
_
.
isEmpty
(
this
.
type
))
{
this
.
sqlType
=
this
.
key
;
}
else
if
(
_
.
includes
(
SUPPORTED_GEOMETRY_TYPES
,
this
.
type
))
{
this
.
sqlType
=
this
.
type
;
}
else
{
throw
new
Error
(
'Supported geometry types are: '
+
SUPPORTED_GEOMETRY_TYPES
.
join
(
', '
));
}
};
util
.
inherits
(
GEOMETRY
,
BaseTypes
.
GEOMETRY
);
GEOMETRY
.
prototype
.
toSql
=
function
()
{
return
this
.
sqlType
;
};
module
.
exports
=
{
module
.
exports
=
{
UUID
:
UUID
UUID
:
UUID
,
GEOMETRY
:
GEOMETRY
};
};
_
.
forIn
(
module
.
exports
,
function
(
DataType
,
key
)
{
_
.
forIn
(
module
.
exports
,
function
(
DataType
,
key
)
{
...
...
lib/dialects/mysql/index.js
View file @
9bbe34b
...
@@ -32,7 +32,8 @@ MysqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.support
...
@@ -32,7 +32,8 @@ MysqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.support
using
:
1
,
using
:
1
,
},
},
indexViaAlter
:
true
,
indexViaAlter
:
true
,
NUMERIC
:
true
NUMERIC
:
true
,
GEOMETRY
:
true
});
});
MysqlDialect
.
prototype
.
Query
=
Query
;
MysqlDialect
.
prototype
.
Query
=
Query
;
...
...
lib/dialects/mysql/query-generator.js
View file @
9bbe34b
'use strict'
;
'use strict'
;
var
Utils
=
require
(
'../../utils'
)
var
Utils
=
require
(
'../../utils'
)
,
DataTypes
=
require
(
'../../data-types'
);
,
DataTypes
=
require
(
'../../data-types'
)
,
SqlString
=
require
(
'../../sql-string'
)
,
Wkt
=
require
(
'wellknown'
);
var
QueryGenerator
=
{
var
QueryGenerator
=
{
dialect
:
'mysql'
,
dialect
:
'mysql'
,
...
@@ -349,6 +351,17 @@ var QueryGenerator = {
...
@@ -349,6 +351,17 @@ var QueryGenerator = {
if
(
identifier
===
'*'
)
return
identifier
;
if
(
identifier
===
'*'
)
return
identifier
;
return
Utils
.
addTicks
(
identifier
,
'`'
);
return
Utils
.
addTicks
(
identifier
,
'`'
);
},
},
escape
:
function
(
value
,
field
)
{
if
(
value
&&
value
.
_isSequelizeMethod
)
{
return
this
.
handleSequelizeMethod
(
value
);
}
else
if
(
value
&&
field
&&
field
.
type
instanceof
DataTypes
.
GEOMETRY
)
{
return
'GeomFromText(\''
+
Wkt
.
stringify
(
value
)
+
'\')'
;
}
else
{
return
SqlString
.
escape
(
value
,
false
,
this
.
options
.
timezone
,
this
.
dialect
,
field
);
}
},
/**
/**
* Generates an SQL query that returns all foreign keys of a table.
* Generates an SQL query that returns all foreign keys of a table.
*
*
...
...
lib/dialects/postgres/index.js
View file @
9bbe34b
...
@@ -43,6 +43,7 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supp
...
@@ -43,6 +43,7 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supp
},
},
NUMERIC
:
true
,
NUMERIC
:
true
,
ARRAY
:
true
,
ARRAY
:
true
,
GEOMETRY
:
true
,
JSON
:
true
,
JSON
:
true
,
JSONB
:
true
,
JSONB
:
true
,
deferrableConstraints
:
true
deferrableConstraints
:
true
...
...
lib/dialects/postgres/query-generator.js
View file @
9bbe34b
...
@@ -898,12 +898,7 @@ var QueryGenerator = {
...
@@ -898,12 +898,7 @@ var QueryGenerator = {
return
SqlString
.
escape
(
JSON
.
stringify
(
v
),
false
,
this
.
options
.
timezone
,
this
.
dialect
,
field
);
return
SqlString
.
escape
(
JSON
.
stringify
(
v
),
false
,
this
.
options
.
timezone
,
this
.
dialect
,
field
);
},
this
).
join
(
','
)
+
']::'
+
jsonType
.
key
+
'[]'
;
},
this
).
join
(
','
)
+
']::'
+
jsonType
.
key
+
'[]'
;
}
else
if
(
value
&&
field
&&
field
.
type
instanceof
DataTypes
.
GEOMETRY
)
{
}
else
if
(
value
&&
field
&&
field
.
type
instanceof
DataTypes
.
GEOMETRY
)
{
var
s
=
"ST_GeomFromGeoJSON('"
+
JSON
.
stringify
(
value
)
+
"'"
;
return
"ST_GeomFromGeoJSON('"
+
JSON
.
stringify
(
value
)
+
"')"
;
if
(
field
.
type
.
srid
)
{
s
+=
+
", "
+
field
.
type
.
srid
;
}
s
+=
")"
;
return
s
;
}
}
return
SqlString
.
escape
(
value
,
false
,
this
.
options
.
timezone
,
this
.
dialect
,
field
);
return
SqlString
.
escape
(
value
,
false
,
this
.
options
.
timezone
,
this
.
dialect
,
field
);
...
@@ -944,6 +939,10 @@ var QueryGenerator = {
...
@@ -944,6 +939,10 @@ var QueryGenerator = {
}
}
return
AbstractQueryGenerator
.
setAutocommitQuery
.
call
(
this
,
value
,
options
);
return
AbstractQueryGenerator
.
setAutocommitQuery
.
call
(
this
,
value
,
options
);
},
geometrySelect
:
function
(
column
)
{
return
this
.
quoteIdentifiers
(
column
);
}
}
};
};
...
...
lib/instance.js
View file @
9bbe34b
...
@@ -362,7 +362,7 @@ Instance.prototype.set = function(key, value, options) {
...
@@ -362,7 +362,7 @@ Instance.prototype.set = function(key, value, options) {
}
}
if
(
this
.
Model
.
_hasGeometryAttributes
&&
this
.
Model
.
_isGeometryAttribute
(
key
)
&&
_
.
isString
(
value
))
{
if
(
this
.
Model
.
_hasGeometryAttributes
&&
this
.
Model
.
_isGeometryAttribute
(
key
)
&&
_
.
isString
(
value
))
{
value
=
this
.
dialect
.
DatatTypes
.
GEOMETRY
.
parse
(
value
);
value
=
this
.
Model
.
attributes
[
key
].
type
.
parse
(
value
);
}
}
if
(
!
options
.
raw
&&
((
primitives
.
indexOf
(
typeof
value
)
===
-
1
&&
value
!==
null
)
||
value
!==
originalValue
))
{
if
(
!
options
.
raw
&&
((
primitives
.
indexOf
(
typeof
value
)
===
-
1
&&
value
!==
null
)
||
value
!==
originalValue
))
{
...
...
test/integration/model/geometry.test.js
0 → 100644
View file @
9bbe34b
'use strict'
;
/* jshint -W030 */
/* jshint -W110 */
var
chai
=
require
(
'chai'
)
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
DataTypes
=
require
(
__dirname
+
'/../../../lib/data-types'
);
var
current
=
Support
.
sequelize
;
describe
(
Support
.
getTestDialectTeaser
(
'Model'
),
function
()
{
if
(
current
.
dialect
.
supports
.
GEOMETRY
)
{
describe
(
'GEOMETRY'
,
function
()
{
beforeEach
(
function
()
{
return
Support
.
prepareTransactionTest
(
this
.
sequelize
).
bind
(
this
).
then
(
function
(
sequelize
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
,
geometry
:
DataTypes
.
GEOMETRY
});
return
this
.
User
.
sync
({
force
:
true
});
});
});
it
(
'should create a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
point
=
{
type
:
'Point'
,
coordinates
:
[
39.807222
,
-
76.984722
]};
return
User
.
create
({
username
:
'username'
,
geometry
:
point
}).
then
(
function
(
newUser
)
{
expect
(
newUser
).
not
.
to
.
be
.
null
;
expect
(
newUser
.
geometry
).
to
.
be
.
deep
.
eql
(
point
);
});
});
it
(
'should update a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
point1
=
{
type
:
'Point'
,
coordinates
:
[
39.807222
,
-
76.984722
]}
,
point2
=
{
type
:
'Point'
,
coordinates
:
[
49.807222
,
-
86.984722
]};
var
props
=
{
username
:
'username'
,
geometry
:
point1
};
return
User
.
create
(
props
).
then
(
function
(
user
)
{
return
User
.
update
({
geometry
:
point2
},
{
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
count
)
{
return
User
.
findOne
({
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
user
)
{
expect
(
user
.
geometry
).
to
.
be
.
deep
.
eql
(
point2
);
});
});
});
describe
(
'GEOMETRY(POINT)'
,
function
()
{
beforeEach
(
function
()
{
return
Support
.
prepareTransactionTest
(
this
.
sequelize
).
bind
(
this
).
then
(
function
(
sequelize
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
,
geometry
:
DataTypes
.
GEOMETRY
(
'POINT'
)
});
return
this
.
User
.
sync
({
force
:
true
});
});
});
it
(
'should create a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
point
=
{
type
:
'Point'
,
coordinates
:
[
39.807222
,
-
76.984722
]};
return
User
.
create
({
username
:
'username'
,
geometry
:
point
}).
then
(
function
(
newUser
)
{
expect
(
newUser
).
not
.
to
.
be
.
null
;
expect
(
newUser
.
geometry
).
to
.
be
.
deep
.
eql
(
point
);
});
});
it
(
'should update a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
point1
=
{
type
:
'Point'
,
coordinates
:
[
39.807222
,
-
76.984722
]}
,
point2
=
{
type
:
'Point'
,
coordinates
:
[
49.807222
,
-
86.984722
]};
var
props
=
{
username
:
'username'
,
geometry
:
point1
};
return
User
.
create
(
props
).
then
(
function
(
user
)
{
return
User
.
update
({
geometry
:
point2
},
{
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
count
)
{
return
User
.
findOne
({
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
user
)
{
expect
(
user
.
geometry
).
to
.
be
.
deep
.
eql
(
point2
);
});
});
});
describe
(
'GEOMETRY(LINESTRING)'
,
function
()
{
beforeEach
(
function
()
{
return
Support
.
prepareTransactionTest
(
this
.
sequelize
).
bind
(
this
).
then
(
function
(
sequelize
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
,
geometry
:
DataTypes
.
GEOMETRY
(
'LINESTRING'
)
});
return
this
.
User
.
sync
({
force
:
true
});
});
});
it
(
'should create a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
point
=
{
type
:
'LineString'
,
'coordinates'
:
[
[
100.0
,
0.0
],
[
101.0
,
1.0
]
]
};
return
User
.
create
({
username
:
'username'
,
geometry
:
point
}).
then
(
function
(
newUser
)
{
expect
(
newUser
).
not
.
to
.
be
.
null
;
expect
(
newUser
.
geometry
).
to
.
be
.
deep
.
eql
(
point
);
});
});
it
(
'should update a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
point1
=
{
type
:
'LineString'
,
coordinates
:
[
[
100.0
,
0.0
],
[
101.0
,
1.0
]
]
}
,
point2
=
{
type
:
'LineString'
,
coordinates
:
[
[
101.0
,
0.0
],
[
102.0
,
1.0
]
]
};
var
props
=
{
username
:
'username'
,
geometry
:
point1
};
return
User
.
create
(
props
).
then
(
function
(
user
)
{
return
User
.
update
({
geometry
:
point2
},
{
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
count
)
{
return
User
.
findOne
({
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
user
)
{
expect
(
user
.
geometry
).
to
.
be
.
deep
.
eql
(
point2
);
});
});
});
describe
(
'GEOMETRY(POLYGON)'
,
function
()
{
beforeEach
(
function
()
{
return
Support
.
prepareTransactionTest
(
this
.
sequelize
).
bind
(
this
).
then
(
function
(
sequelize
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
,
geometry
:
DataTypes
.
GEOMETRY
(
'POLYGON'
)
});
return
this
.
User
.
sync
({
force
:
true
});
});
});
it
(
'should create a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
point
=
{
type
:
'Polygon'
,
coordinates
:
[
[
[
100.0
,
0.0
],
[
101.0
,
0.0
],
[
101.0
,
1.0
],
[
100.0
,
1.0
],
[
100.0
,
0.0
]
]
]};
return
User
.
create
({
username
:
'username'
,
geometry
:
point
}).
then
(
function
(
newUser
)
{
expect
(
newUser
).
not
.
to
.
be
.
null
;
expect
(
newUser
.
geometry
).
to
.
be
.
deep
.
eql
(
point
);
});
});
it
(
'should update a geometry object'
,
function
()
{
var
User
=
this
.
User
;
var
polygon1
=
{
type
:
'Polygon'
,
coordinates
:
[
[
[
100.0
,
0.0
],
[
101.0
,
0.0
],
[
101.0
,
1.0
],
[
100.0
,
1.0
],
[
100.0
,
0.0
]
]
]}
,
polygon2
=
{
type
:
'Polygon'
,
coordinates
:
[
[
[
100.0
,
0.0
],
[
102.0
,
0.0
],
[
102.0
,
1.0
],
[
100.0
,
1.0
],
[
100.0
,
0.0
]
]
]};
var
props
=
{
username
:
'username'
,
geometry
:
polygon1
};
return
User
.
create
(
props
).
then
(
function
(
user
)
{
return
User
.
update
({
geometry
:
polygon2
},
{
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
count
)
{
return
User
.
findOne
({
where
:
{
username
:
props
.
username
}});
}).
then
(
function
(
user
)
{
expect
(
user
.
geometry
).
to
.
be
.
deep
.
eql
(
polygon2
);
});
});
});
}
});
test/unit/sql/data-types.test.js
View file @
9bbe34b
...
@@ -667,28 +667,32 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
...
@@ -667,28 +667,32 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
});
});
}
}
if
(
current
.
dialect
.
supports
.
ARRA
Y
)
{
// if (current.dialect.supports.GEOMETR
Y) {
suite
(
'GEOMETRY'
,
function
()
{
suite
(
'GEOMETRY'
,
function
()
{
testsql
(
'GEOMETRY'
,
DataTypes
.
GEOMETRY
,
{
testsql
(
'GEOMETRY'
,
DataTypes
.
GEOMETRY
,
{
postgres
:
'GEOMETRY'
default
:
'GEOMETRY'
});
});
testsql
(
'GEOMETRY(\'POINT\')'
,
DataTypes
.
GEOMETRY
(
'POINT'
),
{
testsql
(
'GEOMETRY(\'POINT\')'
,
DataTypes
.
GEOMETRY
(
'POINT'
),
{
postgres
:
'GEOMETRY(POINT)'
postgres
:
'GEOMETRY(POINT)'
,
mysql
:
'POINT'
});
});
testsql
(
'GEOMETRY(\'LINESTRING\')'
,
DataTypes
.
GEOMETRY
(
'LINESTRING'
),
{
testsql
(
'GEOMETRY(\'LINESTRING\')'
,
DataTypes
.
GEOMETRY
(
'LINESTRING'
),
{
postgres
:
'GEOMETRY(LINESTRING)'
,
postgres
:
'GEOMETRY(LINESTRING)'
,
mysql
:
'LINESTRING'
});
});
testsql
(
'GEOMETRY(\'POLYGON\')'
,
DataTypes
.
GEOMETRY
(
'POLYGON'
),
{
testsql
(
'GEOMETRY(\'POLYGON\')'
,
DataTypes
.
GEOMETRY
(
'POLYGON'
),
{
postgres
:
'GEOMETRY(POLYGON)'
postgres
:
'GEOMETRY(POLYGON)'
,
mysql
:
'POLYGON'
});
});
testsql
(
'GEOMETRY(\'POINT\',4326)'
,
DataTypes
.
GEOMETRY
(
'POINT'
,
4326
),
{
testsql
(
'GEOMETRY(\'POINT\',4326)'
,
DataTypes
.
GEOMETRY
(
'POINT'
,
4326
),
{
postgres
:
'GEOMETRY(POINT,4326)'
postgres
:
'GEOMETRY(POINT,4326)'
,
mysql
:
'POINT'
});
});
});
});
}
//
}
});
});
});
});
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