com.opengamma.elsql
Class ElSql

java.lang.Object
  extended by com.opengamma.elsql.ElSql

public final class ElSql
extends Object

Main entry point, providing access to a bundle of elsql formatted SQL.

The bundle encapsulates the SQL needed for a particular feature. This will typically correspond to a data access object, or set of related tables.

This class has no dependencies on any external libraries. Similar functionality is available from the ElSqlBundle class, which provides integration with the Spring framework.

This class is immutable and thread-safe.


Method Summary
 ElSqlConfig getConfig()
          Gets the configuration object.
 String getSql(String name)
          Finds SQL for a named fragment key, without specifying parameters.
 String getSql(String name, Map<String,Object> params)
          Finds SQL for a named fragment key, providing a map of SQL parameters.
 String getSql(String name, SqlParams params)
          Finds SQL for a named fragment key, providing the SQL parameters.
static ElSql of(ElSqlConfig config, Class<?> type)
          Loads external SQL based for the specified type.
static ElSql parse(ElSqlConfig config, URL... resources)
          Parses a bundle from a resource locating a file, specify the config.
 ElSql withConfig(ElSqlConfig config)
          Returns a copy of this bundle with a different configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

of

public static ElSql of(ElSqlConfig config,
                       Class<?> type)
Loads external SQL based for the specified type.

The type is used to identify the location and name of the ".elsql" file. The loader will attempt to find and use two files, using the full name of the type to query the class path for resources.

The first resource searched for is optional - the file will have the suffix "-ConfigName.elsql", such as "com/foo/Bar-MySql.elsql". The second resource searched for is mandatory - the file will just have the ".elsql" suffix, such as "com/foo/Bar.elsql".

The config is designed to handle some, but not all, database differences. Other differences should be handled by creating and using a database specific override file (the first optional resource is the override file).

Parameters:
config - the config, not null
type - the type, not null
Returns:
the bundle, not null
Throws:
IllegalArgumentException - if the input cannot be parsed or if no matching resource exists

parse

public static ElSql parse(ElSqlConfig config,
                          URL... resources)
Parses a bundle from a resource locating a file, specify the config.

This parses a list of resources, expressed as URLs. Named blocks in later resources override blocks with the same name in earlier resources.

The config is designed to handle some, but not all, database differences. Other differences are handled via the override resources passed in.

Each resource is a URL. A null URL is permitted and ignored. This allows classpath resources, obtained from Class.getResource(String) or ClassLoader.getResource(String) to be called and passed in directly as those methods return null when the target does not exist.

Parameters:
config - the config to use, not null
resources - the resources to load, not null, may contain nulls which are ignored
Returns:
the external identifier, not null
Throws:
IllegalArgumentException - if the input cannot be parsed or if none of the resources exists

getConfig

public ElSqlConfig getConfig()
Gets the configuration object.

Returns:
the config, not null

withConfig

public ElSql withConfig(ElSqlConfig config)
Returns a copy of this bundle with a different configuration.

This does not reload the underlying resources.

Parameters:
config - the new config, not null
Returns:
a bundle with the config updated, not null

getSql

public String getSql(String name)
Finds SQL for a named fragment key, without specifying parameters.

This finds, processes and returns a named block from the bundle. Note that if the SQL contains tags that depend on variables, like AND or LIKE, then an error will be thrown.

Parameters:
name - the name, not null
Returns:
the SQL, not null
Throws:
IllegalArgumentException - if there is no fragment with the specified name
RuntimeException - if a problem occurs

getSql

public String getSql(String name,
                     SqlParams params)
Finds SQL for a named fragment key, providing the SQL parameters.

This finds, processes and returns a named block from the bundle. The parameters are used to provide intelligent processing of SQL based on the actual data in the request.

See MapSqlParams and SpringSqlParams.

Parameters:
name - the name, not null
params - the SQL parameters, not null
Returns:
the SQL, not null
Throws:
IllegalArgumentException - if there is no fragment with the specified name
RuntimeException - if a problem occurs

getSql

public String getSql(String name,
                     Map<String,Object> params)
Finds SQL for a named fragment key, providing a map of SQL parameters.

This finds, processes and returns a named block from the bundle. The parameters are used to provide intelligent processing of SQL based on the actual data in the request.

Parameters:
name - the name, not null
params - the SQL parameters, not null
Returns:
the SQL, not null
Throws:
IllegalArgumentException - if there is no fragment with the specified name
RuntimeException - if a problem occurs


Copyright 2009-Present by OpenGamma Inc. and individual contributors, released under the Apache License Version 2.0