This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 206233
Collapse All | Expand All

(-)a/db.dataview/src/org/netbeans/modules/db/dataview/output/DataView.java (-1 / +1 lines)
Lines 102-108 Link Here
102
        try {
102
        try {
103
            dv.dataPage = new DataViewPageContext(pageSize);
103
            dv.dataPage = new DataViewPageContext(pageSize);
104
            dv.execHelper = new SQLExecutionHelper(dv);
104
            dv.execHelper = new SQLExecutionHelper(dv);
105
            SQLExecutionHelper.initialDataLoad(dv, dbConn, dv.execHelper);
105
            dv.execHelper.initialDataLoad();
106
            dv.stmtGenerator = new SQLStatementGenerator(dv);
106
            dv.stmtGenerator = new SQLStatementGenerator(dv);
107
        } catch (Exception ex) {
107
        } catch (Exception ex) {
108
            dv.setErrorStatusText(ex);
108
            dv.setErrorStatusText(ex);
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/output/DataViewTableUI.java (-5 / +2 lines)
Lines 155-172 Link Here
155
            Object obj = dataView.getDataViewPageContext().getColumnData(
155
            Object obj = dataView.getDataViewPageContext().getColumnData(
156
                    table.convertRowIndexToModel(row),
156
                    table.convertRowIndexToModel(row),
157
                    table.convertColumnIndexToModel(column));
157
                    table.convertColumnIndexToModel(column));
158
            if (value == null) {
159
                return c;
160
            }
161
158
162
            if (isSelected) {
159
            if (isSelected) {
163
                if (obj != null && value.equals(obj)) {
160
                if ((obj == null && value == null) || (obj != null && value != null && value.equals(obj))) {
164
                    c.setForeground(gray);
161
                    c.setForeground(gray);
165
                } else {
162
                } else {
166
                    c.setForeground(Color.ORANGE);
163
                    c.setForeground(Color.ORANGE);
167
                }
164
                }
168
            } else {
165
            } else {
169
                if (obj != null && value.equals(obj)) {
166
                if ((obj == null && value == null) || (obj != null && value != null && value.equals(obj))) {
170
                    c.setForeground(table.getForeground());
167
                    c.setForeground(table.getForeground());
171
                } else {
168
                } else {
172
                    c.setForeground(green);
169
                    c.setForeground(green);
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/output/SQLExecutionHelper.java (-32 / +28 lines)
Lines 57-63 Link Here
57
import java.util.logging.Level;
57
import java.util.logging.Level;
58
import java.util.logging.Logger;
58
import java.util.logging.Logger;
59
import javax.swing.table.TableModel;
59
import javax.swing.table.TableModel;
60
import org.netbeans.api.db.explorer.DatabaseConnection;
61
import org.netbeans.modules.db.dataview.meta.DBConnectionFactory;
60
import org.netbeans.modules.db.dataview.meta.DBConnectionFactory;
62
import org.netbeans.modules.db.dataview.meta.DBException;
61
import org.netbeans.modules.db.dataview.meta.DBException;
63
import org.netbeans.modules.db.dataview.meta.DBMetaDataFactory;
62
import org.netbeans.modules.db.dataview.meta.DBMetaDataFactory;
Lines 87-103 Link Here
87
        this.dataView = dataView;
86
        this.dataView = dataView;
88
    }
87
    }
89
88
90
    static void initialDataLoad(DataView dv, DatabaseConnection dbConn, SQLExecutionHelper execHelper) throws SQLException {
89
    void initialDataLoad() throws SQLException {
91
        Statement stmt = null;
90
        Statement stmt = null;
92
        try {
91
        try {
93
            Connection conn = DBConnectionFactory.getInstance().getConnection(dbConn);
92
            Connection conn = DBConnectionFactory.getInstance().getConnection(dataView.getDatabaseConnection());
94
            String msg = "";
93
            String msg = "";
95
            if (conn == null) {
94
            if (conn == null) {
96
                Throwable ex = DBConnectionFactory.getInstance().getLastException();
95
                Throwable ex = DBConnectionFactory.getInstance().getLastException();
97
                if (ex != null) {
96
                if (ex != null) {
98
                    msg = ex.getMessage();
97
                    msg = ex.getMessage();
99
                } else {
98
                } else {
100
                    msg = NbBundle.getMessage(SQLExecutionHelper.class, "MSG_connection_failure", dv.getDatabaseConnection());
99
                    msg = NbBundle.getMessage(SQLExecutionHelper.class, "MSG_connection_failure", dataView.getDatabaseConnection());
101
                }
100
                }
102
                NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
101
                NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
103
                DialogDisplayer.getDefault().notifyLater(nd);
102
                DialogDisplayer.getDefault().notifyLater(nd);
Lines 105-141 Link Here
105
            }
104
            }
106
105
107
            DBMetaDataFactory dbMeta = new DBMetaDataFactory(conn);
106
            DBMetaDataFactory dbMeta = new DBMetaDataFactory(conn);
108
            dv.setLimitSupported(dbMeta.supportsLimit());
107
            dataView.setLimitSupported(dbMeta.supportsLimit());
109
            String sql = dv.getSQLString();
108
            String sql = dataView.getSQLString();
110
            boolean isSelect = execHelper.isSelectStatement(sql);
109
            boolean isSelect = isSelectStatement(sql);
111
110
112
            stmt = execHelper.prepareSQLStatement(conn, sql);
111
            stmt = prepareSQLStatement(conn, sql);
113
            execHelper.executeSQLStatement(stmt, sql);
112
            executeSQLStatement(stmt, sql);
114
113
115
            if (dv.getUpdateCount() != -1) {
114
            if (dataView.getUpdateCount() != -1) {
116
                if (!conn.getAutoCommit()) {
115
                if (!conn.getAutoCommit()) {
117
                    conn.commit();
116
                    conn.commit();
118
                }
117
                }
119
                return;
118
                return;
120
            }
119
            }
121
120
122
            ResultSet resultSet = null;
121
            ResultSet rs = null;
123
            try {
122
            if(dataView.hasResultSet()) {
124
                resultSet = stmt.getResultSet();
123
                rs = stmt.getResultSet();
125
                if (resultSet == null) {
124
            }
126
                    if (!conn.getAutoCommit()) {
125
127
                        conn.commit();
126
            if (rs == null) {
128
                    }
127
                if (!conn.getAutoCommit()) {
129
                    return;
128
                    conn.commit();
130
                }
129
                }
131
                Collection<DBTable> tables = dbMeta.generateDBTables(resultSet, sql, isSelect);
130
                return;
132
                DataViewDBTable dvTable = new DataViewDBTable(tables);
133
                dv.setDataViewDBTable(dvTable);
134
                execHelper.loadDataFrom(resultSet);
135
            } finally {
136
                DataViewUtils.closeResources(resultSet);
137
            }
131
            }
138
            execHelper.getTotalCount(isSelect, sql, stmt);
132
            Collection<DBTable> tables = dbMeta.generateDBTables(rs, sql, isSelect);
133
            DataViewDBTable dvTable = new DataViewDBTable(tables);
134
            dataView.setDataViewDBTable(dvTable);
135
            loadDataFrom(rs);
136
            DataViewUtils.closeResources(rs);
137
            getTotalCount(isSelect, sql, stmt);
139
        } finally {
138
        } finally {
140
            DataViewUtils.closeResources(stmt);
139
            DataViewUtils.closeResources(stmt);
141
        }
140
        }
Lines 413-419 Link Here
413
        String title = NbBundle.getMessage(SQLExecutionHelper.class, "LBL_sql_executequery");
412
        String title = NbBundle.getMessage(SQLExecutionHelper.class, "LBL_sql_executequery");
414
        SQLStatementExecutor executor = new SQLStatementExecutor(dataView, title, dataView.getSQLString()) {
413
        SQLStatementExecutor executor = new SQLStatementExecutor(dataView, title, dataView.getSQLString()) {
415
414
416
            private ResultSet rs = null;
417
            private Statement stmt = null;
415
            private Statement stmt = null;
418
            boolean lastEditState = dataView.isEditable();
416
            boolean lastEditState = dataView.isEditable();
419
417
Lines 428-435 Link Here
428
                try {
426
                try {
429
                    executeSQLStatement(stmt, sql);
427
                    executeSQLStatement(stmt, sql);
430
                    if (dataView.hasResultSet()) {
428
                    if (dataView.hasResultSet()) {
431
                        rs = stmt.getResultSet();
429
                        ResultSet rs = stmt.getResultSet();
432
                        loadDataFrom(rs);
430
                        loadDataFrom(rs);
431
                        DataViewUtils.closeResources(rs);
433
                    } else {
432
                    } else {
434
                        return;
433
                        return;
435
                    }
434
                    }
Lines 443-451 Link Here
443
                        dataView.removeComponents();
442
                        dataView.removeComponents();
444
                    }
443
                    }
445
                    throw sqlEx;
444
                    throw sqlEx;
446
447
                } finally {
448
                    DataViewUtils.closeResources(rs);
449
                }
445
                }
450
446
451
                // Get total row count
447
                // Get total row count
Lines 603-610 Link Here
603
            try {
599
            try {
604
                isResultSet = stmt.execute(appendLimitIfRequired(sql));
600
                isResultSet = stmt.execute(appendLimitIfRequired(sql));
605
            } catch (NullPointerException ex) {
601
            } catch (NullPointerException ex) {
606
                    LOGGER.log(Level.SEVERE, "Failed to execute SQL Statement [" + sql + "], cause: " + ex);
602
                LOGGER.log(Level.SEVERE, "Failed to execute SQL Statement [" + sql + "], cause: " + ex);
607
                    throw new SQLException(ex);
603
                throw new SQLException(ex);
608
            } catch (SQLException sqlExc) {
604
            } catch (SQLException sqlExc) {
609
                if (sqlExc.getErrorCode() == 1064 && sqlExc.getSQLState().equals("37000")) {
605
                if (sqlExc.getErrorCode() == 1064 && sqlExc.getSQLState().equals("37000")) {
610
                    isResultSet = stmt.execute(sql);
606
                    isResultSet = stmt.execute(sql);
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/output/SQLStatementGenerator.java (+3 lines)
Lines 184-189 Link Here
184
            if (value != null && DataViewUtils.isSQLConstantString(value)) {
184
            if (value != null && DataViewUtils.isSQLConstantString(value)) {
185
                String constStr = ((String) value).substring(1, ((String) value).length() - 1);
185
                String constStr = ((String) value).substring(1, ((String) value).length() - 1);
186
                updateStmt.append(" = ").append(constStr);
186
                updateStmt.append(" = ").append(constStr);
187
            // NULL ist reported as an SQL constant, so treat it as such
188
            } else if ( value == null ) {
189
                updateStmt.append(" = NULL"); // NOI18N
187
            } else { // ELSE literals
190
            } else { // ELSE literals
188
                updateStmt.append(" = ?"); // NOI18N
191
                updateStmt.append(" = ?"); // NOI18N
189
                values.add(value);
192
                values.add(value);
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/Bundle.properties (+9 lines)
Lines 39-41 Link Here
39
# Portions Copyrighted 2008 Sun Microsystems, Inc.
39
# Portions Copyrighted 2008 Sun Microsystems, Inc.
40
40
41
ResultSetJXTable.columnControl.tooltip=Select/deselect columns
41
ResultSetJXTable.columnControl.tooltip=Select/deselect columns
42
43
nullLob.title=Set to NULL
44
saveLob.title=Save to file
45
loadLob.title=Set from file
46
loadLob.sourceNotReadable=Source file can't be read.\nCheck file/directory permissions.
47
saveLob.unknownFormat=Unknown Format for Lob-Handling
48
saveLob.targetNotWriteable=Target file can't be written.\nCheck file/directory permissions.
49
saveLob.failure=Failure while saving file: {0}
50
saveLob.dbError=DB-Failure: {0}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetCellRenderer.java (+61 lines)
Lines 46-51 Link Here
46
import java.awt.Color;
46
import java.awt.Color;
47
import java.awt.Component;
47
import java.awt.Component;
48
import java.awt.Font;
48
import java.awt.Font;
49
import java.sql.Blob;
50
import java.sql.Clob;
51
import java.sql.SQLException;
49
import java.text.SimpleDateFormat;
52
import java.text.SimpleDateFormat;
50
import javax.swing.JComponent;
53
import javax.swing.JComponent;
51
import javax.swing.JLabel;
54
import javax.swing.JLabel;
Lines 90-95 Link Here
90
    private final TableCellRenderer NUMNBER_RENDERER = new NumberObjectCellRenderer();
93
    private final TableCellRenderer NUMNBER_RENDERER = new NumberObjectCellRenderer();
91
    private final TableCellRenderer BOOLEAN_RENDERER = new BooleanCellRenderer();
94
    private final TableCellRenderer BOOLEAN_RENDERER = new BooleanCellRenderer();
92
    private final TableCellRenderer CELL_FOCUS_RENDERER = new CellFocusCustomRenderer();
95
    private final TableCellRenderer CELL_FOCUS_RENDERER = new CellFocusCustomRenderer();
96
    private final TableCellRenderer BLOB_RENDERER = new BlobCellRenderer();
97
    private final TableCellRenderer CLOB_RENDERER = new ClobCellRenderer();
93
98
94
    public ResultSetCellRenderer() {
99
    public ResultSetCellRenderer() {
95
        super(new StringValue() {
100
        super(new StringValue() {
Lines 123-128 Link Here
123
            return DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
128
            return DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
124
        } else if (value instanceof Boolean) {
129
        } else if (value instanceof Boolean) {
125
            return BOOLEAN_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
130
            return BOOLEAN_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
131
        } else if (value instanceof Blob) {
132
            return BLOB_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
133
        } else if (value instanceof Clob) {
134
            return CLOB_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
126
        } else {
135
        } else {
127
             Component c = CELL_FOCUS_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
136
             Component c = CELL_FOCUS_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
128
            //c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
137
            //c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Lines 202-204 Link Here
202
        return c;
211
        return c;
203
    }
212
    }
204
}
213
}
214
215
class BlobCellRenderer extends SQLConstantsCellRenderer {
216
    @Override
217
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
218
        if (!(value instanceof Blob)) {
219
            throw new IllegalArgumentException("BlobCellRenderer can only be used for Blobs");
220
        }
221
        try {
222
            Long size = 0L;
223
            size = ((Blob) value).length();
224
            StringBuilder stringValue = new StringBuilder();
225
            stringValue.append("<BLOB ");
226
            if(size < 1000) {
227
                stringValue.append(String.format("%1$d bytes", size));
228
            } else if ( size < 1000000) {
229
                stringValue.append(String.format("%1$d kB", size / 1000));
230
            } else {
231
                stringValue.append(String.format("%1$d MB", size / 1000000));
232
            }
233
            stringValue.append(">");
234
            return super.getTableCellRendererComponent(table, stringValue.toString(), isSelected, hasFocus, row, column);
235
        } catch (SQLException ex) {
236
            return super.getTableCellRendererComponent(table, "<BLOB of unkown size>", isSelected, hasFocus, row, column);
237
        }
238
    }
239
}
240
241
class ClobCellRenderer extends SQLConstantsCellRenderer {
242
    @Override
243
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
244
        if (!(value instanceof Clob)) {
245
            throw new IllegalArgumentException("ClobCellRenderer can only be used for Blobs");
246
        }
247
        try {
248
            Long size = 0L;
249
            size = ((Clob) value).length();
250
            StringBuilder stringValue = new StringBuilder();
251
            stringValue.append("<CLOB ");
252
            if(size < 1000) {
253
                stringValue.append(String.format("%1$d Chars", size));
254
            } else if ( size < 1000000) {
255
                stringValue.append(String.format("%1$d kChars", size / 1000));
256
            } else {
257
                stringValue.append(String.format("%1$d MChars", size / 1000000));
258
            }
259
            stringValue.append(">");
260
            return super.getTableCellRendererComponent(table, stringValue.toString(), isSelected, hasFocus, row, column);
261
        } catch (SQLException ex) {
262
            return super.getTableCellRendererComponent(table, "<CLOB of unkown size>", isSelected, hasFocus, row, column);
263
        }
264
    }
265
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java (-1 / +5 lines)
Lines 47-52 Link Here
47
import java.awt.event.KeyEvent;
47
import java.awt.event.KeyEvent;
48
import java.awt.event.KeyListener;
48
import java.awt.event.KeyListener;
49
import java.awt.event.MouseEvent;
49
import java.awt.event.MouseEvent;
50
import java.sql.Blob;
51
import java.sql.Clob;
50
import java.sql.Timestamp;
52
import java.sql.Timestamp;
51
import java.text.SimpleDateFormat;
53
import java.text.SimpleDateFormat;
52
import java.util.ArrayList;
54
import java.util.ArrayList;
Lines 158-164 Link Here
158
        setDefaultEditor(Object.class, new StringTableCellEditor(txtFld));
160
        setDefaultEditor(Object.class, new StringTableCellEditor(txtFld));
159
        setDefaultEditor(String.class, new StringTableCellEditor(txtFld));
161
        setDefaultEditor(String.class, new StringTableCellEditor(txtFld));
160
        setDefaultEditor(java.sql.Time.class, new StringTableCellEditor(txtFld));
162
        setDefaultEditor(java.sql.Time.class, new StringTableCellEditor(txtFld));
161
163
        setDefaultEditor(Blob.class, new BlobFieldTableCellEditor());
164
        setDefaultEditor(Clob.class, new ClobFieldTableCellEditor());
165
        
162
        JTextField numFld = new JTextField();
166
        JTextField numFld = new JTextField();
163
        txtFld.addKeyListener(kl);
167
        txtFld.addKeyListener(kl);
164
        setDefaultEditor(Number.class, new NumberFieldEditor(numFld));
168
        setDefaultEditor(Number.class, new NumberFieldEditor(numFld));
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetTableCellEditor.java (-1 / +388 lines)
Lines 39-74 Link Here
39
 * 
39
 * 
40
 * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
40
 * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
41
 */
41
 */
42
43
package org.netbeans.modules.db.dataview.table;
42
package org.netbeans.modules.db.dataview.table;
44
43
45
import java.awt.BorderLayout;
44
import java.awt.BorderLayout;
46
import java.awt.Component;
45
import java.awt.Component;
47
import java.awt.Dimension;
46
import java.awt.Dimension;
47
import java.awt.Font;
48
import java.awt.Insets;
48
import java.awt.Insets;
49
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
50
import java.awt.event.ActionListener;
51
import java.awt.event.KeyEvent;
51
import java.awt.event.KeyEvent;
52
import java.awt.event.KeyListener;
52
import java.awt.event.KeyListener;
53
import java.awt.event.MouseEvent;
53
import java.awt.event.MouseEvent;
54
import java.io.File;
55
import java.io.FileInputStream;
56
import java.io.FileOutputStream;
57
import java.io.IOException;
58
import java.io.InputStream;
59
import java.io.InputStreamReader;
60
import java.io.OutputStreamWriter;
61
import java.io.Reader;
62
import java.io.Writer;
63
import java.nio.charset.Charset;
64
import java.sql.Blob;
65
import java.sql.Clob;
66
import java.sql.SQLException;
54
import java.sql.Timestamp;
67
import java.sql.Timestamp;
55
import java.text.DateFormat;
68
import java.text.DateFormat;
56
import java.text.ParseException;
69
import java.text.ParseException;
57
import java.text.SimpleDateFormat;
70
import java.text.SimpleDateFormat;
71
import java.util.ArrayList;
72
import java.util.Collections;
73
import java.util.Comparator;
58
import java.util.EventObject;
74
import java.util.EventObject;
75
import java.util.List;
59
import javax.swing.AbstractCellEditor;
76
import javax.swing.AbstractCellEditor;
60
import javax.swing.Action;
77
import javax.swing.Action;
61
import javax.swing.ActionMap;
78
import javax.swing.ActionMap;
62
import javax.swing.BorderFactory;
79
import javax.swing.BorderFactory;
63
import javax.swing.DefaultCellEditor;
80
import javax.swing.DefaultCellEditor;
81
import javax.swing.DefaultComboBoxModel;
64
import javax.swing.InputMap;
82
import javax.swing.InputMap;
83
import javax.swing.JButton;
84
import javax.swing.JComboBox;
65
import javax.swing.JComponent;
85
import javax.swing.JComponent;
86
import javax.swing.JFileChooser;
87
import javax.swing.JMenuItem;
66
import javax.swing.JOptionPane;
88
import javax.swing.JOptionPane;
89
import javax.swing.JPanel;
90
import javax.swing.JPopupMenu;
67
import javax.swing.JScrollPane;
91
import javax.swing.JScrollPane;
68
import javax.swing.JTable;
92
import javax.swing.JTable;
69
import javax.swing.JTextArea;
93
import javax.swing.JTextArea;
70
import javax.swing.JTextField;
94
import javax.swing.JTextField;
71
import javax.swing.KeyStroke;
95
import javax.swing.KeyStroke;
96
import javax.swing.SwingConstants;
72
import javax.swing.SwingUtilities;
97
import javax.swing.SwingUtilities;
73
import javax.swing.UIManager;
98
import javax.swing.UIManager;
74
import javax.swing.table.TableCellEditor;
99
import javax.swing.table.TableCellEditor;
Lines 77-87 Link Here
77
import org.jdesktop.swingx.JXPanel;
102
import org.jdesktop.swingx.JXPanel;
78
import org.jdesktop.swingx.renderer.JRendererCheckBox;
103
import org.jdesktop.swingx.renderer.JRendererCheckBox;
79
import org.netbeans.modules.db.dataview.meta.DBColumn;
104
import org.netbeans.modules.db.dataview.meta.DBColumn;
105
import org.netbeans.modules.db.dataview.util.FileBackedBlob;
106
import org.netbeans.modules.db.dataview.util.FileBackedClob;
80
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper;
107
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper;
81
import org.netbeans.modules.db.dataview.util.DataViewUtils;
108
import org.netbeans.modules.db.dataview.util.DataViewUtils;
82
import org.netbeans.modules.db.dataview.util.JXDateTimePicker;
109
import org.netbeans.modules.db.dataview.util.JXDateTimePicker;
83
import org.netbeans.modules.db.dataview.util.TimestampType;
110
import org.netbeans.modules.db.dataview.util.TimestampType;
84
import org.openide.awt.StatusDisplayer;
111
import org.openide.awt.StatusDisplayer;
112
import org.openide.util.Exceptions;
113
import org.openide.util.NbBundle;
85
import org.openide.windows.WindowManager;
114
import org.openide.windows.WindowManager;
86
115
87
/**
116
/**
Lines 486-489 Link Here
486
    }
515
    }
487
}
516
}
488
517
518
class BlobFieldTableCellEditor extends AbstractCellEditor
519
        implements TableCellEditor,
520
        ActionListener {
489
521
522
    protected static final String EDIT = "edit";
523
    protected Blob currentValue;
524
    protected JButton button;
525
    protected JPopupMenu popup;
526
    protected JTable table;
527
528
    public BlobFieldTableCellEditor() {
529
        button = new JButton();
530
        button.setActionCommand(EDIT);
531
        button.addActionListener(this);
532
        button.setContentAreaFilled(false);
533
        button.setOpaque(false);
534
        button.setBorderPainted(false);
535
        button.setRolloverEnabled(false);
536
        button.setAlignmentX(0);
537
        button.setHorizontalAlignment(SwingConstants.LEFT);
538
        button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9));
539
540
        popup = new JPopupMenu();
541
        final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title"));
542
        miLobSaveAction.addActionListener(new ActionListener() {
543
544
            @Override
545
            public void actionPerformed(ActionEvent e) {
546
                saveLobToFile(currentValue);
547
                fireEditingCanceled();
548
            }
549
        });
550
        popup.add(miLobSaveAction);
551
        final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title"));
552
        miLobLoadAction.addActionListener(new ActionListener() {
553
554
            @Override
555
            public void actionPerformed(ActionEvent e) {
556
                Object newValue = loadLobFromFile();
557
                if (newValue != null) {
558
                    currentValue = (Blob) newValue;
559
                }
560
                fireEditingStopped();
561
            }
562
        });
563
        popup.add(miLobLoadAction);
564
        final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title"));
565
        miLobNullAction.addActionListener(new ActionListener() {
566
567
            @Override
568
            public void actionPerformed(ActionEvent e) {
569
                currentValue = null;
570
                fireEditingStopped();
571
            }
572
        });
573
        popup.add(miLobNullAction);
574
575
    }
576
577
    public void actionPerformed(ActionEvent e) {
578
        if (EDIT.equals(e.getActionCommand())) {
579
            popup.show(button, 0, button.getHeight());
580
        }
581
    }
582
583
    @Override
584
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
585
        currentValue = (java.sql.Blob) value;
586
        if (currentValue != null) {
587
            try {
588
                long size = currentValue.length();
589
                StringBuilder stringValue = new StringBuilder();
590
                stringValue.append("<BLOB ");
591
                if (size < 1000) {
592
                    stringValue.append(String.format("%1$d bytes", size));
593
                } else if (size < 1000000) {
594
                    stringValue.append(String.format("%1$d kB", size / 1000));
595
                } else {
596
                    stringValue.append(String.format("%1$d MB", size / 1000000));
597
                }
598
                stringValue.append(">");
599
                button.setText(stringValue.toString());
600
            } catch (SQLException ex) {
601
                button.setText("<BLOB of unknown size>");
602
            }
603
        } else {
604
            button.setText("<NULL>");
605
        }
606
        this.table = table;
607
        return button;
608
    }
609
610
    @Override
611
    public Object getCellEditorValue() {
612
        return currentValue;
613
    }
614
615
    @Override
616
    public boolean isCellEditable(EventObject anEvent) {
617
        if (anEvent instanceof MouseEvent) {
618
            return ((MouseEvent) anEvent).getClickCount() >= 2;
619
        }
620
        return super.isCellEditable(anEvent);
621
    }
622
623
    private void saveLobToFile(Blob b) {
624
        JFileChooser c = new JFileChooser();
625
        int fileDialogState = c.showSaveDialog(table);
626
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
627
            File f = c.getSelectedFile();
628
            InputStream is = null;
629
            FileOutputStream fos = null;
630
            try {
631
                is = b.getBinaryStream();
632
                fos = new FileOutputStream(f);
633
                int read = 0;
634
                byte[] buffer = new byte[1024];
635
                while((read = is.read(buffer)) > 0) {
636
                    fos.write(buffer, 0, read);
637
                }
638
            } catch (IOException ex) {
639
                throw new RuntimeException(ex);
640
            } catch (SQLException ex) {
641
                throw new RuntimeException(ex);
642
            } finally {
643
                try {
644
                    if(fos != null) fos.close();
645
                } catch (IOException ex) {
646
                    Exceptions.printStackTrace(ex);
647
                }
648
                try {
649
                    if(is != null) is.close();
650
                } catch (IOException ex) {
651
                    Exceptions.printStackTrace(ex);
652
                }
653
            }
654
        }
655
    }
656
    
657
    private Blob loadLobFromFile() {
658
        JFileChooser c = new JFileChooser();
659
        Blob result = null;
660
        int fileDialogState = c.showOpenDialog(table);
661
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
662
            File f = c.getSelectedFile();
663
            FileInputStream fis = null;
664
            try {
665
                fis = new FileInputStream(f);
666
                result = new FileBackedBlob(fis);
667
            } catch (IOException ex) {
668
                throw new RuntimeException(ex);
669
            } catch (SQLException ex) {
670
                throw new RuntimeException(ex);
671
            } finally {
672
                try {
673
                    if(fis != null) fis.close();
674
                } catch (IOException ex) {
675
                    Exceptions.printStackTrace(ex);
676
                }
677
            }
678
        }
679
        return result;
680
    }
681
}
682
683
class ClobFieldTableCellEditor extends AbstractCellEditor
684
        implements TableCellEditor,
685
        ActionListener {
686
687
    private class CharsetSelector extends JPanel {
688
        private JComboBox charsetSelect;
689
        
690
        CharsetSelector() {
691
            List<Charset> charset = new ArrayList<Charset>(Charset.availableCharsets().values());
692
            Collections.sort(charset, new Comparator<Charset>() {
693
                @Override
694
                public int compare(Charset o1, Charset o2) {
695
                    return o1.displayName().compareTo(o2.displayName());
696
                }
697
            });
698
            charsetSelect = new JComboBox();
699
            charsetSelect.setModel(new DefaultComboBoxModel(charset.toArray()));
700
            charsetSelect.setSelectedItem(Charset.defaultCharset());
701
            this.add(charsetSelect);
702
        }
703
704
        public Charset getSelectedCharset() {
705
            return (Charset) charsetSelect.getSelectedItem();
706
        }
707
708
        public void setSelectedCharset(Charset selectedCharset) {
709
            charsetSelect.setSelectedItem(selectedCharset);
710
        }
711
    }
712
    
713
    protected static final String EDIT = "edit";
714
    protected Clob currentValue;
715
    protected JButton button;
716
    protected JPopupMenu popup;
717
    protected JTable table;
718
719
    public ClobFieldTableCellEditor() {
720
        button = new JButton();
721
        button.setActionCommand(EDIT);
722
        button.addActionListener(this);
723
        button.setContentAreaFilled(false);
724
        button.setOpaque(false);
725
        button.setBorderPainted(false);
726
        button.setRolloverEnabled(false);
727
        button.setAlignmentX(0);
728
        button.setHorizontalAlignment(SwingConstants.LEFT);
729
        button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9));
730
731
        popup = new JPopupMenu();
732
        final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title"));
733
        miLobSaveAction.addActionListener(new ActionListener() {
734
735
            @Override
736
            public void actionPerformed(ActionEvent e) {
737
                saveLobToFile(currentValue);
738
                fireEditingCanceled();
739
            }
740
        });
741
        popup.add(miLobSaveAction);
742
        final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title"));
743
        miLobLoadAction.addActionListener(new ActionListener() {
744
745
            @Override
746
            public void actionPerformed(ActionEvent e) {
747
                Object newValue = loadLobFromFile();
748
                if (newValue != null) {
749
                    currentValue = (Clob) newValue;
750
                }
751
                fireEditingStopped();
752
            }
753
        });
754
        popup.add(miLobLoadAction);
755
        final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title"));
756
        miLobNullAction.addActionListener(new ActionListener() {
757
758
            @Override
759
            public void actionPerformed(ActionEvent e) {
760
                currentValue = null;
761
                fireEditingStopped();
762
            }
763
        });
764
        popup.add(miLobNullAction);
765
766
    }
767
768
    public void actionPerformed(ActionEvent e) {
769
        if (EDIT.equals(e.getActionCommand())) {
770
            popup.show(button, 0, button.getHeight());
771
        }
772
    }
773
774
    @Override
775
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
776
        currentValue = (java.sql.Clob) value;
777
        if (currentValue != null) {
778
            try {
779
                long size = currentValue.length();
780
                StringBuilder stringValue = new StringBuilder();
781
                stringValue.append("<CLOB ");
782
                if (size < 1000) {
783
                    stringValue.append(String.format("%1$d Chars", size));
784
                } else if (size < 1000000) {
785
                    stringValue.append(String.format("%1$d kChars", size / 1000));
786
                } else {
787
                    stringValue.append(String.format("%1$d MChars", size / 1000000));
788
                }
789
                stringValue.append(">");
790
                button.setText(stringValue.toString());
791
            } catch (SQLException ex) {
792
                button.setText("<CLOB of unknown size>");
793
            }
794
        } else {
795
            button.setText("<NULL>");
796
        }
797
        this.table = table;
798
        return button;
799
    }
800
801
    @Override
802
    public Object getCellEditorValue() {
803
        return currentValue;
804
    }
805
806
    @Override
807
    public boolean isCellEditable(EventObject anEvent) {
808
        if (anEvent instanceof MouseEvent) {
809
            return ((MouseEvent) anEvent).getClickCount() >= 2;
810
        }
811
        return super.isCellEditable(anEvent);
812
    }
813
    
814
    private void saveLobToFile(Clob b) {
815
        CharsetSelector charset = new CharsetSelector();
816
        JFileChooser c = new JFileChooser();
817
        c.setAccessory(charset);
818
        int fileDialogState = c.showSaveDialog(table);
819
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
820
            File f = c.getSelectedFile();
821
            Reader r = null;
822
            Writer w = null;
823
            try {
824
                r = b.getCharacterStream();
825
                w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset());
826
                int read = 0;
827
                char[] buffer = new char[1024];
828
                while((read = r.read(buffer)) > 0) {
829
                    w.write(buffer, 0, read);
830
                }
831
            } catch (IOException ex) {
832
                throw new RuntimeException(ex);
833
            } catch (SQLException ex) {
834
                throw new RuntimeException(ex);
835
            } finally {
836
                try {
837
                    if(w != null) w.close();
838
                } catch (IOException ex) {
839
                    Exceptions.printStackTrace(ex);
840
                }
841
                try {
842
                    if(r != null) r.close();
843
                } catch (IOException ex) {
844
                    Exceptions.printStackTrace(ex);
845
                }
846
            }
847
        }
848
    }
849
    
850
    private Clob loadLobFromFile() {
851
        CharsetSelector charset = new CharsetSelector();
852
        JFileChooser c = new JFileChooser();
853
        c.setAccessory(charset);
854
        Clob result = null;
855
        int fileDialogState = c.showOpenDialog(table);
856
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
857
            File f = c.getSelectedFile();
858
            Reader r = null;
859
            try {
860
                r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset());
861
                result = new FileBackedClob(r);
862
            } catch (IOException ex) {
863
                throw new RuntimeException(ex);
864
            } catch (SQLException ex) {
865
                throw new RuntimeException(ex);
866
            } finally {
867
                try {
868
                    if(r != null) r.close();
869
                } catch (IOException ex) {
870
                    Exceptions.printStackTrace(ex);
871
                }
872
            }
873
        }
874
        return result;
875
    }
876
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetTableModel.java (-2 / +7 lines)
Lines 43-48 Link Here
43
 */
43
 */
44
package org.netbeans.modules.db.dataview.table;
44
package org.netbeans.modules.db.dataview.table;
45
45
46
import java.sql.Blob;
47
import java.sql.Clob;
46
import java.sql.Date;
48
import java.sql.Date;
47
import java.sql.Time;
49
import java.sql.Time;
48
import java.sql.Timestamp;
50
import java.sql.Timestamp;
Lines 92-100 Link Here
92
94
93
            case Types.CHAR:
95
            case Types.CHAR:
94
            case Types.VARCHAR:
96
            case Types.VARCHAR:
95
            case Types.LONGVARCHAR:
96
            case -15:
97
            case -15:
97
            case -16:
98
            case -9:
98
            case -9:
99
            case -8:
99
            case -8:
100
                return String.class;
100
                return String.class;
Lines 103-110 Link Here
103
            case Types.BINARY:
103
            case Types.BINARY:
104
            case Types.VARBINARY:
104
            case Types.VARBINARY:
105
            case Types.LONGVARBINARY:
105
            case Types.LONGVARBINARY:
106
            case Types.BLOB:
107
                return Blob.class;
108
            case Types.LONGVARCHAR:
109
            case -16:
106
            case Types.CLOB:
110
            case Types.CLOB:
107
            case 2011: /*NCLOB */
111
            case 2011: /*NCLOB */
112
                return Clob.class;
108
            case Types.OTHER:
113
            case Types.OTHER:
109
            default:
114
            default:
110
                return Object.class;
115
                return Object.class;
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/util/DBReadWriteHelper.java (-103 / +100 lines)
Lines 43-50 Link Here
43
 */
43
 */
44
package org.netbeans.modules.db.dataview.util;
44
package org.netbeans.modules.db.dataview.util;
45
45
46
import java.io.ByteArrayInputStream;
46
import java.io.InputStream;
47
import java.io.StringReader;
48
import java.math.BigDecimal;
47
import java.math.BigDecimal;
49
import java.sql.Blob;
48
import java.sql.Blob;
50
import java.sql.Clob;
49
import java.sql.Clob;
Lines 160-184 Link Here
160
                    return bddata;
159
                    return bddata;
161
                }
160
                }
162
            }
161
            }
163
            case Types.INTEGER: 
162
            case Types.INTEGER:
164
            case Types.SMALLINT: 
163
            case Types.SMALLINT:
165
            case Types.TINYINT: {
164
            case Types.TINYINT: {
166
		try {
165
                try {
167
			int idata = rs.getInt(index);
166
                    int idata = rs.getInt(index);
168
			if (rs.wasNull()) {
167
                    if (rs.wasNull()) {
169
			    return null;
168
                        return null;
170
			} else {
169
                    } else {
171
			    return new Integer(idata);
170
                        return new Integer(idata);
172
			}
171
                    }
173
		} catch (java.sql.SQLDataException ex) {
172
                } catch (java.sql.SQLDataException ex) {
174
			long ldata = rs.getLong(index);
173
                    long ldata = rs.getLong(index);
175
			if (rs.wasNull()) {
174
                    if (rs.wasNull()) {
176
			    return null;
175
                        return null;
177
			} else {
176
                    } else {
178
			    return new Long(ldata);
177
                        return new Long(ldata);
179
			}
178
                    }
180
			
179
181
		}
180
                }
182
            }
181
            }
183
            // JDBC/ODBC bridge JDK1.4 brings back -9 for nvarchar columns in
182
            // JDBC/ODBC bridge JDK1.4 brings back -9 for nvarchar columns in
184
            // MS SQL Server tables.
183
            // MS SQL Server tables.
Lines 186-194 Link Here
186
            // JDBC introduced NCHAR(-15), and NVARCHAR (-9), NLONGVARCHAR (-16)
185
            // JDBC introduced NCHAR(-15), and NVARCHAR (-9), NLONGVARCHAR (-16)
187
            case Types.CHAR:
186
            case Types.CHAR:
188
            case Types.VARCHAR:
187
            case Types.VARCHAR:
189
            case Types.LONGVARCHAR:
190
            case -15:
188
            case -15:
191
            case -16:
192
            case -9:
189
            case -9:
193
            case -8: {
190
            case -8: {
194
                String sdata = rs.getString(index);
191
                String sdata = rs.getString(index);
Lines 198-222 Link Here
198
                    return sdata;
195
                    return sdata;
199
                }
196
                }
200
            }
197
            }
201
198
            case Types.BIT: {
202
            case Types.BIT:
199
                byte[] bdata = rs.getBytes(index);
200
                if (rs.wasNull()) {
201
                    return null;
202
                } else {
203
                    Byte[] internal = new Byte[bdata.length];
204
                    for (int i = 0; i < bdata.length; i++) {
205
                        internal[i] = new Byte(bdata[i]);
206
                    }
207
                    String bStr = BinaryToStringConverter.convertToString(internal, BinaryToStringConverter.BINARY, true);
208
                    if (colType == Types.BIT && col.getPrecision() != 0 && col.getPrecision() < bStr.length()) {
209
                        return bStr.substring(bStr.length() - col.getPrecision());
210
                    }
211
                }
212
            }
203
            case Types.BINARY:
213
            case Types.BINARY:
204
            case Types.VARBINARY:
214
            case Types.VARBINARY:
205
            case Types.LONGVARBINARY: {
215
            case Types.LONGVARBINARY:
216
            case Types.BLOB: {
217
                // Try to get a blob object (delay loading till the data is used!)
206
                try {
218
                try {
207
                    byte[] bdata = rs.getBytes(index);
219
                    Blob blob = rs.getBlob(index);
208
                    if (rs.wasNull()) {
220
221
                    Object result = null;
222
                    
223
                    if (! rs.wasNull()) {
224
                        result = new FileBackedBlob(blob.getBinaryStream());
225
                    }
226
                    
227
                    blob.free();
228
                    
229
                    return result;
230
                } catch (SQLException ex) {
231
                    // Ok - can happen - the jdbc driver might not support
232
                    // blob data or can for example not provide a longvarbinary
233
                    // as blob - so fall back to our implementation of blob
234
                }
235
                try {
236
                    InputStream is = rs.getBinaryStream(index);
237
                    if (is == null) {
209
                        return null;
238
                        return null;
210
                    } else {
239
                    } else {
211
                        Byte[] internal = new Byte[bdata.length];
240
                        return new FileBackedBlob(is);
212
                        for (int i = 0; i < bdata.length; i++) {
213
                            internal[i] = new Byte(bdata[i]);
214
                        }
215
                        String bStr = BinaryToStringConverter.convertToString(internal, BinaryToStringConverter.BINARY, true);
216
                        if (colType == Types.BIT && col.getPrecision() != 0 && col.getPrecision() < bStr.length()) {
217
                            return bStr.substring(bStr.length() - col.getPrecision());
218
                        }
219
                        return bStr;
220
                    }
241
                    }
221
                } catch (SQLDataException x) {
242
                } catch (SQLDataException x) {
222
                    // wrong mapping JavaDB JDBC Type -4
243
                    // wrong mapping JavaDB JDBC Type -4
Lines 233-273 Link Here
233
                    }
254
                    }
234
                }
255
                }
235
            }
256
            }
236
            case Types.BLOB: {
257
            case Types.LONGVARCHAR:
237
                // We always get the BLOB, even when we are not reading the contents.
258
            case -16:
238
                // Since the BLOB is just a pointer to the BLOB data rather than the
259
            case Types.CLOB:
239
                // data itself, this operation should not take much time (as opposed
260
            case 2011: /*NCLOB */ {
240
                // to getting all of the data in the blob).
261
                // Try to get a blob object (delay loading till the data is used!)
241
                Blob blob = rs.getBlob(index);
262
                try {
263
                    Clob clob = rs.getClob(index);
242
264
265
                    if (rs.wasNull()) {
266
                        return null;
267
                    } else {
268
                        return new FileBackedClob(clob.getCharacterStream());
269
                    }
270
                } catch (SQLException ex) {
271
                    // Ok - can happen - the jdbc driver might not support
272
                    // clob data or can for example not provide a longvarchar
273
                    // as clob - so fall back to our implementation of clob
274
                }
275
                String sdata = rs.getString(index);
243
                if (rs.wasNull()) {
276
                if (rs.wasNull()) {
244
                    return null;
277
                    return null;
245
                }
278
                } else {
246
                // BLOB exists, so try to read the data from it
279
                    return new FileBackedClob(sdata);
247
                byte[] blobData = null;
248
                if (blob != null) {
249
                    blobData = blob.getBytes(1, Math.min((int) blob.length(), 2000));
250
                }
251
                Byte[] internal = new Byte[blobData.length];
252
                for (int i = 0; i < blobData.length; i++) {
253
                    internal[i] = new Byte(blobData[i]);
254
                }
255
                return BinaryToStringConverter.convertToString(internal, BinaryToStringConverter.HEX, false);
256
            }
257
            case Types.CLOB:
258
            case 2011: /*NCLOB */ {
259
                // We always get the CLOB, even when we are not reading the contents.
260
                // Since the CLOB is just a pointer to the CLOB data rather than the
261
                // data itself, this operation should not take much time (as opposed
262
                // to getting all of the data in the clob).
263
                Clob clob = rs.getClob(index);
264
265
                if (rs.wasNull()) {
266
                    return null;
267
                }
268
                // CLOB exists, so try to read the data from it
269
                if (clob != null) {
270
                    return clob.getSubString(1, Math.min((int) clob.length(), 2000));
271
                }
280
                }
272
            }
281
            }
273
            case Types.OTHER:
282
            case Types.OTHER:
Lines 359-371 Link Here
359
368
360
                case Types.BINARY:
369
                case Types.BINARY:
361
                case Types.VARBINARY:
370
                case Types.VARBINARY:
362
                    ps.setBytes(index, valueObj.toString().getBytes());
363
                    break;
364
365
                case Types.LONGVARBINARY:
371
                case Types.LONGVARBINARY:
366
                case Types.BLOB:
372
                case Types.BLOB:
367
                    byte[] byteval = valueObj.toString().getBytes();
373
                    ps.setBinaryStream(index, ((Blob) valueObj).getBinaryStream());
368
                    ps.setBinaryStream(index, new ByteArrayInputStream(byteval), byteval.length);
369
                    break;
374
                    break;
370
375
371
                case Types.CHAR:
376
                case Types.CHAR:
Lines 373-387 Link Here
373
                case -15:
378
                case -15:
374
                case -9:
379
                case -9:
375
                case -8:
380
                case -8:
376
                case -16:
377
                    ps.setString(index, valueObj.toString());
381
                    ps.setString(index, valueObj.toString());
378
                    break;
382
                    break;
379
383
384
                case Types.LONGVARCHAR:
385
                case -16:
380
                case Types.CLOB:
386
                case Types.CLOB:
381
                case Types.LONGVARCHAR:
382
                case 2011: /*NCLOB */
387
                case 2011: /*NCLOB */
383
                    String charVal = valueObj.toString();
388
                    ps.setCharacterStream(index, ((Clob) valueObj).getCharacterStream());
384
                    ps.setCharacterStream(index, new StringReader(charVal), charVal.length());
385
                    break;
389
                    break;
386
390
387
                default:
391
                default:
Lines 445-480 Link Here
445
                    return valueObj instanceof BigDecimal ? valueObj : new BigDecimal(valueObj.toString());
449
                    return valueObj instanceof BigDecimal ? valueObj : new BigDecimal(valueObj.toString());
446
450
447
                case Types.INTEGER: {
451
                case Types.INTEGER: {
448
                        long ldata = Long.parseLong(valueObj.toString());
452
                    long ldata = Long.parseLong(valueObj.toString());
449
                        if(ldata >= ((long) Integer.MIN_VALUE) && ldata <= ((long) Integer.MAX_VALUE)) {
453
                        if(ldata >= ((long) Integer.MIN_VALUE) && ldata <= ((long) Integer.MAX_VALUE)) {
450
                            return new Integer((int) ldata);
454
                        return new Integer((int) ldata);
451
                        } else if ( ldata < maxUnsignedInt ) {
455
                        } else if ( ldata < maxUnsignedInt ) {
452
                            return new Long(ldata);
456
                        return new Long(ldata);
453
                        } else {
457
                    } else {
454
                            throw new NumberFormatException("Illegal value for java.sql.Type.Integer");
458
                        throw new NumberFormatException("Illegal value for java.sql.Type.Integer");
455
                        }
459
                    }
456
                }
460
                }
457
461
458
                case Types.SMALLINT: {
462
                case Types.SMALLINT: {
459
                        int idata = Integer.parseInt(valueObj.toString());
463
                    int idata = Integer.parseInt(valueObj.toString());
460
                        if(idata >= ((int) Short.MIN_VALUE) && idata <= ((int) Short.MAX_VALUE)) {
464
                        if(idata >= ((int) Short.MIN_VALUE) && idata <= ((int) Short.MAX_VALUE)) {
461
                            return new Short((short) idata);
465
                        return new Short((short) idata);
462
                        } else if ( idata < maxUnsignedShort ) {
466
                        } else if ( idata < maxUnsignedShort ) {
463
                            return new Integer(idata);
467
                        return new Integer(idata);
464
                        } else {
468
                    } else {
465
                            throw new NumberFormatException("Illegal value for java.sql.Type.SMALLINT");
469
                        throw new NumberFormatException("Illegal value for java.sql.Type.SMALLINT");
466
                        }
470
                    }
467
                }
471
                }
468
472
469
                case Types.TINYINT: {
473
                case Types.TINYINT: {
470
                        short sdata = Short.parseShort(valueObj.toString());
474
                    short sdata = Short.parseShort(valueObj.toString());
471
                        if(sdata >= ((short) Byte.MIN_VALUE) && sdata <= ((short) Byte.MAX_VALUE)) {
475
                        if(sdata >= ((short) Byte.MIN_VALUE) && sdata <= ((short) Byte.MAX_VALUE)) {
472
                            return new Byte((byte) sdata);
476
                        return new Byte((byte) sdata);
473
                        } else if ( sdata < maxUnsignedByte ) {
477
                        } else if ( sdata < maxUnsignedByte ) {
474
                            return new Short(sdata);
478
                        return new Short(sdata);
475
                        } else {
479
                    } else {
476
                            throw new NumberFormatException("Illegal value for java.sql.Type.TINYINT");
480
                        throw new NumberFormatException("Illegal value for java.sql.Type.TINYINT");
477
                        }
481
                    }
478
                }
482
                }
479
483
480
                case Types.CHAR:
484
                case Types.CHAR:
Lines 504-518 Link Here
504
                case Types.BINARY:
508
                case Types.BINARY:
505
                case Types.VARBINARY:
509
                case Types.VARBINARY:
506
                case Types.LONGVARBINARY:
510
                case Types.LONGVARBINARY:
511
                case Types.BLOB:
507
                case Types.CLOB:
512
                case Types.CLOB:
508
                case Types.BLOB:
509
                    char[] bytes = valueObj.toString().toCharArray();
510
                    Byte[] internal = new Byte[bytes.length];
511
                    for (int i = 0; i < bytes.length; i++) {
512
                        internal[i] = new Byte((byte) bytes[i]);
513
                    }
514
                    return BinaryToStringConverter.convertToString(internal, BinaryToStringConverter.BINARY, true);
515
516
                case Types.OTHER:
513
                case Types.OTHER:
517
                default:
514
                default:
518
                    return valueObj;
515
                    return valueObj;
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/util/FileBackedBlob.java (+269 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.util;
43
44
import java.io.File;
45
import java.io.FileInputStream;
46
import java.io.FileNotFoundException;
47
import java.io.IOException;
48
import java.io.InputStream;
49
import java.io.OutputStream;
50
import java.io.RandomAccessFile;
51
import java.sql.Blob;
52
import java.sql.SQLException;
53
import org.openide.util.Exceptions;
54
55
/**
56
 * A storage implementing a _subset_ of the Blob Interface backed by a file
57
 * 
58
 * Currently the following function are not implemented:
59
 * - all position functions
60
 * - getBinaryStream(long pos, long length)
61
 * 
62
 * @author mblaesing
63
 */
64
public class FileBackedBlob implements Blob {
65
66
    private boolean freed = false;
67
    private File backingFile;
68
69
    public FileBackedBlob() throws SQLException {
70
        try {
71
            backingFile = File.createTempFile("netbeans-db-blob", null);
72
            backingFile.deleteOnExit();
73
        } catch (IOException ex) {
74
            throw new SQLException(ex);
75
        }
76
    }
77
78
    public FileBackedBlob(InputStream is) throws SQLException {
79
        this();
80
        OutputStream os = null;
81
        try {
82
            os = setBinaryStream(1);
83
            int read = 0;
84
            byte[] buffer = new byte[(int) Math.pow(2, 18)];
85
            while ((read = is.read(buffer)) > 0) {
86
                os.write(buffer, 0, read);
87
            }
88
        } catch (IOException ex) {
89
            throw new SQLException(ex);
90
        } finally {
91
            try {
92
                if(os != null) os.close();
93
            } catch (IOException ex) {
94
                Exceptions.printStackTrace(ex);
95
            }
96
            try {
97
                if(is != null) is.close();
98
            } catch (IOException ex) {
99
                Exceptions.printStackTrace(ex);
100
            }
101
        }
102
    }
103
104
    @Override
105
    public long length() throws SQLException {
106
        checkFreed();
107
        return backingFile.length();
108
    }
109
110
    @Override
111
    public byte[] getBytes(long pos, int length) throws SQLException {
112
        checkFreed();
113
        checkPos(pos);
114
        checkLength(length);
115
        InputStream is = null;
116
        try {
117
            is = new FileInputStream(backingFile);
118
            is.skip(pos - 1);
119
            byte[] result = new byte[length];
120
            is.read(result);
121
            return result;
122
        } catch (IOException ex) {
123
            throw new SQLException(ex);
124
        } finally {
125
            try {
126
                is.close();
127
            } catch (IOException ex) {
128
                Exceptions.printStackTrace(ex);
129
            }
130
        }
131
    }
132
133
    @Override
134
    public InputStream getBinaryStream() throws SQLException {
135
        checkFreed();
136
        try {
137
            return new FileInputStream(backingFile);
138
        } catch (FileNotFoundException ex) {
139
            throw new SQLException(ex);
140
        }
141
    }
142
143
    @Override
144
    public long position(byte[] pattern, long start) throws SQLException {
145
        checkFreed();
146
        checkPos(start);
147
        throw new UnsupportedOperationException("Not supported yet.");
148
    }
149
150
    @Override
151
    public long position(Blob pattern, long start) throws SQLException {
152
        checkFreed();
153
        checkPos(start);
154
        throw new UnsupportedOperationException("Not supported yet.");
155
    }
156
157
    @Override
158
    public int setBytes(long pos, byte[] bytes) throws SQLException {
159
        checkFreed();
160
        checkPos(pos);
161
        return setBytes(pos, bytes, 0, bytes.length);
162
    }
163
164
    @Override
165
    public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
166
        checkFreed();
167
        checkPos(pos);
168
        RandomAccessFile raf = null;
169
        try {
170
            raf = new RandomAccessFile(backingFile, "rw");
171
            raf.seek(pos - 1);
172
            int border = Math.min(bytes.length, offset + len);
173
            int written = 0;
174
            for (int i = offset; i < border; i++) {
175
                raf.write(bytes[i]);
176
                written++;
177
            }
178
            return written;
179
        } catch (IOException ex) {
180
            throw new SQLException(ex);
181
        } finally {
182
            try {
183
                raf.close();
184
            } catch (IOException ex) {
185
                Exceptions.printStackTrace(ex);
186
            }
187
        }
188
189
    }
190
191
    @Override
192
    public OutputStream setBinaryStream(long pos) throws SQLException {
193
        checkFreed();
194
        checkPos(pos);
195
196
        try {
197
            final RandomAccessFile raf = new RandomAccessFile(backingFile, "rw");
198
            try {
199
                raf.seek(pos - 1);
200
            } catch (IOException ex) {
201
                raf.close();
202
                throw new SQLException(ex);
203
            }
204
205
            return new RandomAccessOutputStream(raf);
206
        } catch (IOException ex) {
207
            throw new SQLException(ex);
208
        }
209
    }
210
211
    @Override
212
    public void truncate(long len) throws SQLException {
213
        checkFreed();
214
        RandomAccessFile raf = null;
215
        try {
216
            raf  = new RandomAccessFile(backingFile, "rw");
217
            raf.setLength(len);
218
        } catch (IOException ex) {
219
            throw new SQLException(ex);
220
        } finally {
221
            try {
222
                raf.close();
223
            } catch (IOException ex) {
224
                Exceptions.printStackTrace(ex);
225
            }
226
        }
227
    }
228
229
    @Override
230
    public void free() throws SQLException {
231
        if (!freed) {
232
            backingFile.delete();
233
            freed = true;
234
        }
235
    }
236
237
    @Override
238
    public InputStream getBinaryStream(long pos, long length) throws SQLException {
239
        checkFreed();
240
        throw new UnsupportedOperationException("Not supported yet.");
241
    }
242
243
    private void checkFreed() throws SQLException {
244
        if (freed) {
245
            throw new SQLException("Blob already freed");
246
        }
247
    }
248
249
    private void checkPos(long pos) throws SQLException {
250
        if (pos < 1) {
251
            throw new SQLException("Illegal Value for position: " + Long.toString(pos));
252
        }
253
    }
254
255
    private void checkLength(long length) throws SQLException {
256
        if (length < 0) {
257
            throw new SQLException("Illegal Value for length: " + Long.toString(length));
258
        }
259
    }
260
    
261
    protected void finalize() throws Throwable {
262
        free();
263
        super.finalize();
264
    }
265
    
266
    File getBackingFile() {
267
        return backingFile;
268
    }
269
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/util/FileBackedClob.java (+294 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.util;
43
44
import java.io.File;
45
import java.io.FileInputStream;
46
import java.io.FileNotFoundException;
47
import java.io.FileOutputStream;
48
import java.io.FileReader;
49
import java.io.IOException;
50
import java.io.InputStream;
51
import java.io.InputStreamReader;
52
import java.io.OutputStream;
53
import java.io.OutputStreamWriter;
54
import java.io.RandomAccessFile;
55
import java.io.Reader;
56
import java.io.UnsupportedEncodingException;
57
import java.io.Writer;
58
import java.nio.CharBuffer;
59
import java.sql.Clob;
60
import java.sql.SQLException;
61
import org.openide.util.Exceptions;
62
63
/**
64
 * A storage implementing a _subset_ of the Blob Interface backed by a file
65
 * 
66
 * Currently the following function are not implemented:
67
 * - all position functions
68
 * - getCharacterStream(long pos, long length)
69
 * - setAsciiStream
70
 * - getAsciiStream
71
 * 
72
 * @author mblaesing
73
 */
74
public class FileBackedClob implements Clob {
75
76
    private boolean freed = false;
77
    private File backingFile;
78
79
    public FileBackedClob() throws SQLException {
80
        try {
81
            backingFile = File.createTempFile("netbeans-db-blob", null);
82
            backingFile.deleteOnExit();
83
        } catch (IOException ex) {
84
            throw new SQLException(ex);
85
        }
86
    }
87
88
    public FileBackedClob(String init) throws SQLException {
89
        this();
90
        this.setString(1, init);
91
    }
92
93
    public FileBackedClob(Reader r) throws SQLException {
94
        this();
95
        Writer w = setCharacterStream(1);
96
        int read = 0;
97
        char[] buffer = new char[(int) Math.pow(2, 18)];
98
        try {
99
            while ((read = r.read(buffer)) > 0) {
100
                w.write(buffer, 0, read);
101
            }
102
        } catch (IOException ex) {
103
            throw new SQLException(ex);
104
        } finally {
105
            try {
106
                if (w != null) {
107
                    w.close();
108
                }
109
            } catch (IOException ex) {
110
                Exceptions.printStackTrace(ex);
111
            }
112
            try {
113
                if (r != null) {
114
                    r.close();
115
                }
116
            } catch (IOException ex) {
117
                Exceptions.printStackTrace(ex);
118
            }
119
        }
120
    }
121
122
    @Override
123
    public long length() throws SQLException {
124
        checkFreed();
125
        return backingFile.length() / 4;
126
    }
127
128
    @Override
129
    public void truncate(long len) throws SQLException {
130
        checkFreed();
131
        RandomAccessFile raf = null;
132
        try {
133
            raf = new RandomAccessFile(backingFile, "rw");
134
            raf.setLength(len * 4);
135
        } catch (IOException ex) {
136
            throw new SQLException(ex);
137
        } finally {
138
            try {
139
                raf.close();
140
            } catch (IOException ex) {
141
                Exceptions.printStackTrace(ex);
142
            }
143
        }
144
    }
145
146
    @Override
147
    public void free() throws SQLException {
148
        if (!freed) {
149
            backingFile.delete();
150
            freed = true;
151
        }
152
    }
153
154
    private void checkFreed() throws SQLException {
155
        if (freed) {
156
            throw new SQLException("Blob already freed");
157
        }
158
    }
159
160
    private void checkPos(long pos) throws SQLException {
161
        if (pos < 1) {
162
            throw new SQLException("Illegal Value for position: " + Long.toString(pos));
163
        }
164
    }
165
166
    private void checkLength(long length) throws SQLException {
167
        if (length < 0) {
168
            throw new SQLException("Illegal Value for length: " + Long.toString(length));
169
        }
170
    }
171
172
    @Override
173
    public String getSubString(long pos, int length) throws SQLException {
174
        checkFreed();
175
        checkPos(pos);
176
        checkLength(length);
177
        Reader r = null;
178
        try {
179
            r = new InputStreamReader(new FileInputStream(backingFile), "UTF32");
180
            r.skip(pos - 1);
181
            CharBuffer c = CharBuffer.allocate(length);
182
            r.read(c);
183
            c.rewind();
184
            return c.toString();
185
        } catch (IOException ex) {
186
            throw new SQLException(ex);
187
        } finally {
188
            try {
189
                r.close();
190
            } catch (IOException ex) {
191
                Exceptions.printStackTrace(ex);
192
            }
193
        }
194
195
    }
196
197
    @Override
198
    public Reader getCharacterStream() throws SQLException {
199
        checkFreed();
200
        Reader r;
201
        try {
202
            r = new InputStreamReader(new FileInputStream(backingFile), "UTF32");
203
        } catch (FileNotFoundException ex) {
204
            throw new SQLException(ex);
205
        } catch (UnsupportedEncodingException ex) {
206
            throw new SQLException(ex);
207
        }
208
        return r;
209
    }
210
211
    @Override
212
    public InputStream getAsciiStream() throws SQLException {
213
        throw new UnsupportedOperationException("Not supported yet.");
214
    }
215
216
    @Override
217
    public long position(String searchstr, long start) throws SQLException {
218
        throw new UnsupportedOperationException("Not supported yet.");
219
    }
220
221
    @Override
222
    public long position(Clob searchstr, long start) throws SQLException {
223
        throw new UnsupportedOperationException("Not supported yet.");
224
    }
225
226
    @Override
227
    public int setString(long pos, String str) throws SQLException {
228
        return setString(pos, str, 0, str.length());
229
    }
230
231
    @Override
232
    public int setString(long pos, String str, int offset, int len) throws SQLException {
233
        checkFreed();
234
        checkPos(pos);
235
        Writer w = null;
236
        try {
237
            w = setCharacterStream(pos);
238
            w.write(str.substring(offset, Math.min(offset + len, str.length())));
239
            return len;
240
        } catch (IOException ex) {
241
            throw new SQLException(ex);
242
        } finally {
243
            try {
244
                w.close();
245
            } catch (IOException ex) {
246
                Exceptions.printStackTrace(ex);
247
            }
248
        }
249
    }
250
251
    @Override
252
    public OutputStream setAsciiStream(long pos) throws SQLException {
253
        checkFreed();
254
        checkPos(pos);
255
        throw new UnsupportedOperationException("Not supported yet.");
256
    }
257
258
    @Override
259
    public Writer setCharacterStream(long pos) throws SQLException {
260
        checkFreed();
261
        checkPos(pos);
262
263
        try {
264
            final RandomAccessFile raf = new RandomAccessFile(backingFile, "rw");
265
            try {
266
                raf.seek((pos - 1) * 4);
267
            } catch (IOException ex) {
268
                raf.close();
269
                throw new SQLException(ex);
270
            }
271
272
            return new OutputStreamWriter(new RandomAccessOutputStream(raf), "UTF32");
273
        } catch (IOException ex) {
274
            throw new SQLException(ex);
275
        }
276
    }
277
278
    @Override
279
    public Reader getCharacterStream(long pos, long length) throws SQLException {
280
        checkFreed();
281
        checkPos(pos);
282
        checkLength(length);
283
        throw new UnsupportedOperationException("Not supported yet.");
284
    }
285
286
    protected void finalize() throws Throwable {
287
        free();
288
        super.finalize();
289
    }
290
291
    File getBackingFile() {
292
        return backingFile;
293
    }
294
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/util/RandomAccessInputStream.java (+75 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.util;
43
44
import java.io.IOException;
45
import java.io.InputStream;
46
import java.io.RandomAccessFile;
47
48
class RandomAccessInputStream extends InputStream {
49
50
    private RandomAccessFile raf;
51
52
    public RandomAccessInputStream(RandomAccessFile raf) {
53
        this.raf = raf;
54
    }
55
56
    @Override
57
    public int read(byte[] b) throws IOException {
58
        return raf.read(b);
59
    }
60
61
    @Override
62
    public int read(byte[] b, int off, int len) throws IOException {
63
        return raf.read(b, off, len);
64
    }
65
66
    @Override
67
    public int read() throws IOException {
68
        return raf.read();
69
    }
70
71
    @Override
72
    public void close() throws IOException {
73
        raf.close();
74
    }
75
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/util/RandomAccessOutputStream.java (+70 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.util;
43
44
import java.io.IOException;
45
import java.io.OutputStream;
46
import java.io.RandomAccessFile;
47
48
class RandomAccessOutputStream extends OutputStream {
49
50
    private RandomAccessFile raf;
51
52
    public RandomAccessOutputStream(RandomAccessFile raf) {
53
        this.raf = raf;
54
    }
55
56
    @Override
57
    public void write(byte[] b) throws IOException {
58
        raf.write(b);
59
    }
60
61
    @Override
62
    public void write(int b) throws IOException {
63
        raf.write(b);
64
    }
65
66
    @Override
67
    public void close() throws IOException {
68
        raf.close();
69
    }
70
}
(-)a/db.dataview/test/unit/src/org/netbeans/modules/db/dataview/output/SQLExecutionHelperTest.java (-2 / +2 lines)
Lines 94-100 Link Here
94
        int pageSize = 5;
94
        int pageSize = 5;
95
        DataView dv = DataView.create(dbconn, sqlString, pageSize);
95
        DataView dv = DataView.create(dbconn, sqlString, pageSize);
96
        SQLExecutionHelper execHelper = new SQLExecutionHelper(dv);
96
        SQLExecutionHelper execHelper = new SQLExecutionHelper(dv);
97
        SQLExecutionHelper.initialDataLoad(dv, dbconn, execHelper);
97
        execHelper.initialDataLoad();
98
        assertNotNull(execHelper);
98
        assertNotNull(execHelper);
99
        assertEquals(sqlString, dv.getSQLString());
99
        assertEquals(sqlString, dv.getSQLString());
100
        assertEquals(true, dv.hasResultSet());
100
        assertEquals(true, dv.hasResultSet());
Lines 105-111 Link Here
105
        ResultSet rs = conn.createStatement().executeQuery(context.getSqlSelect());
105
        ResultSet rs = conn.createStatement().executeQuery(context.getSqlSelect());
106
        DataView dv = DataView.create(dbconn, context.getSqlSelect(), pageSize);
106
        DataView dv = DataView.create(dbconn, context.getSqlSelect(), pageSize);
107
        SQLExecutionHelper instance = dv.getSQLExecutionHelper();
107
        SQLExecutionHelper instance = dv.getSQLExecutionHelper();
108
        instance.loadDataFrom(rs);
108
        instance.initialDataLoad();
109
        assertNotNull(instance);
109
        assertNotNull(instance);
110
    }
110
    }
111
}
111
}
(-)a/db.dataview/test/unit/src/org/netbeans/modules/db/dataview/util/FileBackedBlobTest.java (+292 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.util;
43
44
import java.io.ByteArrayInputStream;
45
import java.io.IOException;
46
import java.io.InputStream;
47
import java.io.OutputStream;
48
import org.junit.After;
49
import org.junit.AfterClass;
50
import org.junit.Before;
51
import org.junit.BeforeClass;
52
import org.junit.Test;
53
import static org.junit.Assert.*;
54
import org.openide.util.Exceptions;
55
56
/**
57
 *
58
 * @author mblaesing
59
 */
60
public class FileBackedBlobTest {
61
62
    private static byte[] testCase1;
63
    private static byte[] testCase2;
64
    private static byte[] testCase3;
65
66
    private static void assertStreamEquals(InputStream is1, InputStream is2) {
67
        try {
68
            long position = 0;
69
            while (true) {
70
                int input1 = is1.read();
71
                int input2 = is2.read();
72
                if (input1 != input2) {
73
                    throw new AssertionError("Streams differ at position: " + Long.toString(position));
74
                }
75
                if (input1 == -1 || input2 == -1) {
76
                    return;
77
                }
78
                position++;
79
            }
80
        } catch (IOException ex) {
81
            throw new AssertionError(ex);
82
        } finally {
83
            try {
84
                is1.close();
85
            } catch (IOException ex) {
86
                Exceptions.printStackTrace(ex);
87
            }
88
            try {
89
                is2.close();
90
            } catch (IOException ex) {
91
                Exceptions.printStackTrace(ex);
92
            }
93
        }
94
    }
95
96
    public FileBackedBlobTest() {
97
    }
98
99
    @BeforeClass
100
    public static void setUpClass() throws Exception {
101
        byte[] testPattern = "TestCase".getBytes();
102
        int testLength = testPattern.length;
103
        testCase1 = new byte[10];
104
        testCase2 = new byte[1024];
105
        testCase3 = new byte[1024 * 1024];
106
        for (int i = 0; i < testCase1.length; i++) {
107
            testCase1[i] = testPattern[ i % testLength];
108
        }
109
        for (int i = 0; i < testCase2.length; i++) {
110
            testCase2[i] = testPattern[ i % testLength];
111
        }
112
        for (int i = 0; i < testCase3.length; i++) {
113
            testCase3[i] = testPattern[ i % testLength];
114
        }
115
    }
116
117
    @AfterClass
118
    public static void tearDownClass() throws Exception {
119
        testCase1 = null;
120
        testCase2 = null;
121
        testCase3 = null;
122
    }
123
124
    @Before
125
    public void setUp() {
126
    }
127
128
    @After
129
    public void tearDown() {
130
    }
131
132
    @Test
133
    public void testLength() throws Exception {
134
        FileBackedBlob b;
135
        b = new FileBackedBlob(new ByteArrayInputStream(testCase1));
136
        assertEquals(b.length(), testCase1.length);
137
        b.free();
138
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
139
        assertEquals(b.length(), testCase2.length);
140
        b.free();
141
        b = new FileBackedBlob(new ByteArrayInputStream(testCase3));
142
        assertEquals(b.length(), testCase3.length);
143
        b.free();
144
    }
145
146
    @Test
147
    public void testGetBytes() throws Exception {
148
        FileBackedBlob b;
149
        // Each case is tested with the complete array and then with den range
150
        // as index: 5.-9.
151
        byte[] shortReference = new byte[5];
152
        System.arraycopy(testCase1, 5, shortReference, 0, 5);
153
154
        b = new FileBackedBlob(new ByteArrayInputStream(testCase1));
155
        assertArrayEquals(b.getBytes(1, (int) b.length()), testCase1);
156
        assertArrayEquals(b.getBytes(6, 5), shortReference);
157
        b.free();
158
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
159
        assertArrayEquals(b.getBytes(1, (int) b.length()), testCase2);
160
        assertArrayEquals(b.getBytes(6, 5), shortReference);
161
        b.free();
162
        b = new FileBackedBlob(new ByteArrayInputStream(testCase3));
163
        assertArrayEquals(b.getBytes(1, (int) b.length()), testCase3);
164
        assertArrayEquals(b.getBytes(6, 5), shortReference);
165
        b.free();
166
    }
167
168
    @Test
169
    public void testGetBinaryStream_0args() throws Exception {
170
        FileBackedBlob b;
171
        b = new FileBackedBlob(new ByteArrayInputStream(testCase1));
172
        assertStreamEquals(b.getBinaryStream(), new ByteArrayInputStream(testCase1));
173
        b.free();
174
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
175
        assertStreamEquals(b.getBinaryStream(), new ByteArrayInputStream(testCase2));
176
        b.free();
177
        b = new FileBackedBlob(new ByteArrayInputStream(testCase3));
178
        assertStreamEquals(b.getBinaryStream(), new ByteArrayInputStream(testCase3));
179
        b.free();
180
    }
181
182
    @Test
183
    public void testSetBytes_long_byteArr() throws Exception {
184
        FileBackedBlob b;
185
        byte[] test1 = "test".getBytes("ASCII");
186
        byte[] test2 = "test0123456789".getBytes("ASCII");
187
        byte[] firstPartReference = new byte[testCase2.length - test1.length - 4];
188
        byte[] secondPartReference = new byte[4];
189
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
190
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
191
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
192
        b.setBytes(testCase2.length - test1.length - 4 + 1, test1);
193
        assertArrayEquals(firstPartReference, b.getBytes(1, testCase2.length - test1.length - 4));
194
        assertArrayEquals(secondPartReference, b.getBytes(testCase2.length - 4, 4));
195
        assertArrayEquals(test1, b.getBytes(testCase2.length - 4 - test1.length + 1, test1.length));
196
        assertEquals(b.length(), 1024);
197
        b.setBytes(testCase2.length - test1.length - 4 + 1, test2);
198
        assertArrayEquals(firstPartReference, b.getBytes(1, testCase2.length - test1.length - 4));
199
        assertEquals(b.length(), 1024 - test1.length - 4 + test2.length);
200
        assertArrayEquals(test2, b.getBytes(b.length() - test2.length + 1, test2.length));
201
        b.free();
202
    }
203
204
    @Test
205
    public void testSetBytes_4args() throws Exception {
206
        FileBackedBlob b;
207
        byte[] test1 = "test".getBytes("ASCII");
208
        byte[] test2 = "01test23456789".getBytes("ASCII");
209
        byte[] firstPartReference = new byte[testCase2.length - test1.length - 4];
210
        byte[] secondPartReference = new byte[4];
211
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
212
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
213
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
214
        b.setBytes(testCase2.length - test1.length - 4 + 1, test2, 2, 4);
215
        assertArrayEquals(firstPartReference, b.getBytes(1, testCase2.length - test1.length - 4));
216
        assertArrayEquals(secondPartReference, b.getBytes(testCase2.length - 4, 4));
217
        assertArrayEquals(test1, b.getBytes(testCase2.length - 4 - test1.length + 1, test1.length));
218
        assertEquals(b.length(), 1024);
219
        b.setBytes(testCase2.length - test1.length - 4 + 1, test2);
220
        assertArrayEquals(firstPartReference, b.getBytes(1, testCase2.length - test1.length - 4));
221
        assertEquals(b.length(), 1024 - test1.length - 4 + test2.length);
222
        assertArrayEquals(test2, b.getBytes(b.length() - test2.length + 1, test2.length));
223
        b.free();
224
    }
225
226
    @Test
227
    public void testSetBinaryStream() throws Exception {
228
        FileBackedBlob b;
229
        byte[] test1 = "test".getBytes("ASCII");
230
        byte[] test2 = "test0123456789".getBytes("ASCII");
231
        byte[] firstPartReference = new byte[testCase2.length - test1.length - 4];
232
        byte[] secondPartReference = new byte[4];
233
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
234
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
235
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
236
        OutputStream os = b.setBinaryStream(testCase2.length - test1.length - 4 + 1);
237
        os.write(test1);
238
        os.close();
239
        assertArrayEquals(firstPartReference, b.getBytes(1, testCase2.length - test1.length - 4));
240
        assertArrayEquals(secondPartReference, b.getBytes(testCase2.length - 4, 4));
241
        assertArrayEquals(test1, b.getBytes(testCase2.length - 4 - test1.length + 1, test1.length));
242
        assertEquals(b.length(), 1024);
243
        os = b.setBinaryStream(testCase2.length - test1.length - 4 + 1);
244
        os.write(test2);
245
        os.close();
246
        assertArrayEquals(firstPartReference, b.getBytes(1, testCase2.length - test1.length - 4));
247
        assertEquals(b.length(), 1024 - test1.length - 4 + test2.length);
248
        assertArrayEquals(test2, b.getBytes(b.length() - test2.length + 1, test2.length));
249
        b.free();
250
    }
251
252
    @Test
253
    public void testTruncate() throws Exception {
254
        FileBackedBlob b;
255
        byte[] reference;
256
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
257
        assertEquals(b.length(), testCase2.length);
258
        b.truncate(1024);
259
        assertEquals(b.length(), 1024);
260
        reference = new byte[1024];
261
        System.arraycopy(testCase2, 0, reference, 0, 1024);
262
        assertStreamEquals(b.getBinaryStream(), new ByteArrayInputStream(reference));
263
        b.truncate(10);
264
        assertEquals(b.length(), 10);
265
        reference = new byte[10];
266
        System.arraycopy(testCase2, 0, reference, 0, 10);
267
        assertStreamEquals(b.getBinaryStream(), new ByteArrayInputStream(reference));
268
        b.free();
269
    }
270
271
    @Test
272
    public void testFree() throws Exception {
273
        FileBackedBlob b;
274
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
275
        assertTrue(b.getBackingFile().exists());
276
        b.free();
277
        assertFalse(b.getBackingFile().exists());
278
    }
279
280
    @Test
281
    public void testFinalize() throws Exception {
282
        FileBackedBlob b;
283
        b = new FileBackedBlob(new ByteArrayInputStream(testCase2));
284
        assertTrue(b.getBackingFile().exists());
285
        try {
286
            b.finalize();
287
        } catch (Throwable ex) {
288
            throw new AssertionError(ex);
289
        }
290
        assertFalse(b.getBackingFile().exists());
291
    }
292
}
(-)a/db.dataview/test/unit/src/org/netbeans/modules/db/dataview/util/FileBackedClobTest.java (+287 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.util;
43
44
import java.io.CharArrayReader;
45
import java.io.IOException;
46
import org.openide.util.Exceptions;
47
import java.io.Reader;
48
import java.io.Writer;
49
import org.junit.After;
50
import org.junit.AfterClass;
51
import org.junit.Before;
52
import org.junit.BeforeClass;
53
import org.junit.Test;
54
import static org.junit.Assert.*;
55
56
/**
57
 *
58
 * @author mblaesing
59
 */
60
public class FileBackedClobTest {
61
    private static char[] testCase1;
62
    private static char[] testCase2;
63
    private static char[] testCase3;
64
65
    private static void assertCharacterStreamEquals(Reader is1, Reader is2) {
66
        try {
67
            long position = 0;
68
            while (true) {
69
                int input1 = is1.read();
70
                int input2 = is2.read();
71
                if (input1 != input2) {
72
                    throw new AssertionError("Reader differ at position: " + Long.toString(position));
73
                }
74
                if (input1 == -1 || input2 == -1) {
75
                    return;
76
                }
77
                position++;
78
            }
79
        } catch (IOException ex) {
80
            throw new AssertionError(ex);
81
        } finally {
82
            try {
83
                is1.close();
84
            } catch (IOException ex) {
85
                Exceptions.printStackTrace(ex);
86
            }
87
            try {
88
                is2.close();
89
            } catch (IOException ex) {
90
                Exceptions.printStackTrace(ex);
91
            }
92
        }
93
    }
94
95
    @BeforeClass
96
    public static void setUpClass() throws Exception {
97
        char[] testPattern = "Testäöüß".toCharArray();
98
        int testLength = testPattern.length;
99
        testCase1 = new char[10];
100
        testCase2 = new char[1024];
101
        testCase3 = new char[1024 * 1024];
102
        for (int i = 0; i < testCase1.length; i++) {
103
            testCase1[i] = testPattern[ i % testLength];
104
        }
105
        for (int i = 0; i < testCase2.length; i++) {
106
            testCase2[i] = testPattern[ i % testLength];
107
        }
108
        for (int i = 0; i < testCase3.length; i++) {
109
            testCase3[i] = testPattern[ i % testLength];
110
        }
111
    }
112
113
    
114
    public FileBackedClobTest() {
115
    }
116
117
    @AfterClass
118
    public static void tearDownClass() throws Exception {
119
        testCase1 = null;
120
        testCase2 = null;
121
        testCase3 = null;
122
    }
123
    
124
    @Before
125
    public void setUp() {
126
    }
127
    
128
    @After
129
    public void tearDown() {
130
    }
131
132
    @Test
133
    public void testLength() throws Exception {
134
        FileBackedClob c;
135
        c = new FileBackedClob(new CharArrayReader(testCase1));
136
        assertEquals(c.length(), testCase1.length);
137
        assertEquals(c.getBackingFile().length(), testCase1.length * 4);
138
        c.free();
139
        c = new FileBackedClob(new String(testCase2));
140
        assertEquals(c.length(), testCase2.length);
141
        assertEquals(c.getBackingFile().length(), testCase2.length * 4);
142
        c.free();
143
        c = new FileBackedClob(new CharArrayReader(testCase3));
144
        assertEquals(c.length(), testCase3.length);
145
        assertEquals(c.getBackingFile().length(), testCase3.length * 4);
146
        c.free();
147
    }
148
149
    @Test
150
    public void testTruncate() throws Exception {
151
        FileBackedClob c;
152
        c = new FileBackedClob(new CharArrayReader(testCase1));
153
        c.truncate(5);
154
        assertEquals(c.length(), 5);
155
        assertEquals(c.getBackingFile().length(), 5 * 4);
156
        c.free();
157
        c = new FileBackedClob(new String(testCase2));
158
        c.truncate(42);
159
        assertEquals(c.length(), 42);
160
        assertEquals(c.getBackingFile().length(), 42 * 4);
161
        c.free();
162
        c = new FileBackedClob(new CharArrayReader(testCase3));
163
        c.truncate(1024);
164
        assertEquals(c.length(), 1024);
165
        assertEquals(c.getBackingFile().length(), 1024 * 4);
166
        c.free();
167
    }
168
169
    @Test
170
    public void testGetSubString() throws Exception {
171
        FileBackedClob c;
172
        char[] referenceChars = new char[5];
173
        System.arraycopy(testCase1, 5, referenceChars, 0, 5);
174
        String reference = new String(referenceChars);
175
        c = new FileBackedClob(new CharArrayReader(testCase1));
176
        assertEquals(reference, c.getSubString(6, 5));
177
        c.free();
178
        c = new FileBackedClob(new String(testCase2));
179
        assertEquals( reference, c.getSubString(6, 5));
180
        c.free();
181
        c = new FileBackedClob(new CharArrayReader(testCase3));
182
        assertEquals(reference, c.getSubString(6, 5));
183
        c.free();
184
    }
185
186
    @Test
187
    public void testGetCharacterStream_0args() throws Exception {
188
        FileBackedClob c;
189
        c = new FileBackedClob(new CharArrayReader(testCase1));
190
        assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase1));
191
        c.free();
192
        c = new FileBackedClob(new String(testCase2));
193
        assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase2));
194
        c.free();
195
        c = new FileBackedClob(new CharArrayReader(testCase3));
196
        assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase3));
197
        c.free();
198
    }
199
200
    @Test
201
    public void testSetString_long_String() throws Exception {
202
        FileBackedClob b;
203
        char[] test1 = "test".toCharArray();
204
        char[] test2 = "test0123456789".toCharArray();
205
        char[] firstPartReference = new char[testCase2.length - test1.length - 4];
206
        char[] secondPartReference = new char[4];
207
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
208
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
209
        b = new FileBackedClob(new CharArrayReader(testCase2));
210
        b.setString(testCase2.length - test1.length - 4 + 1, new String(test1));
211
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
212
        assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4));
213
        assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length));
214
        assertEquals(b.length(), 1024);
215
        b.setString(testCase2.length - test1.length - 4 + 1, new String(test2));
216
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
217
        assertEquals(b.length(), 1024 - test1.length - 4 + test2.length);
218
        assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length));
219
        b.free();
220
    }
221
222
    @Test
223
    public void testSetString_4args() throws Exception {
224
        FileBackedClob b;
225
        char[] test1 = "test".toCharArray();
226
        char[] test2 = "01test23456789".toCharArray();
227
        char[] firstPartReference = new char[testCase2.length - test1.length - 4];
228
        char[] secondPartReference = new char[4];
229
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
230
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
231
        b = new FileBackedClob(new CharArrayReader(testCase2));
232
        b.setString(testCase2.length - test1.length - 4 + 1, new String(test2), 2, 4);
233
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
234
        assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4));
235
        assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length));
236
        assertEquals(b.length(), 1024);
237
        b.free();
238
    }
239
240
    @Test
241
    public void testSetCharacterStream() throws Exception {
242
        FileBackedClob b;
243
        char[] test1 = "test".toCharArray();
244
        char[] test2 = "test0123456789".toCharArray();
245
        char[] firstPartReference = new char[testCase2.length - test1.length - 4];
246
        char[] secondPartReference = new char[4];
247
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
248
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
249
        b = new FileBackedClob(new CharArrayReader(testCase2));
250
        Writer os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1);
251
        os.write(test1);
252
        os.close();
253
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
254
        assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4));
255
        assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length));
256
        assertEquals(b.length(), 1024);
257
        os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1);
258
        os.write(test2);
259
        os.close();
260
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
261
        assertEquals(b.length(), 1024 - test1.length - 4 + test2.length);
262
        assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length));
263
        b.free();
264
    }
265
266
    @Test
267
    public void testFinalize() throws Exception {
268
        FileBackedClob b;
269
        b = new FileBackedClob(new CharArrayReader(testCase2));
270
        assertTrue(b.getBackingFile().exists());
271
        try {
272
            b.finalize();
273
        } catch (Throwable ex) {
274
            Exceptions.printStackTrace(ex);
275
        }
276
        assertFalse(b.getBackingFile().exists());
277
    }
278
    
279
    @Test
280
    public void testFree() throws Exception {
281
        FileBackedClob b;
282
        b = new FileBackedClob(new CharArrayReader(testCase2));
283
        assertTrue(b.getBackingFile().exists());
284
        b.free();
285
        assertFalse(b.getBackingFile().exists());
286
    }
287
}

Return to bug 206233