Skip to content

Commit

Permalink
[QP-9265] Insert Set 구문에서 identifier 파싱 오류 수정 (#92)
Browse files Browse the repository at this point in the history
## Changes

### AS-IS

MySQL, MariaDB 에서 지원되는 `INSERT .... SET` 쿼리의 경우 identifier 가
`{테이블명}.{컬럼명}` 형식으로 들어오는데, 기존 QSI 에서는 이 경우 컬럼명이 아니라 테이블명을 파싱하기 때문에 에러를
일으킵니다.

또한, default 값이 지정돼있으나 not null 제약조건이 있는 경우 insert 를 통해 null 을 대입하면
default 값이 할당되나, 기존 코드는 null 을 대입하는 것으로 보고 not null 에러를 일으킵니다.

### TO-BE

identifier 를 적절히 파싱하고, default 있는 경우에 null 대입하면 적절히 default 대입하도록 수정합니다.

또한, 테스트 케이스 실행 결과를 검증하는 부분을 적절히 수정합니다.


## Note

이 이슈로 인한 NotNullConstraints 에러는 테이블을 가져오면서 각 컬럼의 IsNullable 정보를 적절히 가져오는
구현을 했던 경우에만 발생하는 에러이므로, IsNullable 정보를 적절히 가져오는 구현을 하지 않았던 경우에는 에러가 발생하지
않았습니다.
  • Loading branch information
irwin-chequer authored Jun 14, 2024
1 parent 4cd0608 commit 81b1754
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

+-------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------+------------+-----------+-------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+-------------+
| 1 | MORRIS | BABO | null |
+-------------------------------------------------+
+-------------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------+------------+-----------+-------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+-------------------+
| 1 | MORRIS | BABO | CURRENT_TIMESTAMP |
+-------------------------------------------------------+
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

+-------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------+------------+-----------+-------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+-------------+
| default | MORRIS | BABO | default |
+-------------------------------------------------+
+-------------------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------------+------------+-----------+-------------------+
| actor_id | first_name | last_name | last_update |
+----------------+------------+-----------+-------------------+
| AUTO_INCREMENT | MORRIS | BABO | CURRENT_TIMESTAMP |
+-------------------------------------------------------------+
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

+-------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------+------------+-----------+-------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+-------------+
| 1 | MORRIS | BABO | null |
+-------------------------------------------------+
+-------------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------+------------+-----------+-------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+-------------------+
| 1 | MORRIS | BABO | CURRENT_TIMESTAMP |
+-------------------------------------------------------+
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

+-------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------+------------+-----------+-------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+-------------+
| default | MORRIS | BABO | default |
+-------------------------------------------------+
+-------------------------------------------------------------+
| qsi_unit_tests.actor - INSERT |
+----------------+------------+-----------+-------------------+
| actor_id | first_name | last_name | last_update |
+----------------+------------+-----------+-------------------+
| AUTO_INCREMENT | MORRIS | BABO | CURRENT_TIMESTAMP |
+-------------------------------------------------------------+
15 changes: 10 additions & 5 deletions Qsi/Analyzers/Action/QsiActionAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ protected virtual SetColumnTarget ResolveSetColumnTarget(

protected virtual QsiTableColumn ResolveNotNullableColumnWithInvalidDefault(IEnumerable<QsiTableColumn> columns, IEnumerable<ColumnTarget> columnTargets)
{
HashSet<string> targetNames = columnTargets.Select(ct => ct.DeclaredName.SubIdentifier(0).ToString()).ToHashSet();
HashSet<string> targetNames = columnTargets.Select(ct => ct.DeclaredName.SubIdentifier(^1).ToString()).ToHashSet();

return columns
.FirstOrDefault(x => !targetNames.Contains(x.Name.Value) && !x.IsNullable && x.Default is null);
Expand Down Expand Up @@ -685,7 +685,10 @@ private async ValueTask ProcessQueryValues(TableDataInsertContext context, IQsiT

foreach (var row in dataTable.Rows)
{
PopulateInsertRow(context, pivot => row.Items[pivot.SourceOrder]);
PopulateInsertRow(context, pivot =>
{
return row.Items[pivot.SourceOrder];
});
}

var tableAnalyzer = context.Engine.GetAnalyzer<QsiTableAnalyzer>();
Expand Down Expand Up @@ -801,9 +804,11 @@ private void PopulateInsertRow(TableDataInsertContext context, DataValueSelector
{
ref var item = ref targetRow.Items[pivot.DestinationOrder];

item = pivot.SourceColumn is not null
? valueSelector(pivot)
: ResolveDefaultColumnValue(pivot);
if (pivot.SourceColumn is not null)
item = valueSelector(pivot);

if (item?.Value is null)
item = ResolveDefaultColumnValue(pivot);

if (item.Value is null && target.Table.Columns[pivot.DestinationOrder].IsNullable == false)
throw new QsiException(QsiError.NotNullConstraints, pivot.DestinationColumn.Name.Value);
Expand Down

0 comments on commit 81b1754

Please sign in to comment.