Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions service/jdbc.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
19 changes: 19 additions & 0 deletions service/test_jdbc.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -63,6 +72,14 @@ function itShouldReadFromTable() {
readFromTable();
}

/**
* Tests readFromTableUsingGetRows function of jdbc.gs
*/
function itShouldReadFromTableUsingGetRows() {
console.log('itShouldReadFromTableUsingGetRows');
readFromTableUsingGetRows();
}

/**
* Runs all the tests
*/
Expand All @@ -73,4 +90,6 @@ function RUN_ALL_TESTS() {
itShouldWriteOneRecord();
itShouldWriteManyRecords();
itShouldReadFromTable();
itShouldReadFromTableUsingGetRows();
itShouldWriteManyRecordsUsingExecuteBatch();
}
Loading