Skip to content
Permalink
Browse files
[misc] typecast new methods tiny/short/datetime
  • Loading branch information
rusher committed Jun 8, 2021
1 parent a8d9e7e commit 1ebb0be
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 76 deletions.
@@ -113,6 +113,18 @@ class BinaryDecoder {
(nullBitmap[Math.floor((index + 2) / 8)] & (1 << (index + 2) % 8)) > 0
? null
: packet.readFloat();
column.tiny = () =>
(nullBitmap[Math.floor((index + 2) / 8)] & (1 << (index + 2) % 8)) > 0
? null
: column.signed()
? packet.readInt8()
: packet.readUInt8();
column.short = () =>
(nullBitmap[Math.floor((index + 2) / 8)] & (1 << (index + 2) % 8)) > 0
? null
: column.signed()
? packet.readInt16()
: packet.readUInt16();
column.int = () =>
(nullBitmap[Math.floor((index + 2) / 8)] & (1 << (index + 2) % 8)) > 0
? null
@@ -129,6 +141,11 @@ class BinaryDecoder {
(nullBitmap[Math.floor((index + 2) / 8)] & (1 << (index + 2) % 8)) > 0
? null
: packet.readBinaryDate(opts);
column.datetime = () =>
(nullBitmap[Math.floor((index + 2) / 8)] & (1 << (index + 2) % 8)) > 0
? null
: packet.readBinaryDateTime(opts);

column.geometry = () => {
if ((nullBitmap[Math.floor((index + 2) / 8)] & (1 << (index + 2) % 8)) > 0) {
if (column.dataTypeName) {
@@ -81,10 +81,13 @@ class TextDecoder {
column.string = () => packet.readStringLength();
column.buffer = () => packet.readBufferLengthEncoded();
column.float = () => packet.readFloatLengthCoded();
column.tiny = () => packet.readIntLengthEncoded();
column.short = () => packet.readIntLengthEncoded();
column.int = () => packet.readIntLengthEncoded();
column.long = () => packet.readBigIntLengthEncoded();
column.decimal = () => packet.readDecimalLengthEncoded();
column.date = () => packet.readDateTime(opts);
column.date = () => packet.readDate(opts);
column.datetime = () => packet.readDateTime(opts);
column.geometry = () => {
return column.readGeometry();
};
@@ -14,49 +14,20 @@ describe('TypeCast', () => {
return next();
};

it('query level typecast function', function (done) {
shareConn
.query({
sql: "SELECT 'blaBLA' as upper, 'blaBLA' as lower, 'blaBLA' as std, '1' as r",
typeCast: changeCaseCast
})
.then((rows) => {
assert.deepEqual(rows, [{ upper: 'BLABLA', lower: 'blabla', std: 'blaBLA', r: '1' }]);
done();
})
.catch(done);
});

it('connection level typecast function', function (done) {
base
.createConnection({ typeCast: changeCaseCast })
.then((conn) => {
conn
.query("SELECT 'blaBLA' as upper, 'blaBLA' as lower, 'blaBLA' as std, '1' as r")
.then((rows) => {
assert.deepEqual(rows, [{ upper: 'BLABLA', lower: 'blabla', std: 'blaBLA', r: '1' }]);
conn.end();
done();
})
.catch(done);
})
.catch(done);
it('query level typecast function', async function () {
const rows = await shareConn.query({
sql: "SELECT 'blaBLA' as upper, 'blaBLA' as lower, 'blaBLA' as std, '1' as r",
typeCast: changeCaseCast
});
assert.deepEqual(rows, [{ upper: 'BLABLA', lower: 'blabla', std: 'blaBLA', r: '1' }]);
});

it('compatibility automatic cast', function (done) {
base
.createConnection({ typeCast: true })
.then((conn) => {
conn
.query("SELECT '1' as r")
.then((rows) => {
assert.deepEqual(rows, [{ r: '1' }]);
conn.end();
done();
})
.catch(done);
})
.catch(done);
it('query level typecast function execute', async function () {
const rows = await shareConn.execute({
sql: "SELECT 'blaBLA' as upper, 'blaBLA' as lower, 'blaBLA' as std, '1' as r",
typeCast: changeCaseCast
});
assert.deepEqual(rows, [{ upper: 'BLABLA', lower: 'blabla', std: 'blaBLA', r: '1' }]);
});

it('connection level typecast function', async function () {
@@ -68,55 +39,103 @@ describe('TypeCast', () => {
conn.end();
});

it('cast fields', function (done) {
it('connection level typecast function execute', async function () {
const conn = await base.createConnection({ typeCast: changeCaseCast });
const rows = await conn.execute(
"SELECT 'blaBLA' as upper, 'blaBLA' as lower, 'blaBLA' as std, '1' as r"
);
assert.deepEqual(rows, [{ upper: 'BLABLA', lower: 'blabla', std: 'blaBLA', r: '1' }]);
conn.end();
});

it('compatibility automatic cast', async function () {
const conn = await base.createConnection({ typeCast: true });
const rows = await conn.query("SELECT '1' as r");
assert.deepEqual(rows, [{ r: '1' }]);
conn.end();
});

it('compatibility automatic cast execute', async function () {
const conn = await base.createConnection({ typeCast: true });
const rows = await conn.execute("SELECT '1' as r");
assert.deepEqual(rows, [{ r: '1' }]);
conn.end();
});

it('cast fields', async function () {
const checkCaseType = (field, next) => {
assert.equal(field.type, 'VAR_STRING');
assert.equal(field.columnLength, base.utf8Collation() ? 24 : 6);
return next();
};
const rows = await shareConn.query({
sql: "SELECT 'blaBLA' as upper",
typeCast: checkCaseType
});
assert.deepEqual(rows, [{ upper: 'blaBLA' }]);
});

it('cast fields execute', async function () {
const checkCaseType = (field, next) => {
assert.equal(field.type, 'VAR_STRING');
assert.equal(field.columnLength, base.utf8Collation() ? 24 : 6);
return next();
};
shareConn
.query({
sql: "SELECT 'blaBLA' as upper",
typeCast: checkCaseType
})
.then((rows) => {
assert.deepEqual(rows, [{ upper: 'blaBLA' }]);
done();
})
.catch(done);
const rows = await shareConn.execute({
sql: "SELECT 'blaBLA' as upper",
typeCast: checkCaseType
});
assert.deepEqual(rows, [{ upper: 'blaBLA' }]);
});

it('TINY(1) to boolean cast', async function () {
const tinyToBoolean = (column, next) => {
if (column.type == 'TINY' && column.columnLength === 1) {
const val = column.int();
const val = column.tiny();
return val === null ? null : val === 1;
}
if (column.type == 'SHORT') {
const val = column.short();
return val === null ? null : val + 1;
}
if (column.type == 'INT') {
const val = column.int();
return val === null ? null : val + 1;
}
return next();
};
const conn = await base.createConnection({ typeCast: tinyToBoolean });
await conn.query('DROP TABLE IF EXISTS tinyToBool');
await conn.query('CREATE TABLE tinyToBool(b1 TINYINT(1), b2 TINYINT(2))');
await conn.query('CREATE TABLE tinyToBool(b1 TINYINT(1), b2 TINYINT(2), b3 SMALLINT, b4 INT)');
await conn.beginTransaction();
await conn.query('INSERT INTO tinyToBool VALUES (0,0), (1,1), (2,2), (null,null)');
const rows = await conn.query('SELECT * from tinyToBool');
await conn.query(
'INSERT INTO tinyToBool VALUES (0,0,0,0), (1,1,1,1), (2,2,2,2), (null,null,null,null)'
);
let rows = await conn.query('SELECT * from tinyToBool');
assert.deepEqual(rows, [
{ b1: false, b2: 0 },
{ b1: true, b2: 1 },
{ b1: false, b2: 2 },
{ b1: null, b2: null }
{ b1: false, b2: 0, b3: 1, b4: 1 },
{ b1: true, b2: 1, b3: 2, b4: 2 },
{ b1: false, b2: 2, b3: 3, b4: 3 },
{ b1: null, b2: null, b3: null, b4: null }
]);
rows = await conn.execute('SELECT * from tinyToBool');
assert.deepEqual(rows, [
{ b1: false, b2: 0, b3: 1, b4: 1 },
{ b1: true, b2: 1, b3: 2, b4: 2 },
{ b1: false, b2: 2, b3: 3, b4: 3 },
{ b1: null, b2: null, b3: null, b4: null }
]);
conn.end();
});

it('long cast', async function () {
const longCast = (column, next) => {
if (column.type == 'TINY' && column.columnLength === 1) {
const val = column.long();
const val = column.tiny();
return val == null ? null : Number(val);
}
if (column.type == 'VAR_STRING') {
const val = column.decimal();
const val = column.string();
return val == null ? null : Number(val);
}
return next();
@@ -128,34 +147,50 @@ describe('TypeCast', () => {
await conn.query(
"INSERT INTO stupidCast VALUES (0,'0.1'), (1,'1.1')," + " (2,'2.2'), (null,null)"
);
const rows = await conn.query('SELECT * from stupidCast');
assert.deepEqual(rows, [
const expected = [
{ b1: 0, b2: 0.1 },
{ b1: 1, b2: 1.1 },
{ b1: 2, b2: 2.2 },
{ b1: null, b2: null }
]);
];
let rows = await conn.query('SELECT * from stupidCast');
assert.deepEqual(rows, expected);
rows = await conn.execute('SELECT * from stupidCast');
assert.deepEqual(rows, expected);
conn.end();
});

it('date cast', async function () {
const longCast = (column, next) => {
if (column.type == 'VAR_STRING') {
let da = column.date();
if (column.type == 'TIMESTAMP' || column.type == 'DATETIME') {
let da = column.datetime();
return da == null ? null : da.getMinutes();
}
if (column.type == 'DATE') {
let da = column.date();
return da == null ? null : da.getMonth() + 1;
}
return next();
};
const conn = await base.createConnection({ typeCast: longCast });
await conn.query('DROP TABLE IF EXISTS stupidCast');
await conn.query('CREATE TABLE stupidCast(b1 varchar(100))');
await conn.query('CREATE TABLE stupidCast(b1 DATETIME default null,b2 DATE default null)');
await conn.beginTransaction();
await conn.query(
"INSERT INTO stupidCast VALUES ('1999-01-31" +
" 12:13:14.000'), ('1999-01-31 12:16:15'), (null)"
'INSERT INTO stupidCast VALUES ' +
"('1999-01-31 12:13:14.000', '1999-01-31'), " +
"('1999-01-31 12:16:15', '1999-02-15')" +
', (null, null)'
);
const rows = await conn.query('SELECT * from stupidCast');
assert.deepEqual(rows, [{ b1: 13 }, { b1: 16 }, { b1: null }]);
let rows = await conn.query('SELECT * from stupidCast');
const expected = [
{ b1: 13, b2: 1 },
{ b1: 16, b2: 2 },
{ b1: null, b2: null }
];
assert.deepEqual(rows, expected);
rows = await conn.execute('SELECT * from stupidCast');
assert.deepEqual(rows, expected);
conn.end();
});

@@ -179,8 +214,7 @@ describe('TypeCast', () => {
coordinates: [20, 10]
}
]);
const rows = await conn.query('SELECT * from stupidCast');
assert.deepEqual(rows, [
const expected = [
{
b1: {
type: 'Point',
@@ -202,7 +236,11 @@ describe('TypeCast', () => {
? { type: 'Point' }
: null
}
]);
];
let rows = await conn.query('SELECT * from stupidCast');
assert.deepEqual(rows, expected);
rows = await conn.execute('SELECT * from stupidCast');
assert.deepEqual(rows, expected);
conn.end();
});
});

0 comments on commit 1ebb0be

Please sign in to comment.