Skip to content

Commit fb03db1

Browse files
committed
1 parent 71d62f9 commit fb03db1

2 files changed

Lines changed: 155 additions & 77 deletions

File tree

tools/src/main/java/com/orientechnologies/orient/console/OConsoleDatabaseApp.java

Lines changed: 140 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializerFactory;
6565
import com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerStringAbstract;
6666
import com.orientechnologies.orient.core.sql.OCommandSQL;
67+
import com.orientechnologies.orient.core.sql.filter.OSQLPredicate;
6768
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
6869
import com.orientechnologies.orient.core.storage.OCluster;
6970
import com.orientechnologies.orient.core.storage.ORawBuffer;
@@ -88,6 +89,7 @@ public class OConsoleDatabaseApp extends OrientConsole implements OCommandOutput
8889
protected ODatabaseDocument currentDatabase;
8990
protected String currentDatabaseName;
9091
protected ORecordInternal<?> currentRecord;
92+
protected int currentRecordIdx;
9193
protected List<OIdentifiable> currentResultSet;
9294
protected OServerAdmin serverAdmin;
9395
private int lastPercentStep;
@@ -136,6 +138,52 @@ public void run() {
136138
System.exit(result);
137139
}
138140

141+
protected static boolean setTerminalToCBreak() throws IOException, InterruptedException {
142+
// set the console to be character-buffered instead of line-buffered
143+
int result = stty("-icanon min 1");
144+
if (result != 0) {
145+
return false;
146+
}
147+
148+
// disable character echoing
149+
stty("-echo");
150+
return true;
151+
}
152+
153+
/**
154+
* Execute the stty command with the specified arguments against the current active terminal.
155+
*/
156+
protected static int stty(final String args) throws IOException, InterruptedException {
157+
String cmd = "stty " + args + " < /dev/tty";
158+
159+
return exec(new String[] { "sh", "-c", cmd });
160+
}
161+
162+
/**
163+
* Execute the specified command and return the output (both stdout and stderr).
164+
*/
165+
protected static int exec(final String[] cmd) throws IOException, InterruptedException {
166+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
167+
168+
Process p = Runtime.getRuntime().exec(cmd);
169+
int c;
170+
InputStream in = p.getInputStream();
171+
172+
while ((c = in.read()) != -1) {
173+
bout.write(c);
174+
}
175+
176+
in = p.getErrorStream();
177+
178+
while ((c = in.read()) != -1) {
179+
bout.write(c);
180+
}
181+
182+
p.waitFor();
183+
184+
return p.exitValue();
185+
}
186+
139187
@Override
140188
protected boolean isCollectingCommands(final String iLine) {
141189
return iLine.startsWith("js");
@@ -145,7 +193,7 @@ protected boolean isCollectingCommands(final String iLine) {
145193
protected void onBefore() {
146194
super.onBefore();
147195

148-
currentResultSet = new ArrayList<OIdentifiable>();
196+
setResultset(new ArrayList<OIdentifiable>());
149197

150198
OGlobalConfiguration.STORAGE_KEEP_OPEN.setValue(false);
151199

@@ -655,6 +703,18 @@ public void releaseCluster(
655703
message("\n\nCluster '" + iClusterName + "' was released successfully");
656704
}
657705

706+
@ConsoleCommand(description = "Move the current record cursor to the next one in result set")
707+
public void next() {
708+
setCurrentRecord(currentRecordIdx + 1);
709+
dumpRecordDetails();
710+
}
711+
712+
@ConsoleCommand(description = "Move the current record cursor to the previous one in result set")
713+
public void prev() {
714+
setCurrentRecord(currentRecordIdx - 1);
715+
dumpRecordDetails();
716+
}
717+
658718
@ConsoleCommand(splitInWords = false, description = "Alter a class in the database schema")
659719
public void alterClass(@ConsoleParameter(name = "command-text", description = "The command text to execute") String iCommandText) {
660720
sqlCommand("alter", iCommandText, "\nClass updated successfully\n", false);
@@ -706,7 +766,7 @@ public void traverse(@ConsoleParameter(name = "query-text", description = "The t
706766
}
707767

708768
long start = System.currentTimeMillis();
709-
currentResultSet = currentDatabase.command(new OCommandSQL("traverse " + iQueryText)).execute();
769+
setResultset((List<OIdentifiable>) currentDatabase.command(new OCommandSQL("traverse " + iQueryText)).execute());
710770

711771
float elapsedSeconds = getElapsedSecs(start);
712772

@@ -737,7 +797,7 @@ public void select(@ConsoleParameter(name = "query-text", description = "The que
737797
}
738798

739799
final long start = System.currentTimeMillis();
740-
currentResultSet = currentDatabase.query(new OSQLSynchQuery<ODocument>(iQueryText, limit).setFetchPlan("*:1"));
800+
setResultset((List<OIdentifiable>) currentDatabase.query(new OSQLSynchQuery<ODocument>(iQueryText, limit).setFetchPlan("*:1")));
741801

742802
float elapsedSeconds = getElapsedSecs(start);
743803

@@ -746,14 +806,56 @@ public void select(@ConsoleParameter(name = "query-text", description = "The que
746806
message("\n\n" + currentResultSet.size() + " item(s) found. Query executed in " + elapsedSeconds + " sec(s).");
747807
}
748808

809+
@ConsoleCommand(splitInWords = false, description = "Move from current record by evaluating a predicate against current record")
810+
public void move(@ConsoleParameter(name = "text", description = "The sql predicate to evaluate") final String iText) {
811+
if (iText == null)
812+
return;
813+
814+
if (currentRecord == null)
815+
return;
816+
817+
final Object result = new OSQLPredicate(iText).evaluate(currentRecord, null, null);
818+
819+
if (result != null) {
820+
if (result instanceof OIdentifiable) {
821+
setResultset(new ArrayList<OIdentifiable>());
822+
currentRecord = ((OIdentifiable) result).getRecord();
823+
dumpRecordDetails();
824+
} else if (result instanceof List<?>) {
825+
setResultset((List<OIdentifiable>) result);
826+
dumpResultSet(-1);
827+
} else if (result instanceof Iterator<?>) {
828+
final List<OIdentifiable> list = new ArrayList<OIdentifiable>();
829+
while (((Iterator) result).hasNext())
830+
list.add(((Iterator<OIdentifiable>) result).next());
831+
setResultset(list);
832+
dumpResultSet(-1);
833+
} else
834+
setResultset(new ArrayList<OIdentifiable>());
835+
}
836+
}
837+
838+
@ConsoleCommand(splitInWords = false, description = "Evaluate a predicate against current record")
839+
public void eval(@ConsoleParameter(name = "text", description = "The sql predicate to evaluate") final String iText) {
840+
if (iText == null)
841+
return;
842+
843+
if (currentRecord == null)
844+
return;
845+
846+
final Object result = new OSQLPredicate(iText).evaluate(currentRecord, null, null);
847+
if (result != null)
848+
out.println("\n" + result);
849+
}
850+
749851
@SuppressWarnings("unchecked")
750852
@ConsoleCommand(splitInWords = false, description = "Execute javascript commands in the console")
751853
public void js(
752854
@ConsoleParameter(name = "text", description = "The javascript to execute. Use 'db' to reference to a document database, 'gdb' for a graph database") final String iText) {
753855
if (iText == null)
754856
return;
755857

756-
currentResultSet.clear();
858+
resetResultSet();
757859

758860
final OCommandExecutorScript cmd = new OCommandExecutorScript();
759861
cmd.parse(new OCommandScript("Javascript", iText));
@@ -774,6 +876,9 @@ else if (result instanceof Collection<?>) {
774876
currentResultSet = new ArrayList<OIdentifiable>();
775877
Collections.addAll(currentResultSet, (OIdentifiable[]) result);
776878
}
879+
880+
setResultset(currentResultSet);
881+
777882
dumpResultSet(-1);
778883
message("Client side script executed in %f sec(s). Returned %d records", elapsedSeconds, currentResultSet.size());
779884
} else
@@ -789,7 +894,7 @@ public void jss(
789894
if (iText == null)
790895
return;
791896

792-
currentResultSet.clear();
897+
resetResultSet();
793898

794899
long start = System.currentTimeMillis();
795900
Object result = currentDatabase.command(new OCommandScript("Javascript", iText)).execute();
@@ -805,6 +910,9 @@ else if (result instanceof Collection<?>) {
805910
currentResultSet = new ArrayList<OIdentifiable>();
806911
Collections.addAll(currentResultSet, (OIdentifiable[]) result);
807912
}
913+
914+
setResultset(currentResultSet);
915+
808916
dumpResultSet(-1);
809917
message("Server side script executed in %f sec(s). Returned %d records", elapsedSeconds, currentResultSet.size());
810918
} else
@@ -923,7 +1031,7 @@ public void dropProperty(@ConsoleParameter(name = "command-text", description =
9231031
public void browseClass(@ConsoleParameter(name = "class-name", description = "The name of the class") final String iClassName) {
9241032
checkForDatabase();
9251033

926-
currentResultSet.clear();
1034+
resetResultSet();
9271035

9281036
final int limit = Integer.parseInt(properties.get("limit"));
9291037

@@ -937,7 +1045,7 @@ public void browseCluster(
9371045
@ConsoleParameter(name = "cluster-name", description = "The name of the cluster") final String iClusterName) {
9381046
checkForDatabase();
9391047

940-
currentResultSet.clear();
1048+
resetResultSet();
9411049

9421050
final int limit = Integer.parseInt(properties.get("limit"));
9431051

@@ -962,7 +1070,7 @@ public void displayRecord(
9621070
throw new OException("The record requested is not part of current result set (0"
9631071
+ (currentResultSet.size() > 0 ? "-" + (currentResultSet.size() - 1) : "") + ")");
9641072

965-
currentRecord = currentResultSet.get(recNumber).getRecord();
1073+
setCurrentRecord(recNumber);
9661074
}
9671075

9681076
dumpRecordDetails();
@@ -1742,6 +1850,14 @@ public ORecordInternal<?> getCurrentRecord() {
17421850
return currentRecord;
17431851
}
17441852

1853+
protected void setCurrentRecord(final int iIndex) {
1854+
currentRecordIdx = iIndex;
1855+
if (iIndex < currentResultSet.size())
1856+
currentRecord = (ORecordInternal<?>) currentResultSet.get(iIndex);
1857+
else
1858+
currentRecord = null;
1859+
}
1860+
17451861
/** Should be used only by console commands */
17461862
public List<OIdentifiable> getCurrentResultSet() {
17471863
return currentResultSet;
@@ -1791,7 +1907,9 @@ public void checkCurrentObject() {
17911907
}
17921908

17931909
private void dumpRecordDetails() {
1794-
if (currentRecord instanceof ODocument) {
1910+
if (currentRecord == null)
1911+
return;
1912+
else if (currentRecord instanceof ODocument) {
17951913
ODocument rec = (ODocument) currentRecord;
17961914
message("\n--------------------------------------------------");
17971915
message("\nODocument - Class: %s id: %s v.%s", rec.getClassName(), rec.getIdentity().toString(), rec.getRecordVersion()
@@ -1866,6 +1984,7 @@ private void browseRecords(final int limit, final OIdentifiableIterator<?> it) {
18661984
currentResultSet.clear();
18671985
while (it.hasNext() && currentResultSet.size() <= limit)
18681986
currentResultSet.add(it.next());
1987+
setResultset(currentResultSet);
18691988

18701989
tableFormatter.writeRecords(currentResultSet, limit);
18711990
}
@@ -1879,7 +1998,7 @@ private Object sqlCommand(final String iExpectedCommand, String iReceivedCommand
18791998

18801999
iReceivedCommand = iExpectedCommand + " " + iReceivedCommand.trim();
18812000

1882-
currentResultSet.clear();
2001+
resetResultSet();
18832002

18842003
final long start = System.currentTimeMillis();
18852004

@@ -1952,60 +2071,14 @@ protected void printApplicationInfo() {
19522071
message("\nType 'help' to display all the commands supported.");
19532072
}
19542073

1955-
protected static boolean setTerminalToCBreak() throws IOException, InterruptedException {
1956-
// set the console to be character-buffered instead of line-buffered
1957-
int result = stty("-icanon min 1");
1958-
if (result != 0) {
1959-
return false;
1960-
}
1961-
1962-
// disable character echoing
1963-
stty("-echo");
1964-
return true;
1965-
}
1966-
19672074
protected void dumpResultSet(final int limit) {
19682075
new OTableFormatter(this).setMaxWidthSize(Integer.parseInt(properties.get("width"))).writeRecords(currentResultSet, limit);
19692076
}
19702077

1971-
/**
1972-
* Execute the stty command with the specified arguments against the current active terminal.
1973-
*/
1974-
protected static int stty(final String args) throws IOException, InterruptedException {
1975-
String cmd = "stty " + args + " < /dev/tty";
1976-
1977-
return exec(new String[] { "sh", "-c", cmd });
1978-
}
1979-
19802078
protected float getElapsedSecs(final long start) {
19812079
return (float) (System.currentTimeMillis() - start) / 1000;
19822080
}
19832081

1984-
/**
1985-
* Execute the specified command and return the output (both stdout and stderr).
1986-
*/
1987-
protected static int exec(final String[] cmd) throws IOException, InterruptedException {
1988-
ByteArrayOutputStream bout = new ByteArrayOutputStream();
1989-
1990-
Process p = Runtime.getRuntime().exec(cmd);
1991-
int c;
1992-
InputStream in = p.getInputStream();
1993-
1994-
while ((c = in.read()) != -1) {
1995-
bout.write(c);
1996-
}
1997-
1998-
in = p.getErrorStream();
1999-
2000-
while ((c = in.read()) != -1) {
2001-
bout.write(c);
2002-
}
2003-
2004-
p.waitFor();
2005-
2006-
return p.exitValue();
2007-
}
2008-
20092082
protected void printError(final Exception e) {
20102083
if (properties.get("debug") != null && Boolean.parseBoolean(properties.get("debug"))) {
20112084
message("\n\n!ERROR:");
@@ -2043,4 +2116,15 @@ else if (serverAdmin != null)
20432116
protected String getPrompt() {
20442117
return String.format("orientdb%s> ", getContext());
20452118
}
2119+
2120+
protected void setResultset(final List<OIdentifiable> iResultset) {
2121+
currentResultSet = iResultset;
2122+
currentRecordIdx = 0;
2123+
currentRecord = currentResultSet.isEmpty() ? null : (ORecordInternal<?>) currentResultSet.get(0).getRecord();
2124+
}
2125+
2126+
protected void resetResultSet() {
2127+
currentResultSet.clear();
2128+
currentRecord = null;
2129+
}
20462130
}

0 commit comments

Comments
 (0)