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