-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsqlupdate.html
More file actions
198 lines (164 loc) · 4.89 KB
/
sqlupdate.html
File metadata and controls
198 lines (164 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<html>
<head>
<title>SqlUpdate | Ebean</title>
<meta name="layout" content="_layout2/base-docs.html"/>
<meta name="bread1" content="Query" href="/docs/query/"/>
<meta name="bread2" content="SqlUpdate" href="/docs/query/sqlupdate"/>
<#assign n0_docs="true">
<#assign n1_query="true">
<#assign sqlupdate= "true">
</head>
<body>
<h2>SqlUpdate</h2>
<p>
<em>SqlUpdate</em> allows us to execute <em>sql insert</em>, <em>sql update</em> or <em>sql delete</em> statements.
</p>
<p> </p>
<h5>Example: simple update</h5>
```java
String sql = "update audit_log set description = description where id = ?";
int row = DB.sqlUpdate(sql)
.setParameter(1, 42)
.execute();
```
<p> </p>
<h5>Example: insert with getGeneratedKeys</h5>
```java
String sql = "insert into e_person_online (email, online_status, when_updated) " +
"values (:email, :online, current_time)";
SqlUpdate sqlUpdate = DB.sqlUpdate(sql)
.setGetGeneratedKeys(true)
.setParameter("email", email)
.setParameter("online", false);
sqlUpdate.execute();
Object key = sqlUpdate.getGeneratedKey();
```
<p> </p>
<h2 id="collections" class="art">Binding collections</h2>
<p>
To bind collections/arrays of values into an IN clause we need to use
indexed positioned parameters like <code>?1, ?2, ?3</code> etc or
named parameters like <code>:ids, :names</code> etc.
</p>
```java
// using ?1 index positioned parameter
String sql = "delete from customer where ref_id in (?1)";
int rows = DB.sqlUpdate(sql)
.setParameter(asList(9991, 9992, 9993))
.execute();
```
```sql
delete from customer where ref_id in (?,?,?)
```
```java
// using :ids named parameter
String sql = "delete from customer where ref_id in (:ids)";
int rows = DB.sqlUpdate(sql)
.setParameter("ids", asList(9991, 9992, 9993))
.execute();
```
```sql
delete from customer where ref_id in (?,?,?)
```
<p> </p>
<h5>Example: binding multiple lists</h5>
```java
String sql = "delete from customer where ref_id in (:ids) and name in (:names)";
int rows = DB.sqlUpdate(sql)
.setParameter("ids", asList(9991, 9992, 9993))
.setParameter("names", asList("rob", "jim"))
.execute();
```
```sql
delete from customer where ref_id in (?,?,?) and name in (?,?)
```
<h2 id="cte" class="art">CTE - Common table expression</h2>
<p>
When we want to perform a bulk update using a <em>common table expression</em> this is
easiest done using SqlUpdate.
</p>
<h5>CTE Example</h5>
```java
String sql = """
WITH t AS (
SELECT s.id AS _id
FROM store_price s
JOIN promotion_vw p ...
WHERE ...
)
UPDATE store_price
SET promotion_price = null, price = active_price, price_reason = ?,
version = version+1, when_modified = current_timestamp
FROM t
WHERE id = t._id
"""
int rows = DB.sqlUpdate(sql)
.setParameter(1, "Expired promotions")
.execute();
```
<h2 id="addBatch" class="art">AddBatch and ExecuteBatch</h2>
<p>
We use <em>addBatch()</em> and <em>executeBatch()</em> to explicitly use JDBC batching.
</p>
```java
String sql = "insert into audit_log (id, description, modified_description) values (?,?,?)";
SqlUpdate insert = DB.sqlUpdate(sql);
try (Transaction txn = DB.beginTransaction()) {
insert.setNextParameter(10000);
insert.setNextParameter("hello");
insert.setNextParameter("foo");
insert.addBatch();
insert.setNextParameter(10001);
insert.setNextParameter("goodbye");
insert.setNextParameter("bar");
insert.addBatch();
insert.setNextParameter(10002);
insert.setNextParameter("chow");
insert.setNextParameter("baz");
insert.addBatch();
int[] rows = insert.executeBatch();
txn.commit();
}
```
<h2 id="upsert" class="art">Upsert</h2>
<p>
We can execute upsert sql which is generally database specific.
</p>
<h4>Upsert - Postgres</h4>
```java
String sql =
"insert into e_person_online (email, online_status, when_updated) values (?, ?, now()) " +
"on conflict (email) do update set when_updated=now(), online_status = ?";
String email = "foo@one.com";
Object key = DB.sqlUpdate(sql)
.setGetGeneratedKeys(true)
.setParameter(1, email)
.setParameter(2, true)
.setParameter(3, true)
.executeGetKey();
```
<p> </p>
<h4>Upsert - MySql</h4>
```java
String email = "bar@one.com";
String sql =
"insert into e_person_online (email, online_status, when_updated) values (?, ?, current_time) " +
"on duplicate key update when_updated=current_time, online_status = ?";
Object key = DB.sqlUpdate(sql)
.setGetGeneratedKeys(true)
.setParameter(1, email)
.setParameter(2, true)
.setParameter(3, true)
.executeGetKey();
```
<h2 id="l2cache" class="art">L2 cache</h2>
<p>
If L2 caching is being used Ebean will by default automatically try and determine what
table modifications are performed and use this to invalidate appropriate parts of the L2 cache.
</p>
<p>
Use <code>setAutoTableMod(false)</code> to turn this off.
</p>
<@next_edit "CallableSql" "/docs/query/callablesql" "/docs/query/sqlupdate.html"/>
</body>
</html>