cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r439969 - /incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java
Date Mon, 04 Sep 2006 08:06:22 GMT
Author: aadamchik
Date: Mon Sep  4 01:06:18 2006
New Revision: 439969

URL: http://svn.apache.org/viewvc?view=rev&rev=439969
Log:
adding insert statement support

Modified:
    incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java

Modified: incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java?view=diff&rev=439969&r1=439968&r2=439969
==============================================================================
--- incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java
(original)
+++ incubator/cayenne/main/trunk/integration-test/itest-common/src/main/java/org/apache/cayenne/jpa/itest/ItestDBUtils.java
Mon Sep  4 01:06:18 2006
@@ -19,6 +19,7 @@
 package org.apache.cayenne.jpa.itest;
 
 import java.sql.Connection;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
@@ -26,6 +27,57 @@
 import java.sql.Timestamp;
 
 public class ItestDBUtils {
+
+    /**
+     * Inserts a single row.
+     */
+    public static void insert(String table, String[] columns, Object[] values)
+            throws SQLException {
+
+        if (columns.length != values.length) {
+            throw new IllegalArgumentException(
+                    "Columns and values arrays have different sizes: "
+                            + columns.length
+                            + " and "
+                            + values.length);
+        }
+
+        if (columns.length == 0) {
+            throw new IllegalArgumentException("No columns");
+        }
+
+        StringBuilder sql = new StringBuilder("INSERT INTO ");
+        sql.append(table).append(" (").append(columns[0]);
+        for (int i = 1; i < columns.length; i++) {
+            sql.append(", ").append(columns[i]);
+        }
+
+        sql.append(") VALUES (?");
+        for (int i = 1; i < values.length; i++) {
+            sql.append(", ?");
+        }
+        sql.append(")");
+
+        Connection c = getConnection();
+        try {
+
+            PreparedStatement st = c.prepareStatement(sql.toString());
+            try {
+                for (int i = 0; i < values.length; i++) {
+                    st.setObject(i + 1, values[i]);
+                }
+
+                st.executeUpdate();
+            }
+            finally {
+                st.close();
+            }
+            c.commit();
+        }
+        finally {
+            c.close();
+        }
+    }
 
     public static int deleteAll(String table) throws SQLException {
         String sql = "delete from " + table;



Mime
View raw message