Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hyee committed Sep 16, 2015
1 parent 1911554 commit 6574f10
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 91 deletions.
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,56 @@
License
============
MIT license, meaning that you are completely free to redistribute, modify, or sell it with almost no restrictions.

dbcli : a CLI utility for DBA (Oracle/DB2)
=====================================================

[![Licence](http://img.shields.io/badge/Licence-MIT-brightgreen.svg)](COPYRIGHT)

Description
=============
-----------------------

DBCLI is a database command line utility that developed with LUA language, and bridges to JDBC in order to operate database.<br>
It is designed to support multiple databases, and includes 2 type of modules, one is the public functions, and another one is the database-specific functions.<br>
Currently only public and Oracle and DB2 functions are developed, and only support windows platform.<br><br>

For Oracle db, it does not aim to support all functions of SQL*Plus, but to provide the features other than SQL*Plus:<br>
1) Pretty print of the select statement and the flexible controll of the grid layouts(see set command)<br>
2) The 'alias' command to reduce the frequent inputs<br>
3) The powerful "snap" and "ora" commands as the replacement of the SQL*Plus script, so that DBA is able to quick define and execute the SQL templates with the smallest input<br>
4) Functions are easy to extend or deploy, because they are all developed with lua language<br>

* Pretty print of the select statement and the flexible control of the grid layouts(see set command)
* The `alias` command to reduce the frequent inputs
* The powerful `snap` and `ora` commands as the replacement of the SQL*Plus script, so that DBA is able to quickly define and execute the SQL templates with the smallest input
* Functions are easy to extend or deploy, because they are all developed with lua language


Build and Configuration
============
DBCLI does not require compile before its use, directly click "Download ZIP" and extract the file. Branch "Compat" contains the shrinked JRE bundle, you may switch to that branch to download.<br>
-----------------------
DBCLI does not require compile before its use, but depends on JRE 1.7+. Directly click `Download ZIP` and extract.<br/>
Branch `with-JRE` contains the shrinking JRE bundle, if you don't have JRE installed, you may switch to that branch to download. Due to the region and charset differences, this JRE bundle is not guaranteed to work well.<br/>
###Configure terminal window
To avoid the word wrap in the terminal window which affects the print layout, following settings are recommended in Windows command window:<br>
1. In the "Layout" tab, set buffer width and buffer height as 500+<br>
2. In the "Font" tab, use Roster font with the size is 6 * 12<br>
* In the `Layout` tab, set buffer width and buffer height as `500+`
* In the `Font` tab, use Roster font with the size is `6 * 12`

###Configure enviroment
Before running dbcli, make sure that you have installed JRE 1.7+ in your local PC.<br>
Open dbcli.bat with text editor, modify the "JRE_HOME" and "TNS_ADM" variables as your PC's situation, then click the bat to run.<br>
It also support command line options with "<command>=<args>" format, all available options can be found by typing "help":<br>
Create file `init.cfg` under the "data" directory with following content:

SET JRE_HOME=<JRE HOME>
SET TNS_ADM=<location of tnsnames.ora>

Of which `TNS_ADM` is optional unless you need to connect Oracle via tnsnames.ora, or may also refer to `init_sample.cfg` for more settings. An example is:

SET JRE_HOME=d:\soft\java
SET TNS_ADM=d:\Soft\InstanceClient\network\admin

It also support command line options with `"<command>=<args>"` format, all available options can be found by typing `help`:<br>

[drag]:https://raw.githubusercontent.com/hyee/dbcli/master/help.gif
![][drag]

For example: dbcli.bat "connect=tiger/scott@orcl"<br><br>
For example: `dbcli.bat "connect=tiger/scott@orcl"`<br><br>

Besides the above commands, all database statements are also supported(i.e select/update/delete)<br><br>

Quick Start
============
After the above configurations, you are able to successfully start the dbcli shell by executing dbcli.bat or bin\ConsoleZ\Console.exe.<br>
-----------------------
After the above configurations, you are able to successfully start the dbcli shell by executing `dbcli.bat` or `bin\ConsoleZ\Console.exe`.<br>
Below are some common use of the utility:<br>

1. "conn" or "connect" to connect to the database with or without installing Oracle client, and "reconn" to re-connect.
Expand All @@ -48,11 +59,11 @@ Below are some common use of the utility:<br>
4. "ora" command to execute pre-defined scripts under the "ora" sub-directory. i.e., ora ashtop, ora actives
5. "alias" command which is similar to the Linux command
6. "set" command to config the shell enviroment
7. 'q' or CTRL + D to abort running SQL
7. 'q' or CTRL + D or CTRL + C to abort running SQL
8. "help" or "help -a" to see other available commands.

References
============
-----------------------
The project depends on some binary files of open-source projects without modifications, the binaries have been included into this project.
Below is the list:<br>

Expand All @@ -72,6 +83,6 @@ Below is the list:<br>
Besides, some SQL scripts are from internet.

About ANSICON
============
-----------------------
ANSICON provides more ANSI escape features than JLine, especially when running bash commands(i.e.: top) via SSH.<br/>
If you have concern on ANSICON, please remove it from bin, and edit data\init.cfg to add line "set ANSICON_CMD="
If you have concern on ANSICON, please remove it from bin, and edit `data\init.cfg` to add line `set ANSICON_CMD=`
2 changes: 1 addition & 1 deletion bin/ConsoleZ/console.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<styles caption="1" resizable="1" taskbar_button="1" border="0" inside_border="2" tray_icon="1" quake_like="0" jumplist="1" integrated_ime="1" quake_animation_time="300" hide_when_inactive="0">
<selection_color r="128" g="128" b="128"/>
</styles>
<position x="0" y="0" dock="0" snap="-1" z_order="0" save_position="1" w="1518" h="916" save_size="1"/>
<position x="-7" y="2" dock="0" snap="-1" z_order="0" save_position="1" w="1514" h="905" save_size="1"/>
<transparency type="0" active_alpha="206" inactive_alpha="255" r="0" g="0" b="0"/>
<fullscreen start_in_fullscreen="0" fullscreen_monitor="0"/>
</appearance>
Expand Down
Binary file modified lib/dbcli.jar
Binary file not shown.
Binary file modified lib/jline.jar
Binary file not shown.
1 change: 0 additions & 1 deletion lua/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ function env.parse_args(cmd,rest)
end

function env.force_end_input()
if not env.pending_command() then return end
if curr_stmt then
local stmt={multi_cmd,env.parse_args(multi_cmd,curr_stmt)}
multi_cmd,curr_stmt=nil,nil
Expand Down
9 changes: 9 additions & 0 deletions oracle/ora/redef.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*[[Generate the script for online-redefintion on target table, no DDL will be taken. Usage: redef [owner.]<table_name>]]*/

ora _find_object &V1

DECLARE
p
BEGIN

END;
21 changes: 2 additions & 19 deletions src/copy_to_git.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,8 @@ cd ..
mkdir "%target%"
cd /d "%target%"
REM git pull
del /s /f "%target%\*.lua"
del /s /f "%target%\*.jar"
del /s /f "%target%\*.zip"
del /s /f "%target%\*.gz"
del /s /f "%target%\*.cfg"
del /s /f "%target%\*.sql"
del /s /f "%target%\*.chm"
del /s /f "%target%\*.bat"
del /s /f "%target%\*.txt"
del /s /f "%target%\*.bak"
del /s /f "%target%\*.so"
del /s /f "%target%\*.log"
del /s /f "%target%\*.dll"
del /s /f "%target%\*.bat"
del /s /f "%target%\*.exe"
del /s /f "%target%\*.java"
del /s /f "%target%\*.class"
del /s /f "%target%\*.chart"
del /s /f "%target%\*.snap"
forfiles /c "cmd /c if @isdir==TRUE (if @file NEQ \".git\" if @file NEQ \"aliases\" (del /S/F/Q @file\*.*)) else (del /S/F/Q @file)"

cd /d "%~dp0"
cd ..
copy data\*_sample.cfg "%target%\data"
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/dbcli/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class EventReader implements Runnable {
public void run() {
try {
int ch = in.read(1L);
if (ch <= 0) return;
if (ch < -1) return;
//System.out.println(ch);
for (int i = 0; i < keys.length; i++) {
if (ch != keys[i] && keys[i] != '*') continue;
Expand Down
90 changes: 43 additions & 47 deletions src/java/org/dbcli/SSHExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import com.jcraft.jsch.*;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.CompletionHandler;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
Expand All @@ -26,6 +25,7 @@ public class SSHExecutor {
public String host;
public String user;
public int port;
PrintWriter writer;
public String password;
public String prompt;
public ChannelShell shell;
Expand Down Expand Up @@ -59,19 +59,7 @@ public SSHExecutor(String host, int port, String user, String password, String l
connect(host, port, user, password, linePrefix);
}

public void output(String message, boolean newLine) {
StringBuilder sb = new StringBuilder((linePrefix + message).length());
if (newLine) sb.append(linePrefix);
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (c == '\r') continue;
sb.append(c);
if (c == '\n') sb.append(linePrefix);
}
if (!newLine) System.out.print(sb.toString());
else System.out.println(sb.toString());
System.out.flush();
}


public void connect(String host, int port, String user, final String password, String linePrefix) throws Exception {
try {
Expand All @@ -87,10 +75,8 @@ public void connect(String host, int port, String user, final String password, S
session.setServerAliveInterval((int) TimeUnit.SECONDS.toMillis(10));
session.setServerAliveCountMax(10);
session.setTimeout(0);
session.setInputStream(System.in);
session.setOutputStream(System.out);
session.connect();
session.setUserInfo((UserInfo) new SSHUserInfo("jsch"));
session.setUserInfo(new SSHUserInfo("jsch"));
this.host = host;
this.port = port;
this.user = user;
Expand All @@ -102,6 +88,7 @@ public void connect(String host, int port, String user, final String password, S
shellWriter = new PipedOutputStream(pipeIn);
pr = new Printer();
pr.reset(true);
writer=new PrintWriter((TERMTYPE!="none")?new OutputStreamWriter(System.out,Console.charset):Console.writer);
Interrupter.listen("SSHExecutor", new InterruptCallback() {
@Override
public void interrupt(ActionEvent e) throws Exception {
Expand All @@ -113,6 +100,7 @@ public void interrupt(ActionEvent e) throws Exception {
});
shell.setInputStream(pipeIn);
shell.setOutputStream(pr);

shell.setEnv("TERM", TERMTYPE == "none" ? "ansi" : TERMTYPE);
shell.setPty(true);
shell.setPtyType(TERMTYPE == "none" ? "ansi" : TERMTYPE, COLS, ROWS, 0, 0);
Expand All @@ -123,6 +111,10 @@ public void interrupt(ActionEvent e) throws Exception {
}
}

public void setEnv(String name,String value) {

}

public void setTermType(String termType, int cols, int rows) throws Exception{
TERMTYPE = termType.intern();
COLS = cols;
Expand All @@ -134,7 +126,12 @@ public void setTermType(String termType, int cols, int rows) throws Exception{
}

public boolean isConnect() {
return shell == null ? false : shell.isConnected();
if(shell==null) return false;
if(!shell.isConnected()) {
close();
return false;
}
return true;
}

public void close() {
Expand All @@ -143,10 +140,7 @@ public void close() {
isWating = false;
if (pr != null) pr.close();
if (shellWriter != null) shellWriter.close();
if (shell != null) {
shell.getInputStream().close();
shell.disconnect();
}
if (shell != null) shell.disconnect();
if (session != null) session.disconnect();
} catch (Exception e) {
//e.printStackTrace();
Expand Down Expand Up @@ -243,40 +237,45 @@ public boolean promptYesNo(String arg0) {
}

public void showMessage(String m) {
output(m, true);
System.out.println(m);
}
}

class Printer extends OutputStream {
StringBuilder sb;
ByteBuffer buf= ByteBuffer.allocateDirect(1000000);
StringBuilder sb = new StringBuilder(128);
char lastChar;
Pattern p = Pattern.compile("\33\\[[\\d\\;]+[mK]");

boolean ignoreMessage;

public Printer() {
buf.order(ByteOrder.nativeOrder());
reset(false);
}

@Override
public void write(int i) throws IOException {
char c = (char) i;
//if (i == 0 || c=='\r') c=' ';
buf.put((byte)i);
sb.append(c);
if (c == '\n') {
lastLine= sb.toString();
flush();
isStart = false;
buf.clear();
sb.setLength(0);
} else isEnd = (lastChar == '$' || lastChar == '>' || lastChar == '#') && c == ' ';
} isEnd = (lastChar == '$' || lastChar == '>' || lastChar == '#') && c == ' ';
lastChar = c;
}

public String getPrompt() {
return sb.toString() == "" ? null : sb.toString();
return sb.length() == 0 ? null : p.matcher(sb.toString()).replaceAll("");
}

public void reset(boolean ignoreMessage) {
sb = new StringBuilder(128);
buf.clear();
sb.setLength(0);
lastChar = '\0';
isEnd = false;
isStart = true;
Expand All @@ -285,28 +284,25 @@ public void reset(boolean ignoreMessage) {
}

@Override
public synchronized void flush() throws IOException {
if (isStart || isEnd || sb.length() == 0) return;
lastLine = sb.toString();
public synchronized void flush() {
if (isStart || isEnd || buf.position() == 0) return;
int pos=buf.position();
buf.flip();
byte[] b=new byte[pos];
buf.get(b);
String line = new String(b);
isStart = false;
buf.clear();
if (!ignoreMessage) {
if (TERMTYPE == "none") {
Console.writer.write(p.matcher(lastLine).replaceAll(""));
Console.writer.flush();
} else {
System.out.print(lastLine);
System.out.flush();
}

if (TERMTYPE == "none") line=p.matcher(line).replaceAll("");
writer.print(line);
writer.flush();
}
isStart = false;
sb.setLength(0);
}

@Override
public void close() {
reset(false);
sb = null;
//printer.close();
}
}
}
}

0 comments on commit 6574f10

Please sign in to comment.