|
| 1 | +* New Update and UpdateAsync upgrade: CASE |
| 2 | + |
| 3 | +** A new feature added to allow developers to programmatically set CASE WHEN when assigning values. Feature includes grouping in sub statements () or |
| 4 | +** to allow condition to point to a column variable instead of a direct paramater value. SQL injection friendly |
| 5 | + |
| 6 | +** Original Update Statement for multiple records using anonymous objects: |
| 7 | + |
| 8 | +*** foreach (var item in data) |
| 9 | + |
| 10 | +*** { |
| 11 | + |
| 12 | +*** object obj = new |
| 13 | + |
| 14 | +*** { |
| 15 | + |
| 16 | +*** MyField = item.Value |
| 17 | + |
| 18 | +*** }; |
| 19 | + |
| 20 | +*** cnt += await QueryFactory.Query(tableName).Where("Id", item.Id).UpdateAsync(value); |
| 21 | + |
| 22 | + |
| 23 | +*** } |
| 24 | + |
| 25 | +*** return cnt; |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +** New Update with select case using multi-level array systems |
| 32 | +** version 1 : allows is equal condition only for now |
| 33 | +** For the Else it will always fill with name of field itself , self assigning. |
| 34 | +** This happens if format is wrong as well. |
| 35 | +** The else protects you fro your field to be set back to NULL |
| 36 | + |
| 37 | +*** Warning: Limitation is requires , Suggest 200 rows for low number columns, |
| 38 | +*** 25 for higher number columns or clauses. |
| 39 | + |
| 40 | + |
| 41 | + var datac = data.Chunk(200); // breaking data up to 200 rows |
| 42 | + |
| 43 | + //each holds for each update set, which allows multiple value setting as older Update |
| 44 | + List<object[]> cases = []; |
| 45 | + |
| 46 | + if (datac.Any()) foreach (var d in datac) |
| 47 | + { |
| 48 | + |
| 49 | + try |
| 50 | + { |
| 51 | + foreach (var item in d) //Build case when statement , standard 3 |
| 52 | + { |
| 53 | + cases.Add(["Id", item.Id, item.Value]); |
| 54 | + } |
| 55 | + object obj = new |
| 56 | + { |
| 57 | + MyField= cases.ToArray() |
| 58 | + }; |
| 59 | + cases.Clear(); |
| 60 | + |
| 61 | + //if data set is smaller than whole table , best to use in statement to reduce cost |
| 62 | + cnt += await QueryFactory.Query(tableName) |
| 63 | + .WhereIn("Id", d.Select(dd => dd.Id).ToArray()) |
| 64 | + .UpdateAsync(value); |
| 65 | + } |
| 66 | + catch { throw; } |
| 67 | + finally { cases.Clear(); } |
| 68 | + } |
| 69 | + else cases.Clear(); |
| 70 | + |
| 71 | + return cnt; |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +**standard: Case WHEN x = A then Y... END: |
| 77 | +*** In your cases array the flow is [x,A,Y]. |
| 78 | +*** Assignmet value is always last. |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +** Available Feaure 1 : While its common to do 3 items for basic, when can extend the criteria with AND and OR |
| 85 | +** It combine, the array column after the orevioud criteria field must be an AND or OR, unless using , () or * explained later |
| 86 | + |
| 87 | +*** Note: Assignmet value is always last. you can use AND,&&,& or OR,||,|, <>. Not case sensitive. |
| 88 | + |
| 89 | +*** Case WHEN x = A AND z = B then Y ... END: |
| 90 | +*** In your cases array the flow is [x,A,"AND",z,B,Y] |
| 91 | +*** Case WHEN x = A OR z = B then Y ... END: |
| 92 | +*** Array the flow is [x,A,"OR",z,B,Y] |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +** Available Feaure 2 : Subset (). This allows seperating your "And" & "Or" blocks |
| 99 | +*** ex: case when (a = 1 or a = 5) and (b = 7 and c = 2) |
| 100 | +*** This can be placed anywhere before the assignment column or * assignment column, |
| 101 | +*** if you forget to add the ) to close, the engine |
| 102 | +*** will compensate. |
| 103 | + |
| 104 | +*** Case WHEN (x = A AND z = B) OR J = C then Y ... END: |
| 105 | +*** Array the flow is ["(",x,A,"AND",z,B,")","OR",j,c,Y] |
| 106 | +*** Case WHEN (x = A OR z = B) AND (J = C AND K = D) then Y ... END: |
| 107 | +*** Array the flow is ["(",x,A,"OR",z,B,")","AND","(",j,c,"AND",k,d,")" Y] |
| 108 | + |
| 109 | + |
| 110 | +** Available Feaure 3 : To Another Column Field (*). This allows criteria to check if column equals another column (field) |
| 111 | +*** Case WHEN (colx = colb AND colz = colx) then Y ... END: |
| 112 | +*** Array the flow is [,colx,*',colb,"AND",colz,colx, Y] |
0 commit comments