/*

   Derby - Class SimpleApp

   Copyright 2001, 2004 The Apache Software Foundation or its licensors, as applicable.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.Properties;


/**
 * This sample program is a minimal JDBC application showing
 * JDBC access to Derby.
 *
 * Instructions for how to run this program are
 * given in <A HREF=example.html>example.html</A>.
 *
 * Derby applications can run against Derby running in an embedded
 * or a client/server framework. When Derby runs in an embedded framework,
 * the Derby application and Derby run in the same JVM. The application
 * starts up the Derby engine. When Derby runs in a client/server framework,
 * the application runs in a different JVM from Derby. The application only needs
 * to start the client driver, and the connectivity framework provides network connections.
 * (The server must already be running.)
 *
 * <p>When you run this application, give one of the following arguments:
 *    * embedded (default, if none specified)
 *    * derbyclient (will use the Net client driver to access Network Server)
 *    * jccjdbcclient (if Derby is running embedded in the JCC Server framework)
 *
 * @author janet
 */
public class SimpleApp
{
    /* the default framework is embedded*/
    public String framework = "embedded";
    public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    public String protocol = "jdbc:derby:";

    public static void main(String[] args)
    {
        new SimpleApp().go(args);
    }

    void go(String[] args)
    {
        /* parse the arguments to determine which framework is desired*/
        parseArguments(args);

        System.out.println("SimpleApp starting in " + framework + " mode.");

        try
        {
            /*
               The driver is installed by loading its class.
               In an embedded environment, this will start up Derby, since it is not already running.
             */
            Class.forName(driver).newInstance();
            System.out.println("Loaded the appropriate driver.");

            Connection conn = null;
            Properties props = new Properties();
            props.put("user", "user1");
            props.put("password", "user1");

            /*
               The connection specifies create=true to cause
               the database to be created. To remove the database,
               remove the directory derbyDB and its contents.
               The directory derbyDB will be created under
               the directory that the system property
               derby.system.home points to, or the current
               directory if derby.system.home is not set.
             */
            System.setProperty("derby.storage.fileSyncTransactionLog", "true");
            conn = DriverManager.getConnection(protocol +
                    "derbyDB;create=true", props);

            System.out.println("Connected to and created database derbyDB");

            conn.setAutoCommit(false);

            /*
               Creating a statement lets us issue commands against
               the connection.
             */
            Statement s = conn.createStatement();

            /*
               We create a table, add a few rows, and update one.
             */
            s.execute("create table derbyDB(num int, addr varchar(40))");
            System.out.println("Created table derbyDB");
            s.execute("insert into derbyDB values (1956,'Webster St.')");
            System.out.println("Inserted 1956 Webster");
            
            // inserting calls to shutdown and get the connection again
            // to test that the connection can be obtained a second time
            // on Mac OS X after setting the fileSyncTransactionLog property.
            conn.commit();
            System.out.println("shutdown");
            try {
              DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (Exception e) {}
            System.out.println("filesync: " + System.getProperty("derby.storage.fileSyncTransactionLog"));
            System.out.println("getting new connection");
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(protocol +
                    "derbyDB", props);
            System.out.println("filesync: " + System.getProperty("derby.storage.fileSyncTransactionLog"));
            conn.setAutoCommit(false);
            s = conn.createStatement();
            System.out.println("trying next insert");
            //

            s.execute("insert into derbyDB values (1910,'Union St.')");
            System.out.println("Inserted 1910 Union");
            s.execute(
                "update derbyDB set num=180, addr='Grand Ave.' where num=1956");
            System.out.println("Updated 1956 Webster to 180 Grand");

            s.execute(
                "update derbyDB set num=300, addr='Lakeshore Ave.' where num=180");
            System.out.println("Updated 180 Grand to 300 Lakeshore");

            /*
               We select the rows and verify the results.
             */
            ResultSet rs = s.executeQuery(
                    "SELECT num, addr FROM derbyDB ORDER BY num");

            if (!rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            if (rs.getInt(1) != 300)
            {
                throw new Exception("Wrong row returned");
            }

            if (!rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            if (rs.getInt(1) != 1910)
            {
                throw new Exception("Wrong row returned");
            }

            if (rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            System.out.println("Verified the rows");

            s.execute("drop table derbyDB");
            System.out.println("Dropped table derbyDB");

            /*
               We release the result and statement resources.
             */
            rs.close();
            s.close();
            System.out.println("Closed result set and statement");

            /*
               We end the transaction and the connection.
             */
            conn.commit();
            conn.close();
            System.out.println("Committed transaction and closed connection");

            /*
               In embedded mode, an application should shut down Derby.
               If the application fails to shut down Derby explicitly,
               the Derby does not perform a checkpoint when the JVM shuts down, which means
               that the next connection will be slower.
               Explicitly shutting down Derby with the URL is preferred.
               This style of shutdown will always throw an "exception".
             */
            boolean gotSQLExc = false;

            if (framework.equals("embedded"))
            {
                try
                {
                    DriverManager.getConnection("jdbc:derby:;shutdown=true");
                }
                catch (SQLException se)
                {
                    gotSQLExc = true;
                }

                if (!gotSQLExc)
                {
                    System.out.println("Database did not shut down normally");
                }
                else
                {
                    System.out.println("Database shut down normally");
                }
            }
        }
        catch (Throwable e)
        {
            System.out.println("exception thrown:");

            if (e instanceof SQLException)
            {
                printSQLError((SQLException) e);
            }
            else
            {
                e.printStackTrace();
            }
        }

        System.out.println("SimpleApp finished");
    }

    static void printSQLError(SQLException e)
    {
        while (e != null)
        {
            System.out.println(e.toString());
            e = e.getNextException();
        }
    }

    private void parseArguments(String[] args)
    {
        int length = args.length;

        for (int index = 0; index < length; index++)
        {
            if (args[index].equalsIgnoreCase("jccjdbcclient"))
            {
                framework = "jccjdbc";
                driver = "com.ibm.db2.jcc.DB2Driver";
                protocol = "jdbc:derby:net://localhost:1527/";
            }
            if (args[index].equalsIgnoreCase("derbyclient"))
            {
                framework = "derbyclient";
                driver = "org.apache.derby.jdbc.ClientDriver";
                protocol = "jdbc:derby://localhost:1527/";
            }
        }
    }
}
