diff --git a/service/jdbc.gs b/service/jdbc.gs index 28587741c..4f783b36b 100644 --- a/service/jdbc.gs +++ b/service/jdbc.gs @@ -125,6 +125,35 @@ function writeManyRecords() { console.log('Failed with an error %s', err.message); } } + +/** + * Write 500 rows of data to a table in a single batch. + * Recommended for faster writes + */ +function writeManyRecordsUsingExecuteBatch() { + try { + const conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd); + conn.setAutoCommit(false); + + const start = new Date(); + const stmt = conn.prepareStatement('INSERT INTO entries ' + + '(guestName, content) values (?, ?)'); + const params = []; + for (let i = 0; i < 500; i++) { + params.push(['Name ' + i, 'Hello, world ' + i]); + } + + const batch = stmt.executeBatch(params); + conn.commit(); + conn.close(); + + const end = new Date(); + console.log('Time elapsed: %sms for %s rows.', end - start, batch.length); + } catch (err) { + // TODO(developer) - Handle exception from the API + console.log('Failed with an error %s', err.message); + } +} // [END apps_script_jdbc_write] // [START apps_script_jdbc_read] @@ -158,4 +187,36 @@ function readFromTable() { console.log('Failed with an error %s', err.message); } } + +/** + * Read up to 1000 rows of data from the table and log them. + * Recommended for faster reads + */ +function readFromTableUsingGetRows() { + try { + const conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd); + const start = new Date(); + const stmt = conn.createStatement(); + stmt.setMaxRows(1000); + const results = stmt.executeQuery('SELECT * FROM entries'); + const numCols = results.getMetaData().getColumnCount(); + const getRowArgs = []; + for (let col = 0; col < numCols; col++) { + getRowArgs.push(`getString(${col + 1})`); + } + const rows = results.getRows(getRowArgs.join(',')); + for (let i = 0; i < rows.length; i++) { + console.log(rows[i].join('\t')); + } + + results.close(); + stmt.close(); + + const end = new Date(); + console.log('Time elapsed: %sms', end - start); + } catch (err) { + // TODO(developer) - Handle exception from the API + console.log('Failed with an error %s', err.message); + } +} // [END apps_script_jdbc_read] diff --git a/service/test_jdbc.gs b/service/test_jdbc.gs index e58e9f569..875fc30ab 100644 --- a/service/test_jdbc.gs +++ b/service/test_jdbc.gs @@ -55,6 +55,15 @@ function itShouldWriteManyRecords() { writeManyRecords(); } +/** + * Tests writeManyRecordsUsingExecuteBatch function of jdbc.gs + */ +function itShouldWriteManyRecordsUsingExecuteBatch() { + console.log('itShouldWriteManyRecordsUsingExecuteBatch'); + writeManyRecordsUsingExecuteBatch(); +} + + /** * Tests readFromTable function of jdbc.gs */ @@ -63,6 +72,14 @@ function itShouldReadFromTable() { readFromTable(); } +/** + * Tests readFromTableUsingGetRows function of jdbc.gs + */ +function itShouldReadFromTableUsingGetRows() { + console.log('itShouldReadFromTableUsingGetRows'); + readFromTableUsingGetRows(); +} + /** * Runs all the tests */ @@ -73,4 +90,6 @@ function RUN_ALL_TESTS() { itShouldWriteOneRecord(); itShouldWriteManyRecords(); itShouldReadFromTable(); + itShouldReadFromTableUsingGetRows(); + itShouldWriteManyRecordsUsingExecuteBatch(); }