Class ClasspathSqlLocator


  • public final class ClasspathSqlLocator
    extends java.lang.Object
    Locates SQL in .sql files on the classpath. Given a class and method name, for example com.foo.Bar#query, load a classpath resource name like com/foo/Bar/query.sql. The contents are then parsed, cached, and returned for use by a statement.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  ClasspathSqlLocator.Holder  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.ClassLoader,​java.util.Map<java.lang.String,​java.lang.String>> cache  
      private java.util.function.Function<java.io.InputStream,​java.lang.String> parser  
      private static java.lang.String SQL_EXTENSION  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static ClasspathSqlLocator create()
      Return a new ClasspathSqlLocator that returns SQL without modifying it.
      static java.lang.String findSqlOnClasspath​(java.lang.Class<?> type, java.lang.String methodName)
      Deprecated.
      create() an instance instead of using static methods
      static java.lang.String findSqlOnClasspath​(java.lang.String name)
      Deprecated.
      create() an instance instead of using static methods
      java.lang.String getResource​(java.lang.ClassLoader classLoader, java.lang.String path)
      Returns resource's contents as a string at the specified path by the specified classloader.
      java.lang.String getResource​(java.lang.String path)
      Returns resource's contents as a string at the specified path.
      static java.lang.String getResourceOnClasspath​(java.lang.ClassLoader classLoader, java.lang.String path)
      Deprecated.
      create() an instance instead of using static methods
      static java.lang.String getResourceOnClasspath​(java.lang.String path)
      Deprecated.
      create() an instance instead of using static methods
      java.lang.String locate​(java.lang.Class<?> type, java.lang.String methodName)
      Locates SQL for the given type and name.
      java.lang.String locate​(java.lang.String name)
      Locates SQL for the given fully-qualified name.
      private static java.io.InputStream openStream​(java.lang.ClassLoader classLoader, java.lang.String path)  
      private static java.lang.String readAsString​(java.io.InputStream is)  
      private java.lang.String readResource​(java.lang.ClassLoader classLoader, java.lang.String path)  
      static ClasspathSqlLocator removingComments()
      Return a new ClasspathSqlLocator that returns SQL with comments removed.
      private static java.lang.ClassLoader selectClassLoader()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • cache

        private final java.util.Map<java.lang.ClassLoader,​java.util.Map<java.lang.String,​java.lang.String>> cache
      • parser

        private final java.util.function.Function<java.io.InputStream,​java.lang.String> parser
    • Constructor Detail

      • ClasspathSqlLocator

        private ClasspathSqlLocator​(CheckedFunction<java.io.InputStream,​java.lang.String> parser)
    • Method Detail

      • findSqlOnClasspath

        @Deprecated
        public static java.lang.String findSqlOnClasspath​(java.lang.Class<?> type,
                                                          java.lang.String methodName)
        Deprecated.
        create() an instance instead of using static methods
        Locates SQL for the given type and name. Example: Given a type com.foo.Bar and a name of baz, looks for a resource named com/foo/Bar/baz.sql on the classpath and returns its contents as a String.
        Parameters:
        type - the type that "owns" the given SQL. Dictates the directory path to the SQL resource file on the classpath.
        methodName - the SQL statement name (usually a method or field name from the type).
        Returns:
        the located SQL.
      • findSqlOnClasspath

        @Deprecated
        public static java.lang.String findSqlOnClasspath​(java.lang.String name)
        Deprecated.
        create() an instance instead of using static methods
        Locates SQL for the given fully-qualified name. Example: Given the name com.foo.Bar.baz, looks for a resource named com/foo/Bar/baz.sql on the classpath and returns its contents as a String.
        Parameters:
        name - fully qualified name.
        Returns:
        the located SQL.
      • getResourceOnClasspath

        @Deprecated
        public static java.lang.String getResourceOnClasspath​(java.lang.String path)
        Deprecated.
        create() an instance instead of using static methods
        Returns resource's contents as a string at the specified path. The path should point directly to the resource at the classpath. The resource is loaded by the current thread's classloader.
        Parameters:
        path - the resource path
        Returns:
        the resource's contents
        See Also:
        ClassLoader.getResource(String)
      • getResourceOnClasspath

        @Deprecated
        public static java.lang.String getResourceOnClasspath​(java.lang.ClassLoader classLoader,
                                                              java.lang.String path)
        Deprecated.
        create() an instance instead of using static methods
        Returns resource's contents as a string at the specified path by the specified classloader. The path should point directly to the resource at the classpath. The classloader should have access to the resource.
        Parameters:
        classLoader - the classloader which loads the resource
        path - the resource path
        Returns:
        the resource's contents
        See Also:
        ClassLoader.getResource(String)
      • removingComments

        public static ClasspathSqlLocator removingComments()
        Return a new ClasspathSqlLocator that returns SQL with comments removed.
        Returns:
        A new ClasspathSqlLocator that returns SQL with comments removed.
      • create

        public static ClasspathSqlLocator create()
        Return a new ClasspathSqlLocator that returns SQL without modifying it.
        Returns:
        A new ClasspathSqlLocator that returns SQL without modifying it.
      • locate

        public java.lang.String locate​(java.lang.Class<?> type,
                                       java.lang.String methodName)
        Locates SQL for the given type and name. Example: Given a type com.foo.Bar and a name of baz, looks for a resource named com/foo/Bar/baz.sql on the classpath and returns its contents as a String.
        Parameters:
        type - the type that "owns" the given SQL. Dictates the directory path to the SQL resource file on the classpath.
        methodName - the SQL statement name (usually a method or field name from the type).
        Returns:
        the located SQL.
      • locate

        public java.lang.String locate​(java.lang.String name)
        Locates SQL for the given fully-qualified name. Example: Given the name com.foo.Bar.baz, looks for a resource named com/foo/Bar/baz.sql on the classpath and returns its contents as a String.
        Parameters:
        name - fully qualified name.
        Returns:
        the located SQL.
      • getResource

        public java.lang.String getResource​(java.lang.String path)
        Returns resource's contents as a string at the specified path. The path should point directly to the resource at the classpath. The resource is loaded by the current thread's classloader.
        Parameters:
        path - the resource path
        Returns:
        the resource's contents
        See Also:
        ClassLoader.getResource(String)
      • getResource

        public java.lang.String getResource​(java.lang.ClassLoader classLoader,
                                            java.lang.String path)
        Returns resource's contents as a string at the specified path by the specified classloader. The path should point directly to the resource at the classpath. The classloader should have access to the resource.
        Parameters:
        classLoader - the classloader which loads the resource
        path - the resource path
        Returns:
        the resource's contents
        See Also:
        ClassLoader.getResource(String)
      • readResource

        private java.lang.String readResource​(java.lang.ClassLoader classLoader,
                                              java.lang.String path)
      • readAsString

        private static java.lang.String readAsString​(java.io.InputStream is)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • openStream

        private static java.io.InputStream openStream​(java.lang.ClassLoader classLoader,
                                                      java.lang.String path)
      • selectClassLoader

        private static java.lang.ClassLoader selectClassLoader()