Overview Installation VTL reference Configuration Architecture Java reference Velosurf FAQ |
Velosurf OverviewContent of this page: What is Velosurf? What is Velosurf?Velosurf is a java database mapping layer, for the Velocity template engine. It is meant for ease-of-use, genericity and efficiency.
Velosurf main features are:
Velosurf can be used as a standard Velocity tool (check the Velocity Tools subproject, a great architectural component allowing re-usable tools to be easily plugged in Web applications) as well as a generic java database abstraction layer. Why Velosurf?The main goal of Velosurf is to spare template writers the pain of rewriting specific database mapping layers in Java for each project involving Velocity and database entities. It is also meant to have a clean separation between SQL, Java and VTL. Persistence layers are hard to design and maintain, and have been made obsolete by database caching technology. So why not have a thin and generic mapping engine that will fetch values directly from the database when you need them? With Velosurf, object properties can represent, not just column values, but also complex queries, something difficult to do with a persistence framework. Last but not least: developers often try to protect users from many concepts that may appear too complex or too weird to them. But even if a data model is complex, its complexity has nothing or little to do with technical constraints, and everything to do with logic and modeling. The spirit of Velosurf is that those constraints should be shared and exposed to all people involved in a project - like designers - who should be as competent as developers are (who said more?) to deal with business logic. In a nutshell, Velosurf allows developers to expose a data model where properties are table fields or SQL queries and designers to easily traverse this data model by means of the 'dot' operator. Velosurf principlesVelosurf provides an 'out-of-the-box' automatized mapping to database tables and fields, along with the ability to easily define custom entities, queries and actions in SQL. Velosurf also allows the overloading of the mapping Java objects. Terminology: Using the Velocity variable that references the Velosurf tool in the Velocity context, template writers can access all (or a subset of) the tables of a database, that are called Velosurf entities. Entities properties are attributes (that can be table columns as long as customized queries). Entities methods that trigger database update/delete/insert queries are actions. Each realisation of an entity is an instance. Velosurf uses an XML configuration file that:
All queries are done via a pool of prepared statements automatically handled by Velosurf. Concurrent accesses are taken into account. Entities, Instances, Attributes and Actions
What does the syntax look like?Task: fetch a single value of the database and display it: Assuming that you've got the following table in your database:
and that it contains the value (1,'Hello, World!') then you can write the following VTL expression: $db.message.fetch(1).msg_text
Or, to be more explicit: #set( $hello = $db.message.fetch(1) )
and this would of course produce: Hello World!
Task: fetch multiple rows of a single column and display them in sequential order: Still with the same table: #foreach($message in $db.messages)
Task: insert a value in the database: Assuming that $values is an empty map: #set( $values.msg_text = 'How do you do?' )
If you are using the velosurf.tool.HttpQueryTool tool to parse HTTP query parameters, you can pass it directly to an insert or an update method. However, please note that you should not modify database values from within templates if you want your Webapp to follow the MVC paradigm. A simple exampleThe code in sample.vm allows the user to view the content of the 'my_strings' table (key-value pairs), and to add or remove values. Then, it gives an example of a 1-n join between tables 'user' and 'message'.
A more complex example is given in the Detailed featuresYou can start using Velosurf right out-the-box just relying on the reverse-engeenering it provides. Next, by explicitely declaring entities in velosurf.xml, you can extend its functionnalities:
Inside a Veltools Webapp, Velosurf provides the following additionnal features:
The syntax for those two last features is detailed in the Configuration section. Check also the bookstore sample. Caching: If an entity in your application is very often fetched, you can decide to cache it (by adding caching="soft" to its declaration). For instance, it could be a good choice if you've got a framed Webapp (which of course one should try to avoid, frames are bad ;o) and that each frame request will have to fetch the current user based on an id stored as a cookie. Working with several schemas: two options here.
Connection pool: Velosurf starts with |