[34894] | 1 | import com.mysql.cj.exceptions.MysqlErrorNumbers;
|
---|
[34830] | 2 | import picocli.CommandLine;
|
---|
| 3 | import picocli.CommandLine.Command;
|
---|
| 4 | import picocli.CommandLine.Option;
|
---|
[34896] | 5 | import picocli.CommandLine.Parameters;
|
---|
[34830] | 6 |
|
---|
| 7 | import java.sql.*;
|
---|
| 8 | import java.util.HashMap;
|
---|
| 9 | import com.google.gson.Gson;
|
---|
| 10 | import java.io.FileWriter;
|
---|
[34897] | 11 | import java.util.concurrent.Callable;
|
---|
[34830] | 12 |
|
---|
| 13 | /*
|
---|
| 14 | TODO:
|
---|
| 15 |
|
---|
| 16 | */
|
---|
| 17 |
|
---|
[34900] | 18 | //-u su41Tipple -p --port 3306 --dbname koicarp_test
|
---|
[34880] | 19 | //@/home/su41/configFileTest
|
---|
[34830] | 20 |
|
---|
| 21 | /*
|
---|
| 22 | The mixinStandardHelpOptions = true adds in --help and --version options
|
---|
| 23 | */
|
---|
| 24 | @Command (name="sqlCli", description = "Arguments for database connection", mixinStandardHelpOptions = true)
|
---|
[34897] | 25 | public class sqlCli implements Callable<Integer> {
|
---|
[34900] | 26 |
|
---|
[34830] | 27 | //our cli arguments
|
---|
[34894] | 28 | @Option(names = {"-u", "--user"}, description = "The user name",
|
---|
| 29 | required = true)
|
---|
[34830] | 30 | String userName;
|
---|
| 31 |
|
---|
| 32 | //interactive allows the user to omit the argument value and instead enter it as a prompt
|
---|
| 33 | /*
|
---|
[34900] | 34 | NOTE: if you are running an interactive option on an IDE, most likely the IDE will be using
|
---|
| 35 | the javaw.exe library which tries to mimic the terminal.
|
---|
[34830] | 36 | The point is that interactive option would usually make whatever you enter hidden (it doesn't
|
---|
| 37 | echo back) but on the IDE it will echo back.
|
---|
| 38 | */
|
---|
[34894] | 39 | @Option(names = {"-p", "--password"}, description = "User's password", interactive = true,
|
---|
| 40 | required = true)
|
---|
[34830] | 41 | String password;
|
---|
| 42 |
|
---|
[34885] | 43 | @Option(names = {"--port"}, description="Port number", defaultValue="3306")
|
---|
[34896] | 44 | int port;
|
---|
[34830] | 45 |
|
---|
[34889] | 46 | @Option(names = {"--dbname"}, description="Name of the database", defaultValue="koicarp_test")
|
---|
[34830] | 47 | String dbName;
|
---|
| 48 |
|
---|
[34903] | 49 | //project id - it should be required right?
|
---|
| 50 | //if they omit, the query would come up empty.
|
---|
| 51 | @Option(names = {"--projectID"}, description = "ID number of the project")
|
---|
| 52 | String projectID;
|
---|
| 53 |
|
---|
[34897] | 54 | @Option(names = {"--host"}, description = "Type of host e.g. localhost ",
|
---|
| 55 | required = true, defaultValue = "localhost")
|
---|
| 56 | String host;
|
---|
[34830] | 57 |
|
---|
[34896] | 58 | /*
|
---|
[34897] | 59 | this parameter can be read in at any point on the options line.
|
---|
| 60 | but that's only because it's the only parameter at the moment.
|
---|
[34900] | 61 | If we add more parameters, will have to start indexing
|
---|
[34897] | 62 | https://picocli.info/quick-guide.html#_options_and_parameters
|
---|
| 63 | */
|
---|
[34896] | 64 | @Parameters(description = "Name of the output JSON file. E.g. yourFileName.json")
|
---|
| 65 | String jsonOutputFile;
|
---|
| 66 |
|
---|
[34897] | 67 | public Integer call(){
|
---|
[34896] | 68 | //if it doesn't end with ...
|
---|
| 69 | if(!jsonOutputFile.endsWith(".json")){
|
---|
| 70 | System.out.println("Invalid file format.");
|
---|
| 71 | System.out.println("The file to save the output to must be a .json file.");
|
---|
| 72 | //exit code other than 0 means it's not fine.
|
---|
[34897] | 73 | return 1;
|
---|
[34896] | 74 | }
|
---|
[34897] | 75 | //else we're good to go.
|
---|
| 76 | // databaseConnect(userName, password, port, dbName, projectID, jsonOutputFile, host);
|
---|
[34903] | 77 | int status = databaseConnect(userName, password, port, dbName, projectID, jsonOutputFile, host);
|
---|
[34897] | 78 | return status;
|
---|
[34830] | 79 | }
|
---|
| 80 |
|
---|
| 81 | public static void main(String... args) {
|
---|
| 82 | int exitCode = new CommandLine(new sqlCli()).execute(args);
|
---|
| 83 | System.exit(exitCode);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
[34897] | 87 | private static int databaseConnect(String user, String pass, int port, String dbName,
|
---|
[34903] | 88 | String projectID, String fileName, String host){
|
---|
[34830] | 89 | Connection conn;
|
---|
| 90 | Statement stmt;
|
---|
[34900] | 91 | //return status for code to send back to commandLine.execute
|
---|
[34897] | 92 | int returnStatus = 0;
|
---|
[34880] | 93 | // String dbUrl = "jdbc:mysql://localhost:" + port +"/"+ dbName;
|
---|
[34830] | 94 |
|
---|
[34880] | 95 | //TODO: are we changing / putting the type of database into a variable too?
|
---|
| 96 | String dbUrl = "jdbc:mysql://" + host + ":" + port +"/"+ dbName;
|
---|
| 97 |
|
---|
| 98 | //originally in the try ()
|
---|
| 99 | // FileWriter file = new FileWriter("tippleData.json")
|
---|
[34889] | 100 | System.out.println("Saving output to:");
|
---|
[34894] | 101 | System.out.println(" " + fileName);
|
---|
[34900] | 102 |
|
---|
[34894] | 103 | try (FileWriter file = new FileWriter(fileName)){
|
---|
[34830] | 104 | //register the jdbc driver
|
---|
| 105 | Class.forName("com.mysql.cj.jdbc.Driver");
|
---|
| 106 | //opening the connection
|
---|
[34885] | 107 | System.out.println("Connecting to database:");
|
---|
[34894] | 108 | System.out.println(" " + dbUrl);
|
---|
[34885] | 109 |
|
---|
[34830] | 110 | conn = DriverManager.getConnection(dbUrl, user, pass);
|
---|
| 111 |
|
---|
| 112 | //create and execute query
|
---|
| 113 | stmt = conn.createStatement();
|
---|
[34880] | 114 | //remember to put in the projectId parameter instead of hardcoding it
|
---|
[34894] | 115 |
|
---|
| 116 |
|
---|
[34905] | 117 | /* Step 1: retrieve all the content items associated with the given projectId */
|
---|
| 118 |
|
---|
[34903] | 119 | String sql = "SELECT name, id, typeId, created from td_ContentItem where projectId="+projectID;
|
---|
[34905] | 120 |
|
---|
| 121 | // consider usingLIMIT to get the returned results in controled batch size, rather than getting
|
---|
| 122 | // all the results at once.
|
---|
| 123 |
|
---|
[34830] | 124 | ResultSet myResult = stmt.executeQuery(sql);
|
---|
| 125 | ResultSetMetaData resultMetaData = myResult.getMetaData();
|
---|
| 126 |
|
---|
| 127 | int dbColumn = resultMetaData.getColumnCount();
|
---|
[34899] | 128 | System.out.println(" Number of columns in table retrieved: " + dbColumn);
|
---|
[34830] | 129 |
|
---|
[34905] | 130 |
|
---|
| 131 | /* Step 2: Iterate through each content item, and retrieve the media files that content item has */
|
---|
| 132 |
|
---|
| 133 | /* The following is an example SELECT statement (for a hard-wired contentId = 333) that retrieves all the
|
---|
| 134 | images (both the original name, and the hashed 'path' that maps to the media files on the file
|
---|
| 135 | system (the contentItem itself at the time of writing was in projectID=4) */
|
---|
| 136 |
|
---|
| 137 | /*
|
---|
| 138 | SELECT mFile.filename, mFile.path
|
---|
| 139 |
|
---|
| 140 | FROM td_ManagedFile AS mFile, td_ContentItem AS cItem, td_ContentItem_MediaItem AS cMediaItem, td_MediaItem AS mItem
|
---|
| 141 |
|
---|
| 142 | WHERE
|
---|
| 143 |
|
---|
| 144 | cItem.id = 333
|
---|
| 145 |
|
---|
| 146 | And cMediaItem.contentItemId = cItem.id
|
---|
| 147 |
|
---|
| 148 | and cMediaItem.mediaItemId = mItem.id
|
---|
| 149 |
|
---|
| 150 | And mItem.fileId = mFile.id
|
---|
| 151 | */
|
---|
| 152 |
|
---|
| 153 |
|
---|
| 154 |
|
---|
[34894] | 155 | //create gson object and have the file write the opening bracket for valid json
|
---|
[34830] | 156 | Gson myGSon = new Gson();
|
---|
[34894] | 157 | file.write("[");
|
---|
[34830] | 158 |
|
---|
| 159 | while(myResult.next()){
|
---|
| 160 | //string/object as key/value pair
|
---|
| 161 | HashMap<String, Object> dbRow = new HashMap<String, Object>(dbColumn);
|
---|
| 162 |
|
---|
| 163 | for(int i = 1; i <= dbColumn; i++){
|
---|
| 164 | /*
|
---|
[34894] | 165 | doing checking this way could mean we have to have a case
|
---|
[34830] | 166 | for every data type,
|
---|
| 167 | if a data type changes in the database that's probably fine, but
|
---|
[34896] | 168 | if a new data type is sent in, we'll have update the statements
|
---|
[34830] | 169 | */
|
---|
| 170 | /*
|
---|
| 171 | Without this kind of check though, the output would throw a pretty nasty
|
---|
| 172 | warning about illegal reflective access. Though it would still print the
|
---|
[34894] | 173 | date out fine. The warning:
|
---|
[34830] | 174 |
|
---|
| 175 | WARNING: An illegal reflective access operation has occurred
|
---|
[34894] | 176 | WARNING: Illegal reflective access by com.google.gson.internal.bind.ReflectiveTypeAdapterFactory (file:/home/su41/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.7/751f548c85fa49f330cecbb1875893f971b33c4e/gson-2.7.jar) to field java.time.LocalDateTime.date
|
---|
| 177 | WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.bind.ReflectiveTypeAdapterFactory
|
---|
| 178 | WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
|
---|
| 179 | WARNING: All illegal access operations will be denied in a future release
|
---|
[34830] | 180 | */
|
---|
| 181 | if(resultMetaData.getColumnTypeName(i).equals("DATETIME")){
|
---|
| 182 | dbRow.put(resultMetaData.getColumnName(i), myResult.getDate(i));
|
---|
| 183 | }
|
---|
| 184 | else{
|
---|
| 185 | dbRow.put(resultMetaData.getColumnName(i), myResult.getObject(i));
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
[34905] | 188 | String dbRowAsJson = myGSon.toJson(dbRow);
|
---|
| 189 |
|
---|
| 190 |
|
---|
[34830] | 191 | //saving to json file
|
---|
[34880] | 192 | file.write(dbRowAsJson);
|
---|
[34894] | 193 |
|
---|
[34880] | 194 | //Adding commas between each row that we write to our file, as we need it to be valid json
|
---|
[34830] | 195 | if(!myResult.isLast()){
|
---|
| 196 | file.write(",");
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
[34880] | 199 | //write the closing bracket, and flush the writer
|
---|
[34830] | 200 | file.write("]");
|
---|
| 201 | file.flush();
|
---|
[34880] | 202 | file.close();
|
---|
[34830] | 203 | //System.out.println(myList);
|
---|
[34880] | 204 | System.out.println("Closing connection...");
|
---|
[34830] | 205 | myResult.close();
|
---|
| 206 | stmt.close();
|
---|
| 207 | conn.close();
|
---|
[34894] | 208 | System.out.println("Connection closed");
|
---|
[34830] | 209 | }
|
---|
[34894] | 210 | catch(SQLException sqlError){
|
---|
[34900] | 211 | // looking at the sql error code is good for debugging.
|
---|
| 212 | // can ref: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-error-sqlstates.html
|
---|
| 213 | // to see what they're for
|
---|
| 214 |
|
---|
[34896] | 215 | // System.out.println("MySQL error code: " + sqlError.getErrorCode());
|
---|
[34900] | 216 |
|
---|
| 217 |
|
---|
[34896] | 218 | //prints out the error message from sql, then check to see which error code
|
---|
| 219 | // came back and what the code matches.
|
---|
| 220 | // if it's access denied, usually something to do with username, password or db name
|
---|
[34894] | 221 | System.out.println(sqlError.getMessage());
|
---|
[34896] | 222 |
|
---|
[34894] | 223 | if(sqlError.getErrorCode() == MysqlErrorNumbers.ER_ACCESS_DENIED_ERROR){
|
---|
| 224 | System.out.println("Error with database credentials. Double check your: ");
|
---|
| 225 | System.out.println(" -'Username'");
|
---|
| 226 | System.out.println(" -'Password' ");
|
---|
| 227 | }
|
---|
[34900] | 228 |
|
---|
| 229 | if(sqlError.getErrorCode() == MysqlErrorNumbers.ER_DBACCESS_DENIED_ERROR){
|
---|
| 230 | System.out.println("Error with database credentials. Double check your: ");
|
---|
| 231 | System.out.println(" -'Database name' ");
|
---|
| 232 | }
|
---|
[34897] | 233 | returnStatus = 1;
|
---|
[34894] | 234 |
|
---|
| 235 | }
|
---|
[34830] | 236 | catch(Exception e){
|
---|
| 237 | //catch for class.name
|
---|
| 238 | e.printStackTrace();
|
---|
[34880] | 239 | //print out wrong username or password instead of the stack trace?
|
---|
| 240 | System.out.println("encountered error");
|
---|
[34897] | 241 | returnStatus = 2;
|
---|
[34830] | 242 | }
|
---|
[34897] | 243 | return returnStatus;
|
---|
[34830] | 244 | }
|
---|
| 245 | }
|
---|