Changeset 34880 for gs2-extensions


Ignore:
Timestamp:
2021-02-16T13:36:22+13:00 (3 years ago)
Author:
davidb
Message:

Early working version that generates json ouput file. Committing at this point to test on a fresh checkout

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/tipple-bridge/trunk/src/src/main/java/sqlCli.java

    r34830 r34880  
    33import picocli.CommandLine.Command;
    44import picocli.CommandLine.Option;
    5 import picocli.CommandLine.Parameters;
    65
    7 import java.io.File;
    86import java.sql.*;
    97import java.util.HashMap;
    108import com.google.gson.Gson;
    11 import com.google.gson.JsonObject;
    129import java.io.FileWriter;
    1310
     
    1512TODO:
    1613for the CLI:
    17 - default values
    18 - read in config files
    19 -
     14- Error handling
     15- Subcommands?
    2016
    2117 */
    2218
     19//-u su41Tipple -p --port 3306 --dbname tippleTest1
     20//@/home/su41/configFileTest
    2321
    2422/*
     
    2826public class sqlCli implements Runnable {
    2927    //our cli arguments
    30     // -- need to set the default value, which if we set that then the required = true fields can be removed
     28
    3129    @Option(names = {"-u", "--user"}, description = "The user name")
    3230    String userName;
     
    5149    String projectID;
    5250
    53     @Option(names = {"--fileName"}, description = "Name of the db", required = true)
     51    //defaultValue in case the user doesn't enter in an option for it
     52    @Option(names = {"--fileName"}, description = "Name of the db",
     53            required = true, defaultValue = "myData.json")
    5454    String fileName;
    5555
    56 //-u su41Tipple -p --port 3306 --dbname tippleTest1
     56    @Option(names = {"--host"}, description = "Type of host e.g. localhost ",
     57            required = true, defaultValue = "localhost")
     58    String host;
     59
    5760
    5861    public void run() {
    5962        System.out.println("Hello, " + userName);
    60         databaseConnect(userName, password, portNo, dbName);
     63        databaseConnect(userName, password, portNo, dbName, projectID, fileName, host);
    6164    }
    6265
     
    6669    }
    6770
    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
    7271
    73  */
    74 
    75 
    76     private static void databaseConnect(String user, String pass, int port, String dbName){
     72    private static void databaseConnect(String user, String pass, int port, String dbName,
     73                                        String projectID, String filename, String host){
    7774        Connection conn;
    7875        Statement stmt;
    7976
    80         String dbUrl = "jdbc:mysql://localhost:" + port +"/"+ dbName;
     77       // String dbUrl = "jdbc:mysql://localhost:" + port +"/"+ dbName;
    8178
    82         //rewrite
    83         try (FileWriter file = new FileWriter("tippleData.json")){
     79        //TODO: are we changing / putting the type of database into a variable too?
     80        String dbUrl = "jdbc:mysql://" + host + ":" + port +"/"+ dbName;
     81
     82        //originally in the try ()
     83        // FileWriter file = new FileWriter("tippleData.json")
     84        try (FileWriter file = new FileWriter(filename)){
    8485            //register the jdbc driver
    8586            Class.forName("com.mysql.cj.jdbc.Driver");
     
    9192            //create and execute query
    9293            stmt = conn.createStatement();
    93             String sql = "SELECT name, id, typeId, created from td_ContentItem where projectId=3";
     94            //remember to put in the projectId parameter instead of hardcoding it
     95            String sql = "SELECT name, id, typeId, created from td_ContentItem where projectId="+projectID;
    9496/*
    9597            String sql = "select td_Document.id, td_Document.content " +
    9698                    "from td_Document, td_ContentItem, td_ContentItem_Document " +
    97                     "where td_ContentItem.projectId=3 and td_ContentItem.id = td_ContentItem_Document.ContentItemId " +
     99                    "where td_ContentItem.projectId=" + projectID + " and td_ContentItem.id =
     100                    td_ContentItem_Document.ContentItemId " +
    98101                    "and td_ContentItem_Document.documentId = td_Document.id;";
    99102*/
     
    104107            System.out.println("columns " + dbColumn);
    105108
    106             //List<HashMap<String, Object>> myList = new ArrayList<>();
    107109            Gson myGSon = new Gson();
    108             file.write("[");
     110           file.write("[");
     111
     112            //does nothing, assuming the try catch picks up the error first,
     113            if(!myResult.next()){
     114                System.out.println("Access denied");
     115            }
    109116
    110117            while(myResult.next()){
    111 
    112118                //string/object as key/value pair
    113119                HashMap<String, Object> dbRow = new HashMap<String, Object>(dbColumn);
     
    134140WARNING: All illegal access operations will be denied in a future release
    135141                     */
    136 
    137 
    138 
    139142                    if(resultMetaData.getColumnTypeName(i).equals("DATETIME")){
    140143                        dbRow.put(resultMetaData.getColumnName(i), myResult.getDate(i));
     
    147150                       // System.out.println("no datetime datetime");
    148151                    }
    149 
    150 
    151152                }
    152                 //myList.add(dbRow);
    153153                //JsonObject jsonObject = myGSon.toJsonTree(dbRow).getAsJsonObject();
    154154
    155155                // using a hashmap wont work with this as its not an array
    156156               // JsonArray jsonArray = myGSon.toJsonTree(dbRow).getAsJsonArray();
    157                 String anObject = myGSon.toJson(dbRow);
     157               String dbRowAsJson = myGSon.toJson(dbRow);
    158158
    159                     //printing stuff out
     159                    //printing statements for checking output
    160160               // System.out.println("As normal: " + dbRow);
    161161               // System.out.println("====");
    162162                //System.out.println("As JSON: " + anObject);
    163163
    164 
    165164                //saving to json file
    166165
    167                 file.write(anObject);
     166                file.write(dbRowAsJson);
    168167                //file.write(jsonArray.toString());
     168                //Adding commas between each row that we write to our file, as we need it to be valid json
    169169                if(!myResult.isLast()){
    170170                    file.write(",");
    171171                }
    172172            }
     173            //write the closing bracket, and flush the writer
    173174            file.write("]");
    174175            file.flush();
     176            file.close();
    175177            //System.out.println(myList);
     178            System.out.println("Closing connection...");
    176179            myResult.close();
    177180            stmt.close();
     
    181184            //catch for class.name
    182185            e.printStackTrace();
     186            //print out wrong username or password instead of the stack trace?
     187            System.out.println("encountered error");
    183188        }
    184189    }
Note: See TracChangeset for help on using the changeset viewer.