forked from googleworkspace/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdbc.gs
136 lines (115 loc) · 3.86 KB
/
jdbc.gs
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
// [START apps_script_jdbc_create]
// Replace the variables in this block with real values.
// You can find the "Instance connection name" in the Google Cloud
// Platform Console, on the instance Overview page.
var connectionName = 'Instance_connection_name';
var rootPwd = 'root_password';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';
var root = 'root';
var instanceUrl = 'jdbc:google:mysql://' + connectionName;
var dbUrl = instanceUrl + '/' + db;
/**
* Create a new database within a Cloud SQL instance.
*/
function createDatabase() {
var conn = Jdbc.getCloudSqlConnection(instanceUrl, root, rootPwd);
conn.createStatement().execute('CREATE DATABASE ' + db);
}
/**
* Create a new user for your database with full privileges.
*/
function createUser() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, root, rootPwd);
var stmt = conn.prepareStatement('CREATE USER ? IDENTIFIED BY ?');
stmt.setString(1, user);
stmt.setString(2, userPwd);
stmt.execute();
conn.createStatement().execute('GRANT ALL ON `%`.* TO ' + user);
}
/**
* Create a new table in the database.
*/
function createTable() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
conn.createStatement().execute('CREATE TABLE entries ' +
'(guestName VARCHAR(255), content VARCHAR(255), ' +
'entryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(entryID));');
}
// [END apps_script_jdbc_create]
// [START apps_script_jdbc_write]
// Replace the variables in this block with real values.
// You can find the "Instance connection name" in the Google Cloud
// Platform Console, on the instance Overview page.
var connectionName = 'Instance_connection_name';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';
var dbUrl = 'jdbc:google:mysql://' + connectionName + '/' + db;
/**
* Write one row of data to a table.
*/
function writeOneRecord() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
var stmt = conn.prepareStatement('INSERT INTO entries ' +
'(guestName, content) values (?, ?)');
stmt.setString(1, 'First Guest');
stmt.setString(2, 'Hello, world');
stmt.execute();
}
/**
* Write 500 rows of data to a table in a single batch.
*/
function writeManyRecords() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
conn.setAutoCommit(false);
var start = new Date();
var stmt = conn.prepareStatement('INSERT INTO entries ' +
'(guestName, content) values (?, ?)');
for (var i = 0; i < 500; i++) {
stmt.setString(1, 'Name ' + i);
stmt.setString(2, 'Hello, world ' + i);
stmt.addBatch();
}
var batch = stmt.executeBatch();
conn.commit();
conn.close();
var end = new Date();
Logger.log('Time elapsed: %sms for %s rows.', end - start, batch.length);
}
// [END apps_script_jdbc_write]
// [START apps_script_jdbc_read]
/**
* Replace the variables in this block with real values.
* You can find the "Instance connection name" in the Google Cloud
* Platform Console, on the instance Overview page.
*/
var connectionName = 'Instance_connection_name';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';
var dbUrl = 'jdbc:google:mysql://' + connectionName + '/' + db;
/**
* Read up to 1000 rows of data from the table and log them.
*/
function readFromTable() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
var start = new Date();
var stmt = conn.createStatement();
stmt.setMaxRows(1000);
var results = stmt.executeQuery('SELECT * FROM entries');
var numCols = results.getMetaData().getColumnCount();
while (results.next()) {
var rowString = '';
for (var col = 0; col < numCols; col++) {
rowString += results.getString(col + 1) + '\t';
}
Logger.log(rowString);
}
results.close();
stmt.close();
var end = new Date();
Logger.log('Time elapsed: %sms', end - start);
}
// [END apps_script_jdbc_read]