Skip to content
This repository was archived by the owner on Oct 26, 2023. It is now read-only.

Commit 3f72425

Browse files
author
Thomas Risberg
committed
Working on examples for Oracle
1 parent a6b7ff7 commit 3f72425

File tree

11 files changed

+423
-122
lines changed

11 files changed

+423
-122
lines changed

oracle/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ eclipse {
1414
}
1515

1616
repositories {
17+
mavenLocal()
1718
mavenCentral()
18-
maven { url "http://repo.springsource.org/libs-snapshot" }
19+
maven { url "http://repo.springsource.org/libs-milestone" }
1920
}
2021

2122
dependencies {

oracle/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ sdCommonsVersion = 1.3.0.RELEASE
1111
groovyVersion = 1.8.6
1212

1313
# Spring Data JDBC
14-
sdJdbcVersion = 1.0.0.RC3
14+
sdJdbcVersion = 1.0.0.RC4
1515

1616
# spring-data-book properties
1717
version = 1.0.0.BUILD-SNAPSHOT

oracle/src/main/java/com/oreilly/springdata/jdbc/domain/Address.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
*/
5-
public class Address extends AbstractEntity {
5+
public class Address {
66
private String street, city, country;
77

88
public Address() {

oracle/src/main/java/com/oreilly/springdata/jdbc/oracle/CustomerRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ public interface CustomerRepository {
1919
void delete(Customer customer);
2020

2121
Customer findByEmailAddress(EmailAddress emailAddress);
22+
23+
void saveAddresses(Customer customer);
2224
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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.sql.ARRAY;
7+
import oracle.sql.STRUCT;
8+
import oracle.sql.StructDescriptor;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.jdbc.support.oracle.StructMapper;
11+
import org.springframework.jdbc.core.JdbcTemplate;
12+
13+
import javax.sql.DataSource;
14+
import java.sql.Connection;
15+
import java.sql.SQLException;
16+
import java.util.List;
17+
18+
public class JdbcTemplateCustomerRepository implements CustomerRepository {
19+
20+
private JdbcTemplate jdbcTemplate;
21+
22+
private static CustomerMapper customerMapper = new CustomerMapper();
23+
24+
private static AddressMapper addressMapper = new AddressMapper();
25+
26+
27+
@Autowired
28+
public void setDataSource(DataSource dataSource) {
29+
this.jdbcTemplate = new JdbcTemplate(dataSource);
30+
}
31+
32+
@Override
33+
public Customer findById(Long id) {
34+
return null;
35+
}
36+
37+
@Override
38+
public List<Customer> findAll() {
39+
return null;
40+
}
41+
42+
@Override
43+
public void save(Customer customer) {
44+
}
45+
46+
@Override
47+
public void delete(Customer customer) {
48+
49+
}
50+
51+
@Override
52+
public Customer findByEmailAddress(EmailAddress emailAddress) {
53+
return null;
54+
}
55+
56+
@Override
57+
public void saveAddresses(Customer customer) {
58+
}
59+
60+
61+
private static class CustomerMapper implements StructMapper<Customer> {
62+
63+
private static AddressMapper addressMapper = new AddressMapper();
64+
65+
@Override
66+
public STRUCT toStruct(Customer customer, Connection conn, String typeName) throws SQLException {
67+
StructDescriptor descriptor = new StructDescriptor(typeName, conn);
68+
Object[] values = new Object[5];
69+
values[0] = customer.getId();
70+
values[1] = customer.getFirstName();
71+
values[2] = customer.getLastName();
72+
values[3] = customer.getEmailAddress().toString();
73+
return new STRUCT(descriptor, conn, values);
74+
}
75+
76+
@Override
77+
public Customer fromStruct(STRUCT struct) throws SQLException {
78+
Object[] attr = struct.getAttributes();
79+
Customer customer = new Customer();
80+
if (attr[0] != null && attr[0] instanceof Number) {
81+
customer.setId(Long.valueOf(((Number)attr[0]).longValue()));
82+
}
83+
if (attr[1] != null && attr[1] instanceof String) {
84+
customer.setFirstName((String) attr[1]);
85+
}
86+
if (attr[2] != null && attr[2] instanceof String) {
87+
customer.setLastName((String) attr[2]);
88+
}
89+
if (attr[3] != null && attr[3] instanceof String) {
90+
customer.setEmailAddress(new EmailAddress((String)attr[3]));
91+
}
92+
if (attr[4] != null && attr[4] instanceof ARRAY) {
93+
ARRAY addrArray = (ARRAY) attr[4];
94+
Object[] addrStructs = (Object[]) addrArray.getArray();
95+
for (Object o : addrStructs) {
96+
if (o instanceof STRUCT) {
97+
customer.addAddress(addressMapper.fromStruct((STRUCT) o));
98+
}
99+
}
100+
}
101+
return customer;
102+
}
103+
}
104+
105+
private static class AddressMapper implements StructMapper<Address> {
106+
107+
@Override
108+
public STRUCT toStruct(Address source, Connection conn, String typeName) throws SQLException {
109+
StructDescriptor descriptor = new StructDescriptor("ADDRESS_TYPE", conn);
110+
Object[] values = new Object[3];
111+
values[0] = source.getStreet();
112+
values[1] = source.getCity();
113+
values[2] = source.getCountry().toString();
114+
return new STRUCT(descriptor, conn, values);
115+
}
116+
117+
@Override
118+
public Address fromStruct(STRUCT struct) throws SQLException {
119+
Object[] attr = struct.getAttributes();
120+
Address address = new Address();
121+
if (attr[0] != null && attr[0] instanceof String) {
122+
address.setStreet((String) attr[0]);
123+
}
124+
if (attr[1] != null && attr[1] instanceof String) {
125+
address.setCity((String) attr[1]);
126+
}
127+
if (attr[2] != null && attr[2] instanceof String) {
128+
address.setCountry((String) attr[2]);
129+
}
130+
return address;
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)