jdk/src/share/classes/com/sun/rowset/internal/CachedRowSetWriter.java
author lancea
Sat, 04 Sep 2010 15:30:23 -0400
changeset 6530 bfb7b294dd14
parent 5506 202f599c92aa
child 6692 aef4e294026f
permissions -rw-r--r--
6680198: UnmarshalException caused by incompatible serialVersionUID Reviewed-by: sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.rowset.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.sql.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.sql.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.rowset.*;
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
    34
import java.text.MessageFormat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sql.rowset.*;
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
    36
import javax.sql.rowset.serial.SQLInputImpl;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
    37
import javax.sql.rowset.serial.SerialArray;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
    38
import javax.sql.rowset.serial.SerialBlob;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
    39
import javax.sql.rowset.serial.SerialClob;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
    40
import javax.sql.rowset.serial.SerialStruct;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.sql.rowset.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The facility called on internally by the <code>RIOptimisticProvider</code> implementation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * propagate changes back to the data source from which the rowset got its data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * A <code>CachedRowSetWriter</code> object, called a writer, has the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * method <code>writeData</code> for writing modified data to the underlying data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This method is invoked by the rowset internally and is never invoked directly by an application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A writer also has public methods for setting and getting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the <code>CachedRowSetReader</code> object, called a reader, that is associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * with the writer. The remainder of the methods in this class are private and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * are invoked internally, either directly or indirectly, by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <code>writeData</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Typically the <code>SyncFactory</code> manages the <code>RowSetReader</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the <code>RowSetWriter</code> implementations using <code>SyncProvider</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Standard JDBC RowSet implementations provide an object instance of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * writer by invoking the <code>SyncProvider.getRowSetWriter()</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
    62
 * @version 0.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author Jonathan Bruce
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see javax.sql.rowset.spi.SyncProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see javax.sql.rowset.spi.SyncFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @see javax.sql.rowset.spi.SyncFactoryException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class CachedRowSetWriter implements TransactionalWriter, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * The <code>Connection</code> object that this writer will use to make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * connection to the data source to which it will write data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private transient Connection con;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * The SQL <code>SELECT</code> command that this writer will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * internally. The method <code>initSQLStatements</code> builds this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * command by supplying the words "SELECT" and "FROM," and using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * metadata to get the table name and column names .
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private String selectCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * The SQL <code>UPDATE</code> command that this writer will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * internally to write data to the rowset's underlying data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The method <code>initSQLStatements</code> builds this <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private String updateCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * The SQL <code>WHERE</code> clause the writer will use for update
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * statements in the <code>PreparedStatement</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * it sends to the underlying data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private String updateWhere;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * The SQL <code>DELETE</code> command that this writer will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * internally to delete a row in the rowset's underlying data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private String deleteCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * The SQL <code>WHERE</code> clause the writer will use for delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * statements in the <code>PreparedStatement</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * it sends to the underlying data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private String deleteWhere;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * The SQL <code>INSERT INTO</code> command that this writer will internally use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * to insert data into the rowset's underlying data source.  The method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <code>initSQLStatements</code> builds this command with a question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * mark parameter placeholder for each column in the rowset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private String insertCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * An array containing the column numbers of the columns that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * needed to uniquely identify a row in the <code>CachedRowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * for which this <code>CachedRowSetWriter</code> object is the writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private int[] keyCols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * An array of the parameters that should be used to set the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * placeholders in a <code>PreparedStatement</code> object that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * writer will execute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private Object[] params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * The <code>CachedRowSetReader</code> object that has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * set as the reader for the <code>CachedRowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * for which this <code>CachedRowSetWriter</code> object is the writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private CachedRowSetReader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * The <code>ResultSetMetaData</code> object that contains information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * about the columns in the <code>CachedRowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * for which this <code>CachedRowSetWriter</code> object is the writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private ResultSetMetaData callerMd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * The number of columns in the <code>CachedRowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * for which this <code>CachedRowSetWriter</code> object is the writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private int callerColumnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * This <code>CachedRowSet<code> will hold the conflicting values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *  retrieved from the db and hold it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private CachedRowSetImpl crsResolve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * This <code>ArrayList<code> will hold the values of SyncResolver.*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private ArrayList status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * This will check whether the same field value has changed both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * in database and CachedRowSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private int iChangedValsInDbAndCRS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * This will hold the number of cols for which the values have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * changed only in database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private int iChangedValsinDbOnly ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private JdbcRowSetResourceBundle resBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public CachedRowSetWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
       try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
               resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
       } catch(IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
               throw new RuntimeException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * Propagates changes in the given <code>RowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * back to its underlying data source and returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * if successful. The writer will check to see if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * the data in the pre-modified rowset (the original values) differ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * from the data in the underlying data source.  If data in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * source has been modified by someone else, there is a conflict,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * and in that case, the writer will not write to the data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * In other words, the writer uses an optimistic concurrency algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * It checks for conflicts before making changes rather than restricting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * access for concurrent users.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * This method is called by the rowset internally when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * the application invokes the method <code>acceptChanges</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * The <code>writeData</code> method in turn calls private methods that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * it defines internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * The following is a general summary of what the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * <code>writeData</code> does, much of which is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * through calls to its own internal methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * <OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * <LI>Creates a <code>CachedRowSet</code> object from the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *     <code>RowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * <LI>Makes a connection with the data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *   <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *      <LI>Disables autocommit mode if it is not already disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *      <LI>Sets the transaction isolation level to that of the rowset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *   </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * <LI>Checks to see if the reader has read new data since the writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *     was last called and, if so, calls the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *    <code>initSQLStatements</code> to initialize new SQL statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *   <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *       <LI>Builds new <code>SELECT</code>, <code>UPDATE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *           <code>INSERT</code>, and <code>DELETE</code> statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *       <LI>Uses the <code>CachedRowSet</code> object's metadata to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *           determine the table name, column names, and the columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *           that make up the primary key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *   </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * <LI>When there is no conflict, propagates changes made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *     <code>CachedRowSet</code> object back to its underlying data source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *   <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *      <LI>Iterates through each row of the <code>CachedRowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *          to determine whether it has been updated, inserted, or deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *      <LI>If the corresponding row in the data source has not been changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *          since the rowset last read its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *          values, the writer will use the appropriate command to update,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *          insert, or delete the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *      <LI>If any data in the data source does not match the original values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *          for the <code>CachedRowSet</code> object, the writer will roll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *          back any changes it has made to the row in the data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *   </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * @return <code>true</code> if changes to the rowset were successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 *         written to the rowset's underlying data source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public boolean writeData(RowSetInternal caller) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        boolean conflict = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        boolean showDel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        PreparedStatement pstmtIns = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        iChangedValsInDbAndCRS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        iChangedValsinDbOnly = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // We assume caller is a CachedRowSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        CachedRowSetImpl crs = (CachedRowSetImpl)caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // crsResolve = new CachedRowSetImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        this.crsResolve = new CachedRowSetImpl();;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // The reader is registered with the writer at design time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // This is not required, in general.  The reader has logic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // to get a JDBC connection, so call it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        con = reader.connect(caller);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (con == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new SQLException(resBundle.handleGetObject("crswriter.connect").toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         // Fix 6200646.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         // Don't change the connection or transaction properties. This will fail in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         // J2EE container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (con.getAutoCommit() == true)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            con.setAutoCommit(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        con.setTransactionIsolation(crs.getTransactionIsolation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        initSQLStatements(crs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int iColCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        RowSetMetaDataImpl rsmdWrite = (RowSetMetaDataImpl)crs.getMetaData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        RowSetMetaDataImpl rsmdResolv = new RowSetMetaDataImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        iColCount = rsmdWrite.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int sz= crs.size()+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        status = new ArrayList(sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        status.add(0,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        rsmdResolv.setColumnCount(iColCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        for(int i =1; i <= iColCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            rsmdResolv.setColumnType(i, rsmdWrite.getColumnType(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            rsmdResolv.setColumnName(i, rsmdWrite.getColumnName(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            rsmdResolv.setNullable(i, ResultSetMetaData.columnNullableUnknown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        this.crsResolve.setMetaData(rsmdResolv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // moved outside the insert inner loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        //pstmtIns = con.prepareStatement(insertCmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (callerColumnCount < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // No data, so return success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (reader.getCloseConnection() == true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    con.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // We need to see rows marked for deletion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        showDel = crs.getShowDeleted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        crs.setShowDeleted(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // Look at all the rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        crs.beforeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int rows =1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        while (crs.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (crs.rowDeleted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                // The row has been deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (conflict = (deleteOriginalRow(crs, this.crsResolve)) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                       status.add(rows, new Integer(SyncResolver.DELETE_ROW_CONFLICT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                      // delete happened without any occurrence of conflicts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                      // so update status accordingly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                       status.add(rows, new Integer(SyncResolver.NO_ROW_CONFLICT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
           } else if (crs.rowInserted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                // The row has been inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                pstmtIns = con.prepareStatement(insertCmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if ( (conflict = insertNewRow(crs, pstmtIns, this.crsResolve)) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                          status.add(rows, new Integer(SyncResolver.INSERT_ROW_CONFLICT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                      // insert happened without any occurrence of conflicts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                      // so update status accordingly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                       status.add(rows, new Integer(SyncResolver.NO_ROW_CONFLICT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            } else  if (crs.rowUpdated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                  // The row has been updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                       if ( conflict = (updateOriginalRow(crs)) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                             status.add(rows, new Integer(SyncResolver.UPDATE_ROW_CONFLICT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
               } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                      // update happened without any occurrence of conflicts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                      // so update status accordingly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                      status.add(rows, new Integer(SyncResolver.NO_ROW_CONFLICT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
               /** The row is neither of inserted, updated or deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                *  So set nulls in the this.crsResolve for this row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                *  as nothing is to be done for such rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                *  Also note that if such a row has been changed in database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                *  and we have not changed(inserted, updated or deleted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                *  that is fine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                int icolCount = crs.getMetaData().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                status.add(rows, new Integer(SyncResolver.NO_ROW_CONFLICT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                this.crsResolve.moveToInsertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                for(int cols=0;cols<iColCount;cols++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                   this.crsResolve.updateNull(cols+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                } //end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                this.crsResolve.insertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                this.crsResolve.moveToCurrentRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                } //end if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         rows++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
      } //end while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // close the insert statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if(pstmtIns!=null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        pstmtIns.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        crs.setShowDeleted(showDel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
      boolean boolConf = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
      for (int j=1;j<status.size();j++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
          // ignore status for index = 0 which is set to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
          if(! ((status.get(j)).equals(new Integer(SyncResolver.NO_ROW_CONFLICT)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
              // there is at least one conflict which needs to be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
              boolConf = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        crs.beforeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        this.crsResolve.beforeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    if(boolConf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        SyncProviderException spe = new SyncProviderException(status.size() - 1+resBundle.handleGetObject("crswriter.conflictsno").toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        //SyncResolver syncRes = spe.getSyncResolver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         SyncResolverImpl syncResImpl = (SyncResolverImpl) spe.getSyncResolver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         syncResImpl.setCachedRowSet(crs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         syncResImpl.setCachedRowSetResolver(this.crsResolve);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         syncResImpl.setStatus(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         syncResImpl.setCachedRowSetWriter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        throw spe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
       /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
       if (conflict == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            con.rollback();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            con.commit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (reader.getCloseConnection() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                       con.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
  } //end writeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 * Updates the given <code>CachedRowSet</code> object's underlying data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 * source so that updates to the rowset are reflected in the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 * data source, and returns <code>false</code> if the update was successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * A return value of <code>true</code> indicates that there is a conflict,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 * meaning that a value updated in the rowset has already been changed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 * someone else in the underlying data source.  A conflict can also exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 * if, for example, more than one row in the data source would be affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
 * by the update or if no rows would be affected.  In any case, if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * a conflict, this method does not update the underlying data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
 * This method is called internally by the method <code>writeData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 * if a row in the <code>CachedRowSet</code> object for which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 * <code>CachedRowSetWriter</code> object is the writer has been updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 * @return <code>false</code> if the update to the underlying data source is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 *         successful; <code>true</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private boolean updateOriginalRow(CachedRowSet crs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        PreparedStatement pstmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        int idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // Select the row from the database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        ResultSet origVals = crs.getOriginalRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        origVals.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            updateWhere = buildWhereClause(updateWhere, origVals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
             /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
              *  The following block of code is for checking a particular type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
              *  query where in there is a where clause. Without this block, if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
              *  SQL statement is built the "where" clause will appear twice hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
              *  the DB errors out and a SQLException is thrown. This code also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
              *  considers that the where clause is in the right place as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
              *  CachedRowSet object would already have been populated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
              *  query before coming to this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
              **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            String tempselectCmd = selectCmd.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            int idxWhere = tempselectCmd.indexOf("where");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if(idxWhere != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
               String tempSelect = selectCmd.substring(0,idxWhere);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
               selectCmd = tempSelect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            pstmt = con.prepareStatement(selectCmd + updateWhere,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                        ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            for (i = 0; i < keyCols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (params[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    pstmt.setObject(++idx, params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                pstmt.setMaxRows(crs.getMaxRows());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                pstmt.setMaxFieldSize(crs.getMaxFieldSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                pstmt.setEscapeProcessing(crs.getEscapeProcessing());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                pstmt.setQueryTimeout(crs.getQueryTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                // Older driver don't support these operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            ResultSet rs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            rs = pstmt.executeQuery();
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   518
            ResultSetMetaData rsmd = rs.getMetaData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   520
            if (rs.next()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                if (rs.next()) {
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   522
                   /** More than one row conflict.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    *  If rs has only one row we are able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    *  uniquely identify the row where update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    *  have to happen else if more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    *  row implies we cannot uniquely identify the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    *  where we have to do updates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    *  crs.setKeyColumns needs to be set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    *  come out of this situation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                   return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                // don't close the rs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                // we require the record in rs to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                // rs.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                // pstmt.close();
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   539
                rs.first();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                // how many fields need to be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                int colsNotChanged = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                Vector cols = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                String updateExec = new String(updateCmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                Object orig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                Object curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                Object rsval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                boolean boolNull = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                Object objVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                // There's only one row and the cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                // needs to be on that row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                boolean flag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
          this.crsResolve.moveToInsertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
          for (i = 1; i <= callerColumnCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                orig = origVals.getObject(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                curr = crs.getObject(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                rsval = rs.getObject(i);
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   563
                /*
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   564
                 * the following block creates equivalent objects
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   565
                 * that would have been created if this rs is populated
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   566
                 * into a CachedRowSet so that comparison of the column values
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   567
                 * from the ResultSet and CachedRowSet are possible
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   568
                 */
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   569
                Map map = (crs.getTypeMap() == null)?con.getTypeMap():crs.getTypeMap();
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   570
                if (rsval instanceof Struct) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   571
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   572
                    Struct s = (Struct)rsval;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   573
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   574
                    // look up the class in the map
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   575
                    Class c = null;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   576
                    c = (Class)map.get(s.getSQLTypeName());
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   577
                    if (c != null) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   578
                        // create new instance of the class
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   579
                        SQLData obj = null;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   580
                        try {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   581
                            obj = (SQLData)c.newInstance();
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   582
                        } catch (java.lang.InstantiationException ex) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   583
                            throw new SQLException(MessageFormat.format(resBundle.handleGetObject("cachedrowsetimpl.unableins").toString(),
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   584
                            ex.getMessage()));
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   585
                        } catch (java.lang.IllegalAccessException ex) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   586
                            throw new SQLException(MessageFormat.format(resBundle.handleGetObject("cachedrowsetimpl.unableins").toString(),
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   587
                            ex.getMessage()));
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   588
                        }
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   589
                        // get the attributes from the struct
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   590
                        Object attribs[] = s.getAttributes(map);
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   591
                        // create the SQLInput "stream"
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   592
                        SQLInputImpl sqlInput = new SQLInputImpl(attribs, map);
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   593
                        // read the values...
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   594
                        obj.readSQL(sqlInput, s.getSQLTypeName());
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   595
                        rsval = obj;
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   596
                    }
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   597
                } else if (rsval instanceof SQLData) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   598
                    rsval = new SerialStruct((SQLData)rsval, map);
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   599
                } else if (rsval instanceof Blob) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   600
                    rsval = new SerialBlob((Blob)rsval);
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   601
                } else if (rsval instanceof Clob) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   602
                    rsval = new SerialClob((Clob)rsval);
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   603
                } else if (rsval instanceof java.sql.Array) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   604
                    rsval = new SerialArray((java.sql.Array)rsval, map);
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   605
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                // reset boolNull if it had been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                boolNull = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                /** This addtional checking has been added when the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                 *  in the DB is null, but the DB had a different value when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                 *  data was actaully fetched into the CachedRowSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                if(rsval == null && orig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                   // value in db has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    // don't proceed with synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    // get the value in db and pass it to the resolver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    iChangedValsinDbOnly++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                   // Set the boolNull to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                   // in order to set the actual value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                     boolNull = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                     objVal = rsval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                /** Adding the checking for rsval to be "not" null or else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                 *  it would through a NullPointerException when the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                 *  are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                else if(rsval != null && (!rsval.equals(orig)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    // value in db has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    // don't proceed with synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    // get the value in db and pass it to the resolver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    iChangedValsinDbOnly++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                   // Set the boolNull to false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                   // in order to set the actual value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                     boolNull = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                     objVal = rsval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                } else if (  (orig == null || curr == null) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        /** Adding the additonal condition of checking for "flag"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                         *  boolean variable, which would otherwise result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                         *  building a invalid query, as the comma would not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                         *  added to the query string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        if (first == false || flag == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                          updateExec += ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        updateExec += crs.getMetaData().getColumnName(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        cols.add(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        updateExec += " = ? ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                /** Adding the extra condition for orig to be "not" null as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                 *  condition for orig to be null is take prior to this, if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                 *  is not added it will result in a NullPointerException when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                 *  the values are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                }  else if (orig.equals(curr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                       colsNotChanged++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                     //nothing to update in this case since values are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                /** Adding the extra condition for orig to be "not" null as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                 *  condition for orig to be null is take prior to this, if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                 *  is not added it will result in a NullPointerException when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                 *  the values are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                } else if(orig.equals(curr) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                      // When values from db and values in CachedRowSet are not equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                      // if db value is same as before updation for each col in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                      // the row before fetching into CachedRowSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                      // only then we go ahead with updation, else we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                      // throw SyncProviderException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                      // if value has changed in db after fetching from db
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                      // for some cols of the row and at the same time, some other cols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                      // have changed in CachedRowSet, no synchronization happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                      // Synchronization happens only when data when fetching is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                      // same or at most has changed in cachedrowset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                      // check orig value with what is there in crs for a column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                      // before updation in crs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                         if(crs.columnUpdated(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                             if(rsval.equals(orig)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                               // At this point we are sure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                               // the value updated in crs was from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                               // what is in db now and has not changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                 if (flag == false || first == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                                    updateExec += ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                                updateExec += crs.getMetaData().getColumnName(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                cols.add(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                updateExec += " = ? ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                                flag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                               // Here the value has changed in the db after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                               // data was fetched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                               // Plus store this row from CachedRowSet and keep it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                               // in a new CachedRowSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                               boolNull= false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                               objVal = rsval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                               iChangedValsInDbAndCRS++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    if(!boolNull) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        this.crsResolve.updateObject(i,objVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                                      this.crsResolve.updateNull(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                } //end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   723
                rs.close();
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   724
                pstmt.close();
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
   725
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
               this.crsResolve.insertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                   this.crsResolve.moveToCurrentRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                 * if nothing has changed return now - this can happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                 * if column is updated to the same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                 * if colsNotChanged == callerColumnCount implies we are updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                 * the database with ALL COLUMNS HAVING SAME VALUES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                 * so skip going to database, else do as usual.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                if ( (first == false && cols.size() == 0)  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                     colsNotChanged == callerColumnCount ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                if(iChangedValsInDbAndCRS != 0 || iChangedValsinDbOnly != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                   return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                updateExec += updateWhere;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                pstmt = con.prepareStatement(updateExec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                // Comments needed here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                for (i = 0; i < cols.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    Object obj = crs.getObject(((Integer)cols.get(i)).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    if (obj != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                        pstmt.setObject(i + 1, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                        pstmt.setNull(i + 1,crs.getMetaData().getColumnType(i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                idx = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                // Comments needed here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                for (i = 0; i < keyCols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    if (params[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        pstmt.setObject(++idx, params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                i = pstmt.executeUpdate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
               /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                * i should be equal to 1(row count), because we update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                * one row(returned as row count) at a time, if all goes well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                * if 1 != 1, this implies we have not been able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                * do updations properly i.e there is a conflict in database
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                * versus what is in CachedRowSet for this particular row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                 return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                 * Cursor will be here, if the ResultSet may not return even a single row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                 * i.e. we can't find the row where to update because it has been deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                 * etc. from the db.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                 * Present the whole row as null to user, to force null to be sync'ed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                 * and hence nothing to be synced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                 * NOTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                 * ------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                 * In the database if a column that is mapped to java.sql.Types.REAL stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                 * a Double value and is compared with value got from ResultSet.getFloat()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                 * no row is retrieved and will throw a SyncProviderException. For details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                 * see bug Id 5053830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        } catch (SQLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // if executeUpdate fails it will come here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            // update crsResolve with null rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            this.crsResolve.moveToInsertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            for(i = 1; i <= callerColumnCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
               this.crsResolve.updateNull(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            this.crsResolve.insertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            this.crsResolve.moveToCurrentRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * Inserts a row that has been inserted into the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * <code>CachedRowSet</code> object into the data source from which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         * the rowset is derived, returning <code>false</code> if the insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * was successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
         * @param crs the <code>CachedRowSet</code> object that has had a row inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
         *            and to whose underlying data source the row will be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * @param pstmt the <code>PreparedStatement</code> object that will be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         *              to execute the insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * @return <code>false</code> to indicate that the insertion was successful;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         *         <code>true</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    private boolean insertNewRow(CachedRowSet crs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        PreparedStatement pstmt, CachedRowSetImpl crsRes) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        int icolCount = crs.getMetaData().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        boolean returnVal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        PreparedStatement pstmtSel = con.prepareStatement(selectCmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        ResultSet rs, rs2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        DatabaseMetaData dbmd = con.getMetaData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        rs = pstmtSel.executeQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        String table = crs.getTableName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        rs2 = dbmd.getPrimaryKeys(null, null, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        String [] primaryKeys = new String[icolCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        while(rs2.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            String pkcolname = rs2.getString("COLUMN_NAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            primaryKeys[k] = pkcolname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            k++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        if(rs.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            for(int j=0;j<primaryKeys.length;j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                if(primaryKeys[j] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    if(crs.getObject(primaryKeys[j]) == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    String crsPK = (crs.getObject(primaryKeys[j])).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    String rsPK = (rs.getObject(primaryKeys[j])).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    if(crsPK.equals(rsPK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                        returnVal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        this.crsResolve.moveToInsertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                        for(i = 1; i <= icolCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                            String colname = (rs.getMetaData()).getColumnName(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                            if(colname.equals(primaryKeys[j]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                                this.crsResolve.updateObject(i,rsPK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                                this.crsResolve.updateNull(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                        this.crsResolve.insertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                        this.crsResolve.moveToCurrentRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if(returnVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            return returnVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            for (i = 1; i <= icolCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                Object obj = crs.getObject(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    pstmt.setObject(i, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    pstmt.setNull(i,crs.getMetaData().getColumnType(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
             i = pstmt.executeUpdate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
             return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        } catch (SQLException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
             * Cursor will come here if executeUpdate fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
             * There can be many reasons why the insertion failed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
             * one can be violation of primary key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
             * Hence we cannot exactly identify why the insertion failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
             * Present the current row as a null row to the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
             **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            this.crsResolve.moveToInsertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            for(i = 1; i <= icolCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
               this.crsResolve.updateNull(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            this.crsResolve.insertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            this.crsResolve.moveToCurrentRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
 * Deletes the row in the underlying data source that corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
 * a row that has been deleted in the given <code> CachedRowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
 * and returns <code>false</code> if the deletion was successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
 * This method is called internally by this writer's <code>writeData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
 * method when a row in the rowset has been deleted. The values in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
 * deleted row are the same as those that are stored in the original row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
 * of the given <code>CachedRowSet</code> object.  If the values in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
 * original row differ from the row in the underlying data source, the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
 * in the data source is not deleted, and <code>deleteOriginalRow</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
 * returns <code>true</code> to indicate that there was a conflict.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
 * @return <code>false</code> if the deletion was successful, which means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
 *         there was no conflict; <code>true</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
 * @throws SQLException if there was a database access error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    private boolean deleteOriginalRow(CachedRowSet crs, CachedRowSetImpl crsRes) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        PreparedStatement pstmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        int idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        String strSelect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    // Select the row from the database.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        ResultSet origVals = crs.getOriginalRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        origVals.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        deleteWhere = buildWhereClause(deleteWhere, origVals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        pstmt = con.prepareStatement(selectCmd + deleteWhere,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        for (i = 0; i < keyCols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (params[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                pstmt.setObject(++idx, params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            pstmt.setMaxRows(crs.getMaxRows());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            pstmt.setMaxFieldSize(crs.getMaxFieldSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            pstmt.setEscapeProcessing(crs.getEscapeProcessing());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            pstmt.setQueryTimeout(crs.getQueryTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
             * Older driver don't support these operations...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        ResultSet rs = pstmt.executeQuery();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        if (rs.next() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            if (rs.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                // more than one row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            rs.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            // Now check all the values in rs to be same in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            // db also before actually going ahead with deleting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            boolean boolChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            crsRes.moveToInsertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            for (i = 1; i <= crs.getMetaData().getColumnCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                Object original = origVals.getObject(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                Object changed = rs.getObject(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                if(original != null && changed != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                  if(! (original.toString()).equals(changed.toString()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                      boolChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                      crsRes.updateObject(i,origVals.getObject(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                   crsRes.updateNull(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
           crsRes.insertRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
           crsRes.moveToCurrentRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
           if(boolChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
               // do not delete as values in db have changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
               // deletion will not happen for this row from db
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                   // exit now returning true. i.e. conflict
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
               return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                // delete the row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                // Go ahead with deleting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                // don't do anything here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            String cmd = deleteCmd + deleteWhere;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            pstmt = con.prepareStatement(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            idx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            for (i = 0; i < keyCols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                if (params[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                    pstmt.setObject(++idx, params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            if (pstmt.executeUpdate() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            pstmt.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            // didn't find the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        // no conflict
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * Sets the reader for this writer to the given reader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    public void setReader(CachedRowSetReader reader) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        this.reader = reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Gets the reader for this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    public CachedRowSetReader getReader() throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * Composes a <code>SELECT</code>, <code>UPDATE</code>, <code>INSERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * and <code>DELETE</code> statement that can be used by this writer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * write data to the data source backing the given <code>CachedRowSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @ param caller a <code>CachedRowSet</code> object for which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *                <code>CachedRowSetWriter</code> object is the writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    private void initSQLStatements(CachedRowSet caller) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        callerMd = caller.getMetaData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        callerColumnCount = callerMd.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (callerColumnCount < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            // No data, so return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
         * If the RowSet has a Table name we should use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
         * This is really a hack to get round the fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
         * a lot of the jdbc drivers can't provide the tab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        String table = caller.getTableName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        if (table == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
             * attempt to build a table name using the info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
             * that the driver gave us for the first column
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
             * in the source result set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            table = callerMd.getTableName(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            if (table == null || table.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                throw new SQLException(resBundle.handleGetObject("crswriter.tname").toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        String catalog = callerMd.getCatalogName(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            String schema = callerMd.getSchemaName(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        DatabaseMetaData dbmd = con.getMetaData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
         * Compose a SELECT statement.  There are three parts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        // Project List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        selectCmd = "SELECT ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        for (i=1; i <= callerColumnCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            selectCmd += callerMd.getColumnName(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            if ( i <  callerMd.getColumnCount() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                selectCmd += ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                selectCmd += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        // FROM clause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        selectCmd += "FROM " + buildTableName(dbmd, catalog, schema, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
         * Compose an UPDATE statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        updateCmd = "UPDATE " + buildTableName(dbmd, catalog, schema, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
         *  The following block of code is for checking a particular type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
         *  query where in there is a where clause. Without this block, if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
         *  SQL statement is built the "where" clause will appear twice hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
         *  the DB errors out and a SQLException is thrown. This code also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
         *  considers that the where clause is in the right place as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
         *  CachedRowSet object would already have been populated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
         *  query before coming to this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        String tempupdCmd = updateCmd.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        int idxupWhere = tempupdCmd.indexOf("where");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if(idxupWhere != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
           updateCmd = updateCmd.substring(0,idxupWhere);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        updateCmd += "SET ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * Compose an INSERT statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        insertCmd = "INSERT INTO " + buildTableName(dbmd, catalog, schema, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        // Column list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        insertCmd += "(";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        for (i=1; i <= callerColumnCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            insertCmd += callerMd.getColumnName(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            if ( i <  callerMd.getColumnCount() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                insertCmd += ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                insertCmd += ") VALUES (";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        for (i=1; i <= callerColumnCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            insertCmd += "?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            if (i < callerColumnCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                insertCmd += ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                insertCmd += ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * Compose a DELETE statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        deleteCmd = "DELETE FROM " + buildTableName(dbmd, catalog, schema, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
         * set the key desriptors that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
         * needed to construct where clauses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        buildKeyDesc(caller);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * Returns a fully qualified table name built from the given catalog and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * table names. The given metadata object is used to get the proper order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * and separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @param dbmd a <code>DatabaseMetaData</code> object that contains metadata
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *          about this writer's <code>CachedRowSet</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @param catalog a <code>String</code> object with the rowset's catalog
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *          name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @param table a <code>String</code> object with the name of the table from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *          which this writer's rowset was derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @return a <code>String</code> object with the fully qualified name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *          table from which this writer's rowset was derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    private String buildTableName(DatabaseMetaData dbmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        String catalog, String schema, String table) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
       // trim all the leading and trailing whitespaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
       // white spaces can never be catalog, schema or a table name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        String cmd = new String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        catalog = catalog.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        schema = schema.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        table = table.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        if (dbmd.isCatalogAtStart() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            if (catalog != null && catalog.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                cmd += catalog + dbmd.getCatalogSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            if (schema != null && schema.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                cmd += schema + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            cmd += table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            if (schema != null && schema.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                cmd += schema + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            cmd += table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            if (catalog != null && catalog.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                cmd += dbmd.getCatalogSeparator() + catalog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        cmd += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * Assigns to the given <code>CachedRowSet</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * <code>params</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * field an array whose length equals the number of columns needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * to uniquely identify a row in the rowset. The array is given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * values by the method <code>buildWhereClause</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * If the <code>CachedRowSet</code> object's <code>keyCols</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * field has length <code>0</code> or is <code>null</code>, the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * is set with the column number of every column in the rowset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * Otherwise, the array in the field <code>keyCols</code> is set with only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * the column numbers of the columns that are required to form a unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * identifier for a row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * @param crs the <code>CachedRowSet</code> object for which this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *     <code>CachedRowSetWriter</code> object is the writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    private void buildKeyDesc(CachedRowSet crs) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        keyCols = crs.getKeyColumns();
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1236
        ResultSetMetaData resultsetmd = crs.getMetaData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        if (keyCols == null || keyCols.length == 0) {
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1238
            ArrayList<Integer> listKeys = new ArrayList<Integer>();
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1239
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1240
            for (int i = 0; i < callerColumnCount; i++ ) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1241
                if(resultsetmd.getColumnType(i+1) != java.sql.Types.CLOB &&
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1242
                        resultsetmd.getColumnType(i+1) != java.sql.Types.STRUCT &&
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1243
                        resultsetmd.getColumnType(i+1) != java.sql.Types.SQLXML &&
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1244
                        resultsetmd.getColumnType(i+1) != java.sql.Types.BLOB &&
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1245
                        resultsetmd.getColumnType(i+1) != java.sql.Types.ARRAY &&
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1246
                        resultsetmd.getColumnType(i+1) != java.sql.Types.OTHER )
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1247
                    listKeys.add(i+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            }
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1249
            keyCols = new int[listKeys.size()];
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1250
            for (int i = 0; i < listKeys.size(); i++ )
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1251
                keyCols[i] = listKeys.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        params = new Object[keyCols.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
         * Constructs an SQL <code>WHERE</code> clause using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         * string as a starting point. The resulting clause will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
         * a column name and " = ?" for each key column, that is, each column
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
         * that is needed to form a unique identifier for a row in the rowset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
         * This <code>WHERE</code> clause can be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
         * a <code>PreparedStatement</code> object that updates, inserts, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
         * deletes a row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
         * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
         * This method uses the given result set to access values in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
         * <code>CachedRowSet</code> object that called this writer.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         * values are used to build the array of parameters that will serve as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
         * replacements for the "?" parameter placeholders in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
         * <code>PreparedStatement</code> object that is sent to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         * <code>CachedRowSet</code> object's underlying data source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         * @param whereClause a <code>String</code> object that is an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         *                    string ("")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         * @param rs a <code>ResultSet</code> object that can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
         *           to access the <code>CachedRowSet</code> object's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
         * @return a <code>WHERE</code> clause of the form "<code>WHERE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
         *         columnName = ? AND columnName = ? AND columnName = ? ..."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
         * @throws SQLException if a database access error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    private String buildWhereClause(String whereClause,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                    ResultSet rs) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        whereClause = "WHERE ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        for (int i = 0; i < keyCols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    whereClause += "AND ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            whereClause += callerMd.getColumnName(keyCols[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            params[i] = rs.getObject(keyCols[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            if (rs.wasNull() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                whereClause += " IS NULL ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                whereClause += " = ? ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        return whereClause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    void updateResolvedConflictToDB(CachedRowSet crs, Connection con) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
          //String updateExe = ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
          PreparedStatement pStmt  ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
          String strWhere = "WHERE " ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
          String strExec =" ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
          String strUpdate = "UPDATE ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
          int icolCount = crs.getMetaData().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
          int keyColumns[] = crs.getKeyColumns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
          Object param[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
          String strSet="";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        strWhere = buildWhereClause(strWhere, crs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        if (keyColumns == null || keyColumns.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            keyColumns = new int[icolCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            for (int i = 0; i < keyColumns.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                keyColumns[i] = ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
          param = new Object[keyColumns.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         strUpdate = "UPDATE " + buildTableName(con.getMetaData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                            crs.getMetaData().getCatalogName(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                           crs.getMetaData().getSchemaName(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                           crs.getTableName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         // changed or updated values will become part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         // set clause here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         strUpdate += "SET ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        for (int i=1; i<=icolCount;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
           if (crs.columnUpdated(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                  if (first == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                    strSet += ", ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                 strSet += crs.getMetaData().getColumnName(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                 strSet += " = ? ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                 first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
         } //end if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
      } //end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         // keycols will become part of where clause
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
         strUpdate += strSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
         strWhere = "WHERE ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        for (int i = 0; i < keyColumns.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    strWhere += "AND ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            strWhere += crs.getMetaData().getColumnName(keyColumns[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            param[i] = crs.getObject(keyColumns[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            if (crs.wasNull() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                strWhere += " IS NULL ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                strWhere += " = ? ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
          strUpdate += strWhere;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        pStmt = con.prepareStatement(strUpdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        int idx =0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
          for (int i = 0; i < icolCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
             if(crs.columnUpdated(i+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
              Object obj = crs.getObject(i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
              if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                  pStmt.setObject(++idx, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
              } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                  pStmt.setNull(i + 1,crs.getMetaData().getColumnType(i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
             } //end if ..else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
           } //end if crs.column...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        } //end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
          // Set the key cols for after WHERE =? clause
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
          for (int i = 0; i < keyColumns.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
              if (param[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                  pStmt.setObject(++idx, param[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        int id = pStmt.executeUpdate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    public void commit() throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        con.commit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        if (reader.getCloseConnection() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            con.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     public void commit(CachedRowSetImpl crs, boolean updateRowset) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        con.commit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        if(updateRowset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
          if(crs.getCommand() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            crs.execute(con);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (reader.getCloseConnection() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            con.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    public void rollback() throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        con.rollback();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        if (reader.getCloseConnection() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            con.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    public void rollback(Savepoint s) throws SQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        con.rollback(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        if (reader.getCloseConnection() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            con.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
6530
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1427
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1428
        // Default state initialization happens here
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1429
        ois.defaultReadObject();
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1430
        // Initialization of  Res Bundle happens here .
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1431
        try {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1432
           resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1433
        } catch(IOException ioe) {
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1434
            throw new RuntimeException(ioe);
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1435
        }
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1436
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1437
    }
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1438
bfb7b294dd14 6680198: UnmarshalException caused by incompatible serialVersionUID
lancea
parents: 5506
diff changeset
  1439
    static final long serialVersionUID =-8506030970299413976L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
}