• Donald H. (Donnie) Pinkston, III's avatar
    Add shell-commands to interactive client · ceec8c9f
    Donald H. (Donnie) Pinkston, III authored
    NanoDB client now supports shell commands of the form \cmd, which are
    handled within the InteractiveClient, not by the server.  There are two
    commands:
    
    \help prints out useful help information
    
    \source filename.sql will run all commands contained in the file
    filename.sql
    
    There are surely interesting wrinkles to iron out.  For example, if the
    sourced file contains an "EXIT" command then it will exit the client.
    Also, an error should probably terminate the evaluation of a sourced
    file, but currently it does not.
    ceec8c9f
ExclusiveClient.java 2.18 KB
package edu.caltech.nanodb.client;


import java.io.IOException;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import edu.caltech.nanodb.commands.Command;
import edu.caltech.nanodb.server.CommandResult;
import edu.caltech.nanodb.server.NanoDBException;
import edu.caltech.nanodb.server.NanoDBServer;
import org.antlr.v4.runtime.misc.ParseCancellationException;


/**
 * <p>
 * This class is used for starting the NanoDB database in "exclusive mode,"
 * where only a single client interacts directly with the database system.
 * In fact, the exclusive client embeds the {@link NanoDBServer} instance in
 * the client object, since there is no need to interact with the server over
 * a network socket.
 * </p>
 * <p>
 * <b>Note that it is <u>wrong</u> to start multiple exclusive clients against
 * the same data directory!!!</b>  Exclusive-mode operation expects that only
 * this server is interacting with the data files.  For concurrent access from
 * multiple clients, see the {@link SharedServerClient} class.
 * </p>
 */
public class ExclusiveClient extends InteractiveClient {
    private static Logger logger = LogManager.getLogger(ExclusiveClient.class);


    /** The server that this exclusive client is using. */
    private NanoDBServer server;


    @Override
    public void startup() {
        // Start up the various database subsystems that require initialization.
        server = new NanoDBServer();
        try {
            server.startup();
        }
        catch (NanoDBException e) {
            System.out.println("DATABASE STARTUP FAILED:");
            e.printStackTrace(System.out);
            System.exit(1);
        }
    }


    @Override
    public CommandResult handleCommand(String command) {
        return server.doCommand(command, false);
    }


    @Override
    public void shutdown() {
        // Shut down the various database subsystems that require cleanup.
        if (!server.shutdown())
            System.out.println("DATABASE SHUTDOWN FAILED.");
    }


    public static void main(String args[]) {
        ExclusiveClient client = new ExclusiveClient();

        client.startup();
        client.mainloop();
        client.shutdown();
    }
}