er2code simplifies database access for programming by auto-generating code. It is a useful tool in software development for developers who want to make accessing databases quick and easy.
If you are using Python for your application, SQLObject is an excellent alternative to er2code. It is much cleaner and works with all kinds of databases. The idea behind the er2code project was to ease the burden on programmers by automating the process of writing boring and tedious database wrappers. Such tasks can lead to many errors that result from sleeping while typing.
The primary approach taken by er2code is mapping every relation or table of the database to a class of the same name, which contains functions for manipulating the attributes in the relation. The er2code script has a long list of “Functions” containing specific attribute requirements that, when satisfied, generate code. These functions contain plain text that, when combined with identifiers, generate a unique text string value for each attribute.
However, it is not as simple as that since concepts such as constructs, handling foreign relations like one-to-many, and deleting rows from tables must also be considered. The struct block layout of the file in er2code is somewhat similar to C structs and SQL. Every relation has its struct block defined such as:
User {
... attributes go here ...
}
In the above statement, when the er2code script is executed, it creates a User class, and any attribute defined within that relation is identified by the attribute's name, followed by one or more whitespace-separated attribute type definers and terminated by a semicolon.
For example, if the following attribute description is defined under the User relation:
User {
id serial;
username string(30) default(noname) sort unique index;
password string(40);
email string(100);
}
Users will have four attributes: id, username, password, and email. The id attribute is automatically incremented integer and defined as a primary key. The username attribute is a 30-character string, defined as sort, meaning that the result when the users list method is called should be sorted based on this attribute. It is also of type index, which means it should be possible to find users according to the value of this attribute. The password attribute is a 40-character string, and the email attribute is a 100-character string.
Using the above user attribute descriptions with er2code to generate PHP code should produce the following code:
// Connect to the database.
$db = new DataBase ();
// Find and print the User with the username halfdan.
$halfdan = $db->findUserByUsername( 'halfdan' );
if ($halfdan != null ) {
print $halfdan->getUsername() . ' ';
}
// List users sorted by username.
foreach ( $db->listUser() as $user ) {
print '' . $user->getName() . '';
}
Overall, er2code is an efficient tool that can automate tedious database wrapper writing. The program can be improved to be more versatile by adding support for other programming languages and database types, as its current support is limited to PHP MySQL combination.