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 128546
Collapse All | Expand All

(-)a/db/apichanges.xml (+13 lines)
Lines 105-110 Link Here
105
    <!-- ACTUAL CHANGES BEGIN HERE: -->
105
    <!-- ACTUAL CHANGES BEGIN HERE: -->
106
106
107
    <changes>
107
    <changes>
108
        <change>
109
            <api name="database_explorer_api"/>
110
            <summary>Add ability to connect a database connection directly with no UI</summary>
111
            <version major="1" minor="26"/>
112
            <date day="26" month="6" year="2008"/>
113
            <author login="davidvc"/>
114
            <compatibility addition="yes"/>
115
            <description>
116
                Add the ability to connect a database connection directly from the API
117
                without having any kind of UI pop up, be it a dialog or a progress window.
118
            </description>
119
            <issue number="128546"/>
120
        </change>
108
        <change>
121
        <change>
109
            <api name="database_explorer_api"/>
122
            <api name="database_explorer_api"/>
110
            <summary>Add ability to remove a database connection</summary>
123
            <summary>Add ability to remove a database connection</summary>
(-)a/db/arch.xml (+4 lines)
Lines 325-330 Link Here
325
    <a href="@TOP@org/netbeans/api/db/explorer/DatabaseConnection.html#getJDBCConnection()">getJDBCConnection()</a>
325
    <a href="@TOP@org/netbeans/api/db/explorer/DatabaseConnection.html#getJDBCConnection()">getJDBCConnection()</a>
326
    method.
326
    method.
327
   </p>
327
   </p>
328
   <p>
329
     If you want to connect to the database without showing a dialog or any kind of UI, you can us the
330
     <a href="@TOP@org/netbeans/api/db/explorer/ConnectionManager.html#connect(org.netbeans.api.db.explorer.DatabaseConnection)">DatabaseConnectoin.connect()</a>
331
     method.
328
  </usecase>
332
  </usecase>
329
  <usecase id="connections-combo-box" name="Displaying the database connections in the UI">
333
  <usecase id="connections-combo-box" name="Displaying the database connections in the UI">
330
   <p>
334
   <p>
(-)a/db/manifest.mf (-1 / +1 lines)
Lines 1-7 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.db/1
2
OpenIDE-Module: org.netbeans.modules.db/1
3
OpenIDE-Module-Install: org/netbeans/modules/db/DatabaseModule.class
3
OpenIDE-Module-Install: org/netbeans/modules/db/DatabaseModule.class
4
OpenIDE-Module-Implementation-Version: 5
4
OpenIDE-Module-Implementation-Version: 26
5
OpenIDE-Module-Layer: org/netbeans/modules/db/resources/mf-layer.xml
5
OpenIDE-Module-Layer: org/netbeans/modules/db/resources/mf-layer.xml
6
OpenIDE-Module-Requires: org.netbeans.api.javahelp.Help
6
OpenIDE-Module-Requires: org.netbeans.api.javahelp.Help
7
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/db/resources/Bundle.properties
7
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/db/resources/Bundle.properties
(-)a/db/src/org/netbeans/api/db/explorer/ConnectionManager.java (+19 lines)
Lines 138-143 Link Here
138
        }
138
        }
139
        ((RootNodeInfo)RootNode.getInstance().getInfo()).addConnectionNoConnect(dbconn.getDelegate());
139
        ((RootNodeInfo)RootNode.getInstance().getInfo()).addConnectionNoConnect(dbconn.getDelegate());
140
    }
140
    }
141
     /**
142
     * Connects this connection to the database <b>without opening any
143
     * dialog</b>.  If not all the necessary parameters are set, this
144
     * method will throw an exception (versus prompting the user for input>
145
     * <p>
146
     * This method is called synchronously, and will throw an exception
147
     * if called on the AWT event thread
148
     */
149
    public void connect(DatabaseConnection dbconn) throws DatabaseException {
150
        if (dbconn == null) {
151
            throw new NullPointerException();
152
        }
153
        if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) {
154
            throw new IllegalStateException("This connection is not added to the ConnectionManager."); // NOI18N
155
        }
156
        dbconn.getDelegate().connectSync();
157
    }
158
159
    /**
141
    
160
    
142
    /**
161
    /**
143
     * Remove an existing connection from the Database Explorer.  This method 
162
     * Remove an existing connection from the Database Explorer.  This method 
(-)a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java (-138 / +137 lines)
Lines 42-49 Link Here
42
package org.netbeans.modules.db.explorer;
42
package org.netbeans.modules.db.explorer;
43
43
44
import java.awt.Component;
44
import java.awt.Component;
45
import java.beans.PropertyChangeListener;
45
import java.beans.PropertyChangeSupport;
46
import java.beans.PropertyChangeSupport;
46
import java.beans.PropertyChangeListener;
47
import java.beans.PropertyVetoException;
47
import java.beans.PropertyVetoException;
48
import java.io.ObjectStreamException;
48
import java.io.ObjectStreamException;
49
import java.sql.Connection;
49
import java.sql.Connection;
Lines 58-72 Link Here
58
import java.util.Set;
58
import java.util.Set;
59
import java.util.logging.Level;
59
import java.util.logging.Level;
60
import java.util.logging.Logger;
60
import java.util.logging.Logger;
61
import org.netbeans.modules.db.explorer.actions.ConnectAction;
62
import org.openide.util.Lookup;
61
import org.openide.util.Lookup;
63
import org.openide.util.LookupEvent;
62
import org.openide.util.LookupEvent;
64
import org.openide.util.LookupListener;
63
import org.openide.util.LookupListener;
65
import org.openide.util.Mutex;
66
64
67
import org.openide.util.NbBundle;
65
import org.openide.util.NbBundle;
68
import org.openide.util.RequestProcessor;
69
import org.openide.util.Task ;
70
66
71
import org.netbeans.lib.ddl.DBConnection;
67
import org.netbeans.lib.ddl.DBConnection;
72
import org.netbeans.lib.ddl.DDLException;
68
import org.netbeans.lib.ddl.DDLException;
Lines 75-85 Link Here
75
import org.netbeans.api.db.explorer.JDBCDriverManager;
71
import org.netbeans.api.db.explorer.JDBCDriverManager;
76
72
77
import org.netbeans.modules.db.ExceptionListener;
73
import org.netbeans.modules.db.ExceptionListener;
74
import org.netbeans.modules.db.explorer.actions.ConnectAction;
78
import org.netbeans.modules.db.explorer.infos.ConnectionNodeInfo;
75
import org.netbeans.modules.db.explorer.infos.ConnectionNodeInfo;
79
import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
76
import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
77
import org.netbeans.modules.db.explorer.infos.RootNodeInfo;
80
import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
78
import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
81
import org.netbeans.modules.db.explorer.nodes.RootNode;
79
import org.netbeans.modules.db.explorer.nodes.RootNode;
82
83
import org.netbeans.modules.db.runtime.DatabaseRuntimeManager;
80
import org.netbeans.modules.db.runtime.DatabaseRuntimeManager;
84
import org.netbeans.spi.db.explorer.DatabaseRuntime;
81
import org.netbeans.spi.db.explorer.DatabaseRuntime;
85
import org.openide.explorer.ExplorerManager;
82
import org.openide.explorer.ExplorerManager;
Lines 87-93 Link Here
87
import org.openide.nodes.NodeNotFoundException;
84
import org.openide.nodes.NodeNotFoundException;
88
import org.openide.nodes.NodeOp;
85
import org.openide.nodes.NodeOp;
89
import org.openide.util.Exceptions;
86
import org.openide.util.Exceptions;
87
import org.openide.util.Mutex;
88
import org.openide.util.RequestProcessor;
90
import org.openide.windows.TopComponent;
89
import org.openide.windows.TopComponent;
90
91
91
92
92
93
/**
93
/**
Lines 99-105 Link Here
99
 * open connection.
99
 * open connection.
100
 */
100
 */
101
public class DatabaseConnection implements DBConnection {
101
public class DatabaseConnection implements DBConnection {
102
    
102
103
    private static final Logger LOGGER = Logger.getLogger(DatabaseConnection.class.getName());
103
    private static final Logger LOGGER = Logger.getLogger(DatabaseConnection.class.getName());
104
    private static final boolean LOG = LOGGER.isLoggable(Level.FINE);
104
    private static final boolean LOG = LOGGER.isLoggable(Level.FINE);
105
105
Lines 131-137 Link Here
131
131
132
    /** Connection name */
132
    /** Connection name */
133
    private String name;
133
    private String name;
134
    
134
135
    /**
135
    /**
136
     * The API DatabaseConnection (delegates to this instance)
136
     * The API DatabaseConnection (delegates to this instance)
137
     */
137
     */
Lines 159-165 Link Here
159
                    openConnectionServices = null;
159
                    openConnectionServices = null;
160
                }
160
                }
161
            }
161
            }
162
        });            
162
        });
163
    }
163
    }
164
164
165
    /** Default constructor */
165
    /** Default constructor */
Lines 167-173 Link Here
167
        dbconn = DatabaseConnectionAccessor.DEFAULT.createDatabaseConnection(this);
167
        dbconn = DatabaseConnectionAccessor.DEFAULT.createDatabaseConnection(this);
168
        propertySupport = new PropertyChangeSupport(this);
168
        propertySupport = new PropertyChangeSupport(this);
169
    }
169
    }
170
    
170
171
    /** Advanced constructor
171
    /** Advanced constructor
172
     * Allows to specify all needed information.
172
     * Allows to specify all needed information.
173
     * @param driver Driver URL
173
     * @param driver Driver URL
Lines 178-190 Link Here
178
    public DatabaseConnection(String driver, String database, String user, String password) {
178
    public DatabaseConnection(String driver, String database, String user, String password) {
179
        this(driver, null, database, null, user, password, false);
179
        this(driver, null, database, null, user, password, false);
180
    }
180
    }
181
    
181
182
    public DatabaseConnection(String driver, String driverName, String database,
182
    public DatabaseConnection(String driver, String driverName, String database,
183
            String theschema, String user, String password) {
183
            String theschema, String user, String password) {
184
        this(driver, driverName, database, theschema, user, password, false);
184
        this(driver, driverName, database, theschema, user, password, false);
185
    }
185
    }
186
    
186
187
    public DatabaseConnection(String driver, String driverName, String database, 
187
    public DatabaseConnection(String driver, String driverName, String database,
188
            String theschema, String user, String password,
188
            String theschema, String user, String password,
189
            boolean rememberPassword) {
189
            boolean rememberPassword) {
190
        this();
190
        this();
Lines 197-203 Link Here
197
        name = getName();
197
        name = getName();
198
        rpwd = Boolean.valueOf(rememberPassword);
198
        rpwd = Boolean.valueOf(rememberPassword);
199
    }
199
    }
200
    
200
201
    public JDBCDriver findJDBCDriver() {
201
    public JDBCDriver findJDBCDriver() {
202
        JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers(drv);
202
        JDBCDriver[] drvs = JDBCDriverManager.getDefault().getDrivers(drv);
203
        if (drvs.length <= 0) {
203
        if (drvs.length <= 0) {
Lines 224-236 Link Here
224
     private OpenConnectionInterface getOpenConnection() {
224
     private OpenConnectionInterface getOpenConnection() {
225
         if (openConnection != null)
225
         if (openConnection != null)
226
             return openConnection;
226
             return openConnection;
227
         
227
228
         openConnection = new OpenConnection();
228
         openConnection = new OpenConnection();
229
         String driver = getDriver();
229
         String driver = getDriver();
230
         if (driver == null) {
230
         if (driver == null) {
231
             return openConnection;
231
             return openConnection;
232
         }
232
         }
233
         
233
234
         // For Java Studio Enterprise. Create instanceof OpenConnection
234
         // For Java Studio Enterprise. Create instanceof OpenConnection
235
         try {
235
         try {
236
             Collection c = getOpenConnections();
236
             Collection c = getOpenConnections();
Lines 246-252 Link Here
246
         }
246
         }
247
         return openConnection;
247
         return openConnection;
248
     }
248
     }
249
     
249
250
    /** Returns driver class */
250
    /** Returns driver class */
251
    public String getDriver() {
251
    public String getDriver() {
252
        return drv;
252
        return drv;
Lines 424-430 Link Here
424
        if (LOG) {
424
        if (LOG) {
425
            LOGGER.log(Level.FINE, "createJDBCConnection()");
425
            LOGGER.log(Level.FINE, "createJDBCConnection()");
426
        }
426
        }
427
        
427
428
        if (drv == null || db == null || usr == null )
428
        if (drv == null || db == null || usr == null )
429
            throw new DDLException(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_InsufficientConnInfo"));
429
            throw new DDLException(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_InsufficientConnInfo"));
430
430
Lines 440-449 Link Here
440
            // For Java Studio Enterprise.
440
            // For Java Studio Enterprise.
441
            getOpenConnection().enable();
441
            getOpenConnection().enable();
442
            startRuntimes();
442
            startRuntimes();
443
            
443
444
            // hack for Derby
444
            // hack for Derby
445
            DerbyConectionEventListener.getDefault().beforeConnect(this);
445
            DerbyConectionEventListener.getDefault().beforeConnect(this);
446
            
446
447
            JDBCDriver useDriver = findJDBCDriver();
447
            JDBCDriver useDriver = findJDBCDriver();
448
            if (useDriver == null) {
448
            if (useDriver == null) {
449
                // will be loaded through DriverManager, make sure it is loaded
449
                // will be loaded through DriverManager, make sure it is loaded
Lines 452-462 Link Here
452
452
453
            Connection connection = DbDriverManager.getDefault().getConnection(db, dbprops, useDriver);
453
            Connection connection = DbDriverManager.getDefault().getConnection(db, dbprops, useDriver);
454
            setConnection(connection);
454
            setConnection(connection);
455
            
455
456
            DatabaseUILogger.logConnection(drv);
456
            DatabaseUILogger.logConnection(drv);
457
            
457
458
            propertySupport.firePropertyChange("connected", null, null);
458
            propertySupport.firePropertyChange("connected", null, null);
459
            
459
460
            // For Java Studio Enterprise.
460
            // For Java Studio Enterprise.
461
            getOpenConnection().disable();
461
            getOpenConnection().disable();
462
462
Lines 471-477 Link Here
471
            //                    message = MessageFormat.format(bundle.getString("EXC_PointbaseServerRejected"), new String[] {message, db}); // NOI18N
471
            //                    message = MessageFormat.format(bundle.getString("EXC_PointbaseServerRejected"), new String[] {message, db}); // NOI18N
472
472
473
            propertySupport.firePropertyChange("failed", null, null);
473
            propertySupport.firePropertyChange("failed", null, null);
474
            
474
475
            // For Java Studio Enterprise.
475
            // For Java Studio Enterprise.
476
            getOpenConnection().disable();
476
            getOpenConnection().disable();
477
477
Lines 493-600 Link Here
493
        }
493
        }
494
    }
494
    }
495
495
496
    public void connect() {
496
    public void connectSync() throws DatabaseException {
497
        try {
498
            doConnect();
499
500
            // Let the CNI know we're connected and set things up
501
            // appropriately
502
            ConnectionNodeInfo cni = findConnectionNodeInfo(getName());
503
            cni.connect(this);
504
        } catch (Exception exc) {
505
            try {
506
                getConnection().close();
507
            } catch (SQLException e) {
508
                LOGGER.log(Level.FINE, null, e);
509
            }
510
            throw new DatabaseException(exc);
511
        }
512
    }
513
514
    private void doConnect() throws DDLException {
515
        if (drv == null || db == null || usr == null )
516
            sendException(new DDLException(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_InsufficientConnInfo")));
517
518
        Properties dbprops = new Properties();
519
        if ( usr.length() > 0 ) {
520
            dbprops.put("user", usr); //NOI18N
521
        }
522
        if ((pwd != null && pwd.length() > 0)) {
523
            dbprops.put("password", pwd); //NOI18N
524
        }
525
526
        Connection conn = null;
527
        try {
528
            propertySupport.firePropertyChange("connecting", null, null);
529
530
            // For Java Studio Enterprise.
531
            getOpenConnection().enable();
532
533
            startRuntimes();
534
535
            // hack for Derby
536
            DerbyConectionEventListener.getDefault().beforeConnect(DatabaseConnection.this);
537
538
            JDBCDriver useDriver = findJDBCDriver();
539
            if (useDriver == null) {
540
                // will be loaded through DriverManager, make sure it is loaded
541
                Class.forName(drv);
542
            }
543
544
            conn = DbDriverManager.getDefault().getConnection(db, dbprops, useDriver);
545
            setConnection(conn);
546
547
            DatabaseUILogger.logConnection(drv);
548
549
            propertySupport.firePropertyChange("connected", null, null);
550
        } catch (Exception e) {
551
            String message = MessageFormat.format(
552
                    NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_CannotEstablishConnection"),
553
                    db, drv, e.getMessage()); // NOI18N
554
555
            propertySupport.firePropertyChange("failed", null, null);
556
557
            if (e instanceof SQLException) {
558
                initSQLException((SQLException)e);
559
            }
560
561
            DDLException ddle = new DDLException(message);
562
            ddle.initCause(e);
563
564
            if (conn != null) {
565
                setConnection(null);
566
                try {
567
                    conn.close();
568
                } catch (SQLException sqle) {
569
                    Logger.getLogger("global").log(Level.WARNING, null, sqle); // NOI18N
570
                }
571
            }
572
573
            throw ddle;
574
        } finally {
575
            getOpenConnection().disable();
576
        }
577
    }
578
579
    public void connectAsync() {
497
        if (LOG) {
580
        if (LOG) {
498
            LOGGER.log(Level.FINE, "connect()");
581
            LOGGER.log(Level.FINE, "connect()");
499
        }
582
        }
500
        
501
        createConnectTask() ;
502
    }
503
583
504
    public Task createConnectTask() {
584
        RequestProcessor.getDefault().post(new Runnable() {
505
        return RequestProcessor.getDefault().post(new Runnable() {
506
            public void run() {
585
            public void run() {
507
                if (drv == null || db == null || usr == null )
508
                    sendException(new DDLException(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_InsufficientConnInfo")));
509
510
                Properties dbprops = new Properties();
511
                if ( usr.length() > 0 ) {
512
                    dbprops.put("user", usr); //NOI18N
513
                }
514
                if ((pwd != null && pwd.length() > 0)) {
515
                    dbprops.put("password", pwd); //NOI18N
516
                }
517
                
518
                Connection conn = null;
519
                try {
586
                try {
520
                    propertySupport.firePropertyChange("connecting", null, null);
587
                    doConnect();
521
                    
588
                } catch (Exception e) {
522
                    // For Java Studio Enterprise.
589
                    sendException(e);
523
                    getOpenConnection().enable();
524
525
                    // For Java Studio Enterprise.
526
                    getOpenConnection().enable();
527
                    startRuntimes();
528
                    
529
                    // hack for Derby
530
                    DerbyConectionEventListener.getDefault().beforeConnect(DatabaseConnection.this);
531
                    
532
                    JDBCDriver useDriver = findJDBCDriver();
533
                    if (useDriver == null) {
534
                        // will be loaded through DriverManager, make sure it is loaded
535
                        Class.forName(drv);
536
                    }
537
                    
538
                    conn = DbDriverManager.getDefault().getConnection(db, dbprops, useDriver);
539
                    setConnection(conn);
540
                    
541
                    DatabaseUILogger.logConnection(drv);
542
                        
543
                    propertySupport.firePropertyChange("connected", null, null);
544
545
                    // For Java Studio Enterprise.
546
                    getOpenConnection().disable();
547
548
                } catch (SQLException e) {
549
                    String message = MessageFormat.format(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_CannotEstablishConnection"), new String[] {db, drv, e.getMessage()}); // NOI18N
550
551
                    //commented out for 3.6 release, need to solve for next Studio release
552
                    // hack for Pointbase Network Server
553
                    //                    if (drv.equals(PointbasePlus.DRIVER))
554
                    //                        if (e.getErrorCode() == PointbasePlus.ERR_SERVER_REJECTED)
555
                    //                            message = MessageFormat.format(bundle.getString("EXC_PointbaseServerRejected"), new String[] {message, db}); // NOI18N
556
557
                    propertySupport.firePropertyChange("failed", null, null);
558
559
                    // For Java Studio Enterprise.
560
                    getOpenConnection().disable();
561
562
                    initSQLException(e);
563
                    DDLException ddle = new DDLException(message);
564
                    ddle.initCause(e);
565
                    sendException(ddle);
566
                    
567
                    if (conn != null) {
568
                        setConnection(null);
569
                        try {
570
                            conn.close();
571
                        } catch (SQLException sqle) {
572
                            Logger.getLogger("global").log(Level.WARNING, null, sqle); // NOI18N
573
                        }
574
                    }
575
                } catch (Exception exc) {
576
                    propertySupport.firePropertyChange("failed", null, null);
577
578
                    // For Java Studio Enterprise.
579
                    getOpenConnection().disable();
580
581
                    sendException(exc);
582
                    
583
                    setConnection(null);
584
                    if (conn != null) {
585
                        try {
586
                            conn.close();
587
                        } catch (SQLException sqle) {
588
                            Logger.getLogger("global").log(Level.WARNING, null, sqle); // NOI18N
589
                        }
590
                    }
591
                }
590
                }
592
            }
591
            }
593
        }, 0);
592
        }, 0);
594
    }
593
    }
595
    
594
596
    /** Calls the initCause() for SQLException with the value
595
    /** Calls the initCause() for SQLException with the value
597
      * of getNextException() so this exception's stack trace contains 
596
      * of getNextException() so this exception's stack trace contains
598
      * the complete data.
597
      * the complete data.
599
      */
598
      */
600
    private void initSQLException(SQLException e) {
599
    private void initSQLException(SQLException e) {
Lines 613-619 Link Here
613
612
614
    private void startRuntimes() {
613
    private void startRuntimes() {
615
        DatabaseRuntime[] runtimes = DatabaseRuntimeManager.getDefault().getRuntimes(drv);
614
        DatabaseRuntime[] runtimes = DatabaseRuntimeManager.getDefault().getRuntimes(drv);
616
        
615
617
        for (int i = 0; i < runtimes.length; i++) {
616
        for (int i = 0; i < runtimes.length; i++) {
618
            DatabaseRuntime runtime = runtimes[i];
617
            DatabaseRuntime runtime = runtimes[i];
619
            if (runtime.isRunning()) {
618
            if (runtime.isRunning()) {
Lines 698-704 Link Here
698
        } catch (Exception exc) {
697
        } catch (Exception exc) {
699
            //IGNORE - not stored in 3.6 and earlier
698
            //IGNORE - not stored in 3.6 and earlier
700
        }
699
        }
701
        
700
702
        // boston setting/pilsen setting?
701
        // boston setting/pilsen setting?
703
        if ((name != null) && (name.equals(DatabaseConnection.SUPPORT))) {
702
        if ((name != null) && (name.equals(DatabaseConnection.SUPPORT))) {
704
            // pilsen
703
            // pilsen
Lines 708-714 Link Here
708
        }
707
        }
709
        name = null;
708
        name = null;
710
        name = getName();
709
        name = getName();
711
        
710
712
        dbconn = DatabaseConnectionAccessor.DEFAULT.createDatabaseConnection(this);
711
        dbconn = DatabaseConnectionAccessor.DEFAULT.createDatabaseConnection(this);
713
    }
712
    }
714
713
Lines 718-738 Link Here
718
        out.writeObject(db);
717
        out.writeObject(db);
719
        out.writeObject(usr);
718
        out.writeObject(usr);
720
        out.writeObject(schema);
719
        out.writeObject(schema);
721
        out.writeObject(DatabaseConnection.SUPPORT);        
720
        out.writeObject(DatabaseConnection.SUPPORT);
722
        out.writeObject(drvname);
721
        out.writeObject(drvname);
723
    }
722
    }
724
723
725
    public String toString() {
724
    public String toString() {
726
        return "Driver:" + getDriver() + "Database:" + getDatabase().toLowerCase() + "User:" + getUser().toLowerCase() + "Schema:" + getSchema().toLowerCase(); // NOI18N
725
        return "Driver:" + getDriver() + "Database:" + getDatabase().toLowerCase() + "User:" + getUser().toLowerCase() + "Schema:" + getSchema().toLowerCase(); // NOI18N
727
    }
726
    }
728
    
727
729
    /**
728
    /**
730
     * Gets the API DatabaseConnection which corresponds to this connection.
729
     * Gets the API DatabaseConnection which corresponds to this connection.
731
     */
730
     */
732
    public org.netbeans.api.db.explorer.DatabaseConnection getDatabaseConnection() {
731
    public org.netbeans.api.db.explorer.DatabaseConnection getDatabaseConnection() {
733
        return dbconn;
732
        return dbconn;
734
    }
733
    }
735
    
734
736
    public void selectInExplorer() {
735
    public void selectInExplorer() {
737
        String nodeName = null;
736
        String nodeName = null;
738
        try {
737
        try {
Lines 741-754 Link Here
741
            Exceptions.printStackTrace(e);
740
            Exceptions.printStackTrace(e);
742
            return;
741
            return;
743
        }
742
        }
744
        
743
745
        // find the Runtime panel top component
744
        // find the Runtime panel top component
746
        // quite hacky, but it will be replaced by the Server Navigator
745
        // quite hacky, but it will be replaced by the Server Navigator
747
        
746
748
        TopComponent runtimePanel = null;
747
        TopComponent runtimePanel = null;
749
        ExplorerManager runtimeExplorer = null;
748
        ExplorerManager runtimeExplorer = null;
750
        Node runtimeNode = null;
749
        Node runtimeNode = null;
751
        
750
752
        for (Iterator i = TopComponent.getRegistry().getOpened().iterator(); i.hasNext();) {
751
        for (Iterator i = TopComponent.getRegistry().getOpened().iterator(); i.hasNext();) {
753
            TopComponent component = (TopComponent)i.next();
752
            TopComponent component = (TopComponent)i.next();
754
            Component[] children = component.getComponents();
753
            Component[] children = component.getComponents();
Lines 761-771 Link Here
761
                }
760
                }
762
            }
761
            }
763
        }
762
        }
764
        
763
765
        if (runtimePanel == null) {
764
        if (runtimePanel == null) {
766
            return;
765
            return;
767
        }
766
        }
768
            
767
769
        Node node = null;
768
        Node node = null;
770
        try {
769
        try {
771
            node = NodeOp.findPath(runtimeNode, new String[] { "Databases", nodeName }); // NOI18N
770
            node = NodeOp.findPath(runtimeNode, new String[] { "Databases", nodeName }); // NOI18N
Lines 773-789 Link Here
773
            Exceptions.printStackTrace(e);
772
            Exceptions.printStackTrace(e);
774
            return;
773
            return;
775
        }
774
        }
776
        
775
777
        try {
776
        try {
778
            runtimeExplorer.setSelectedNodes(new Node[] { node });
777
            runtimeExplorer.setSelectedNodes(new Node[] { node });
779
        } catch (PropertyVetoException e) {
778
        } catch (PropertyVetoException e) {
780
            Exceptions.printStackTrace(e);
779
            Exceptions.printStackTrace(e);
781
            return;
780
            return;
782
        }
781
        }
783
    
782
784
        runtimePanel.requestActive();
783
        runtimePanel.requestActive();
785
    }
784
    }
786
    
785
787
    public void showConnectionDialog() {
786
    public void showConnectionDialog() {
788
        try {
787
        try {
789
            final ConnectionNodeInfo cni = findConnectionNodeInfo(getName());
788
            final ConnectionNodeInfo cni = findConnectionNodeInfo(getName());
Lines 798-804 Link Here
798
            Exceptions.printStackTrace(e);
797
            Exceptions.printStackTrace(e);
799
        }
798
        }
800
    }
799
    }
801
    
800
802
    public Connection getJDBCConnection() {
801
    public Connection getJDBCConnection() {
803
        try {
802
        try {
804
            ConnectionNodeInfo cni = findConnectionNodeInfo(getName());
803
            ConnectionNodeInfo cni = findConnectionNodeInfo(getName());
Lines 810-839 Link Here
810
        }
809
        }
811
        return null;
810
        return null;
812
    }
811
    }
813
    
812
814
    public void disconnect() throws DatabaseException {
813
    public void disconnect() throws DatabaseException {
815
        ConnectionNodeInfo cni = findConnectionNodeInfo(getName());
814
        ConnectionNodeInfo cni = findConnectionNodeInfo(getName());
816
        if (cni != null && cni.getConnection() != null) {
815
        if (cni != null && cni.getConnection() != null) {
817
            cni.disconnect();
816
            cni.disconnect();
818
        }
817
        }
819
    }
818
    }
820
    
819
821
    private ConnectionNodeInfo findConnectionNodeInfo(String connection) throws DatabaseException {
820
    private ConnectionNodeInfo findConnectionNodeInfo(String connection) throws DatabaseException {
822
        assert connection != null;
821
        assert connection != null;
823
        
822
824
        // We can't use the info classes here since surprisingly 
823
        // We can't use the info classes here since surprisingly
825
        // the CNIs found in RootNode.getInstance().getInfo are different than
824
        // the CNIs found in RootNode.getInstance().getInfo are different than
826
        // the ones the ConnectionNodes in the Databases tree listen to.
825
        // the ones the ConnectionNodes in the Databases tree listen to.
827
        
826
828
        // This will account for the "Please wait" node.
827
        // This will account for the "Please wait" node.
829
        Node[] nodes = RootNode.getInstance().getChildren().getNodes(true);
828
        Node[] nodes = RootNode.getInstance().getChildren().getNodes(true);
830
        
829
831
        for (int i = 0; i < nodes.length; i++) {
830
        for (int i = 0; i < nodes.length; i++) {
832
            // Skip nodes registered by node providers
831
            // Skip nodes registered by node providers
833
            if ( ! (nodes[i] instanceof DatabaseNode) ) {
832
            if ( ! (nodes[i] instanceof DatabaseNode) ) {
834
                continue;
833
                continue;
835
            }
834
            }
836
            
835
837
            DatabaseNodeInfo info = (DatabaseNodeInfo)nodes[i].getCookie(DatabaseNodeInfo.class);
836
            DatabaseNodeInfo info = (DatabaseNodeInfo)nodes[i].getCookie(DatabaseNodeInfo.class);
838
            if (info == null) {
837
            if (info == null) {
839
                continue;
838
                continue;
Lines 848-854 Link Here
848
        }
847
        }
849
        return null;
848
        return null;
850
    }
849
    }
851
    
850
852
    private Object readResolve() throws ObjectStreamException {
851
    private Object readResolve() throws ObjectStreamException {
853
        // sometimes deserialized objects have a null propertySuppport, not sure why
852
        // sometimes deserialized objects have a null propertySuppport, not sure why
854
        if (propertySupport == null) {
853
        if (propertySupport == null) {
(-)a/db/src/org/netbeans/modules/db/explorer/actions/ConnectAction.java (-3 / +3 lines)
Lines 268-274 Link Here
268
268
269
                            try {
269
                            try {
270
                                if (dbcon.getConnection() == null || dbcon.getConnection().isClosed())
270
                                if (dbcon.getConnection() == null || dbcon.getConnection().isClosed())
271
                                    dbcon.connect();
271
                                    dbcon.connectAsync();
272
                                else {
272
                                else {
273
                                    dbcon.setSchema(schemaPanel.getSchema());
273
                                    dbcon.setSchema(schemaPanel.getSchema());
274
                                    nfo.setSchema(schemaPanel.getSchema());
274
                                    nfo.setSchema(schemaPanel.getSchema());
Lines 294-300 Link Here
294
                                }
294
                                }
295
                            } catch (SQLException exc) {
295
                            } catch (SQLException exc) {
296
                                //isClosed() method failed, try to connect
296
                                //isClosed() method failed, try to connect
297
                                dbcon.connect();
297
                                dbcon.connectAsync();
298
                            }
298
                            }
299
                            return;
299
                            return;
300
                        }
300
                        }
Lines 364-370 Link Here
364
                    failed = false;
364
                    failed = false;
365
                    
365
                    
366
                    dbcon.addPropertyChangeListener(connectionListener);
366
                    dbcon.addPropertyChangeListener(connectionListener);
367
                    dbcon.connect();
367
                    dbcon.connectAsync();
368
                    
368
                    
369
                    progress.start();
369
                    progress.start();
370
                    progress.switchToIndeterminate();
370
                    progress.switchToIndeterminate();
(-)a/db/src/org/netbeans/modules/db/explorer/actions/ConnectUsingDriverAction.java (-2 / +2 lines)
Lines 280-286 Link Here
280
                        basePanel.setConnectionInfo();
280
                        basePanel.setConnectionInfo();
281
                        try {
281
                        try {
282
                            if (cinfo.getConnection() == null || cinfo.getConnection().isClosed())
282
                            if (cinfo.getConnection() == null || cinfo.getConnection().isClosed())
283
                                cinfo.connect();
283
                                cinfo.connectAsync();
284
                            else {
284
                            else {
285
                                cinfo.setSchema(schemaPanel.getSchema());
285
                                cinfo.setSchema(schemaPanel.getSchema());
286
                                ((RootNodeInfo)RootNode.getInstance().getInfo()).addConnection(cinfo);
286
                                ((RootNodeInfo)RootNode.getInstance().getInfo()).addConnection(cinfo);
Lines 289-295 Link Here
289
                            }
289
                            }
290
                        } catch (SQLException exc) {
290
                        } catch (SQLException exc) {
291
                            //isClosed() method failed, try to connect
291
                            //isClosed() method failed, try to connect
292
                            cinfo.connect();
292
                            cinfo.connectAsync();
293
                        } catch (DatabaseException exc) {
293
                        } catch (DatabaseException exc) {
294
                            Logger.getLogger("global").log(Level.INFO, null, exc);
294
                            Logger.getLogger("global").log(Level.INFO, null, exc);
295
                            DbUtilities.reportError(bundle().getString("ERR_UnableToAddConnection"), exc.getMessage()); // NOI18N
295
                            DbUtilities.reportError(bundle().getString("ERR_UnableToAddConnection"), exc.getMessage()); // NOI18N
(-)a/db/src/org/netbeans/modules/db/explorer/dlg/SchemaPanel.java (-2 / +2 lines)
Lines 214-220 Link Here
214
        Connection con = dbcon.getConnection();
214
        Connection con = dbcon.getConnection();
215
        try {
215
        try {
216
            if (con == null || con.isClosed())
216
            if (con == null || con.isClosed())
217
                dbcon.connect();
217
                dbcon.connectAsync();
218
            else {
218
            else {
219
                RequestProcessor.getDefault().post(new Runnable() {
219
                RequestProcessor.getDefault().post(new Runnable() {
220
                    public void run() {
220
                    public void run() {
Lines 226-232 Link Here
226
            }
226
            }
227
        } catch (SQLException exc) {
227
        } catch (SQLException exc) {
228
            //isClosed() method failed, try to connect
228
            //isClosed() method failed, try to connect
229
            dbcon.connect();
229
            dbcon.connectAsync();
230
        }
230
        }
231
    }//GEN-LAST:event_schemaButtonActionPerformed
231
    }//GEN-LAST:event_schemaButtonActionPerformed
232
232
(-)a/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java (-3 / +9 lines)
Lines 41-55 Link Here
41
41
42
package org.netbeans.api.db.explorer;
42
package org.netbeans.api.db.explorer;
43
43
44
import java.net.URL;
45
import org.netbeans.modules.db.test.TestBase;
46
import org.netbeans.modules.db.test.Util;
44
import org.netbeans.modules.db.test.Util;
45
import org.netbeans.modules.db.util.DBTestBase;
47
46
48
/**
47
/**
49
 *
48
 *
50
 * @author Andrei Badea
49
 * @author Andrei Badea
51
 */
50
 */
52
public class DatabaseConnectionTest extends TestBase {
51
public class DatabaseConnectionTest extends DBTestBase {
53
    
52
    
54
    protected void setUp() throws Exception {
53
    protected void setUp() throws Exception {
55
        super.setUp();
54
        super.setUp();
Lines 92-97 Link Here
92
        assertEquals(dbconn, ConnectionManager.getDefault().getConnections()[0]);
91
        assertEquals(dbconn, ConnectionManager.getDefault().getConnections()[0]);
93
    }
92
    }
94
93
94
    public void testSyncConnection() throws Exception {
95
        DatabaseConnection dbconn = getDatabaseConnection();
96
        ConnectionManager.getDefault().connect(dbconn);
97
        assertTrue(dbconn.getJDBCConnection() != null);
98
        assertFalse(dbconn.getJDBCConnection().isClosed());
99
    }
100
95
    public void testDeleteConnection() throws Exception {
101
    public void testDeleteConnection() throws Exception {
96
        Util.deleteConnectionFiles();
102
        Util.deleteConnectionFiles();
97
        Util.deleteDriverFiles();
103
        Util.deleteDriverFiles();
(-)a/db/test/unit/src/org/netbeans/api/db/sql/support/QuoterTest.java (-3 / +2 lines)
Lines 27-34 Link Here
27
 */
27
 */
28
package org.netbeans.api.db.sql.support;
28
package org.netbeans.api.db.sql.support;
29
29
30
import org.netbeans.api.db.sql.support.SQLIdentifiers;
30
import org.netbeans.modules.db.util.DDLTestBase;
31
import org.netbeans.modules.db.util.DBTestBase;
32
31
33
/**
32
/**
34
 * @author <a href="mailto:david@vancouvering.com">David Van Couvering</a>
33
 * @author <a href="mailto:david@vancouvering.com">David Van Couvering</a>
Lines 36-42 Link Here
36
 * This class is a set of tests to make sure we're quoting identifiers
35
 * This class is a set of tests to make sure we're quoting identifiers
37
 * correctly
36
 * correctly
38
 */
37
 */
39
public class QuoterTest extends DBTestBase {
38
public class QuoterTest extends DDLTestBase {
40
    
39
    
41
    private SQLIdentifiers.Quoter quoter;
40
    private SQLIdentifiers.Quoter quoter;
42
    
41
    
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/actions/AddToIndexDDLTest.java (-2 / +2 lines)
Lines 29-41 Link Here
29
29
30
import java.sql.Types;
30
import java.sql.Types;
31
import java.util.HashSet;
31
import java.util.HashSet;
32
import org.netbeans.modules.db.util.DBTestBase;
32
import org.netbeans.modules.db.util.DDLTestBase;
33
33
34
/**
34
/**
35
 *
35
 *
36
 * @author David
36
 * @author David
37
 */
37
 */
38
public class AddToIndexDDLTest extends DBTestBase {
38
public class AddToIndexDDLTest extends DDLTestBase {
39
39
40
    public AddToIndexDDLTest(String name) {
40
    public AddToIndexDDLTest(String name) {
41
        super(name);
41
        super(name);
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/actions/GrabTableHelperTest.java (-2 / +2 lines)
Lines 36-48 Link Here
36
import org.netbeans.lib.ddl.impl.CreateTable;
36
import org.netbeans.lib.ddl.impl.CreateTable;
37
import org.netbeans.lib.ddl.impl.TableColumn;
37
import org.netbeans.lib.ddl.impl.TableColumn;
38
import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
38
import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
39
import org.netbeans.modules.db.util.DBTestBase;
39
import org.netbeans.modules.db.util.DDLTestBase;
40
import org.netbeans.modules.db.util.InfoHelper;
40
import org.netbeans.modules.db.util.InfoHelper;
41
41
42
/**
42
/**
43
 * @author David Van Couvering
43
 * @author David Van Couvering
44
 */
44
 */
45
public class GrabTableHelperTest extends DBTestBase {
45
public class GrabTableHelperTest extends DDLTestBase {
46
46
47
    public GrabTableHelperTest(String name) {
47
    public GrabTableHelperTest(String name) {
48
        super(name);
48
        super(name);
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/dlg/AddIndexDDLTest.java (-2 / +2 lines)
Lines 29-41 Link Here
29
29
30
import java.sql.Types;
30
import java.sql.Types;
31
import java.util.HashSet;
31
import java.util.HashSet;
32
import org.netbeans.modules.db.util.DBTestBase;
32
import org.netbeans.modules.db.util.DDLTestBase;
33
33
34
/**
34
/**
35
 *
35
 *
36
 * @author David
36
 * @author David
37
 */
37
 */
38
public class AddIndexDDLTest extends DBTestBase {
38
public class AddIndexDDLTest extends DDLTestBase {
39
39
40
    public AddIndexDDLTest(String name) {
40
    public AddIndexDDLTest(String name) {
41
        super(name);
41
        super(name);
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/dlg/AddTableColumnDDLTest.java (-2 / +2 lines)
Lines 27-39 Link Here
27
 */
27
 */
28
package org.netbeans.modules.db.explorer.dlg;
28
package org.netbeans.modules.db.explorer.dlg;
29
29
30
import org.netbeans.modules.db.util.DBTestBase;
30
import org.netbeans.modules.db.util.DDLTestBase;
31
31
32
/**
32
/**
33
 *
33
 *
34
 * @author David
34
 * @author David
35
 */
35
 */
36
public class AddTableColumnDDLTest extends DBTestBase {
36
public class AddTableColumnDDLTest extends DDLTestBase {
37
37
38
    public AddTableColumnDDLTest(String name) {
38
    public AddTableColumnDDLTest(String name) {
39
        super(name);
39
        super(name);
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/dlg/AddViewDDLTest.java (-2 / +2 lines)
Lines 27-35 Link Here
27
 */
27
 */
28
package org.netbeans.modules.db.explorer.dlg;
28
package org.netbeans.modules.db.explorer.dlg;
29
29
30
import org.netbeans.modules.db.util.DBTestBase;
30
import org.netbeans.modules.db.util.DDLTestBase;
31
31
32
public class AddViewDDLTest extends DBTestBase {
32
public class AddViewDDLTest extends DDLTestBase {
33
33
34
    public AddViewDDLTest(String name) {
34
    public AddViewDDLTest(String name) {
35
        super(name);
35
        super(name);
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/dlg/CreateTableDDLTest.java (-2 / +2 lines)
Lines 28-36 Link Here
28
package org.netbeans.modules.db.explorer.dlg;
28
package org.netbeans.modules.db.explorer.dlg;
29
29
30
import java.util.Vector;
30
import java.util.Vector;
31
import org.netbeans.modules.db.util.DBTestBase;
31
import org.netbeans.modules.db.util.DDLTestBase;
32
32
33
public class CreateTableDDLTest extends DBTestBase {
33
public class CreateTableDDLTest extends DDLTestBase {
34
34
35
    public CreateTableDDLTest(String name) {
35
    public CreateTableDDLTest(String name) {
36
        super(name);
36
        super(name);
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/infos/DDLHelperTest.java (-2 / +2 lines)
Lines 28-40 Link Here
28
package org.netbeans.modules.db.explorer.infos;
28
package org.netbeans.modules.db.explorer.infos;
29
29
30
import java.sql.Types;
30
import java.sql.Types;
31
import org.netbeans.modules.db.util.DBTestBase;
31
import org.netbeans.modules.db.util.DDLTestBase;
32
import org.netbeans.modules.db.util.InfoHelper;
32
import org.netbeans.modules.db.util.InfoHelper;
33
33
34
/**
34
/**
35
 * @author <href="mailto:david@vancouvering.com">David Van Couvering</href>
35
 * @author <href="mailto:david@vancouvering.com">David Van Couvering</href>
36
 */
36
 */
37
public class DDLHelperTest extends DBTestBase {
37
public class DDLHelperTest extends DDLTestBase {
38
    InfoHelper helper;
38
    InfoHelper helper;
39
39
40
    public DDLHelperTest(String name) {
40
    public DDLHelperTest(String name) {
(-)a/db/test/unit/src/org/netbeans/modules/db/util/DBTestBase.java (-92 / +48 lines)
Lines 27-32 Link Here
27
 */
27
 */
28
package org.netbeans.modules.db.util;
28
package org.netbeans.modules.db.util;
29
29
30
import java.net.URL;
30
import java.sql.Connection;
31
import java.sql.Connection;
31
import java.sql.DatabaseMetaData;
32
import java.sql.DatabaseMetaData;
32
import java.sql.DriverManager;
33
import java.sql.DriverManager;
Lines 35-53 Link Here
35
import java.sql.ResultSetMetaData;
36
import java.sql.ResultSetMetaData;
36
import java.sql.SQLException;
37
import java.sql.SQLException;
37
import java.sql.Statement;
38
import java.sql.Statement;
38
import java.sql.Types;
39
import java.util.Iterator;
39
import java.util.Iterator;
40
import java.util.Vector;
40
import java.util.Vector;
41
import java.util.logging.Level;
41
import java.util.logging.Level;
42
import java.util.logging.Logger;
42
import java.util.logging.Logger;
43
import org.netbeans.lib.ddl.impl.AddColumn;
43
import org.netbeans.api.db.explorer.ConnectionManager;
44
import org.netbeans.lib.ddl.impl.CreateIndex;
44
import org.netbeans.api.db.explorer.DatabaseConnection;
45
import org.netbeans.lib.ddl.impl.CreateTable;
45
import org.netbeans.api.db.explorer.JDBCDriver;
46
import org.netbeans.lib.ddl.impl.CreateView;
46
import org.netbeans.api.db.explorer.JDBCDriverManager;
47
import org.netbeans.lib.ddl.impl.DriverSpecification;
48
import org.netbeans.lib.ddl.impl.Specification;
49
import org.netbeans.lib.ddl.impl.SpecificationFactory;
50
import org.netbeans.lib.ddl.impl.TableColumn;
51
import org.netbeans.modules.db.test.TestBase;
47
import org.netbeans.modules.db.test.TestBase;
52
48
53
/**
49
/**
Lines 72-85 Link Here
72
    private static String password;
68
    private static String password;
73
    private static String dbname;
69
    private static String dbname;
74
    protected static String dblocation;
70
    protected static String dblocation;
75
71
    private static URL driverJarUrl;
72
    
76
    private static String DRIVER_PROPERTY = "db.driverclass";
73
    private static String DRIVER_PROPERTY = "db.driverclass";
77
    private static String URL_PROPERTY = "db.url";
74
    private static String URL_PROPERTY = "db.url";
78
    private static String USERNAME_PROPERTY = "db.username";
75
    private static String USERNAME_PROPERTY = "db.username";
79
    private static String PASSWORD_PROPERTY = "db.password";
76
    private static String PASSWORD_PROPERTY = "db.password";
80
    private static String DBDIR_PROPERTY = "db.dir";
77
    private static String DBDIR_PROPERTY = "db.dir";
81
    private static String DBNAME_PROPERTY = "db.name";
78
    private static String DBNAME_PROPERTY = "db.name";
82
    
79
83
    private static String quoteString = null;
80
    private static String quoteString = null;
84
    
81
    
85
    // This defines what happens to identifiers when stored in db
82
    // This defines what happens to identifiers when stored in db
Lines 92-107 Link Here
92
    private static int    unquotedCaseRule = RULE_UNDEFINED;
89
    private static int    unquotedCaseRule = RULE_UNDEFINED;
93
    private static int    quotedCaseRule = RULE_UNDEFINED;
90
    private static int    quotedCaseRule = RULE_UNDEFINED;
94
91
95
    protected static SpecificationFactory specfactory;
92
    private static JDBCDriver jdbcDriver;
93
    private static DatabaseConnection dbConnection;
96
    
94
    
97
    protected Connection conn;
95
    protected Connection conn;
98
    protected Specification spec;
99
    protected DriverSpecification drvSpec;
100
96
101
    static {
97
    static {
102
        try {
98
        try {
103
            specfactory = new SpecificationFactory();
104
105
            driverClass = System.getProperty(DRIVER_PROPERTY, 
99
            driverClass = System.getProperty(DRIVER_PROPERTY, 
106
                    "org.apache.derby.jdbc.EmbeddedDriver");
100
                    "org.apache.derby.jdbc.EmbeddedDriver");
107
            dbname = System.getProperty(DBNAME_PROPERTY, "ddltestdb");
101
            dbname = System.getProperty(DBNAME_PROPERTY, "ddltestdb");
Lines 123-128 Link Here
123
                            
117
                            
124
            username = System.getProperty(USERNAME_PROPERTY, "testddl");
118
            username = System.getProperty(USERNAME_PROPERTY, "testddl");
125
            password = System.getProperty(PASSWORD_PROPERTY, "testddl");
119
            password = System.getProperty(PASSWORD_PROPERTY, "testddl");
120
121
            driverJarUrl = Class.forName(driverClass).getProtectionDomain().getCodeSource().getLocation();
122
126
        } catch (Exception e) {
123
        } catch (Exception e) {
127
            LOGGER.log(Level.SEVERE, null, e);
124
            LOGGER.log(Level.SEVERE, null, e);
128
            throw new RuntimeException(e);
125
            throw new RuntimeException(e);
Lines 133-158 Link Here
133
        super(name);
130
        super(name);
134
    }
131
    }
135
132
133
    /**
134
     * Get the DatabaseConnection for the configured Java DB database.  This
135
     * method will create and register the connection the first time it is called
136
     */
137
    protected static DatabaseConnection getDatabaseConnection() throws Exception {
138
        if (dbConnection == null) {
139
            JDBCDriver driver = JDBCDriver.create("derbydriver", "derbydriver", driverClass, new URL[] {driverJarUrl});
140
            JDBCDriverManager.getDefault().addDriver(driver);
141
142
            dbConnection = DatabaseConnection.create(driver, dbUrl, username, "APP", password, false);
143
            ConnectionManager.getDefault().addConnection(dbConnection);
144
        }
145
146
        return dbConnection;
147
    }
148
149
    protected static String getDbUrl() {
150
        return dbUrl;
151
    }
152
153
    protected static String getDriverClass() {
154
        return driverClass;
155
    }
156
157
    protected static String getPassword() {
158
        return password;
159
    }
160
161
    public static String getUsername() {
162
        return username;
163
    }
164
136
    
165
    
137
    public void setUp() throws Exception {
166
    @Override
167
    protected void setUp() throws Exception {
138
        try {
168
        try {
139
            getConnection();
169
            getConnection();
140
            createSchema();
170
            createSchema();
141
            setSchema();
171
            setSchema();
142
            initQuoteString();
172
            initQuoteString();
143
            spec = (Specification)specfactory.createSpecification(conn);
144
            
145
            drvSpec = specfactory.createDriverSpecification(
146
                    spec.getMetaData().getDriverName().trim());
147
            if (spec.getMetaData().getDriverName().trim().equals(
148
                    "jConnect (TM) for JDBC (TM)")) //NOI18N
149
                //hack for Sybase ASE - copied from mainline code
150
                drvSpec.setMetaData(conn.getMetaData());
151
            else
152
                drvSpec.setMetaData(spec.getMetaData());
153
            
154
            drvSpec.setCatalog(conn.getCatalog());
155
            drvSpec.setSchema(SCHEMA);
156
        } catch ( SQLException e ) {
173
        } catch ( SQLException e ) {
157
            SQLException original = e;
174
            SQLException original = e;
158
            while ( e != null ) {
175
            while ( e != null ) {
Lines 296-302 Link Here
296
        }
313
        }
297
    }
314
    }
298
    
315
    
299
    private void initQuoteString() throws Exception {
316
    protected void initQuoteString() throws Exception {
300
        if ( quoteString != null  ) {
317
        if ( quoteString != null  ) {
301
            return;
318
            return;
302
        }
319
        }
Lines 579-645 Link Here
579
        return numrows;
596
        return numrows;
580
    }
597
    }
581
    
598
    
582
    protected void createBasicTable(String tablename, String pkeyName) 
583
            throws Exception {
584
        dropTable(tablename);
585
        CreateTable cmd = spec.createCommandCreateTable(tablename);
586
        cmd.setObjectOwner(SCHEMA);
587
                
588
        // primary key
589
        TableColumn col = cmd.createPrimaryKeyColumn(pkeyName);
590
        col.setColumnType(Types.INTEGER);
591
        col.setNullAllowed(false);
592
        
593
        cmd.execute();
594
    }
595
    
596
    protected void createView(String viewName, String query) throws Exception {
597
        CreateView cmd = spec.createCommandCreateView(viewName);
598
        cmd.setQuery(query);
599
        cmd.setObjectOwner(SCHEMA);
600
        cmd.execute();
601
        
602
        assertFalse(cmd.wasException());        
603
    }
604
605
    protected void createSimpleIndex(String tablename, 
606
            String indexname, String colname) throws Exception {
607
        // Need to get identifier into correct case because we are
608
        // still quoting referred-to identifiers.
609
        tablename = fixIdentifier(tablename);
610
        CreateIndex xcmd = spec.createCommandCreateIndex(tablename);
611
        xcmd.setIndexName(indexname);
612
613
        // *not* unique
614
        xcmd.setIndexType(new String());
615
616
        xcmd.setObjectOwner(SCHEMA);
617
        xcmd.specifyColumn(fixIdentifier(colname));
618
619
        xcmd.execute();        
620
    }
621
    
622
    /**
623
     * Adds a basic column.  Non-unique, allows nulls.
624
     */
625
    protected void addBasicColumn(String tablename, String colname,
626
            int type, int size) throws Exception {
627
        // Need to get identifier into correct case because we are
628
        // still quoting referred-to identifiers.
629
        tablename = fixIdentifier(tablename);
630
        AddColumn cmd = spec.createCommandAddColumn(tablename);
631
        cmd.setObjectOwner(SCHEMA);
632
        TableColumn col = (TableColumn)cmd.createColumn(colname);
633
        col.setColumnType(type);
634
        col.setColumnSize(size);
635
        col.setNullAllowed(true);
636
        
637
        cmd.execute();
638
        if ( cmd.wasException() ) {
639
            throw new Exception("Unable to add column");
640
        }
641
    }
642
    
643
    protected void tearDown() throws Exception {
599
    protected void tearDown() throws Exception {
644
        if ( conn != null ) {
600
        if ( conn != null ) {
645
            try {
601
            try {
(-)a/db/test/unit/src/org/netbeans/modules/db/util/DDLTestBase.java (+167 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.modules.db.util;
41
42
import java.sql.SQLException;
43
import java.sql.Types;
44
import java.util.logging.Level;
45
import java.util.logging.Logger;
46
import org.netbeans.lib.ddl.impl.AddColumn;
47
import org.netbeans.lib.ddl.impl.CreateIndex;
48
import org.netbeans.lib.ddl.impl.CreateTable;
49
import org.netbeans.lib.ddl.impl.CreateView;
50
import org.netbeans.lib.ddl.impl.DriverSpecification;
51
import org.netbeans.lib.ddl.impl.Specification;
52
import org.netbeans.lib.ddl.impl.SpecificationFactory;
53
import org.netbeans.lib.ddl.impl.TableColumn;
54
55
/**
56
 *
57
 * @author David
58
 */
59
public class DDLTestBase extends DBTestBase {
60
    private static Logger LOGGER = Logger.getLogger(DDLTestBase.class.getName());
61
62
    protected static SpecificationFactory specfactory;
63
    protected Specification spec;
64
    protected DriverSpecification drvSpec;
65
66
    static {
67
        try {
68
            specfactory = new SpecificationFactory();
69
        } catch (Exception e) {
70
            LOGGER.log(Level.SEVERE, null, e);
71
            throw new RuntimeException(e);
72
        }
73
    }
74
75
76
    public DDLTestBase(String name) {
77
        super(name);
78
    }
79
80
    protected void setUp() throws Exception {
81
        super.setUp();
82
83
        try {
84
            spec = (Specification)specfactory.createSpecification(conn);
85
86
            drvSpec = specfactory.createDriverSpecification(
87
                    spec.getMetaData().getDriverName().trim());
88
            if (spec.getMetaData().getDriverName().trim().equals(
89
                    "jConnect (TM) for JDBC (TM)")) //NOI18N
90
                //hack for Sybase ASE - copied from mainline code
91
                drvSpec.setMetaData(conn.getMetaData());
92
            else
93
                drvSpec.setMetaData(spec.getMetaData());
94
95
            drvSpec.setCatalog(conn.getCatalog());
96
            drvSpec.setSchema(SCHEMA);
97
        } catch ( SQLException e ) {
98
            SQLException original = e;
99
            while ( e != null ) {
100
                LOGGER.log(Level.SEVERE, null, e);
101
                e = e.getNextException();
102
            }
103
104
            throw original;
105
        }
106
    }
107
    protected void createBasicTable(String tablename, String pkeyName)
108
            throws Exception {
109
        dropTable(tablename);
110
        CreateTable cmd = spec.createCommandCreateTable(tablename);
111
        cmd.setObjectOwner(SCHEMA);
112
113
        // primary key
114
        TableColumn col = cmd.createPrimaryKeyColumn(pkeyName);
115
        col.setColumnType(Types.INTEGER);
116
        col.setNullAllowed(false);
117
118
        cmd.execute();
119
    }
120
121
    protected void createView(String viewName, String query) throws Exception {
122
        CreateView cmd = spec.createCommandCreateView(viewName);
123
        cmd.setQuery(query);
124
        cmd.setObjectOwner(SCHEMA);
125
        cmd.execute();
126
127
        assertFalse(cmd.wasException());
128
    }
129
130
    protected void createSimpleIndex(String tablename,
131
            String indexname, String colname) throws Exception {
132
        // Need to get identifier into correct case because we are
133
        // still quoting referred-to identifiers.
134
        tablename = fixIdentifier(tablename);
135
        CreateIndex xcmd = spec.createCommandCreateIndex(tablename);
136
        xcmd.setIndexName(indexname);
137
138
        // *not* unique
139
        xcmd.setIndexType(new String());
140
141
        xcmd.setObjectOwner(SCHEMA);
142
        xcmd.specifyColumn(fixIdentifier(colname));
143
144
        xcmd.execute();
145
    }
146
147
    /**
148
     * Adds a basic column.  Non-unique, allows nulls.
149
     */
150
    protected void addBasicColumn(String tablename, String colname,
151
            int type, int size) throws Exception {
152
        // Need to get identifier into correct case because we are
153
        // still quoting referred-to identifiers.
154
        tablename = fixIdentifier(tablename);
155
        AddColumn cmd = spec.createCommandAddColumn(tablename);
156
        cmd.setObjectOwner(SCHEMA);
157
        TableColumn col = (TableColumn)cmd.createColumn(colname);
158
        col.setColumnType(type);
159
        col.setColumnSize(size);
160
        col.setNullAllowed(true);
161
162
        cmd.execute();
163
        if ( cmd.wasException() ) {
164
            throw new Exception("Unable to add column");
165
        }
166
    }
167
}

Return to bug 128546