Search in sources :

Example 6 with Path

use of com.teradata.jaqy.interfaces.Path in project jaqy by Teradata.

the class Main method loadInit.

private static void loadInit(Globals globals, JaqyInterpreter interpreter, Display display, Path initFile) {
    LineInput lineInput;
    try {
        Reader reader = new InputStreamReader(Main.class.getResourceAsStream(INTERNAL_INIT_RC), "UTF-8");
        Path startDir = new FilePath(new File(System.getProperty("user.dir")));
        lineInput = new ReaderLineInput(reader, startDir, false);
        interpreter.push(lineInput);
        interpreter.interpret(false);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    if (initFile != null && initFile.exists()) {
        // check if ~/.jqrc exists
        try {
            lineInput = LineInputFactory.getLineInput(initFile, null, false);
            interpreter.push(lineInput);
            interpreter.interpret(false);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Also used : FilePath(com.teradata.jaqy.path.FilePath) Path(com.teradata.jaqy.interfaces.Path) FilePath(com.teradata.jaqy.path.FilePath) ReaderLineInput(com.teradata.jaqy.lineinput.ReaderLineInput) JLineConsoleLineInput(com.teradata.jaqy.lineinput.JLineConsoleLineInput) LineInput(com.teradata.jaqy.interfaces.LineInput) CommandLineInput(com.teradata.jaqy.lineinput.CommandLineInput) ReaderLineInput(com.teradata.jaqy.lineinput.ReaderLineInput)

Example 7 with Path

use of com.teradata.jaqy.interfaces.Path in project jaqy by Teradata.

the class Main method main.

public static void main(String[] args) throws Exception {
    // disable jline trace messages on SLES systems
    jline.internal.Log.setOutput(new PrintStream(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        }
    }));
    Globals globals = new Globals();
    // initiate the name and version
    Package pkg = Main.class.getPackage();
    globals.setName(pkg.getImplementationTitle());
    globals.setVersion(pkg.getImplementationVersion());
    globals.getOs();
    // install Jansi
    if (Os.isWindows()) {
        AnsiConsole.systemInstall();
    }
    // initiate the greeting message.
    try {
        String greet = StringUtils.getStringFromStream(Main.class.getResourceAsStream(GREET_RC));
        globals.setGreeting(greet);
    } catch (Exception ex) {
    }
    // initiate the display
    ConsoleDisplay display = new ConsoleDisplay(globals);
    // Add shutdown hook that closes all sessions on exit.
    JaqyShutdownHook.register(globals);
    globals.getOs();
    // register signal handlers for dealing with Ctrl-C
    if (!Os.isWindows()) {
    // The signal handler only works on Linux
    // new ConsoleSignalHandler (display).register ();
    }
    // now create an initial session and set the session to it.
    Session session = globals.createSession(display);
    // create an interpreter
    JaqyInterpreter interpreter = new JaqyInterpreter(globals, display, session);
    // initiate settings
    SettingSetup.init(globals);
    // initiate commands
    CommandSetup.init(globals);
    // handle command line options
    OptionSetup.init(globals);
    // install predefined helper factories
    HelperSetup.init(globals);
    try {
        globals.loadRC(ClassLoader.getSystemClassLoader(), interpreter);
    } catch (Exception ex) {
    }
    // load initiation scripts
    Path initFile = new FilePath(getDefaultInitFile());
    CommandLine cmdLine = globals.getOptionManager().getCommandLine(args);
    if (cmdLine.hasOption("norc")) {
        initFile = null;
    } else if (cmdLine.hasOption("rcfile")) {
        String fileName = cmdLine.getOptionValue("rcfile");
        initFile = new FilePath(new File(fileName));
    }
    loadInit(globals, interpreter, display, initFile);
    // Now handle command line options
    // We want to do this after loading the initiation script to
    // allow any custom addons to be installed.
    args = globals.getOptionManager().handleOptions(globals, display, args);
    // print the start up screen
    initScreen(globals, display);
    // we are done with the loading phase
    display.setInitiated();
    // display the title
    if (display.isInteractive()) {
        display.showTitle(interpreter);
    }
    // reset the command counter so we get consistent result regardless
    // of the initiation script
    interpreter.resetCommandCount();
    Path currentDir = new FilePath(globals.getDirectory());
    // setup the input
    if (display.isInteractive()) {
        globals.getOs();
        if (Os.isWindows()) {
            // windows
            // 
            // Windows have its own readline-like support for
            // all apps, so we can just use the default system
            // behavior.
            interpreter.push(LineInputFactory.getSimpleLineInput(System.in, currentDir, true));
        } else {
            try {
                // we use JLine other systems.
                interpreter.push(new JLineConsoleLineInput(currentDir));
            } catch (IOException ex) {
                // just in case we fail with JLine,
                // fall back to default.
                interpreter.push(LineInputFactory.getSimpleLineInput(System.in, currentDir, true));
            }
        }
    } else {
        if (!skipStdin) {
            String encoding = null;
            if (System.in.available() == 0) {
                // If the input available bytes is 0, it is possible the input
                // is not available.  So we do not want to guess the input
                // encoding at all.  Instead, use the default.
                encoding = Charset.defaultCharset().displayName();
            }
            interpreter.push(LineInputFactory.getLineInput(System.in, currentDir, encoding, false));
        }
    }
    // Interpret any remaining command line arguments first
    if (args.length > 0) {
        interpreter.push(new CommandLineInput(args, currentDir));
    }
    // parse the user commands
    interpreter.interpret(display.isInteractive());
    if (!skipStdin) {
        globals.log(Level.INFO, "Errors: " + interpreter.getErrorCount() + ", Failures: " + interpreter.getFailureCount());
        System.exit(interpreter.getExitCode());
    }
}
Also used : FilePath(com.teradata.jaqy.path.FilePath) Path(com.teradata.jaqy.interfaces.Path) FilePath(com.teradata.jaqy.path.FilePath) JLineConsoleLineInput(com.teradata.jaqy.lineinput.JLineConsoleLineInput) CommandLine(org.apache.commons.cli.CommandLine) CommandLineInput(com.teradata.jaqy.lineinput.CommandLineInput)

Example 8 with Path

use of com.teradata.jaqy.interfaces.Path in project jaqy by Teradata.

the class ExecCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws Exception {
    if (args.length == 0) {
        interpreter.setParseAction(this, null);
    } else {
        CommandLine cmdLine = getCommandLine(args);
        args = cmdLine.getArgs();
        String charset = null;
        for (Option option : cmdLine.getOptions()) {
            if ("c".equals(option.getOpt())) {
                charset = option.getValue();
            }
        }
        if (args.length > 0) {
            Path file = interpreter.getPath(args[0]);
            if (!file.exists()) {
                interpreter.error("file not found: " + args[0]);
            }
            SessionUtils.checkOpen(interpreter);
            Reader reader = FileUtils.getReader(file.getInputStream(), charset);
            String sql = StringUtils.getStringFromReader(reader);
            Session session = interpreter.getSession();
            session.executeQuery(sql, interpreter, interpreter.getRepeatCount());
            // now reset things
            interpreter.setRepeatCount(1);
            interpreter.setLimit(0);
        } else {
            interpreter.setParseAction(this, null);
        }
    }
}
Also used : Path(com.teradata.jaqy.interfaces.Path) CommandLine(org.apache.commons.cli.CommandLine) Reader(java.io.Reader) Option(org.apache.commons.cli.Option) Session(com.teradata.jaqy.Session)

Example 9 with Path

use of com.teradata.jaqy.interfaces.Path in project jaqy by Teradata.

the class URLPathTest method test2.

@Test(expected = IOException.class)
public void test2() throws Exception {
    HttpPathHandler handler = new HttpPathHandler();
    String url = "https://www.example.com/test.csv";
    Path path = handler.getPath(url, null);
    path.getOutputStream();
}
Also used : Path(com.teradata.jaqy.interfaces.Path) Test(org.junit.Test)

Example 10 with Path

use of com.teradata.jaqy.interfaces.Path in project jaqy by Teradata.

the class CSVExporterFactory method getHandler.

@Override
public JaqyExporter getHandler(CommandLine cmdLine, JaqyInterpreter interpreter) throws Exception {
    Charset charset = DEFAULT_CHARSET;
    CSVFormat format = CSVFormat.DEFAULT;
    HashMap<Integer, CSVExportInfo> exportInfoMap = new HashMap<Integer, CSVExportInfo>();
    CSVNameGen nameGen = new CSVNameGen(DEFAULT_NAME_PATTERN);
    Charset encoding = DEFAULT_CHARSET;
    for (Option option : cmdLine.getOptions()) {
        switch(option.getOpt().charAt(0)) {
            case 'c':
                {
                    charset = Charset.forName(option.getValue());
                    break;
                }
            case 'd':
                {
                    char delimiter = CSVUtils.getChar(option.getValue());
                    if (delimiter == 0)
                        throw new IllegalArgumentException("invalid delimiter: " + option.getValue());
                    format = format.withDelimiter(delimiter);
                    break;
                }
            case 't':
                {
                    format = CSVUtils.getFormat(option.getValue());
                    break;
                }
            case 'n':
                {
                    String fmt = option.getValue();
                    nameGen = new CSVNameGen(fmt);
                    // now check if the name is a valid format.
                    if (fmt.equals(nameGen.getName(1)))
                        interpreter.error("Invalid name pattern: " + fmt);
                    break;
                }
            case 'e':
                {
                    encoding = Charset.forName(option.getValue());
                    break;
                }
            case 'f':
                {
                    int column = Integer.parseInt(option.getValue());
                    if (column < 1) {
                        interpreter.error("Column index cannot be smaller than 1.");
                    }
                    CSVExportInfo info = new CSVExportInfo(nameGen, encoding);
                    exportInfoMap.put(column, info);
                    break;
                }
        }
    }
    String[] args = cmdLine.getArgs();
    if (args.length == 0)
        throw new IllegalArgumentException("missing file name.");
    Path file = interpreter.getPath(args[0]);
    return new CSVExporter(file, charset, format, exportInfoMap);
}
Also used : Path(com.teradata.jaqy.interfaces.Path) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) CSVExportInfo(com.teradata.jaqy.utils.CSVExportInfo) CSVNameGen(com.teradata.jaqy.utils.CSVNameGen) CSVFormat(org.apache.commons.csv.CSVFormat) Option(org.apache.commons.cli.Option)

Aggregations

Path (com.teradata.jaqy.interfaces.Path)12 Test (org.junit.Test)4 Reader (java.io.Reader)3 CommandLine (org.apache.commons.cli.CommandLine)3 Option (org.apache.commons.cli.Option)3 CommandLineInput (com.teradata.jaqy.lineinput.CommandLineInput)2 JLineConsoleLineInput (com.teradata.jaqy.lineinput.JLineConsoleLineInput)2 FilePath (com.teradata.jaqy.path.FilePath)2 Session (com.teradata.jaqy.Session)1 LineInput (com.teradata.jaqy.interfaces.LineInput)1 ReaderLineInput (com.teradata.jaqy.lineinput.ReaderLineInput)1 FileBlob (com.teradata.jaqy.resultset.FileBlob)1 FileClob (com.teradata.jaqy.resultset.FileClob)1 CSVExportInfo (com.teradata.jaqy.utils.CSVExportInfo)1 CSVImportInfo (com.teradata.jaqy.utils.CSVImportInfo)1 CSVNameGen (com.teradata.jaqy.utils.CSVNameGen)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1