Skip to content

Commit 1e9e1e4

Browse files
feat: Add examples for resultSet.getRow() and preparedStatement.executeBatch for faster JDBC operations. (#577)
Co-authored-by: Kartik Maji <[email protected]>
1 parent 6107ad4 commit 1e9e1e4

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

service/jdbc.gs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,35 @@ function writeManyRecords() {
125125
console.log('Failed with an error %s', err.message);
126126
}
127127
}
128+
129+
/**
130+
* Write 500 rows of data to a table in a single batch.
131+
* Recommended for faster writes
132+
*/
133+
function writeManyRecordsUsingExecuteBatch() {
134+
try {
135+
const conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
136+
conn.setAutoCommit(false);
137+
138+
const start = new Date();
139+
const stmt = conn.prepareStatement('INSERT INTO entries ' +
140+
'(guestName, content) values (?, ?)');
141+
const params = [];
142+
for (let i = 0; i < 500; i++) {
143+
params.push(['Name ' + i, 'Hello, world ' + i]);
144+
}
145+
146+
const batch = stmt.executeBatch(params);
147+
conn.commit();
148+
conn.close();
149+
150+
const end = new Date();
151+
console.log('Time elapsed: %sms for %s rows.', end - start, batch.length);
152+
} catch (err) {
153+
// TODO(developer) - Handle exception from the API
154+
console.log('Failed with an error %s', err.message);
155+
}
156+
}
128157
// [END apps_script_jdbc_write]
129158

130159
// [START apps_script_jdbc_read]
@@ -158,4 +187,36 @@ function readFromTable() {
158187
console.log('Failed with an error %s', err.message);
159188
}
160189
}
190+
191+
/**
192+
* Read up to 1000 rows of data from the table and log them.
193+
* Recommended for faster reads
194+
*/
195+
function readFromTableUsingGetRows() {
196+
try {
197+
const conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
198+
const start = new Date();
199+
const stmt = conn.createStatement();
200+
stmt.setMaxRows(1000);
201+
const results = stmt.executeQuery('SELECT * FROM entries');
202+
const numCols = results.getMetaData().getColumnCount();
203+
const getRowArgs = [];
204+
for (let col = 0; col < numCols; col++) {
205+
getRowArgs.push(`getString(${col + 1})`);
206+
}
207+
const rows = results.getRows(getRowArgs.join(','));
208+
for (let i = 0; i < rows.length; i++) {
209+
console.log(rows[i].join('\t'));
210+
}
211+
212+
results.close();
213+
stmt.close();
214+
215+
const end = new Date();
216+
console.log('Time elapsed: %sms', end - start);
217+
} catch (err) {
218+
// TODO(developer) - Handle exception from the API
219+
console.log('Failed with an error %s', err.message);
220+
}
221+
}
161222
// [END apps_script_jdbc_read]

service/test_jdbc.gs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ function itShouldWriteManyRecords() {
5555
writeManyRecords();
5656
}
5757

58+
/**
59+
* Tests writeManyRecordsUsingExecuteBatch function of jdbc.gs
60+
*/
61+
function itShouldWriteManyRecordsUsingExecuteBatch() {
62+
console.log('itShouldWriteManyRecordsUsingExecuteBatch');
63+
writeManyRecordsUsingExecuteBatch();
64+
}
65+
66+
5867
/**
5968
* Tests readFromTable function of jdbc.gs
6069
*/
@@ -63,6 +72,14 @@ function itShouldReadFromTable() {
6372
readFromTable();
6473
}
6574

75+
/**
76+
* Tests readFromTableUsingGetRows function of jdbc.gs
77+
*/
78+
function itShouldReadFromTableUsingGetRows() {
79+
console.log('itShouldReadFromTableUsingGetRows');
80+
readFromTableUsingGetRows();
81+
}
82+
6683
/**
6784
* Runs all the tests
6885
*/
@@ -73,4 +90,6 @@ function RUN_ALL_TESTS() {
7390
itShouldWriteOneRecord();
7491
itShouldWriteManyRecords();
7592
itShouldReadFromTable();
93+
itShouldReadFromTableUsingGetRows();
94+
itShouldWriteManyRecordsUsingExecuteBatch();
7695
}

0 commit comments

Comments
 (0)