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  November 2014

HPS-SVN November 2014

Subject:

r1508 - in /java/trunk/conditions/src/main/java/org/hps/conditions: ConditionsRecord.java QueryBuilder.java

From:

[log in to unmask]

Reply-To:

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

Date:

Thu, 13 Nov 2014 19:53:14 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (113 lines)

Author: [log in to unmask]
Date: Thu Nov 13 11:53:11 2014
New Revision: 1508

Log:
Implement insert command for ConditionsRecord.

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

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsRecord.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsRecord.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/ConditionsRecord.java	Thu Nov 13 11:53:11 2014
@@ -1,6 +1,7 @@
 package org.hps.conditions;
 
 import java.util.Date;
+import java.util.List;
 
 /**
  * This class represents a single record from the primary conditions data table,
@@ -9,9 +10,6 @@
  * 
  * @author Jeremy McCormick <[log in to unmask]>
  */
-// TODO: Check behavior of select, delete, update and insert operations.
-// TODO: Override default behavior of methods in super class that don't make
-// sense.
 public final class ConditionsRecord extends AbstractConditionsObject {
 
     /**
@@ -26,13 +24,25 @@
         public void add(ConditionsRecord record) {
             objects.add(record);
         }
-
-        // FIXME: Should probably override more methods here that don't make
-        // sense for
-        // this type!
     }
 
-    protected ConditionsRecord() {
+    public ConditionsRecord() {
+    }
+    
+    public void insert() throws ConditionsObjectException {
+        if (!isNew())
+            throw new ConditionsObjectException("Record already exists in database and cannot be inserted.");
+        if (isReadOnly())
+            throw new ConditionsObjectException("This object is set to read only mode.");
+        if (fieldValues.size() == 0)
+            throw new ConditionsObjectException("There are no field values to insert.");
+        String query = QueryBuilder.buildInsert(getTableMetaData().getTableName(), this.getFieldValues());        
+        System.out.println(query);
+        List<Integer> keys = DatabaseConditionsManager.getInstance().updateQuery(query);
+        if (keys.size() != 1) {
+            throw new ConditionsObjectException("SQL insert returned wrong number of keys: " + keys.size());
+        }
+        rowId = keys.get(0);
     }
 
     /**

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/QueryBuilder.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/QueryBuilder.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/QueryBuilder.java	Thu Nov 13 11:53:11 2014
@@ -1,6 +1,10 @@
 package org.hps.conditions;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.List;
+
+import org.hps.conditions.ConditionsObject.FieldValueMap;
 
 /**
  * This is a static utility class for building SQL queries for the conditions
@@ -64,6 +68,31 @@
         return buff.toString();
     }
 
+    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
+    
+    public static String buildInsert(String tableName, FieldValueMap fieldValues) {
+        if (fieldValues.size() == 0) {
+            throw new IllegalArgumentException("The FieldValueMap has no values.");
+        }
+        StringBuffer buff = new StringBuffer();
+        buff.append("INSERT INTO " + tableName + " (");
+        for (String fieldName : fieldValues.keySet()) {
+            buff.append(" " + fieldName + ",");
+        }
+        buff.setLength(buff.length() - 1);
+        buff.append(" ) VALUES (");
+        for (Object value : fieldValues.values()) {
+            String insertValue = value.toString();
+            if (value instanceof Date) {
+                insertValue = dateFormat.format((Date) value);
+            }
+            buff.append(" '" + insertValue + "',");
+        }
+        buff.setLength(buff.length() - 1);
+        buff.append(")");
+        return buff.toString();
+    }
+
     public static String buildInsert(String tableName, int collectionID, List<String> columnNames, List<List<String>> rows) {
         StringBuffer buff = new StringBuffer();
         buff.append("INSERT INTO " + tableName + " ( collection_id");

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