| java.lang.Object jodd.db.orm.sqlgen.DbSqlTemplate
All known Subclasses: jodd.db.orm.sqlgen.DbDynamicSqlTemplate,
DbSqlTemplate | public class DbSqlTemplate implements DbSqlGenerator(Code) | | SQL templates provides object-oriented and dynamic sql queries. Using sql templates and its macros makes possible to
write SQL query without specifying table and column names, but using their Java references. Using textual macros
may not sound well, however, it provides significantly better performances then to parse complete sql string,
while still having POSQ (plain old Sql query). Moreover, macros are designed in sich way that by simply
removing them, in most cases the result will be executable sql query.
All macro names have a '$' sign as a prefix. Colum reference macro is different than others since it uses
the '$' just as a prefix for its name. Therefore, this character must be escaped if it is used
anywhere else in the sql query. Of course, if prepared statements are used, then (probably:) no escaping is
necessary.
This is a base class that provides macros for: tables (
DbSqlTemplate.parseTables(String) ),
columns list (
DbSqlTemplate.parseColumns(String) ) and column reference (
DbSqlTemplate.parseReferences(String) ).
These three macros are essential and may be used to build any sql query. Of course, it is not necessary to use
macros, even a mix between standard sql and object-oriented sql is possible.
The parsing order of these basic macros is important. First tables must be resolved, to get all table references.
Then column references has to be resolved, since it has different macro specification. Column list
(and all other block macros) can be parsed afterwards, in any order.
For enhanced version of templates see
DbDynamicSqlTemplate .
|
Inner Class :public static enum ColumnAliasType | |
Method Summary | |
protected void | appendColumnName(StringBuilder result, String tableRef, String tableName, String column, int i) Simply appends column name with optional table reference and alias. | public DbSqlTemplate | columnAliases(boolean aliases) | public DbSqlTemplate | escape(boolean escape) Specifies if escaping character is used. | public String | generateQuery() | public Map<String, String[]> | getColumnData() | public Map<String, Object> | getQueryParameters() | public Map<String, Object> | getQueryReferences() | protected String | nextRegion(StringBuilder destination, String template, String leftBoundary, String rightBoundary) | protected String | nextRegionNoEscape(StringBuilder destination, String template, String leftBoundary, String rightBoundary) | protected String | nextRegionWithEscape(StringBuilder destination, String template, String leftBoundary, String rightBoundary) | protected String | parseColumns(String template) Resolves columns lists macros used in SELECT queries. | public String | parseReferences(String template) Parse column references that are defined with table reference and property name.
The result is table reference with column name. | public String | parseTables(String template) Resolves table macros, allowing using types for table names. | public DbSqlTemplate | setColumnAliasesType(ColumnAliasType aliasesType) Specifies column
DbSqlTemplate.columnAliases(boolean) type and implicitly turns on generation of column aliases. | public DbSqlTemplate | use(String name, Object value) Adds new object reference that are used in template. |
DEFAULT_COLUMN_ALIAS_TYPE | public static ColumnAliasType DEFAULT_COLUMN_ALIAS_TYPE(Code) | | Default column alias type, when they are used.
|
colNdx | protected int[] colNdx(Code) | | |
columnAliases | protected boolean columnAliases(Code) | | |
columnAliasesType | protected ColumnAliasType columnAliasesType(Code) | | |
columnCount | protected int columnCount(Code) | | Column counter for COLUMN_CODE column alias type.
|
escape | protected boolean escape(Code) | | |
escapeChar | protected char escapeChar(Code) | | |
escapeRightBoundary | protected String escapeRightBoundary(Code) | | |
loadedClasses | protected static Map<String, Class> loadedClasses(Code) | | Internal map for speeding up class loading.
|
references | protected Map<String, Object> references(Code) | | Map of object references that are used in templates.
|
tablesRefs | protected Map<String, DbEntityDescriptor> tablesRefs(Code) | | Table references map. Table reference may be:
- table alias
- simple type name, if no alias specified
- object reference, if object reference is used and no alias specified
|
appendColumnName | protected void appendColumnName(StringBuilder result, String tableRef, String tableName, String column, int i)(Code) | | Simply appends column name with optional table reference and alias.
|
escape | public DbSqlTemplate escape(boolean escape)(Code) | | Specifies if escaping character is used. By default, escaping character is ignored, since
this gives somewhat better performances. In case of parsing problems, escaping can be turned on.
Moreover, there is a public static variable that defines this behaviour globally.
|
parseColumns | protected String parseColumns(String template)(Code) | | Resolves columns lists macros used in SELECT queries. Columns are defined as comma-separated
list of column reference. Column reference consist of table reference and property name.
When columns (i.e. properties) are specified explicitly, this macro just resolves column references
to column names. This is equals to column reference macro ($) except this macro may generate aliases.
This macro also expands asterisk column groups to list of all availiable types columns.
Table reference may be either a table alias or object reference.
When just a table reference is specified (no properties or asterisks), macro will generate simple
comma-separated column list, without any aliases and prefixes.
Macro optionally defines column aliases. There are several way how column aliases can be defined:
'TABLE_NAME$COLUMN_NAME', 'tableRef$COLUMN_NAME' or 'col_no_' (default). All these column alias types are
supported by
jodd.db.orm.mapper.ResultSetMapper . This setting should be global for whole application.
It is important which column alias type to use, since the first one is the most informative, but gives
the longest column alias names, and database may complain about its size (e.g. oracle doesn't allow aliases
longer then 30 characters).
Generating column aliases may helps when database doesn't support table names for column data
in result sets meta data (such as Oracle), althought this is not neccessary.
When group column reference is specified (using asteriks or just reference name) and when table reference
is an object reference, only non-null fields will be read. Adding a plus sign (+) in front of reference
will give all properties (including null ones), without object parsing.
Overview:
- $C{ref.id} : ref.ID (as ALIAS)
- $C{ref.*} : ref.ID (as ALIAS), ref.NAME (as ALIAS),...
- $C{ref} : ID, NAME - no alias generation!
- $C{+objref.*} : dynamic mode, all columns are included
- $C{+objref} : dynamic mode, all columns are included
|
parseReferences | public String parseReferences(String template)(Code) | | Parse column references that are defined with table reference and property name.
The result is table reference with column name. Since this macro is the most used one,
it uses only a '$' in front of column reference. Escaping '$' is always turned on for this macro.
|
parseTables | public String parseTables(String template)(Code) | | Resolves table macros, allowing using types for table names. Table definition in table macro
specifies a table reference that will be used by other macros when they need to reference a table.
The most basic way to define a table is by its class name. In this case, table reference is
a simple class name of specified type.
jodd.db.orm.DbOrm may specify common package prefix
that will be appended before table name only if it starts with a dot ('.'). Examples:
$T{foo.boo.Zoo} will define a reference 'Zoo' and generates: 'ZOO Zoo '.
$T{.Zoo} act as above, if package prefix is set to 'foo.boo '.
As in sql, table definition may contains table alias, which then will be used as a table reference.
Example:
$T{foo.boo.Zoo z} will define a reference 'z' and generates 'ZOO z '.
Another way to define a table is by using object references, added by
DbSqlTemplate.use(String,Object) .
In this case entity type is objects type (if not already instance of Class ). When no explicit
alias is specified, generated table will be aliased with its object reference.
When table alias is set to minus '-' then no aliases will be generated (useful for INSERT queries).
This violates the sql standard, but it is used rare,
Table macros must be resolved first, before all other macros!
Overview:
- $T{className} : TABLE_NAME className
- $T{objectRef} : TABLE_NAME objectRef
- $T{anything -} : TABLE_NAME
- $T{anything alias} : TABLE_NAME alias
|
setColumnAliasesType | public DbSqlTemplate setColumnAliasesType(ColumnAliasType aliasesType)(Code) | | Specifies column
DbSqlTemplate.columnAliases(boolean) type and implicitly turns on generation of column aliases.
There are no much reasons for changing default aliases type during the application execution.
|
use | public DbSqlTemplate use(String name, Object value)(Code) | | Adds new object reference that are used in template.
Special case is when entity type is specified as an object references.
|
|
|