Skip to content

Commit

Permalink
fixing datetime and bit issue in DW. (#1931)
Browse files Browse the repository at this point in the history
## Why make this change?

Datawarehouse was having issues with datetime. This is because we are
using string agg. if we dont add the "" to datetime, it is being
deserialized as a number and we get a bug saying there is a - in the
number. FOR JSON converts bit to true or false and hence we need to do
same.

## What is this change?

Hence for datetime we need to add the wrapping "" to ensure correct
deserialization.
if datatype is bit set to true or false.


## How was this tested?

unit tests added. 
integration test done with dab running and dw datetime supported. image:
![Uploading image.png…]()

---------

Co-authored-by: aaronburtle <93220300+aaronburtle@users.noreply.github.com>
  • Loading branch information
rohkhann and aaronburtle committed Dec 20, 2023
1 parent 2902ccb commit 462eb00
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 10 deletions.
4 changes: 4 additions & 0 deletions config-generators/dwsql-commands.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
init --config "dab-config.DwSql.json" --database-type dwsql --set-session-context true --connection-string "Server=tcp:127.0.0.1,1433;Persist Security Info=False;User ID=sa;Password=REPLACEME;MultipleActiveResultSets=False;Connection Timeout=5;" --host-mode Development --cors-origin "http://localhost:5000"
add Publisher --config "dab-config.DwSql.json" --source publishers --permissions "anonymous:read" --source.key-fields "id"
add Stock --config "dab-config.DwSql.json" --source stocks --permissions "anonymous:create,read,update,delete" --source.key-fields "categoryid,pieceid"
add stocks_price --config "dab-config.DwSql.json" --source stocks_price --permissions "authenticated:create,read,update,delete" --source.key-fields "categoryid,pieceid,instant"
add Book --config "dab-config.DwSql.json" --source books --permissions "anonymous:create,read,update,delete" --graphql "book:books" --source.key-fields "id"
add BookWebsitePlacement --config "dab-config.DwSql.json" --source book_website_placements --permissions "anonymous:read" --source.key-fields "id"
add Author --config "dab-config.DwSql.json" --source authors --permissions "anonymous:read" --source.key-fields "id"
Expand All @@ -25,6 +26,9 @@ add Notebook --config "dab-config.DwSql.json" --source "notebooks" --permissions
add Journal --config "dab-config.DwSql.json" --source "journals" --rest true --graphql true --permissions "policy_tester_noupdate:create,delete" --source.key-fields "id"
add ArtOfWar --config "dab-config.DwSql.json" --source "aow" --rest true --graphql false --permissions "anonymous:*" --source.key-fields "NoteNum"
add stocks_view_selected --config "dab-config.DwSql.json" --source stocks_view_selected --source.type "view" --source.key-fields "categoryid,pieceid" --permissions "anonymous:*" --rest true --graphql true
update stocks_price --config "dab-config.DwSql.json" --permissions "anonymous:read"
update stocks_price --config "dab-config.DwSql.json" --permissions "TestNestedFilterFieldIsNull_ColumnForbidden:read" --fields.exclude "price"
update stocks_price --config "dab-config.DwSql.json" --permissions "TestNestedFilterFieldIsNull_EntityReadForbidden:create"
update Publisher --config "dab-config.DwSql.json" --permissions "authenticated:create,read,update,delete" --rest true --graphql true --relationship books --target.entity Book --cardinality many --relationship.fields "id:publisher_id"
update Publisher --config "dab-config.DwSql.json" --permissions "policy_tester_01:create,delete"
update Publisher --config "dab-config.DwSql.json" --permissions "policy_tester_01:update" --fields.include "*"
Expand Down
19 changes: 17 additions & 2 deletions src/Core/Resolvers/DWSqlQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,23 @@ private static string GenerateColumnsAsJson(SqlQueryStructure structure, bool su
if (!subQueryColumn && structure.GetColumnSystemType(column.ColumnName) != typeof(string))
{
col_value = $"CAST([{col_value}] AS NVARCHAR(MAX))";
// Create json. Example: "book.id": 1 would be a sample output.
stringAgg.Append($"N\'\"{escapedLabel}\":\' + ISNULL(STRING_ESCAPE({col_value},'json'),'null')");

Type col_type = structure.GetColumnSystemType(column.ColumnName);

if (col_type == typeof(DateTime))
{
// Need to wrap datetime in quotes to ensure correct deserialization.
stringAgg.Append($"N\'\"{escapedLabel}\":\"\' + ISNULL(STRING_ESCAPE({col_value},'json'),'null') + \'\"\'+");
}
else if (col_type == typeof(Boolean))
{
stringAgg.Append($"N\'\"{escapedLabel}\":\' + ISNULL(IIF({col_value} = 1, 'true', 'false'),'null')");
}
else
{
// Create json. Example: "book.id": 1 would be a sample output.
stringAgg.Append($"N\'\"{escapedLabel}\":\' + ISNULL(STRING_ESCAPE({col_value},'json'),'null')");
}
}
else
{
Expand Down
12 changes: 11 additions & 1 deletion src/Service.Tests/DatabaseSchema-DwSql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ DROP TABLE IF EXISTS book_website_placements;
DROP TABLE IF EXISTS website_users;
DROP TABLE IF EXISTS books;
DROP TABLE IF EXISTS [foo].[magazines];
DROP TABLE IF EXISTS stocks_price;
DROP TABLE IF EXISTS stocks;
DROP TABLE IF EXISTS comics;
DROP TABLE IF EXISTS brokers;
Expand Down Expand Up @@ -125,6 +126,15 @@ CREATE TABLE stocks(
piecesRequired int NOT NULL
);


CREATE TABLE stocks_price(
categoryid int NOT NULL,
pieceid int NOT NULL,
instant datetime NOT NULL,
price int,
is_wholesale_price bit
);

CREATE TABLE brokers(
[ID Number] int NOT NULL,
[First Name] varchar(2048) NOT NULL,
Expand Down Expand Up @@ -229,7 +239,7 @@ INSERT INTO brokers([ID Number], [First Name], [Last Name]) VALUES (1, 'Michael'
INSERT INTO publishers(id, name) VALUES (1234, 'Big Company'), (2345, 'Small Town Publisher'), (2323, 'TBD Publishing One'), (2324, 'TBD Publishing Two Ltd'), (1940, 'Policy Publisher 01'), (1941, 'Policy Publisher 02'), (1156, 'The First Publisher');
INSERT INTO book_author_link(book_id, author_id) VALUES (1, 123), (2, 124), (3, 123), (3, 124), (4, 123), (4, 124), (5, 126);
INSERT INTO stocks(categoryid, pieceid, categoryName, piecesAvailable, piecesRequired) VALUES (1,1,'SciFi',0,0),(2,1,'Tales',0,0),(0,1,'',0,0),(100,99,'Historical',0,0);

INSERT INTO stocks_price (categoryid, pieceid, instant, price, is_wholesale_price) VALUES (2, 1, '2023-08-21 15:11:04', 100, 1);
INSERT INTO notebooks(id, notebookname, color, ownername) VALUES (1, 'Notebook1', 'red', 'Sean'), (2, 'Notebook2', 'green', 'Ani'), (3, 'Notebook3', 'blue', 'Jarupat'), (4, 'Notebook4', 'yellow', 'Aaron');
INSERT INTO journals(id, journalname, color, ownername) VALUES (1, 'Journal1', 'red', 'Sean'), (2, 'Journal2', 'green', 'Ani'), (3, 'Journal3', 'blue', 'Jarupat'), (4, 'Journal4', 'yellow', 'Aaron');
INSERT INTO aow(NoteNum, DetailAssessmentAndPlanning, WagingWar, StrategicAttack) VALUES (1, 'chapter one notes: ', 'chapter two notes: ', 'chapter three notes: ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class DwSqlFindApiTests : FindApiTestBase
$"SELECT * FROM { _integrationTableName } " +
$"WHERE id = 2 FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER"
},
{
"FindByDateTimePKTest",
$"SELECT categoryid, pieceid, FORMAT(instant, 'MMM dd yyyy h:mmtt') as instant, price, is_wholesale_price FROM { _tableWithDateTimePK } " +
$"WHERE categoryid = 2 AND pieceid = 1 AND instant = '2023-08-21 15:11:04' FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER"
},
{
"FindEmptyTable",
$"SELECT * FROM { _emptyTableTableName } " +
Expand Down Expand Up @@ -593,13 +598,6 @@ public override string GetQuery(string key)
return _queryMap[key];
}

[TestMethod]
[Ignore]
public override Task FindByDateTimePKTest()
{
throw new NotImplementedException();
}

// Pending Stored Procedure Support
[TestMethod]
[Ignore]
Expand Down
69 changes: 69 additions & 0 deletions src/Service.Tests/dab-config.DwSql.json
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,75 @@
}
]
},
"stocks_price": {
"source": {
"object": "stocks_price",
"type": "table",
"key-fields": [
"categoryid",
"pieceid",
"instant"
]
},
"graphql": {
"enabled": true,
"type": {
"singular": "stocks_price",
"plural": "stocks_prices"
}
},
"rest": {
"enabled": true
},
"permissions": [
{
"role": "anonymous",
"actions": [
{
"action": "read"
}
]
},
{
"role": "authenticated",
"actions": [
{
"action": "create"
},
{
"action": "read"
},
{
"action": "update"
},
{
"action": "delete"
}
]
},
{
"role": "TestNestedFilterFieldIsNull_ColumnForbidden",
"actions": [
{
"action": "read",
"fields": {
"exclude": [
"price"
]
}
}
]
},
{
"role": "TestNestedFilterFieldIsNull_EntityReadForbidden",
"actions": [
{
"action": "create"
}
]
}
]
},
"Book": {
"source": {
"object": "books",
Expand Down

0 comments on commit 462eb00

Please sign in to comment.