Skip to content

Commit

Permalink
#62 - update support bug fix and doc added
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Dec 26, 2023
1 parent 9d72e4e commit aca3701
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ You can now focus on building business logic and beautiful user interfaces at sp
![DB2Rest- How it works?](assets/db2rest-hiw.png "DB2Rest")



The diagram above shows an application architecture with DB2Rest. DB2Rest provides secure access to the database as REST API within seconds of installation/deployment.
The business logic can be written in your favorite technology frameworks for Java, PHP, Node, .NET or using any serverless framework. The business logic layer uses the database access layer (DBAL) provided
by DB2Rest to query and modify data. The user experience layer can be developed using popular front-end frameworks or low code/node code platforms. This layer can make use of the business logic layer or directly access secure data layer provided by DB2Rest.
Expand Down Expand Up @@ -437,6 +436,37 @@ echo '[
```


**8. Update record with filter **

This PATCH operation updates the film with id = 1001 which was inserted earlier. Filter is optional. In this case it will update all the rows in the table. Hence, use PATCH update with care.

```Shell
curl --request PATCH \
--url 'http://localhost:8080/film?filter=film_id%3D%3D1001' \
--header 'Content-Profile: sakila' \
--header 'Content-Type: application/json' \
--header 'User-Agent: insomnia/8.4.5' \
--data '{
"rental_rate" : 1.99,
"length" : 92
}'
```
**HTTPie**

```Shell
echo '{
"rental_rate" : 1.99,
"length" : 92
}' | \
http PATCH 'http://localhost:8080/film?filter=film_id%3D%3D1001' \
Content-Profile:sakila \
Content-Type:application/json \
User-Agent:insomnia/8.4.5
```


# HTTP Headers
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/com/homihq/db2rest/rest/update/UpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import com.homihq.db2rest.schema.SchemaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jooq.DSLContext;
import org.jooq.Table;
import org.jooq.UpdateConditionStep;
import org.apache.commons.lang3.StringUtils;
import org.jooq.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.jooq.impl.DSL.field;

@Service
@Slf4j
Expand All @@ -31,12 +33,28 @@ public void update(String schemaName, String tableName, Map<String,Object> data,

Table<?> table = schemaService.getTableByNameAndSchema(schemaName, tableName);

UpdateConditionStep<?> updateConditionStep = dslContext.update(table)
.set(data)
.where(whereBuilder.create(table , tableName, filter));
UpdateSetFirstStep<?> updateSetFirstStep = dslContext.update(table);

UpdateSetMoreStep<?> updateSetMoreStep = null;

for(String key : data.keySet()) {
updateSetMoreStep = updateSetFirstStep.set(field(key) , data.get(key));
}

UpdateConditionStep<?> updateConditionStep;
String sql;
List<Object> bindValues;
if(StringUtils.isNotBlank(filter)) {
updateConditionStep = updateSetMoreStep.where(whereBuilder.create(table , tableName, filter));
sql = updateConditionStep.getSQL();
bindValues = updateConditionStep.getBindValues();
}
else{
sql = updateSetMoreStep.getSQL();
bindValues = updateSetMoreStep.getBindValues();
}


String sql = updateConditionStep.getSQL();
List<Object> bindValues = updateConditionStep.getBindValues();
log.info("SQL - {}", sql); // TODO make it conditional
log.info("Bind variables - {}", bindValues);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.homihq.db2rest.rsql.operators;

import org.jooq.Condition;
import org.jooq.types.UNumber;
import org.jooq.types.UShort;

import java.util.List;

Expand All @@ -23,6 +25,7 @@ else if (Boolean.class == type || boolean.class == type) {
else if (Integer.class == type || int.class == type) {
return Integer.valueOf(value);
}

else {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.homihq.db2rest.rsql.operators.handler;

import com.homihq.db2rest.rsql.operators.OperatorHandler;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Condition;
import static org.jooq.impl.DSL.*;


@Slf4j
public class EqualToOperatorHandler implements OperatorHandler {

@Override
public Condition handle(String columnName, String value, Class type) {
log.info("Type - {}", type);

return
field(columnName).eq(val(parseValue(value,type)));

Expand Down

0 comments on commit aca3701

Please sign in to comment.