Database Description Language

er2code / Database Description Language

The layout of the a file is a bit like C structs, and a bit like SQL.

Each relation is defined inside it's own struct block:

User {
   ... attributes go here ...
}

This statement will cause a User class to be created.

Each attribute is defined by the name of the attribute, followed by one or more whitespace separated attribute type definers, and terminated by a semicolon.

User {
    id serial;
    username string(30) default(noname) sort unique index;
    password string(40);
    email string(100);
}

This will cause the User relation to contain four attributes:

  • id which is of type serial, an automatically incremented integer that is defined as the primary key.

  • username which is a 30-character long string, it is defined as sort which means that when a method which lists Users is called, the result should be sorted by this attribute. It also is of type index, which means that it should be possible to find a User based on the value of this attribute.

  • password is a 40-character string.

  • email is a 100-character string.

This is all er2code needs to know to create a class for the User relation, and the DataBase class, which is the one you use to connect to the database, and to list and find relations.

If the description above is used with er2code to generate PHP code, it should make it possible to write code like the following:

// Connect to the database.
$db = new DataBase ();

$halfdan = $db->findUserByUsername ( 'halfdan' );
if ( $halfdan != null ) {
    print $halfdan->getUsername() . ' <' . $halfdan->getEmail() . '>';
}

// List is automatically sorted by username
foreach ( $db->listUser() as $user ) {
    print '<a href="user.php?id=' . $user->getId() . '">' . $user->getName() . '</a>';
}