Skip to content

Commit

Permalink
Blob support, closes #99
Browse files Browse the repository at this point in the history
  • Loading branch information
janmeier committed Aug 19, 2013
1 parent bdd9005 commit 641624e
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
- [FEATURE] bulkCreate() now has a third argument which gives you the ability to validate each row before attempting to bulkInsert [#797](https://github.com/sequelize/sequelize/pull/797). thanks to durango
- [FEATURE] Added `isDirty` to model instances. [#798](https://github.com/sequelize/sequelize/pull/798). Thanks to mstorgaard
- [FEATURE] Added possibility to use env variable for the database connection. [#784](https://github.com/sequelize/sequelize/pull/784). Thanks to sykopomp.
- [FEATURE] Blob support. janmeier
- [REFACTORING] hasMany now uses a single SQL statement when creating and destroying associations, instead of removing each association seperately [690](https://github.com/sequelize/sequelize/pull/690). Inspired by [#104](https://github.com/sequelize/sequelize/issues/104). janmeier
- [REFACTORING] Consistent handling of offset across dialects. Offset is now always applied, and limit is set to max table size of not limit is given [#725](https://github.com/sequelize/sequelize/pull/725). janmeier
- [REFACTORING] Moved Jasmine to Buster and then Buster to Mocha + Chai. sdepold and durango
Expand Down
43 changes: 42 additions & 1 deletion lib/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var FLOAT = function() {
return FLOAT.prototype.construct.apply(this, [FLOAT].concat(Array.prototype.slice.apply(arguments)));
};

var BLOB = function() {
return BLOB.prototype.construct.apply(this, [BLOB].concat(Array.prototype.slice.apply(arguments)));
};

FLOAT._type = FLOAT;
FLOAT._typeName = 'FLOAT';
INTEGER._type = INTEGER;
Expand All @@ -50,11 +54,46 @@ BIGINT._type = BIGINT;
BIGINT._typeName = 'BIGINT';
STRING._type = STRING;
STRING._typeName = 'VARCHAR';
BLOB._type = BLOB
BLOB._typeName = 'BLOB'

STRING.toString = INTEGER.toString = FLOAT.toString = BIGINT.toString = function() {
BLOB.toString = STRING.toString = INTEGER.toString = FLOAT.toString = BIGINT.toString = function() {
return new this._type().toString();
};

BLOB.prototype = {

construct: function(RealType, length) {
if (this instanceof RealType) {
this._typeName = RealType._typeName
if (typeof length === 'string') {
this._length = length
} else {
this._length = ''
}
} else {
return new RealType(length)
}
},

get type() {
return this.toString();
},

toString: function() {
switch (this._length.toLowerCase()) {
case 'tiny':
return 'TINYBLOB'
case 'medium':
return 'MEDIUMBLOB'
case 'long':
return 'LONGBLOB'
default:
return this._typeName
}
}
}

FLOAT.prototype = BIGINT.prototype = INTEGER.prototype = {

construct: function(RealType, length, decimals, unsigned, zerofill) {
Expand Down Expand Up @@ -128,6 +167,7 @@ Object.defineProperty(STRING, 'type', typeDesc);
Object.defineProperty(INTEGER, 'type', typeDesc);
Object.defineProperty(BIGINT, 'type', typeDesc);
Object.defineProperty(FLOAT, 'type', typeDesc);
Object.defineProperty(BLOB, 'type', typeDesc)

Object.defineProperty(INTEGER, 'UNSIGNED', unsignedDesc);
Object.defineProperty(BIGINT, 'UNSIGNED', unsignedDesc);
Expand All @@ -147,6 +187,7 @@ module.exports = {
BOOLEAN: 'TINYINT(1)',
FLOAT: FLOAT,
NOW: 'NOW',
BLOB: BLOB,

get ENUM() {
var result = function() {
Expand Down
4 changes: 4 additions & 0 deletions lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,10 @@ module.exports = (function() {
dataType = dataType.replace(/NOT NULL/, '')
}

if (dataType.lastIndexOf('BLOB') !== -1) {
dataType = 'bytea'
}

if (dataType.match(/^ENUM\(/)) {
dataType = dataType.replace(/^ENUM\(.+\)/, this.pgEscapeAndQuote("enum_" + tableName + "_" + attr))
}
Expand Down
15 changes: 9 additions & 6 deletions lib/dialects/sqlite/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,18 @@ module.exports = (function() {
if (this.sql.indexOf('sqlite_master') !== -1) {
result = results.map(function(resultSet) { return resultSet.name })
} else if (this.send('isSelectQuery')) {
// we need to convert the timestamps into actual date objects

if(!this.options.raw) {
results = results.map(function(result) {
for (var name in result) {
if (result.hasOwnProperty(name) && (metaData.columnTypes[name] === 'DATETIME')) {
var val = result[name];
if(val !== null) {
result[name] = new Date(val+'Z'); // Z means UTC
if (result.hasOwnProperty(name) && metaData.columnTypes[name]) {
if (metaData.columnTypes[name] === 'DATETIME') {
// we need to convert the timestamps into actual date objects
var val = result[name];
if (val !== null) {
result[name] = new Date(val+'Z'); // Z means UTC
}
} else if (metaData.columnTypes[name].lastIndexOf('BLOB') !== -1) {
result[name] = new Buffer(result[name]);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/sql-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
}

if (Buffer.isBuffer(val)) {
return SqlString.bufferToString(val);
return SqlString.bufferToString(val, dialect);
}

if (Array.isArray(val)) {
Expand Down Expand Up @@ -137,7 +137,7 @@ SqlString.dateToString = function(date, timeZone, dialect) {
return moment(dt).format("YYYY-MM-DD HH:mm:ss");
};

SqlString.bufferToString = function(buffer) {
SqlString.bufferToString = function(buffer, dialect) {
var hex = '';
try {
hex = buffer.toString('hex');
Expand All @@ -149,6 +149,10 @@ SqlString.bufferToString = function(buffer) {
}
}

if (dialect === 'postgres') {
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
return "E'\\\\x" + hex+ "'";
}
return "X'" + hex+ "'";
};

Expand Down
60 changes: 60 additions & 0 deletions test/dao-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2898,4 +2898,64 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})

describe("blob", function() {
beforeEach(function(done) {
this.BlobUser = this.sequelize.define("blobUser", {
data: Sequelize.BLOB
})

this.BlobUser.sync({ force: true }).success(function() {
done()
})
})

describe("buffers", function () {
it("should be able to take a buffer as parameter to a BLOB field", function (done) {
this.BlobUser.create({
data: new Buffer('Sequelize')
}).success(function (user) {
expect(user).to.be.ok
done()
})
})

it("should return a buffer when fetching a blob", function (done) {
var self = this
this.BlobUser.create({
data: new Buffer('Sequelize')
}).success(function (user) {
self.BlobUser.find(user.id).success(function (user) {
expect(user.data).to.be.an.instanceOf(Buffer)
expect(user.data.toString()).to.have.string('Sequelize')
done()
})
})
})
})

describe("strings", function () {
it("should be able to take a string as parameter to a BLOB field", function (done) {
this.BlobUser.create({
data: 'Sequelize'
}).success(function (user) {
expect(user).to.be.ok
done()
})
})

it("should return a buffer when fetching a BLOB, even when the BLOB was inserted as a string", function (done) {
var self = this
this.BlobUser.create({
data: 'Sequelize'
}).success(function (user) {
self.BlobUser.find(user.id).success(function (user) {
expect(user.data).to.be.an.instanceOf(Buffer)
expect(user.data.toString()).to.have.string('Sequelize')
done()
})
})
})
})
})
})
5 changes: 5 additions & 0 deletions test/data-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe(Support.getTestDialectTeaser('DataTypes'), function() {
[Sequelize.NOW, 'NOW', 'NOW'],
[Sequelize.BOOLEAN, 'BOOLEAN', 'TINYINT(1)'],

[Sequelize.BLOB, 'BLOB', 'BLOB'],
[Sequelize.BLOB('tiny'), 'BLOB(\'tiny\')', 'TINYBLOB'],
[Sequelize.BLOB('medium'), 'BLOB(\'medium\')', 'MEDIUMBLOB'],
[Sequelize.BLOB('long'), 'BLOB(\'long\')', 'LONGBLOB'],

[Sequelize.INTEGER, 'INTEGER', 'INTEGER'],
[Sequelize.INTEGER.UNSIGNED, 'INTEGER.UNSIGNED', 'INTEGER UNSIGNED'],
[Sequelize.INTEGER(11), 'INTEGER(11)','INTEGER(11)'],
Expand Down
11 changes: 11 additions & 0 deletions test/mysql/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ if (dialect.match(/^mysql/)) {
arguments: ['myTable', {title: "INTEGER"}, {comment: "I'm a comment!"}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` INTEGER) COMMENT 'I\\'m a comment!' ENGINE=InnoDB;"
},
{
arguments: ['myTable', {data: "BLOB"}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`data` BLOB) ENGINE=InnoDB;"
},
{
arguments: ['myTable', {data: "LONGBLOB"}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`data` LONGBLOB) ENGINE=InnoDB;"
},
{
arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {engine: 'MyISAM'}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=MyISAM;"
Expand Down Expand Up @@ -219,6 +227,9 @@ if (dialect.match(/^mysql/)) {
}, {
arguments: ['myTable', {name: 'foo', foo: 1}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
}, {
arguments: ['myTable', {data: new Buffer('Sequelize') }],
expectation: "INSERT INTO `myTable` (`data`) VALUES (X'53657175656c697a65');"
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
Expand Down
11 changes: 11 additions & 0 deletions test/postgres/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ if (dialect.match(/^postgres/)) {
arguments: ['myTable', {title: "INTEGER"}, {comment: "I'm a comment!"}],
expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" INTEGER); COMMENT ON TABLE \"myTable\" IS 'I''m a comment!';",
},
{
arguments: ['myTable', {data: "BLOB"}],
expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"data\" bytea);"
},
{
arguments: ['myTable', {data: "LONGBLOB"}],
expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"data\" bytea);"
},
{
arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}],
expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));"
Expand Down Expand Up @@ -349,6 +357,9 @@ if (dialect.match(/^postgres/)) {
}, {
arguments: ['myTable', {name: 'foo', birthday: moment("2011-03-27 10:01:55 +0000", "YYYY-MM-DD HH:mm:ss Z").toDate()}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"birthday\") VALUES ('foo','2011-03-27 10:01:55.000 +00:00') RETURNING *;"
}, {
arguments: ['myTable', {data: new Buffer('Sequelize') }],
expectation: "INSERT INTO \"myTable\" (\"data\") VALUES (E'\\\\x53657175656c697a65') RETURNING *;"
}, {
arguments: ['myTable', {name: 'foo', foo: 1}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"foo\") VALUES ('foo',1) RETURNING *;"
Expand Down
11 changes: 11 additions & 0 deletions test/sqlite/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ if (dialect === 'sqlite') {
],

createTableQuery: [
{
arguments: ['myTable', {data: "BLOB"}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`data` BLOB);"
},
{
arguments: ['myTable', {data: "LONGBLOB"}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`data` LONGBLOB);"
},
{
arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255));"
Expand All @@ -103,6 +111,9 @@ if (dialect === 'sqlite') {
}, {
arguments: ['myTable', { name: "'bar'" }],
expectation: "INSERT INTO `myTable` (`name`) VALUES ('''bar''');"
}, {
arguments: ['myTable', {data: new Buffer('Sequelize') }],
expectation: "INSERT INTO `myTable` (`data`) VALUES (X'53657175656c697a65');"
}, {
arguments: ['myTable', { name: "bar", value: null }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL);"
Expand Down

0 comments on commit 641624e

Please sign in to comment.