Fix ' when using JCR query based on MessageFormat.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / Jcr.java
index 1ed7469f1d5516bb0c8cf2ad8c2ac0b086dd1a86..72e325d35a40c40ad22a712ae7561fd33ae6ed87 100644 (file)
@@ -203,6 +203,21 @@ public class Jcr {
                }
        }
 
+       /**
+        * Returns the node name with its current index (useful for re-ordering).
+        * 
+        * @see Node#getName()
+        * @see Node#getIndex()
+        * @throws JcrException caused by {@link RepositoryException}
+        */
+       public static String getIndexedName(Node node) {
+               try {
+                       return node.getName() + "[" + node.getIndex() + "]";
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get name of " + node, e);
+               }
+       }
+
        /**
         * @see Node#getProperty(String)
         * @throws JcrException caused by {@link RepositoryException}
@@ -887,6 +902,8 @@ public class Jcr {
        // QUERY
        /** Creates a JCR-SQL2 query using {@link MessageFormat}. */
        public static Query createQuery(QueryManager qm, String sql, Object... args) {
+               // fix single quotes
+               sql = sql.replaceAll("'", "''");
                String query = MessageFormat.format(sql, args);
                try {
                        return qm.createQuery(query, Query.JCR_SQL2);