1 package wrappers; 2 3 import java.io.BufferedReader; 4 import java.io.Console; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.util.Set; 8 9 public class Client3 { 10 static boolean DEBUG = true; 11 static final Console c = System.console(); 12 static Mongo3Interface jdi = null; 13 main(String[] args)14 public static void main(String[] args) { 15 int rc, level = 0; 16 boolean brc, desc = false; 17 Set<String> columns; 18 String[] parms = new String[4]; 19 20 jdi = new Mongo3Interface(DEBUG); 21 22 parms[0] = getLine("URI: ", false); 23 parms[1] = getLine("Database: ", false); 24 parms[2] = null; 25 parms[3] = null; 26 27 if (parms[0] == null) 28 parms[0] = "mongodb://localhost:27017"; 29 30 if (parms[1] == null) 31 parms[1] = "test"; 32 33 rc = jdi.MongoConnect(parms); 34 35 if (rc == 0) { 36 String name, pipeline, query, fields; 37 System.out.println("Successfully connected to " + parms[0]); 38 39 while ((name = getLine("Collection: ", false)) != null) { 40 if (jdi.GetCollection(name)) 41 System.out.println("GetCollection failed"); 42 else 43 System.out.println("Collection size: " + jdi.GetCollSize()); 44 45 pipeline = getLine("Pipeline: ", false); 46 47 if (pipeline == null || (desc = pipeline.equals("*"))) { 48 query = getLine("Filter: ", false); 49 fields = getLine("Proj: ", false); 50 51 if (desc) 52 level = Integer.parseInt(getLine("Level: ", false)); 53 54 brc = jdi.FindColl(query, fields); 55 } else 56 brc = jdi.AggregateColl(pipeline); 57 58 System.out.println("Returned brc = " + brc); 59 60 if (!brc && !desc) { 61 for (int i = 0; jdi.ReadNext() > 0 && i < 10; i++) { 62 columns = jdi.GetColumns(); 63 64 for (String col : columns) 65 System.out.println(col + "=" + jdi.GetField(col)); 66 67 if (name.equalsIgnoreCase("gtst")) 68 System.out.println("gtst=" + jdi.GetField("*")); 69 70 if (name.equalsIgnoreCase("inventory")) { 71 System.out.println("warehouse=" + jdi.GetField("instock.0.warehouse")); 72 System.out.println("quantity=" + jdi.GetField("instock.1.qty")); 73 } // endif inventory 74 75 if (name.equalsIgnoreCase("restaurants")) { 76 System.out.println("score=" + jdi.GetField("grades.0.score")); 77 System.out.println("date=" + jdi.GetField("grades.0.date")); 78 } // endif inventory 79 80 } // endfor i 81 82 } else if (desc) { 83 int ncol; 84 85 for (int i = 0; (ncol = jdi.ReadNext()) > 0 && i < 2; i++) { 86 if (discovery(null, "", ncol, level)) 87 break; 88 89 System.out.println("--------------"); 90 } // endfor i 91 92 } // endif desc 93 94 } // endwhile query 95 96 rc = jdi.MongoDisconnect(); 97 System.out.println("Disconnect returned " + rc); 98 } else 99 System.out.println(jdi.GetErrmsg() + " rc=" + rc); 100 101 } // end of main 102 discovery(Object obj, String name, int ncol, int level)103 private static boolean discovery(Object obj, String name, int ncol, int level) { 104 int[] val = new int[5]; 105 Object ret = null; 106 String bvn = null; 107 108 for (int k = 0; k < ncol; k++) { 109 ret = jdi.ColumnDesc(obj, k, val, level); 110 bvn = jdi.ColDescName(); 111 112 if (ret != null) 113 discovery(ret, name.concat(bvn).concat("."), val[4], level - 1); 114 else if (val[0] > 0) 115 System.out.println( 116 name + bvn + ": type=" + val[0] + " length=" + val[1] + " prec=" + val[2] + " nullable=" + val[3]); 117 else if (val[0] < 0) 118 System.out.println(jdi.GetErrmsg()); 119 120 } // endfor k 121 122 return false; 123 } // end of discovery 124 125 // ================================================================== getLine(String p, boolean b)126 private static String getLine(String p, boolean b) { 127 String response; 128 129 if (c != null) { 130 // Standard console mode 131 if (b) { 132 response = new String(c.readPassword(p)); 133 } else 134 response = c.readLine(p); 135 136 } else { 137 // For instance when testing from Eclipse 138 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 139 140 System.out.print(p); 141 142 try { 143 // Cannot suppress echo for password entry 144 response = in.readLine(); 145 } catch (IOException e) { 146 response = ""; 147 } // end of try/catch 148 149 } // endif c 150 151 return (response.isEmpty()) ? null : response; 152 } // end of getLine 153 154 } // end of class Client 155