Package org.jdbi.v3.core.locator
Class ClasspathSqlLocator
- java.lang.Object
-
- org.jdbi.v3.core.locator.ClasspathSqlLocator
-
public final class ClasspathSqlLocator extends java.lang.ObjectLocates SQL in.sqlfiles on the classpath. Given a class and method name, for examplecom.foo.Bar#query, load a classpath resource name likecom/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 classClasspathSqlLocator.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>>cacheprivate java.util.function.Function<java.io.InputStream,java.lang.String>parserprivate static java.lang.StringSQL_EXTENSION
-
Constructor Summary
Constructors Modifier Constructor Description privateClasspathSqlLocator(CheckedFunction<java.io.InputStream,java.lang.String> parser)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ClasspathSqlLocatorcreate()Return a new ClasspathSqlLocator that returns SQL without modifying it.static java.lang.StringfindSqlOnClasspath(java.lang.Class<?> type, java.lang.String methodName)Deprecated.create()an instance instead of using static methodsstatic java.lang.StringfindSqlOnClasspath(java.lang.String name)Deprecated.create()an instance instead of using static methodsjava.lang.StringgetResource(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.StringgetResource(java.lang.String path)Returns resource's contents as a string at the specified path.static java.lang.StringgetResourceOnClasspath(java.lang.ClassLoader classLoader, java.lang.String path)Deprecated.create()an instance instead of using static methodsstatic java.lang.StringgetResourceOnClasspath(java.lang.String path)Deprecated.create()an instance instead of using static methodsjava.lang.Stringlocate(java.lang.Class<?> type, java.lang.String methodName)Locates SQL for the given type and name.java.lang.Stringlocate(java.lang.String name)Locates SQL for the given fully-qualified name.private static java.io.InputStreamopenStream(java.lang.ClassLoader classLoader, java.lang.String path)private static java.lang.StringreadAsString(java.io.InputStream is)private java.lang.StringreadResource(java.lang.ClassLoader classLoader, java.lang.String path)static ClasspathSqlLocatorremovingComments()Return a new ClasspathSqlLocator that returns SQL with comments removed.private static java.lang.ClassLoaderselectClassLoader()
-
-
-
Field Detail
-
SQL_EXTENSION
private static final java.lang.String SQL_EXTENSION
- See Also:
- Constant Field Values
-
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 methodsLocates SQL for the given type and name. Example: Given a typecom.foo.Barand a name ofbaz, looks for a resource namedcom/foo/Bar/baz.sqlon 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 methodsLocates SQL for the given fully-qualified name. Example: Given the namecom.foo.Bar.baz, looks for a resource namedcom/foo/Bar/baz.sqlon 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 methodsReturns 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 methodsReturns 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 resourcepath- 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 typecom.foo.Barand a name ofbaz, looks for a resource namedcom/foo/Bar/baz.sqlon 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 namecom.foo.Bar.baz, looks for a resource namedcom/foo/Bar/baz.sqlon 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 resourcepath- 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()
-
-