package org.orienteer.devutils;

import java.util.regex.Pattern;

import org.apache.wicket.model.IModel;
import org.apache.wicket.util.io.IOUtils;
import org.apache.wicket.util.string.Strings;

import com.google.common.base.Throwables;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OResultSet;

import ru.ydn.wicket.wicketconsole.IScriptEngine;
import ru.ydn.wicket.wicketconsole.ScriptResult;
import ru.ydn.wicket.wicketorientdb.OrientDbWebSession;
import ru.ydn.wicket.wicketorientdb.model.OQueryModel;

/**
 * {@link IScriptEngine} for execution of SQL in OrientDB
 */
public class ODBScriptEngine implements IScriptEngine {
	
	private static final Pattern SELECT_FROM_PATTERN = Pattern.compile("^select\\s+from", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	@Override
	public String getName() {
		return ODBScriptEngineFactory.ENGINE_NAME;
	}

	@Override
	public ScriptResult eval(String command) {
		ScriptResult result = new ScriptResult(ODBScriptEngineFactory.ENGINE_NAME, command);
		if(!Strings.isEmpty(command)) {
			command = command.trim();
			try {
				if(SELECT_FROM_PATTERN.matcher(command).find()) {
					OQueryModel<ODocument> returnModel =  new OQueryModel<ODocument>(command);
					returnModel.probeOClass(10);
					result.setResultModel(returnModel);
				} else {
					ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
					OCommandSQL comm = new OCommandSQL(command);
					Object resultObject = db.command(comm).execute();
					if(resultObject instanceof OResultSet) {
						StringBuilder sb = new StringBuilder("[");
						boolean first = true;
						for(ODocument doc : ((OResultSet<ODocument>)resultObject)) {
							if(!first) sb.append(", ");
							sb.append(doc.toJSON());
							first=false;
						}
						sb.append("]");
						resultObject = sb.toString();
					}
					result.setResult(resultObject);
				}
			} catch (Exception e) {
				result.setError(Throwables.getStackTraceAsString(e));
			}
		}
		return result;
	}

}

@@ -423,16 +423,90 @@
 andSQL;%0A
+import com.orientechnologies.orient.core.sql.OCommandSQLParsingException;%0A
 import c
@@ -2143,24 +2143,105 @@
 eption e) %7B%0A
+%09%09%09%09if(shouldBeShorted(e)) %7B%0A%09%09%09%09%09result.setError(e.getMessage());%0A%09%09%09%09%7D else %7B%0A%09
 %09%09%09%09result.s
@@ -2286,16 +2286,22 @@
 ng(e));%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2320,12 +2320,118 @@
 sult;%0A%09%7D
+%0A%09%0A%09protected boolean shouldBeShorted(Exception e) %7B%0A%09%09return e instanceof OCommandSQLParsingException;%0A%09%7D
 %0A%0A%7D%0A

Make some sql exceptions shorten in console: fix #430