| | |
|
|
|
Configuring data access
Data access has use cases that range from very simple to very complex; many developers pine for an API that is flexible enough to handle complex situations without making simple tasks cumbersome. "Make the common things easy and the hard things possible." The zero.data.Manager and zero.data.groovy.Manager classes provide an API that solves many common use cases while still allowing for very specific customizations.
The following sections of this article provide information about data access with zero.data:
There are two Manager implementations: one for Groovy (zero.data.groovy.Manager) and one for Java (zero.data.Manager). The Groovy implementation provides additional methods that make use of the dynamic nature of Groovy.
The following sections of this article provide information about configuring data access:
Adding zero.data to your application
If you are using data zero query within a Zero application, you need to resolve a dependency in order to install the required jar files. After creating your application, modify the config/ivy.xml file in your application to add the following line in the dependencies section:
<dependency org="zero" name="zero.data" rev="1.0+"/>
After adding the dependency, issue the resolve command using either Ant or Eclipse.
You can also pull in the jar files you need for database access. For example, adding the following to config/ivy.xml pulls in the Derby client jar file:
<dependency name="derbyclient" org="org.apache.derby" rev="10.2+"/>
Configuring zero.data.Manager and zero.data.groovy.Manager in Project Zero
zero.data.Manager, and its groovier friend, zero.data.groovy.Manager, can be initialized with either a javax.sql.DataSource or a java.sql.Connection as a constructor argument. If you are using the Project Zero zero.config configuration file, Manager.create(String) is a convenience method to create a new instance, allowing you to avoid any reference to JDBC APIs.
The following zero.config file snippet shows the configuration of two databases accessible from a Project Zero application:
# This config is for Apache Derby databases:
[/config/db/reviewDB/config]
class=org.apache.derby.jdbc.ClientDataSource
serverName=localhost
portNumber=1527
databaseName=./db/review
connectionAttributes=create=true
# This config is for MySQL databases:
[/config/db/votingDB/config]
class=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
serverName=db.example.org
portNumber=3306
databaseName=voting
user=root
password=<xor>Lz4sLCgwLTs=
Obfuscating passwords
The second example above, /config/db/votingDB/config, illustrates how to use the XOR encoded password to obfuscate the password. For more details on how to generate a password with this encoding, consult the XOR Encoder documentation. Taking this measure prevents the password from being shown in plain text. Although not secure with encryption or hash, this form of encoding simply keeps honest people honest.
Obtain a data manager
Two databases, reviewDB and votingDB, are configured in the zero.config example above. The former is a local network Derby client and the latter is a remote MySQL database. You can configure as many databases as your application needs.
With databases configured at /config/db/{database name}/config, an application developer can obtain a zero.data.Manager or zero.data.groovy.Manager as follows:
// Java code:
import zero.data.Manager;
...
Manager voteDB = Manager.create("votingDB");
List<Map<String, Object>> results = voteDB.queryList("SELECT * FROM votes WHERE numVotes > ?", 10);
// Groovy code:
def voteDB = zero.data.groovy.Manager.create('votingDB')
def numVotes = 10
def results = voteDB.queryList("SELECT * FROM votes WHERE numVotes > $numVotes")
|
|
r13 - 16 Jan 2008 - 19:12:09 - paynel
|
|
|
| | |