Пример #1
0
  /**
   * ********************************************************************* Implementation of a
   * standard <code>toString()</code> method. This method returns a <code>String</code>
   * representation of the node as a human-readable SQL <code>INSERT</code>.
   *
   * <p>This method is responsible for constructing the SQL Query represented by the <code>
   * InsertNode</code> in a well-formed way so that it can be presented to a <code>Connection</code>
   * and executed properly.
   *
   * @return The <code>String</code> representation of the SQL <code>INSERT</code>.
   */
  public String toString() {
    String s = "";
    InsertNode insert = new InsertNode();
    InsertNode values = new InsertNode();

    for (InsertNode node : this) {
      if (node.getType().equalsIgnoreCase("insert")) {
        insert.addNode(node);
      } else if (node.getType().equalsIgnoreCase("values")) {
        values.addNode(node);
      } else {
        // We have a problem
      }
    }

    s = String.format("%s\n%s;", constructInsertClause(insert), constructValuesClause(values));

    return s;
  }
Пример #2
0
  public static void main(String[] args) {

    //		try {
    //			JSONObject json = new JSONObject("{ 'type' : 'INSERT', " +
    //											   "'table' : 'test_bobby.User', " +
    //									   		   "'insert' : [" +
    //										   			"{'attribute' : 'LastName', 'value' : 'Neelands'}," +
    //										   			"{'attribute' : 'FirstName', 'value' : 'Andrew'}," +
    //										   			"{'attribute' : 'UserType', 'value' : 'student'}," +
    //										   			"{'attribute' : 'CreateTime', 'value' : '2013-01-25 19:27:00'}" +
    //										   				  "]" +
    //										 "}");
    //			System.out.println(json.toString(3));
    //			InsertNode insert = new InsertNode(json);
    //
    //			System.out.println(insert.toString());
    //
    //			insert.execute();
    //		} catch (JSONException e) {
    //			// TODO Auto-generated catch block
    //			e.printStackTrace();
    //		}

    String j =
        "{'type':'INSERT','table':'User','insert':[{'attribute':'FirstName','value':'Test'},{'attribute':'LastName','value':'Tester'},{'attribute':'EmailAddress','value':'*****@*****.**'},{'attribute':'UserType','value':'student'},{'attribute':'CreateTime','value':'2013-3-3 9:5:26'}]}";
    JSONObject json = null;
    try {
      json = new JSONObject(j);
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    InsertNode insert = new InsertNode(json);

    System.out.println(insert.toString());

    insert.execute();

    //		insert.addNode("INSERT", "User (LastName, FirstName, EmailAddress, UserType, CreateTime)");
    //		insert.addNode("VALUES", "('Tester', 'Test', '*****@*****.**', 'student', '2013-02-11
    // 00:56:23')");

    //		Hashtable<String,String> ht = new Hashtable<String,String>();
    //		ht.put("LastName", "Tester");
    //		ht.put("FirstName", "Test");
    //		ht.put("EmailAddress", "*****@*****.**");
    //		ht.put("UserType", "student");
    //		ht.put("CreateTime", "2013-02-11 00:56:23");
    //		insert.addNode("INSERT", "User", ht);

    //		insert.addNode("INSERT", "User (LastName, FirstName, EmailAddress, UserType, CreateTime)");
    //		insert.addNode("VALUES", new String[]{"Tester", "Test", "*****@*****.**", "student",
    // "2013-02-11 00:56:23"});
    //
    //		System.out.print("{");
    //		for(int i = 0; i < insert.getNode(0).getCols().length; i++) {
    //			System.out.print(String.format("%s=%s", insert.getNode(0).getCols()[i],
    // insert.getNode(1).getValues()[i]));
    //			if(i < (insert.getNode(0).getCols().length-1)) {
    //				System.out.print(", ");
    //			}
    //		}
    //		System.out.println("}");
    //		System.out.println(insert.getPairs().toString());
    //
    //		Iterator<Map.Entry<String, String>> iter = insert.getPairs().entrySet().iterator();
    //		while(iter.hasNext()) {
    //			Map.Entry<String, String> entry = iter.next();
    //			System.out.printf("%s = %s\n", entry.getKey(), entry.getValue());
    //		}
    //
    //		System.out.println("TABLE = " + insert.table);
    //
    //		ResultNode result = insert.execute();
    //		System.out.println(result.getNode(0).getNodeWithAttr("UserID").getValue());

    //		Hashtable<String, String> ht = new Hashtable<String,String>();
    //		ht.put("FirstName", "Andrew");
    //		ht.put("LastName", "Neelands");
    //		ht.put("EmailAddress", "*****@*****.**");
    //		ht.put("UserType", "student");
    //
    //		InsertNode insert = new InsertNode();
    //		insert.addNode("INSERT", "test_bobby.User", ht);
    //		System.out.println(insert.toString());

    //		ResultNode result = insert.execute();
    //
    //		System.out.println(result);

    System.exit(1);
  }
Пример #3
0
 /**
  * ********************************************************************* Takes an <code>InsertNode
  * </code> full of only <code>values</code> type, and constructs a SQL <code>VALUES</code> clause
  * as a <code>String</code>.
  *
  * @param values - a <code>InsertNode</code> of nodes of only <code>values</code> type.
  * @return a <code>String</code> formatted as a SQL <code>VALUES</code> clause.
  */
 private static String constructValuesClause(InsertNode values) {
   values = values.getNode(0);
   String clause = "VALUES " + values.getExpression();
   return clause;
 }
Пример #4
0
 /**
  * ********************************************************************* Takes an <code>InsertNode
  * </code> full of only <code>insert</code> type, and constructs a SQL <code>INSERT</code> clause
  * as a <code>String</code>.
  *
  * @param insert - a <code>InsertNode</code> of nodes of only <code>insert</code> type.
  * @return a <code>String</code> formatted as a SQL <code>INSERT</code> clause.
  */
 private static String constructInsertClause(InsertNode insert) {
   Hashtable<String, String> props = ConnectionProperties.getProperties();
   insert = insert.getNode(0);
   String clause = "INSERT INTO " + props.get("dbname") + "." + insert.getExpression();
   return clause;
 }