Created
January 28, 2017 10:16
-
-
Save dilunika/2bff460bcb07f1887f1099280646d07c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VehicleOwnerService { | |
public static Observable<Integer> insert(VehicleOwner vehicleOwner) { | |
InsertStatement insert = new InsertStatement(vehicleOwner); | |
return databaseClient() | |
.getConnectionObservable() | |
.flatMap(con -> con.updateWithParamsObservable(insert.sql(), insert.parameters())) | |
.map(r -> r.getUpdated()); | |
} | |
static class InsertStatement { | |
JsonArray params; | |
InsertStatement(VehicleOwner vo) { | |
String email = vo.getEmail() == null ? "" : vo.getEmail(); | |
String address = vo.getAddress() == null ? Json.encode(new Address()) : Json.encode(vo.getAddress()); | |
this.params = new JsonArray() | |
.add(vo.getFirstName()) | |
.add(vo.getLastName()) | |
.add(vo.getNic()) | |
.add(Json.encode(vo.getContactNumbers())) | |
.add(email) | |
.add(address); | |
} | |
String sql() { | |
return "INSERT INTO admintools.vehicle_owners " + | |
"(first_name, last_name, nic, contact_numbers, email, address) " + | |
"VALUES(?, ?, ?, cast(? as json), ?, cast(? as json));"; | |
} | |
JsonArray parameters() { | |
return params; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment