Skip to content

Commit

Permalink
Use switch offset
Browse files Browse the repository at this point in the history
* Update docker-compose.yml

* Update docker-compose.yml

* Use super escape

* Update docker-compose.yml

* Update docker-compose.yml

* use super.escape

* 2.6.5

* escape string

* use switch offset

* remove unused var

* 2.6.6

* validate date field

* validate date only field

* 2.6.7

* use switch offset

* remove unused var

* 2.6.6

* validate date field

* validate date only field

* 2.6.7
  • Loading branch information
kbarbounakis authored Dec 8, 2022
1 parent 0171913 commit 75cb91c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
27 changes: 25 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/mssql",
"version": "2.6.5",
"version": "2.6.7",
"description": "MOST Web Framework MSSQL Data Adapter",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down Expand Up @@ -57,6 +57,8 @@
"jasmine": "^4.1.0",
"jasmine-spec-reporter": "^7.0.0",
"module-alias": "^2.2.2",
"moment": "^2.29.4",
"moment-timezone": "^0.5.39",
"rimraf": "^3.0.2",
"rollup": "^2.71.1",
"rollup-plugin-dts": "^4.2.1",
Expand Down
32 changes: 31 additions & 1 deletion spec/DateFunctions.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestApplication } from './TestApplication';

import moment from 'moment';
describe('DateFunctions', () => {
/**
* @type {TestApplication}
Expand Down Expand Up @@ -107,4 +107,34 @@ describe('DateFunctions', () => {
});
});

it('should use datetimeoffset', async () => {
await app.executeInTestTranscaction(async (context) => {
let user = await context.model('User').where('name').equal('alexis.rees@example.com')
.silent().getItem();
expect(user).toBeTruthy();
const now = new Date();
user.lastLogon = now;
await context.model('User').silent().save(user);
user = await context.model('User').where('name').equal('alexis.rees@example.com')
.silent().getItem();
expect(user.lastLogon).toEqual(now);
});
});

it('should use date', async () => {
await app.executeInTestTranscaction(async (context) => {
// get AMD Radeon R9 290
let product = await context.model('Product').where('name').equal('AMD Radeon R9 290')
.silent().getItem();
expect(product).toBeTruthy();
const now = moment(new Date()).startOf('day').toDate();
let releaseDate = now;
product.releaseDate = new Date(releaseDate);
await context.model('Product').silent().save(product);
product = await context.model('Product').where('name').equal('AMD Radeon R9 290')
.silent().getItem();
expect(product.releaseDate).toEqual(now);
});
});

});
9 changes: 7 additions & 2 deletions spec/TestApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ const testConnectionOptions = {
'server': process.env.DB_HOST,
'port': parseInt(process.env.DB_PORT, 10),
'user': process.env.DB_USER,
'database': 'test_db'
'database': 'test_db',
'timezone': 'Europe/Athens',
'options': {
'encrypt': false,
'trustServerCertificate': true,
'useUTC': true
}
};

if (process.env.DB_PASSWORD) {
Expand Down Expand Up @@ -210,7 +216,6 @@ class TestApplication extends DataApplication {
return item.abstract ? false : true;
});
const sourceAdapter = new SqliteAdapter(sourceConnectionOptions);
const formatter = new MSSqlFormatter();
for (let entityType of entityTypes) {
TraceUtils.log(`Upgrading ${entityType.name}`);
await new Promise((resolve, reject) => {
Expand Down
20 changes: 12 additions & 8 deletions src/MSSqlFormatter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MOST Web Framework Codename Zero Gravity Copyright (c) 2017-2022, THEMOST LP All rights reserved
import { sprintf } from 'sprintf-js';
import { QueryField, SqlUtils, SqlFormatter } from '@themost/query';
import { QueryField, SqlFormatter } from '@themost/query';

function zeroPad(number, length) {
number = number || 0;
Expand All @@ -11,6 +11,8 @@ function zeroPad(number, length) {
return res;
}



/**
* @class
* @augments {SqlFormatter}
Expand All @@ -21,8 +23,10 @@ class MSSqlFormatter extends SqlFormatter {
*/
constructor() {
super();
const offset = new Date().getTimezoneOffset();
this.settings = {
nameFormat: '[$1]'
nameFormat: '[$1]',
timezone: (offset <= 0 ? '+' : '-') + zeroPad(-Math.floor(offset / 60), 2) + ':' + zeroPad(offset % 60, 2)
};
}
formatLimitSelect(obj) {
Expand Down Expand Up @@ -219,35 +223,35 @@ class MSSqlFormatter extends SqlFormatter {
}

$year(p0) {
return sprintf('DATEPART(year, %s)', this.escape(p0));
return sprintf('DATEPART(year, SWITCHOFFSET(%s, \'%s\'))', this.escape(p0), this.settings.timezone);
}

$month(p0) {
return sprintf('DATEPART(month, %s)', this.escape(p0));
return sprintf('DATEPART(month, SWITCHOFFSET(%s, \'%s\'))', this.escape(p0), this.settings.timezone);
}

$dayOfMonth(p0) {
return sprintf('DATEPART(day, %s)', this.escape(p0));
return sprintf('DATEPART(day, SWITCHOFFSET(%s, \'%s\'))', this.escape(p0), this.settings.timezone);
}

$day(p0) {
return this.$dayOfMonth(p0);
}

$hour(p0) {
return sprintf('DATEPART(hour, %s)', this.escape(p0));
return sprintf('DATEPART(hour, SWITCHOFFSET(%s, \'%s\'))', this.escape(p0), this.settings.timezone);
}

$minute(p0) {
return sprintf('DATEPART(minute, %s)', this.escape(p0));
return sprintf('DATEPART(minute, SWITCHOFFSET(%s, \'%s\'))', this.escape(p0), this.settings.timezone);
}

$minutes(p0) {
return this.$minute(p0);
}

$second(p0) {
return sprintf('DATEPART(second, %s)', this.escape(p0));
return sprintf('DATEPART(second, SWITCHOFFSET(%s, \'%s\'))', this.escape(p0), this.settings.timezone);
}

$seconds(p0) {
Expand Down

0 comments on commit 75cb91c

Please sign in to comment.