Skip to content

Instantly share code, notes, and snippets.

@mmacfadden
Last active November 1, 2015 19:11
Show Gist options
  • Save mmacfadden/7c92c6531c8b3f62a285 to your computer and use it in GitHub Desktop.
Save mmacfadden/7c92c6531c8b3f62a285 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.metadata.function.OFunction;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OResultSet;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
public class NamedMapParameterInFunctionTest {
public static void main(String[] args) {
// Create an in memory database for the test.
ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:NamedMapParameterInFunctionTest");
db.activateOnCurrentThread();
db.create();
// Create a function called identity that just returns the single
// parameter that was passed in.
OFunction function = new OFunction();
function.setName("identity");
function.setCode("return value;");
function.setParameters(Collections.singletonList("value"));
function.setLanguage("javascript");
function.setIdempotent(false);
function.save();
db.getMetadata().getSchema().createClass("TestClass");
// This Works
// Inserting a document with a named parameter passed to a function
// works when the parameter is bound to a list.
List<Object> list = new ArrayList<Object>();
list.add("1");
list.add(2);
HashMap<String, Object> listNamedParam = new HashMap<String, Object>();
listNamedParam.put("id", "id1");
listNamedParam.put("value", list);
db.command(new OCommandSQL("INSERT INTO TestClass (id, value) VALUES (:id, identity(:value))"))
.execute(listNamedParam);
selectAndPrint(db, "id1");
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("key1", 1);
map.put("key2", 2);
HashMap<String, Object> mapNamedParam = new HashMap<String, Object>();
mapNamedParam.put("id", "id2");
mapNamedParam.put("value", map);
//
// This Works
//
// Inserting a document with a direct named parameter works when the
// parameter is bound to a map
db.command(new OCommandSQL("INSERT INTO TestClass (id, value) VALUES (:id, :value)")).execute(mapNamedParam);
selectAndPrint(db, "id2");
HashMap<String, Object> mapNamedParam2 = new HashMap<String, Object>();
mapNamedParam2.put("id", "id3");
mapNamedParam2.put("value", map);
//
// This Does Not Work
//
// Inserting a document with a named parameter passed to a function
// does not work when the parameter is bound to a map
db.command(new OCommandSQL("INSERT INTO TestClass (id, value) VALUES (:id, identity(:value))"))
.execute(mapNamedParam2);
selectAndPrint(db, "id3");
db.close();
}
private static void selectAndPrint(ODatabaseDocumentTx db, String id) {
OSQLSynchQuery<ODocument> selectQuery = new OSQLSynchQuery<ODocument>("SELECT FROM TestClass WHERE id = :id");
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("id", id);
OResultSet<ODocument> result = db.command(selectQuery).execute(params);
System.out.println(result.get(0).toJSON());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment