PHP: RuBisCO ORM -- Protoyp funktioniert
Das Grundprinzip mit einzelnen Klassen für die Datentypen plus Methoden zur Quelltextgenerierung funktioniert. Im Prototyp sind Int und VarChar implementiert.
#!/usr/local/php
<?php
namespace rubisco\tools\orm;
ini_set('include_path', '../:' . ini_get('include_path'));
require_once 'Table.php';
require_once 'Type/Int.php';
require_once 'Type/VarChar.php';
$table = new Table('testOnlyID');
$table->fields[] = new Type\Int('id', 1); // name, min, max
$table->fields[] = new Type\VarChar('status', 20, true); // name, length, notEmpty
echo $table->generateCode();
?>
Macht daraus für die setStatus()-Methode:
public function setStatus($value) {
if(('' !== $value) && (strlen($value) <= 20)) {
$this->_status = $value;
return $this;
} else {
throw new \Exception(__CLASS__ . " Wrong value for status attribute.");
}
// setStatus
Von der Programmierung ist das nicht besonders spannend. Die Vorlagen der Klassen liegen in templates und die dortigen Platzhalter werden mit str_replace() ersetzt. Die generateMethod()-Methode zur Generierung der obigen setStatus() ist folgende:
public function generateMethod() {
$ucName = ucfirst($this->name);
$conds = array();
if($this->notEmpty) {
$conds[] = "('' !== \$value)";
}
$conds[] = "(strlen(\$value) <= {$this->length})";
$conds = join(' && ', $conds);
$code = <<< EOD
public function set{$ucName}(\$value) {
if($conds) {
\$this->_{$this->name} = \$value;
return \$this;
} else {
throw new \Exception(__CLASS__ . " Wrong value for {$this->name} attribute.");
}
}// set{$ucName}
EOD;
return $code;
}// generateMethod
Mehr Informationen nötig
Statt nur CREATE TABLE ... werden dann noch zusätzlich der Applikationsname, der Namensraum und Metadaten wie Copyright, Lizenz, ... benötigt. Spezifische Fehlermeldungen für die einzelnen Überprüfungen müßte es auch noch geben.
Quelltext (MIT Lizenz)
Der komplette Quelltext von RuBisCO ORM liegt in Subversion.
