1 | import com.google.gson.JsonArray;
|
---|
2 | import picocli.CommandLine;
|
---|
3 | import picocli.CommandLine.Command;
|
---|
4 | import picocli.CommandLine.Option;
|
---|
5 | import picocli.CommandLine.Parameters;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.sql.*;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import com.google.gson.Gson;
|
---|
11 | import com.google.gson.JsonObject;
|
---|
12 | import java.io.FileWriter;
|
---|
13 |
|
---|
14 | /*
|
---|
15 | TODO:
|
---|
16 | for the CLI:
|
---|
17 | - default values
|
---|
18 | - read in config files
|
---|
19 | -
|
---|
20 |
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | /*
|
---|
25 | The mixinStandardHelpOptions = true adds in --help and --version options
|
---|
26 | */
|
---|
27 | @Command (name="sqlCli", description = "Arguments for database connection", mixinStandardHelpOptions = true)
|
---|
28 | public class sqlCli implements Runnable {
|
---|
29 | //our cli arguments
|
---|
30 | // -- need to set the default value, which if we set that then the required = true fields can be removed
|
---|
31 | @Option(names = {"-u", "--user"}, description = "The user name")
|
---|
32 | String userName;
|
---|
33 |
|
---|
34 | //interactive allows the user to omit the argument value and instead enter it as a prompt
|
---|
35 | /*
|
---|
36 | NOTE: if you are running an interactive option on an IDE, most likely will be using something
|
---|
37 | like the javaw.exe library which tries to mimic the terminal.
|
---|
38 | The point is that interactive option would usually make whatever you enter hidden (it doesn't
|
---|
39 | echo back) but on the IDE it will echo back.
|
---|
40 | */
|
---|
41 | @Option(names = {"-p", "--password"}, description = "User's password", interactive = true)
|
---|
42 | String password;
|
---|
43 |
|
---|
44 | @Option(names = {"--port"}, description = "Port number")
|
---|
45 | int portNo;
|
---|
46 |
|
---|
47 | @Option(names = {"--dbname"}, description = "Name of the db")
|
---|
48 | String dbName;
|
---|
49 |
|
---|
50 | @Option(names = {"--projectID"}, description = "ID number of the project")
|
---|
51 | String projectID;
|
---|
52 |
|
---|
53 | @Option(names = {"--fileName"}, description = "Name of the db", required = true)
|
---|
54 | String fileName;
|
---|
55 |
|
---|
56 | //-u su41Tipple -p --port 3306 --dbname tippleTest1
|
---|
57 |
|
---|
58 | public void run() {
|
---|
59 | System.out.println("Hello, " + userName);
|
---|
60 | databaseConnect(userName, password, portNo, dbName);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static void main(String... args) {
|
---|
64 | int exitCode = new CommandLine(new sqlCli()).execute(args);
|
---|
65 | System.exit(exitCode);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /*
|
---|
69 | Haven't commited to svn yet because
|
---|
70 | 1. I needed to refactor the code so that the password wasn't hardcoded in
|
---|
71 | 2. I want to get a .svnignore file going but literally cannot figure out how
|
---|
72 |
|
---|
73 | */
|
---|
74 |
|
---|
75 |
|
---|
76 | private static void databaseConnect(String user, String pass, int port, String dbName){
|
---|
77 | Connection conn;
|
---|
78 | Statement stmt;
|
---|
79 |
|
---|
80 | String dbUrl = "jdbc:mysql://localhost:" + port +"/"+ dbName;
|
---|
81 |
|
---|
82 | //rewrite
|
---|
83 | try (FileWriter file = new FileWriter("tippleData.json")){
|
---|
84 | //register the jdbc driver
|
---|
85 | Class.forName("com.mysql.cj.jdbc.Driver");
|
---|
86 | //opening the connection
|
---|
87 | System.out.println("Connecting to database: ");
|
---|
88 | conn = DriverManager.getConnection(dbUrl, user, pass);
|
---|
89 | //conn = DriverManager.getConnection(dbUrl, user, pass);
|
---|
90 |
|
---|
91 | //create and execute query
|
---|
92 | stmt = conn.createStatement();
|
---|
93 | String sql = "SELECT name, id, typeId, created from td_ContentItem where projectId=3";
|
---|
94 | /*
|
---|
95 | String sql = "select td_Document.id, td_Document.content " +
|
---|
96 | "from td_Document, td_ContentItem, td_ContentItem_Document " +
|
---|
97 | "where td_ContentItem.projectId=3 and td_ContentItem.id = td_ContentItem_Document.ContentItemId " +
|
---|
98 | "and td_ContentItem_Document.documentId = td_Document.id;";
|
---|
99 | */
|
---|
100 | ResultSet myResult = stmt.executeQuery(sql);
|
---|
101 | ResultSetMetaData resultMetaData = myResult.getMetaData();
|
---|
102 |
|
---|
103 | int dbColumn = resultMetaData.getColumnCount();
|
---|
104 | System.out.println("columns " + dbColumn);
|
---|
105 |
|
---|
106 | //List<HashMap<String, Object>> myList = new ArrayList<>();
|
---|
107 | Gson myGSon = new Gson();
|
---|
108 | file.write("[");
|
---|
109 |
|
---|
110 | while(myResult.next()){
|
---|
111 |
|
---|
112 | //string/object as key/value pair
|
---|
113 | HashMap<String, Object> dbRow = new HashMap<String, Object>(dbColumn);
|
---|
114 |
|
---|
115 | //maybe use some kind of arrayList?
|
---|
116 | // ArrayList<Map<String, Object>> dbRowArray = new ArrayList<>(dbColumn);
|
---|
117 |
|
---|
118 | for(int i = 1; i <= dbColumn; i++){
|
---|
119 | /*
|
---|
120 | doing checking this way would mean we have to have a case
|
---|
121 | for every data type,
|
---|
122 | if a data type changes in the database that's probably fine, but
|
---|
123 | if a new data type is sent in, the code may not handle it
|
---|
124 | */
|
---|
125 | /*
|
---|
126 | Without this kind of check though, the output would throw a pretty nasty
|
---|
127 | warning about illegal reflective access. Though it would still print the
|
---|
128 | date out fine. Think it switched to using some other java option?
|
---|
129 |
|
---|
130 | WARNING: An illegal reflective access operation has occurred
|
---|
131 | 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
|
---|
132 | WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.bind.ReflectiveTypeAdapterFactory
|
---|
133 | WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
|
---|
134 | WARNING: All illegal access operations will be denied in a future release
|
---|
135 | */
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 | if(resultMetaData.getColumnTypeName(i).equals("DATETIME")){
|
---|
140 | dbRow.put(resultMetaData.getColumnName(i), myResult.getDate(i));
|
---|
141 | //System.out.println("has a datetime");
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | else{
|
---|
146 | dbRow.put(resultMetaData.getColumnName(i), myResult.getObject(i));
|
---|
147 | // System.out.println("no datetime datetime");
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | }
|
---|
152 | //myList.add(dbRow);
|
---|
153 | //JsonObject jsonObject = myGSon.toJsonTree(dbRow).getAsJsonObject();
|
---|
154 |
|
---|
155 | // using a hashmap wont work with this as its not an array
|
---|
156 | // JsonArray jsonArray = myGSon.toJsonTree(dbRow).getAsJsonArray();
|
---|
157 | String anObject = myGSon.toJson(dbRow);
|
---|
158 |
|
---|
159 | //printing stuff out
|
---|
160 | // System.out.println("As normal: " + dbRow);
|
---|
161 | // System.out.println("====");
|
---|
162 | //System.out.println("As JSON: " + anObject);
|
---|
163 |
|
---|
164 |
|
---|
165 | //saving to json file
|
---|
166 |
|
---|
167 | file.write(anObject);
|
---|
168 | //file.write(jsonArray.toString());
|
---|
169 | if(!myResult.isLast()){
|
---|
170 | file.write(",");
|
---|
171 | }
|
---|
172 | }
|
---|
173 | file.write("]");
|
---|
174 | file.flush();
|
---|
175 | //System.out.println(myList);
|
---|
176 | myResult.close();
|
---|
177 | stmt.close();
|
---|
178 | conn.close();
|
---|
179 | }
|
---|
180 | catch(Exception e){
|
---|
181 | //catch for class.name
|
---|
182 | e.printStackTrace();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|