The Basics – Specialized Query Methods
November 5, 2014 Leave a comment
Following the Running a DQL Query post, this post looks at some simple ways to specialize DQL queries based upon their type (e.g., select, set, create, delete, insert, drop, cached, and update) and expected return values, and the benefits such specialization provides. The Basics library of Documentum methods containing the specialized query methods as well as all other methods discussed in this series, can be found here.
The Basics library contains 10 query methods. The method signatures follow:
public static IDfCollection runSelectQuery(String query, IDfSession session) throws DfException
public static dmRecordSet runSelectQueryAsRecordSet(String query, IDfSession session) throws Exception
public static int runUpdateObjectQuery(String query, IDfSession session) throws DfException
public static IDfCollection runCachedQuery(String query, IDfSession session) throws DfException
public static String runCreateObjectQuery(String query, IDfSession session) throws DfException
public static int runDeleteObjectQuery(String query, IDfSession session) throws DfException
public static IDfCollection runExecQuery(String query, IDfSession session) throws DfException
public static int runInsertQuery(String query, IDfSession session) throws DfException
public static int runDeleteQuery(String query, IDfSession session) throws DfException
public static int runUpdateQuery(String query, IDfSession session) throws DfException
I will leave the exploration of the code to you, and instead discuss a few of the benefits of these specialized query methods.
- Using specialized query methods makes your primary code much simpler to create, read, and maintain. You don’t have to clutter the code with IDfQuery and IDfCollection objects.
- These methods provide simpler handling of return values. For example, if you are only interested in a single result value (e.g., number objects updated), you don’t have to clutter your code with loops, etc. to retrieve that value.
- Using specialized query methods, you don’t have to worry about using the correct or most efficient query constants.
- These methods provide rudimentary syntax checking, which could be improved/extended for your purposes. This can help alleviate receiving cryptic error messages from the query engine.
- Because all of these methods eventually call a single “uber” query method, error checking/trapping is centralized.
Here are some quick examples:
// SELECT query = "select r_object_id, object_name from dm_document where folder('/Temp')"; System.out.println("running SELECT query: " + query); col = DCTMBasics.runSelectQuery(query,session); // do something with IDfCollection col.close(); // CACHED query = "select r_object_id, object_name from dm_document where folder('/Temp',descend)"; System.out.println("running CACHED query: " + query); long start = System.currentTimeMillis(); col = DCTMBasics.runCachedQuery(query,session); long stop = System.currentTimeMillis(); long dif = stop - start; // do something with IDfCollection col.close(); System.out.println("duration=" + dif); System.out.println("[running second query]"); start = System.currentTimeMillis(); query = "select r_object_id, object_name from dm_document where folder('/Temp',descend)"; col = DCTMBasics.runCachedQuery(query,session); stop = System.currentTimeMillis(); dif = stop - start; // do something with IDfCollection col.close(); System.out.println("duration=" + dif); // OBJ CREATE query = "create dm_document object set object_name = 'DCTMBasics Test Object' link '/Temp'"; System.out.println("running OBJ CREATE query: " + query); String objId = DCTMBasics.runCreateObjectQuery(query,session); System.out.println("created " + objId); // EXEC query = "execute db_stats"; System.out.println("running EXEC query: " + query); col = DCTMBasics.runExecQuery(query,session); // do something with IDfCollection col.close(); // OBJ UPDATE System.out.println("running OBJ UPDATE query"); query = "update dm_document object set object_name = 'DCTMBasics updated object name' where r_object_id = '" + objId + "'"; int cnt = DCTMBasics.runUpdateObjectQuery(query,session); System.out.println("update " + cnt + " objs"); // OBJ DELETE System.out.println("running OBJ DELETE query"); query = "delete dm_document object where r_object_id = '" + objId + "'"; cnt = DCTMBasics.runDeleteObjectQuery(query,session); System.out.println("deleted " + cnt + " objs");