/*
 * Copyright 1999-2010 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.server.network.protocol.http.command.post;

import java.util.List;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.server.db.OSharedDocumentDatabase;
import com.orientechnologies.orient.server.network.protocol.http.OHttpRequest;
import com.orientechnologies.orient.server.network.protocol.http.OHttpUtils;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommandAuthenticatedDbAbstract;

public class OServerCommandPostCommand extends OServerCommandAuthenticatedDbAbstract {
	private static final String[]	NAMES	= { "POST|command/*" };

	@Override
	@SuppressWarnings("unchecked")
	public boolean execute(final OHttpRequest iRequest) throws Exception {
		String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: command/<database>/sql/[<command-text>]");

		// TRY TO GET THE COMMAND FROM THE URL, THEN FROM THE CONTENT
		final String text = urlParts.length > 3 ? urlParts[3].trim() : iRequest.content;

		iRequest.data.commandInfo = "Command";
		iRequest.data.commandDetail = text;

		ODatabaseDocumentTx db = null;

		final Object response;

		try {
			db = getProfiledDatabaseInstance(iRequest);

			response = db.command(new OCommandSQL(text)).execute();

		} finally {
			if (db != null)
				OSharedDocumentDatabase.release(db);
		}

		if (response instanceof List<?>)
			sendRecordsContent(iRequest, (List<ORecord<?>>) response);
		else if (response == null || response instanceof Integer)
			sendTextContent(iRequest, OHttpUtils.STATUS_OK_CODE, "OK", null, OHttpUtils.CONTENT_TEXT_PLAIN, response);
		else
			sendTextContent(iRequest, OHttpUtils.STATUS_OK_CODE, "OK", null, OHttpUtils.CONTENT_TEXT_PLAIN, response.toString());
		return false;
	}

	@Override
	public String[] getNames() {
		return NAMES;
	}
}

@@ -753,24 +753,162 @@
 il.List;%0D%0A%0D%0A
+import com.orientechnologies.orient.core.command.OCommandManager;%0D%0Aimport com.orientechnologies.orient.core.command.OCommandRequestText;%0D%0A
 import com.o
@@ -1032,67 +1032,8 @@
 d;%0D%0A
-import com.orientechnologies.orient.core.sql.OCommandSQL;%0D%0A
 impo
@@ -1645,16 +1645,22 @@
 on %7B%0D%0A%09%09
+final 
 String%5B%5D
@@ -1738,13 +1738,19 @@
 se%3E/
-sql/%5B
+%3Clanguage%3E/
 %3Ccom
@@ -1759,16 +1759,23 @@
 nd-text%3E
+%5B/limit
 %5D%22);%0D%0A%0D%0A
@@ -1858,74 +1858,237 @@
 ing 
-text = urlParts.length %3E 3 ? urlParts%5B3%5D.trim() : iRequest.content
+language = urlParts.length %3E 2 ? urlParts%5B2%5D.trim() : %22sql%22;%0D%0A%09%09final String text = urlParts.length %3E 3 ? urlParts%5B3%5D.trim() : iRequest.content;%0D%0A%09%09final int limit = urlParts.length %3E 4 ? Integer.parseInt(urlParts%5B4%5D.trim()) : -1
 ;%0D%0A%0D
@@ -2301,51 +2301,190 @@
 %0A%09%09%09
-response = db.command(new OCommandSQL(text)
+final OCommandRequestText cmd = (OCommandRequestText) OCommandManager.instance().getRequester(language);%0D%0A%09%09%09cmd.setText(text);%0D%0A%09%09%09cmd.setLimit(limit);%0D%0A%09%09%09response = db.command(cmd
 ).ex

Support for parametric command execution by setting the command name (sql) and the limit