DB Mapping

FIXME

IGNORE THE FOLLOWING NOTES FOR NOW, AND JUST TAKE A LOOK AT THE SAMPLE CODE !!

DB Mapping 
  JDBC driver 
  MySQL 
  needs ID 
  recall advantages: no necessity for sql 
  Caching Mechanism 
    HopObject.cache 
    clearCache 
    invalidate 
    http://helma.org/stories/77405/ 
  logSQL 
  basic mapping 
    _db 
    _table 
    _id 
    _idgen (for oracle) 
  Simple Property Mappings 
    prop.readonly 
    prop.private

Make sure that the SQL-Statements in mysql.sql are executed before the app is started.

demoSimpleMapping equals demoPrototypes with the difference, that objects are now stored in a relational database, and not in the internal XML database.

Turn on logSQL and watch what happens. Call app.clearCache and see what happens.

Theoretically any JDBC accessible database can be used: MySQL, hsqldb (a pure Java database), Oracle, MS SQL Server, and so forth. As an alternative to a relational database, Helma's XML database can be used. This file-based database is located at [HelmaHome]/db/appname, each file in that directory corresponding to a HopObject. The advantage is, that no explicit mapping is required and the developer does not have to worry about database design up front. On the other hand, this XML-database admittedly can not compete with industry-proven relational databases as MySQL or Oracle regarding reliability and scalability. Furthermore some of Helma's object modelling features are just supported for relational databases. For these reasons we will focus in this documentation on an object model, that is mapped on such a database.

demoSimpleMapping.zip

### mysql.sql ### 
 
CREATE DATABASE demoSimpleMapping; 
USE demoSimpleMapping; 
 
GRANT ALL ON demoSimpleMapping.* TO helma@localhost IDENTIFIED BY 'secret'; 
 
CREATE TABLE person ( 
  person_id MEDIUMINT(10) NOT NULL, 
  person_name TINYTEXT, 
  person_height TINYINT unsigned, 
  person_dateofbirth DATETIME, 
  PRIMARY KEY (person_id) 
); 
 

### db.properties ### 
 
jad.url      = jdbc:mysql://localhost/demoSimpleMapping 
jad.driver   = com.mysql.jdbc.Driver 
jad.user     = helma 
jad.password = secret 
 
 
### Root/functions.js ### 
 
function main_action() { 
   // list all Persons 
   for (var i=0; i<root.count(); i++) { 
      var p = root.get(i); 
      res.write("<li>"); 
      res.write(p.name + " "); 
      if (p.dateofBirth) res.write(p.dateOfBirth.format("dd.MM.yyyy") + " "); 
      res.write("<a href=" + this.href("deletePerson") + 
         "?personId=" + p._id + ">delete this Person </a> ");
      res.write("<a href=" + p.href("edit") + ">edit this Person</a>"); 
   } 
   res.write("<br /><a href=" + this.href("createPerson") + ">create new Person</a>"); 
} 
 
function createPerson_action() { 
   if (req.data.send) { 
      // instantiate a new object of type 'Person' 
      var p = new Person(); 
      // set the property 'name' of the new Person 
      p.name = req.data.name; 
      // set a integer value 
      p.height = 180; 
      // set the date of birth 
      p.dateOfBirth = new Date(1978, 4, 24); 
      // store the new object persistently 
      root.add(p); 
      // now redirect the client to the list of all Persons 
      res.redirect(this.href("main")); 
   } 
   this.renderSkin("createPerson"); 
} 
 
function deletePerson_action() { 
   // if personId is passed in URL, and such a Person exists... 
   if (req.data.personId && root.get(req.data.personId)) { 
      // ... we delete that Person 
      var p = root.get(req.data.personId); 
      p.remove(); 
   } 
   // now redirect the client to the list of all Persons 
   res.redirect(this.href("main")); 
} 
 
 
### Root/createPerson.skin ### 
 
<form action=""> 
  <input type="text" name="name"> 
  <input type="submit" name="send" value="create new Person"> 
</form> 
 
 
### Person/type.properties ### 
 
_db = jad 
_table = person 
 
_id = person_id 
 
name = person_name 
height = person_height 
dateOfBirth = person_dateofbirth 
 
 
### Person/functions.js ### 
 
function edit_action() { 
   // if the form has been submitted... 
   if (req.data.send) { 
      // change the name of that person 
      this.name = req.data.name; 
      // redirect to the root main action 
      res.redirect(root.href("main")); 
   } 
   this.renderSkin("edit"); 
} 
 
 
### Person/edit.skin ### 
 
<form action=""> 
  <input type="text" name="name"> 
  <input type="submit" name="send" value="change name"> 
</form>