LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  April 2015

HPS-SVN April 2015

Subject:

r2709 - /java/trunk/conditions/src/main/java/org/hps/conditions/database/QueryBuilder.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Wed, 15 Apr 2015 19:17:22 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (293 lines)

Author: [log in to unmask]
Date: Wed Apr 15 12:17:16 2015
New Revision: 2709

Log:
Fix prepared insert method.

Modified:
    java/trunk/conditions/src/main/java/org/hps/conditions/database/QueryBuilder.java

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/database/QueryBuilder.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/database/QueryBuilder.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/database/QueryBuilder.java	Wed Apr 15 12:17:16 2015
@@ -15,13 +15,112 @@
 public final class QueryBuilder {
 
     /**
-     * Dot not allow instantiation.
+     * Date formatting for insert statement.
      */
-    private QueryBuilder() {
+    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
+
+    /**
+     * Build an insert statement.
+     *
+     * @param tableName the table name
+     * @param fieldValues the field values
+     * @return the insert statement
+     */
+    public static String buildInsert(final String tableName, final FieldValueMap fieldValues) {
+        if (fieldValues.size() == 0) {
+            throw new IllegalArgumentException("The FieldValueMap has no values.");
+        }
+        final StringBuffer sb = new StringBuffer();
+        sb.append("INSERT INTO " + tableName + " (");
+        for (final String fieldName : fieldValues.keySet()) {
+            sb.append(" " + fieldName + ",");
+        }
+        sb.setLength(sb.length() - 1);
+        sb.append(" ) VALUES (");
+        for (final Object value : fieldValues.values()) {
+            final String insertValue = value.toString();
+            if (value instanceof Date) {
+                sb.append(" STR_TO_DATE( '" + DATE_FORMAT.format((Date) value) + "', '%Y-%m-%d %H:%i:%S' ),");
+            } else {
+                sb.append(" '" + insertValue + "',");
+            }
+        }
+        sb.setLength(sb.length() - 1);
+        sb.append(")");
+        return sb.toString();
+    }
+
+    /*
+     * static String buildUpdate(String tableName, int rowId, String[] fields, Object[] values) { if (fields.length !=
+     * values.length) throw new IllegalArgumentException("The field and value arrays are different lengths.");
+     * StringBuffer buff = new StringBuffer(); buff.append("UPDATE " + tableName + " SET "); for (int i = 0; i <
+     * fields.length; i++) { buff.append(fields[i] + " = '" + values[i] + "', "); } buff.delete(buff.length() - 2,
+     * buff.length() - 1); buff.append(" WHERE id = " + rowId); return buff.toString(); } public static String
+     * buildInsert(String tableName, int collectionId, String[] fields, Object[] values) { if (fields.length !=
+     * values.length) throw new IllegalArgumentException("The field and value arrays are different lengths.");
+     * StringBuffer buff = new StringBuffer(); buff.append("INSERT INTO " + tableName + "( collection_id"); for (String
+     * field : fields) { buff.append(", " + field); } buff.append(" ) VALUES ( " + collectionId); for (Object value :
+     * values) { buff.append(", " + value); } buff.append(") "); return buff.toString(); }
+     */
+
+    /**
+     * Build a SQL insert statement.
+     *
+     * @param tableName the table name
+     * @param collectionID the collection ID
+     * @param columnNames the column names
+     * @param rows the row data
+     * @return the SQL insert statement
+     */
+    public static String buildInsert(final String tableName, final int collectionID, final List<String> columnNames,
+            final List<List<String>> rows) {
+        final StringBuffer buff = new StringBuffer();
+        buff.append("INSERT INTO " + tableName + " ( collection_id");
+        for (final String column : columnNames) {
+            buff.append(", " + column);
+        }
+        buff.append(" ) VALUES ");
+        for (final List<String> values : rows) {
+            buff.append("( ");
+            buff.append(collectionID);
+            for (final String value : values) {
+                buff.append(", '" + value + "'");
+            }
+            buff.append("), ");
+        }
+        buff.setLength(buff.length() - 2);
+        return buff.toString();
+    }
+
+    /**
+     * Build a prepared insert statement for a conditions object.
+     *
+     * @param tableName the name of the table
+     * @param object the conditions object
+     * @return the prepared insert statement
+     */
+    static String buildPreparedInsert(final String tableName, final ConditionsObject object) {
+        if (object.getFieldValues().size() == 0) {
+            throw new IllegalArgumentException("The ConditionsObject has no values set.");
+        }
+        final StringBuffer buffer = new StringBuffer();
+        buffer.append("INSERT INTO " + tableName + "(");
+        for (final String fieldName : object.getFieldValues().keySet()) {
+            buffer.append(" " + fieldName + ",");
+        }
+        buffer.setLength(buffer.length() - 1);
+        buffer.append(") VALUES (");
+        for (int i = 0; i < object.getFieldValues().keySet().size(); i++) {
+            buffer.append(" ?,");
+        }
+        buffer.setLength(buffer.length() - 1);
+        buffer.append(")");
+        return buffer.toString();
     }
 
     /**
      * Build a SQL select query string.
+     *
      * @param tableName the name of the table
      * @param collectionId the collection ID
      * @param fields the list of fields
@@ -37,7 +136,7 @@
         } else {
             // Always implicitly include the row ID.
             buff.append("id, ");
-            for (String fieldName : fields) {
+            for (final String fieldName : fields) {
                 buff.append(fieldName + ", ");
             }
             buff.delete(buff.length() - 2, buff.length() - 1);
@@ -52,140 +151,25 @@
         return buff.toString();
     }
 
-    /*
-    static String buildUpdate(String tableName, int rowId, String[] fields, Object[] values) {
-        if (fields.length != values.length)
-            throw new IllegalArgumentException("The field and value arrays are different lengths.");
-        StringBuffer buff = new StringBuffer();
-        buff.append("UPDATE " + tableName + " SET ");
-        for (int i = 0; i < fields.length; i++) {
-            buff.append(fields[i] + " = '" + values[i] + "', ");
-        }
-        buff.delete(buff.length() - 2, buff.length() - 1);
-        buff.append(" WHERE id = " + rowId);
-        return buff.toString();
-    }
-
-    public static String buildInsert(String tableName, int collectionId, String[] fields, Object[] values) {
-        if (fields.length != values.length)
-            throw new IllegalArgumentException("The field and value arrays are different lengths.");
-        StringBuffer buff = new StringBuffer();
-        buff.append("INSERT INTO " + tableName + "( collection_id");
-        for (String field : fields) {
-            buff.append(", " + field);
-        }
-        buff.append(" ) VALUES ( " + collectionId);
-        for (Object value : values) {
-            buff.append(", " + value);
-        }
-        buff.append(") ");
-        return buff.toString();
-    }
-    */
-
-    /**
-     * Build a prepared insert statement for a conditions object.
-     * @param tableName the name of the table
-     * @param object the conditions object
-     * @return the prepared insert statement
-     */
-    static String buildPreparedInsert(final String tableName, final ConditionsObject object) {
-        if (object.getFieldValues().size() == 0) {
-            throw new IllegalArgumentException("The ConditionsObject has no values set.");
-        }
-        final StringBuffer buffer = new StringBuffer();
-        buffer.append("INSERT INTO " + tableName + "( collection_id, ");
-        for (String fieldName : object.getFieldValues().keySet()) {
-            buffer.append(" " + fieldName + ",");
-        }
-        buffer.setLength(buffer.length() - 1);
-        buffer.append(" ) VALUES ( ?,");
-        for (Object value : object.getFieldValues().values()) {
-            buffer.append(" ?,");
-        }
-        buffer.setLength(buffer.length() - 1);
-        buffer.append(")");
-        return buffer.toString();
-    }
-
-    /**
-     * Date formatting for insert statement.
-     */
-    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
-
-    /**
-     * Build an insert statement.
-     * @param tableName the table name
-     * @param fieldValues the field values
-     * @return the insert statement
-     */
-    public static String buildInsert(final String tableName, final FieldValueMap fieldValues) {
-        if (fieldValues.size() == 0) {
-            throw new IllegalArgumentException("The FieldValueMap has no values.");
-        }
-        final StringBuffer sb = new StringBuffer();
-        sb.append("INSERT INTO " + tableName + " (");
-        for (String fieldName : fieldValues.keySet()) {
-            sb.append(" " + fieldName + ",");
-        }
-        sb.setLength(sb.length() - 1);
-        sb.append(" ) VALUES (");
-        for (Object value : fieldValues.values()) {
-            final String insertValue = value.toString();
-            if (value instanceof Date) {
-                sb.append(" STR_TO_DATE( '" + DATE_FORMAT.format((Date) value) + "', '%Y-%m-%d %H:%i:%S' ),");
-            } else {
-                sb.append(" '" + insertValue + "',");
-            }
-        }
-        sb.setLength(sb.length() - 1);
-        sb.append(")");
-        return sb.toString();
-    }
-
-    /**
-     * Build a SQL insert statement.
-     * @param tableName the table name
-     * @param collectionID the collection ID
-     * @param columnNames the column names
-     * @param rows the row data
-     * @return the SQL insert statement
-     */
-    public static String buildInsert(final String tableName, final int collectionID,
-            final List<String> columnNames, final List<List<String>> rows) {
-        final StringBuffer buff = new StringBuffer();
-        buff.append("INSERT INTO " + tableName + " ( collection_id");
-        for (String column : columnNames) {
-            buff.append(", " + column);
-        }
-        buff.append(" ) VALUES ");
-        for (List<String> values : rows) {
-            buff.append("( ");
-            buff.append(collectionID);
-            for (String value : values) {
-                buff.append(", '" + value + "'");
-            }
-            buff.append("), ");
-        }
-        buff.setLength(buff.length() - 2);
-        return buff.toString();
-    }
-
-    /*
-    static String buildDelete(String tableName, int rowId) {
-        if (rowId <= 0)
-            throw new IllegalArgumentException("Invalid row ID: " + rowId);
-        String query = "DELETE FROM " + tableName + " WHERE id = " + rowId;
-        return query;
-    }
-    */
-
     /**
      * Format the date for insert statement.
+     *
      * @param date the input date
      * @return the formatted date string
      */
     static String formatDate(final Date date) {
         return DATE_FORMAT.format(date);
     }
+
+    /*
+     * static String buildDelete(String tableName, int rowId) { if (rowId <= 0) throw new
+     * IllegalArgumentException("Invalid row ID: " + rowId); String query = "DELETE FROM " + tableName + " WHERE id = "
+     * + rowId; return query; }
+     */
+
+    /**
+     * Dot not allow instantiation.
+     */
+    private QueryBuilder() {
+    }
 }

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use