Java Doc for DbSqlTemplate.java in  » Development » jodd » jodd » db » orm » sqlgen » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jodd » jodd.db.orm.sqlgen 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


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

Field Summary
public static  booleanDEFAULT_COLUMN_ALIAS
     Default value for generating column aliases.
public static  ColumnAliasTypeDEFAULT_COLUMN_ALIAS_TYPE
     Default column alias type, when they are used.
public static  booleanDEFAULT_ESCAPE_MACROS
     Default value for escaping block macros for all new instances.
protected  int[]colNdx
    
protected  booleancolumnAliases
    
protected  ColumnAliasTypecolumnAliasesType
    
protected  intcolumnCount
     Column counter for COLUMN_CODE column alias type.
protected  Map<String, String[]>columnData
     Column or table aliases.
protected  DbOrmdbOrm
    
protected  booleanescape
    
protected  charescapeChar
    
protected  StringescapeLeftBoundary
    
protected  StringescapeRightBoundary
    
protected static  Map<String, Class>loadedClasses
     Internal map for speeding up class loading.
protected  Map<String, Object>parameters
    
protected  Map<String, Object>references
     Map of object references that are used in templates.
protected  Map<String, DbEntityDescriptor>tablesRefs
     Table references map.
protected  Stringtemplate
    

Constructor Summary
public  DbSqlTemplate(String template)
    
public  DbSqlTemplate(String template, DbOrm dbOrm)
    

Method Summary
protected  voidappendColumnName(StringBuilder result, String tableRef, String tableName, String column, int i)
     Simply appends column name with optional table reference and alias.
public  DbSqlTemplatecolumnAliases(boolean aliases)
    
public  DbSqlTemplateescape(boolean escape)
     Specifies if escaping character is used.
public  StringgenerateQuery()
    
public  Map<String, String[]>getColumnData()
    
public  Map<String, Object>getQueryParameters()
    
public  Map<String, Object>getQueryReferences()
    
protected  StringnextRegion(StringBuilder destination, String template, String leftBoundary, String rightBoundary)
    
protected  StringnextRegionNoEscape(StringBuilder destination, String template, String leftBoundary, String rightBoundary)
    
protected  StringnextRegionWithEscape(StringBuilder destination, String template, String leftBoundary, String rightBoundary)
    
protected  StringparseColumns(String template)
     Resolves columns lists macros used in SELECT queries.
public  StringparseReferences(String template)
     Parse column references that are defined with table reference and property name. The result is table reference with column name.
public  StringparseTables(String template)
     Resolves table macros, allowing using types for table names.
public  DbSqlTemplatesetColumnAliasesType(ColumnAliasType aliasesType)
     Specifies column DbSqlTemplate.columnAliases(boolean) type and implicitly turns on generation of column aliases.
public  DbSqlTemplateuse(String name, Object value)
     Adds new object reference that are used in template.

Field Detail
DEFAULT_COLUMN_ALIAS
public static boolean DEFAULT_COLUMN_ALIAS(Code)
Default value for generating column aliases.
See Also:   DbSqlTemplate.columnAliases



DEFAULT_COLUMN_ALIAS_TYPE
public static ColumnAliasType DEFAULT_COLUMN_ALIAS_TYPE(Code)
Default column alias type, when they are used.



DEFAULT_ESCAPE_MACROS
public static boolean DEFAULT_ESCAPE_MACROS(Code)
Default value for escaping block macros for all new instances.
See Also:   DbSqlTemplate.escape(boolean)



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.



columnData
protected Map<String, String[]> columnData(Code)
Column or table aliases.



dbOrm
protected DbOrm dbOrm(Code)



escape
protected boolean escape(Code)



escapeChar
protected char escapeChar(Code)



escapeLeftBoundary
protected String escapeLeftBoundary(Code)



escapeRightBoundary
protected String escapeRightBoundary(Code)



loadedClasses
protected static Map<String, Class> loadedClasses(Code)
Internal map for speeding up class loading.



parameters
protected Map<String, Object> parameters(Code)



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



template
protected String template(Code)




Constructor Detail
DbSqlTemplate
public DbSqlTemplate(String template)(Code)



DbSqlTemplate
public DbSqlTemplate(String template, DbOrm dbOrm)(Code)




Method Detail
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.



columnAliases
public DbSqlTemplate columnAliases(boolean aliases)(Code)
Specifies if column aliases should be created during the generation of column list DbSqlTemplate.parseColumns(String) ;



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.



generateQuery
public String generateQuery()(Code)



getColumnData
public Map<String, String[]> getColumnData()(Code)



getQueryParameters
public Map<String, Object> getQueryParameters()(Code)



getQueryReferences
public Map<String, Object> getQueryReferences()(Code)



nextRegion
protected String nextRegion(StringBuilder destination, String template, String leftBoundary, String rightBoundary)(Code)



nextRegionNoEscape
protected String nextRegionNoEscape(StringBuilder destination, String template, String leftBoundary, String rightBoundary)(Code)



nextRegionWithEscape
protected String nextRegionWithEscape(StringBuilder destination, String template, String leftBoundary, String rightBoundary)(Code)



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.



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.