/
populate_table.script
190 lines (162 loc) · 5.87 KB
/
populate_table.script
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
# This SLAMD script can be used to connect to a relational database over JDBC,
# create a table named 'testTable' and populate it with some number of rows.
# The table will have four columns: employeeID (a sequentially incrementing
# integer), firstName (always the string 'user'), lastName (the same as
# employeeID), and fullName (firstName {space} lastName).
#
# Many of the settings should be specified as script arguments. The script
# arguments that may be used are:
#
# dbdriver -- The fully-qualified name of the JDBC driver to use to connect
# to the database.
# dburl -- The JDBC URL to use to connect to the database.
# dbuser -- The username to use to connect to the database.
# dbpassword -- The password to use to connect to the database.
# numrows -- The number of rows to create in the database.
# Make the JDBC data types available for use.
use com.sun.slamd.scripting.jdbc.JDBCConnectionVariable;
use com.sun.slamd.scripting.jdbc.JDBCPreparedStatementVariable;
use com.sun.slamd.scripting.jdbc.JDBCResultSetVariable;
# Define the variables that we will use.
variable int counter;
variable int numRows;
variable JDBCConnection db;
variable JDBCPreparedStatement statement;
variable string dbDriver;
variable string dbURL;
variable string dbUser;
variable string dbPassword;
variable string errorMessage;
variable string failureReason;
variable string firstName;
variable string fullName;
variable string lastName;
variable string sql;
# Assign values to all the variables that can be set as arguments.
dbDriver = script.getScriptArgument("dbdriver");
dbURL = script.getScriptArgument("dburl");
dbUser = script.getScriptArgument("dbuser");
dbPassword = script.getScriptArgument("dbpassword");
numRows = script.getScriptIntArgument("numrows", 1000);
# Try to load the database driver.
if dbDriver.isNull()
begin
script.logMessage("No DB driver class specified.");
script.exitWithError();
end;
ifnot db.setDriverClass(dbDriver)
begin
errorMessage = "Unable to load DB driver ";
errorMessage = errorMessage.append(dbDriver);
errorMessage = errorMessage.append(": ");
errorMessage = errorMessage.append(db.getFailureReason());
script.logMessage(errorMessage);
script.exitWithError();
end;
else begin
script.debugMessage("Successfully loaded DB driver");
end;
# Try to connect to the database.
if dbURL.isNull()
begin
script.logMessage("No JDBC URL specified to use to connect to the DB");
script.exitWithError();
end;
ifnot db.connect(dbURL, dbUser, dbPassword)
begin
errorMessage = "Unable to connect to the DB ";
errorMessage = errorMessage.append(dbURL);
errorMessage = errorMessage.append(": ");
errorMessage = errorMessage.append(db.getFailureReason());
script.logMessage(errorMessage);
script.exitWithError();
end;
else begin
script.debugMessage("Successfully connected to the DB");
end;
# Try to create a new table in the database.
sql = "CREATE TABLE testTable ( ";
sql = sql.append("employeeID INT NOT NULL PRIMARY KEY, ");
sql = sql.append("firstName VARCHAR(30) NOT NULL, ");
sql = sql.append("lastName VARCHAR(30) NOT NULL, ");
sql = sql.append("fullName VARCHAR(60) NOT NULL );");
db.executeUpdate(sql);
failureReason = db.getFailureReason();
ifnot failureReason.isNull()
begin
errorMessage = "Unable to create a new table: ";
errorMessage = errorMessage.append(failureReason);
script.logMessage(errorMessage);
db.disconnect();
script.exitWithError();
end;
else begin
script.debugMessage("Successfully created the table testTable");
end;
# Try to create an index for the employeeID column of the table.
sql = "ALTER TABLE testTable ADD INDEX testIndex (employeeID);";
db.executeUpdate(sql);
failureReason = db.getFailureReason();
ifnot failureReason.isNull()
begin
errorMessage = "Unable to add an index for the table table: ";
errorMessage = errorMessage.append(failureReason);
script.logMessage(errorMessage);
db.disconnect();
script.exitWithError();
end;
else begin
script.debugMessage("Successfully created the index for testTable");
end;
# Create a prepared statement that we will use to actually create the rows.
sql = "INSERT INTO testTable VALUES (?, ?, ?, ?);";
statement = db.prepareStatement(sql);
if statement.isNull()
begin
errorMessage = "Unable to create prepared statement ";
errorMessage = errorMessage.append(sql);
errorMessage = errorMessage.append(": ");
errorMessage = errorMessage.append(failureReason);
script.logMessage(errorMessage);
db.disconnect();
script.exitWithError();
end;
else begin
script.debugMessage("Successfully created the prepared statement");
end;
# Create a loop that will try to add the appropriate number of users.
counter = 1;
while counter.lessThanOrEqualTo(numrows)
begin
firstName = "User";
lastName = counter.toString();
fullName = firstName;
fullName = firstName.append(" ");
fullName = fullName.append(lastName);
statement.setInteger(1, counter);
statement.setString(2, firstName);
statement.setString(3, lastName);
statement.setString(4, fullName);
statement.executeUpdate();
failureReason = statement.getFailureReason();
ifnot failureReason.isNull()
begin
errorMessage = "Unable to execute SQL update ";
errorMessage = errorMessage.append(sql);
errorMessage = errorMessage.append(": ");
errorMessage = errorMessage.append(failureReason);
script.logMessage(errorMessage);
db.disconnect();
script.exitWithError();
end;
else begin
errorMessage = "Added row ";
errorMessage = errorMessage.append(counter.toString());
script.debugMessage(errorMessage);
end;
counter.increment();
end;
# Disconnect from the database.
db.disconnect();
script.debugMessage("All processing complete.");
script.exit();