-
Notifications
You must be signed in to change notification settings - Fork 295
Add MySQL RESTAPI POST/PUT support and Tests #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
c1068fd
3c6e80c
0e6bc55
b97e70a
d309622
308ce73
2a75ca0
c038f2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -338,14 +338,36 @@ SELECT JSON_OBJECT('id', id) AS data | |
| { | ||
| "PutOne_Insert_Test", | ||
| @" | ||
| SELECT JSON_OBJECT('id', id) AS data | ||
| SELECT JSON_OBJECT('id', id, 'title', title, 'issueNumber', issueNumber ) AS data | ||
| FROM ( | ||
| SELECT id, title, publisher_id | ||
| FROM " + _integrationTableName + @" | ||
| WHERE id > 5000 AND title = 'The Hobbit Returns to The Shire' | ||
| AND publisher_id = 1234 | ||
| SELECT id, title, issueNumber | ||
| FROM " + _integration_NonAutoGenPK_TableName + @" | ||
| WHERE id > 5000 AND title = 'Batman Returns' | ||
| AND issueNumber = 1234 | ||
| ) AS subq | ||
| " | ||
| }, | ||
| { | ||
| "PutOne_Insert_Nullable_Test", | ||
| @"SELECT JSON_OBJECT('id', id, 'title', title, 'issueNumber', issueNumber ) AS data | ||
| FROM ( | ||
| SELECT id, title, issueNumber | ||
| FROM " + _integration_NonAutoGenPK_TableName + @" | ||
| WHERE id = " + $"{STARTING_ID_FOR_TEST_INSERTS + 1}" + @" AND title = 'Times' | ||
| AND issueNumber is NULL | ||
| ) as subq | ||
| " | ||
| }, | ||
| { | ||
| "PutOne_Insert_AutoGenNonPK_Test", | ||
| @"SELECT JSON_OBJECT('id', id, 'title', title, 'volume', volume ) AS data | ||
| FROM ( | ||
| SELECT id, title, volume | ||
| FROM " + _integration_AutoGenNonPK_TableName + @" | ||
| WHERE id = " + $"{STARTING_ID_FOR_TEST_INSERTS}" + @" AND title = 'Star Trek' | ||
| AND volume IS NOT NULL | ||
| ) as subq | ||
| " | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -376,60 +398,18 @@ public override string GetQuery(string key) | |
| return _queryMap[key]; | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task InsertOneTest() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task InsertOneInCompositeKeyTableTest() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task PutOne_Update_Test() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task PutOne_Insert_Test() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task PutOne_Insert_BadReq_Test() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task PutOne_Insert_BadReq_NonNullable_Test() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task PutOne_Insert_PKAutoGen_Test() | ||
| { | ||
| throw new NotImplementedException(); | ||
| throw new NotImplementedException("error: Fail, still able to insert"); | ||
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| [Ignore] | ||
| public override Task PutOne_Insert_BadReq_AutoGen_NonNullable_Test() | ||
| public override Task PutOne_Update_Test() | ||
Aniruddh25 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| throw new NotImplementedException(); | ||
| throw new NotImplementedException("error: While processing your request the server ran into an unexpected error"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,22 +60,33 @@ public string Build(SqlQueryStructure structure) | |
| /// <inheritdoc /> | ||
| public string Build(SqlInsertStructure structure) | ||
| { | ||
| // TODO: these should be put in a transcation | ||
| // No need to put into transaction as LAST_INSERT_ID is session level variable | ||
| return $"INSERT INTO {QuoteIdentifier(structure.TableName)} ({Build(structure.InsertColumns)}) " + | ||
| $"VALUES ({string.Join(", ", (structure.Values))}); " + | ||
| $"SELECT {MakeInsertSelections(structure)}"; | ||
| $" SET @ROWCOUNT=ROW_COUNT(); " + | ||
junsu0ms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $"SELECT {MakeInsertSelections(structure)} WHERE @ROWCOUNT > 0;"; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string Build(SqlUpdateStructure structure) | ||
| { | ||
| // TODO: these should be put in a transaction | ||
| return $"UPDATE {QuoteIdentifier(structure.TableName)} " + | ||
| // Create local variables to store the pk columns | ||
| string sets = String.Join(";\n", structure.PrimaryKey().Select((x, index) => $"SET {"@LU_" + index.ToString()} := 0")); | ||
junsu0ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Fetch the value to local variables | ||
| string updates = String.Join(", ", structure.PrimaryKey().Select((x, index) => | ||
| $"{QuoteIdentifier(x)} = (SELECT {"@LU_" + index.ToString()} := {QuoteIdentifier(x)})")); | ||
|
|
||
| // Select local variables and mapping to original column name | ||
| string select = String.Join(", ", structure.PrimaryKey().Select((x, index) => $"{"@LU_" + index.ToString()} AS {QuoteIdentifier(x)}")); | ||
|
|
||
| return sets + ";\n" + | ||
| $"UPDATE {QuoteIdentifier(structure.TableName)} " + | ||
| $"SET {Build(structure.UpdateOperations, ", ")} " + | ||
| $"WHERE {Build(structure.Predicates)}; " + | ||
| $"SELECT {Build(structure.PrimaryKey())} " + | ||
| $"FROM {QuoteIdentifier(structure.TableName)} " + | ||
| $"WHERE {Build(structure.Predicates)}; "; | ||
| ", " + updates + | ||
| $" WHERE {Build(structure.Predicates)}; " + | ||
| $" SET @ROWCOUNT=ROW_COUNT(); " + | ||
| $"SELECT " + select + $" WHERE @ROWCOUNT > 0;"; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
|
|
@@ -88,8 +99,26 @@ public string Build(SqlDeleteStructure structure) | |
| /// <inheritdoc /> | ||
| public string Build(SqlUpsertQueryStructure structure) | ||
| { | ||
| // TODO: these should be put in a transcation | ||
| throw new NotImplementedException(); | ||
| // Create local variables to store the pk columns | ||
| string sets = String.Join(";\n", structure.PrimaryKey().Select((x, index) => $"SET {"@LU_" + index.ToString()} := 0")); | ||
|
|
||
| // Fetch the value to local variables | ||
| string updates = String.Join(", ", structure.PrimaryKey().Select((x, index) => | ||
| $"{QuoteIdentifier(x)} = (SELECT {"@LU_" + index.ToString()} := {QuoteIdentifier(x)})")); | ||
|
|
||
| // Select local variables and mapping to original column name | ||
| string select = String.Join(", ", structure.PrimaryKey().Select((x, index) => $"{"@LU_" + index.ToString()} AS {QuoteIdentifier(x)}")); | ||
|
|
||
| string insert = $"INSERT INTO {QuoteIdentifier(structure.TableName)} ({Build(structure.InsertColumns)}) " + | ||
| $"VALUES ({string.Join(", ", (structure.Values))}) "; | ||
|
|
||
| return sets + ";\n" + | ||
| insert + " ON DUPLICATE KEY " + | ||
| $"UPDATE {Build(structure.UpdateOperations, ", ")}" + | ||
| $", " + updates + ";" + | ||
| $" SET @ROWCOUNT=ROW_COUNT(); " + | ||
| $"SELECT " + select + $" WHERE @ROWCOUNT = 2;" + | ||
| $"SELECT {MakeUpsertSelections(structure, true)} WHERE @ROWCOUNT = 1;"; | ||
junsu0ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -126,10 +155,44 @@ private string MakeInsertSelections(SqlInsertStructure structure) | |
| { | ||
| List<string> selections = new(); | ||
|
|
||
| List<string> fields = structure.PrimaryKey() | ||
| .Union(structure.InsertColumns).ToList(); | ||
|
|
||
| int index = 0; | ||
| foreach (string colName in fields) | ||
| { | ||
| string quotedColName = QuoteIdentifier(colName); | ||
| if (structure.InsertColumns.Contains(colName)) | ||
| { | ||
| selections.Add($"{structure.Values[index]} AS {quotedColName}"); | ||
| index++; | ||
| } | ||
| else if (structure.GetColumnDefinition(colName).IsAutoGenerated) | ||
| { | ||
| //TODO: This assumes one column PK | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a test for Insert with multi column PKs already :
|
||
| selections.Add($"LAST_INSERT_ID() AS {quotedColName}"); | ||
| } | ||
| } | ||
|
|
||
| return string.Join(", ", selections); | ||
| } | ||
|
|
||
| private string MakeUpsertSelections(SqlUpsertQueryStructure structure, bool includePK) | ||
| { | ||
| List<string> selections = new(); | ||
|
|
||
| List<string> fields = structure.AllColumns(); | ||
|
|
||
| int index = 0; | ||
| foreach (string colName in structure.PrimaryKey()) | ||
| foreach (string colName in fields) | ||
| { | ||
| string quotedColName = QuoteIdentifier(colName); | ||
Aniruddh25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!includePK && structure.PrimaryKey().Contains(colName)) | ||
junsu0ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (structure.InsertColumns.Contains(colName)) | ||
| { | ||
| selections.Add($"{structure.Values[index]} AS {quotedColName}"); | ||
|
|
@@ -139,6 +202,10 @@ private string MakeInsertSelections(SqlInsertStructure structure) | |
| { | ||
| selections.Add($"LAST_INSERT_ID() AS {quotedColName}"); | ||
| } | ||
| else | ||
| { | ||
| selections.Add($"NULL AS {quotedColName}"); | ||
| } | ||
| } | ||
|
|
||
| return string.Join(", ", selections); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| { | ||
| "DataGatewayConfig": { | ||
| "DatabaseConnection": { | ||
| "ConnectionString": "server=localhost;database=datagatewaytest;uid=root;pwd=REPLACEME" | ||
| "ConnectionString": "server=localhost;database=datagatewaytest;Allow User Variables=true;uid=root;pwd=REPLACEME" | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this test work even though its composite PK? I thought it doesnt... So, why remove the ignore ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it works as far as only one autogen column.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so the problem with composite key happens only when both are autogenerated.. I'll note this in the issue #264