|
| 1 | +package com.oreilly.springdata.jdbc.oracle; |
| 2 | + |
| 3 | +import com.oreilly.springdata.jdbc.domain.Address; |
| 4 | +import com.oreilly.springdata.jdbc.domain.Customer; |
| 5 | +import com.oreilly.springdata.jdbc.domain.EmailAddress; |
| 6 | +import oracle.jdbc.OracleTypes; |
| 7 | +import oracle.sql.ARRAY; |
| 8 | +import oracle.sql.STRUCT; |
| 9 | +import oracle.sql.StructDescriptor; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.data.jdbc.support.oracle.SqlReturnStruct; |
| 12 | +import org.springframework.data.jdbc.support.oracle.SqlStructArrayValue; |
| 13 | +import org.springframework.data.jdbc.support.oracle.SqlStructValue; |
| 14 | +import org.springframework.jdbc.core.SqlOutParameter; |
| 15 | +import org.springframework.jdbc.core.SqlParameter; |
| 16 | +import org.springframework.jdbc.core.simple.SimpleJdbcCall; |
| 17 | +import org.springframework.data.jdbc.support.oracle.StructMapper; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | +import java.sql.Connection; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.sql.Types; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +public class JdbcCallCustomerRepository implements CustomerRepository { |
| 28 | + |
| 29 | + private SimpleJdbcCall getCustomerCall; |
| 30 | + |
| 31 | + private SimpleJdbcCall saveCustomerCall; |
| 32 | + |
| 33 | + private SimpleJdbcCall saveAddressesCall; |
| 34 | + |
| 35 | + private static CustomerMapper customerMapper = new CustomerMapper(); |
| 36 | + |
| 37 | + private static AddressMapper addressMapper = new AddressMapper(); |
| 38 | + |
| 39 | + |
| 40 | + @Autowired |
| 41 | + public void setDataSource(DataSource dataSource) { |
| 42 | + |
| 43 | + this.getCustomerCall = new SimpleJdbcCall(dataSource).withProcedureName("get_customer") |
| 44 | + .declareParameters(new SqlOutParameter("out_customer", |
| 45 | + OracleTypes.STRUCT, "CUSTOMER_TYPE", |
| 46 | + new SqlReturnStruct(customerMapper))); |
| 47 | + |
| 48 | + this.saveCustomerCall = new SimpleJdbcCall(dataSource).withFunctionName("save_customer") |
| 49 | + .declareParameters( |
| 50 | + new SqlOutParameter("result", Types.BIGINT), |
| 51 | + new SqlParameter("in_customer", OracleTypes.STRUCT, "CUSTOMER_TYPE")); |
| 52 | + |
| 53 | + this.saveAddressesCall = new SimpleJdbcCall(dataSource).withProcedureName("save_addresses") |
| 54 | + .declareParameters( |
| 55 | + new SqlParameter("in_customer_id", Types.BIGINT), |
| 56 | + new SqlParameter("in_addresses", OracleTypes.ARRAY, "ADDRESS_TABLE_TYPE")); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Customer findById(Long id) { |
| 61 | + Map<String, Object> in = new HashMap<String, Object>(); |
| 62 | + in.put("in_customer_id", id); |
| 63 | + return getCustomerCall.executeObject(Customer.class, in); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public List<Customer> findAll() { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void save(Customer customer) { |
| 73 | + Map<String, Object> in = new HashMap<String, Object>(); |
| 74 | + in.put("in_customer", new SqlStructValue<Customer>(customer, customerMapper)); |
| 75 | + Long id = saveCustomerCall.executeObject(Long.class, in); |
| 76 | + if (customer.getId() == null) { |
| 77 | + customer.setId(id); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void delete(Customer customer) { |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Customer findByEmailAddress(EmailAddress emailAddress) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void saveAddresses(Customer customer) { |
| 93 | + Map<String, Object> in = new HashMap<String, Object>(); |
| 94 | + in.put("in_customer_id", customer.getId()); |
| 95 | + in.put("in_addresses", |
| 96 | + new SqlStructArrayValue<Address>(customer.getAddresses().toArray(new Address[0]), addressMapper)); |
| 97 | + saveAddressesCall.execute(in); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + private static class CustomerMapper implements StructMapper<Customer> { |
| 102 | + |
| 103 | + private static AddressMapper addressMapper = new AddressMapper(); |
| 104 | + |
| 105 | + @Override |
| 106 | + public STRUCT toStruct(Customer customer, Connection conn, String typeName) throws SQLException { |
| 107 | + StructDescriptor descriptor = new StructDescriptor(typeName, conn); |
| 108 | + Object[] values = new Object[5]; |
| 109 | + values[0] = customer.getId(); |
| 110 | + values[1] = customer.getFirstName(); |
| 111 | + values[2] = customer.getLastName(); |
| 112 | + values[3] = customer.getEmailAddress().toString(); |
| 113 | + return new STRUCT(descriptor, conn, values); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Customer fromStruct(STRUCT struct) throws SQLException { |
| 118 | + Object[] attr = struct.getAttributes(); |
| 119 | + Customer customer = new Customer(); |
| 120 | + if (attr[0] != null && attr[0] instanceof Number) { |
| 121 | + customer.setId(Long.valueOf(((Number)attr[0]).longValue())); |
| 122 | + } |
| 123 | + if (attr[1] != null && attr[1] instanceof String) { |
| 124 | + customer.setFirstName((String) attr[1]); |
| 125 | + } |
| 126 | + if (attr[2] != null && attr[2] instanceof String) { |
| 127 | + customer.setLastName((String) attr[2]); |
| 128 | + } |
| 129 | + if (attr[3] != null && attr[3] instanceof String) { |
| 130 | + customer.setEmailAddress(new EmailAddress((String)attr[3])); |
| 131 | + } |
| 132 | + if (attr[4] != null && attr[4] instanceof ARRAY) { |
| 133 | + ARRAY addrArray = (ARRAY) attr[4]; |
| 134 | + Object[] addrStructs = (Object[]) addrArray.getArray(); |
| 135 | + for (Object o : addrStructs) { |
| 136 | + if (o instanceof STRUCT) { |
| 137 | + customer.addAddress(addressMapper.fromStruct((STRUCT) o)); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return customer; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static class AddressMapper implements StructMapper<Address> { |
| 146 | + |
| 147 | + @Override |
| 148 | + public STRUCT toStruct(Address source, Connection conn, String typeName) throws SQLException { |
| 149 | + StructDescriptor descriptor = new StructDescriptor("ADDRESS_TYPE", conn); |
| 150 | + Object[] values = new Object[3]; |
| 151 | + values[0] = source.getStreet(); |
| 152 | + values[1] = source.getCity(); |
| 153 | + values[2] = source.getCountry().toString(); |
| 154 | + return new STRUCT(descriptor, conn, values); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public Address fromStruct(STRUCT struct) throws SQLException { |
| 159 | + Object[] attr = struct.getAttributes(); |
| 160 | + Address address = new Address(); |
| 161 | + if (attr[0] != null && attr[0] instanceof String) { |
| 162 | + address.setStreet((String) attr[0]); |
| 163 | + } |
| 164 | + if (attr[1] != null && attr[1] instanceof String) { |
| 165 | + address.setCity((String) attr[1]); |
| 166 | + } |
| 167 | + if (attr[2] != null && attr[2] instanceof String) { |
| 168 | + address.setCountry((String) attr[2]); |
| 169 | + } |
| 170 | + return address; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments