$b = explode('.', basename($file));
array_pop($b);
- $tn = strtolower(preg_replace('/[A-Z]/','$1_', array_pop($b)));
+ $tn = strtolower(preg_replace('/([A-Z])/','_$1', array_pop($b)));
$tn = preg_replace('/^_+/', '', $tn);
+ if (!empty($_SERVER['argv'][3])) {
+ $tn= $_SERVER['argv'][3];
+ }
+
+
$this->toSQL($tn);
+ $b= basename(dirname($file));
+
+ $do = $this->toDO($b, $tn);
+
+ $dofile = dirname($file).'/DataObjects/'. ucfirst($tn).'.php';
+ if (!file_exists($dofile)) {
+ echo "WRITING $dofile\n";
+ file_put_contents($dofile, $do);
+ } else {
+ // should support AUTOCODE...
+ echo "DELETE $dofile IF YOU WANT TO RECREATED IT..\n";
+ }
die("DONE");
}
$f->name = $o->name;
$f->type = 'VARCHAR';
$f->default = "''";
+ $f->extra = "NOT NULL";
$f->size = min(255,max(8, pow(2, strlen(decbin(($o->width/2)-1)))));
$this->cols[] = $f;
break;
$f->name = isset($o->hiddenName) ? $o->hiddenName : $o->name;
$f->type = 'INT';
$f->size = 11;
+ $f->extra = "NOT NULL";
$f->default = 0;
$this->cols[] = $f;
continue;
$f->type = 'INT';
$f->size = 11;
if ($o->name == 'id') {
- $f->extra = "AUTO_INCREMENT PRIMARY KEY";
+ $f->extra = "NOT NULL AUTO_INCREMENT ";
+ $this->primary_key = $o->name;
} else {
$f->default = 0;
}
+
array_unshift($this->cols, $f);
break;
default:
foreach($this->cols as $i=> $f) {
$out .= $i ? ",\n" : "";
- $out .= " {$f->name} {$f->type}";
+ $row = '';
+ $sz = $f->type ;
if (!empty($f->size)) {
- $out .= "(". $f->size.")";
+ $sz .= "(". $f->size.")";
}
+ $row .= " ".str_pad($sz, 20);
if (!empty($f->extra)) {
- $out .= ' ' . $f->extra;
+ $row .= ' ' . $f->extra;
}
if (isset($f->default)) {
- $out .= " DEFAULT ". $f->default;
+ $row .= " DEFAULT ". $f->default;
}
-
+ $this->cols[$i]->def = $row;
+ $out.= " ".str_pad($f->name, 30) . $row;
+ }
+ if ($this->primary_key) {
+ $out .= ",\n PRIMARY KEY (". $this->primary_key . ")";
}
+
$out .= "\n);";
echo $out;
}
+ function toDO($b,$tn)
+ {
+ $utn = ucfirst($tn);
+ $out = '<?php
+/**
+ * Table Definition for builder
+ */
+require_once \'DB/DataObject.php\';
+
+';
+ $out.="class Pman_{$b}_DataObjects_$utn extends DB_DataObject
+{
+ ###START_AUTOCODE
+ /* the code below is auto generated do not remove the above tag */
+
+ public \$__table = '$tn'; // table name
+";
+ foreach($this->cols as $f) {
+ $out .= ' public $' . str_pad($f->name.';',30 ). '// ' . $f->def . "\n";
+
+ }
+ $out.="
+ /* the code above is auto generated do not remove the tag below */
+ ###END_AUTOCODE
+
+
+}";
+ echo "\n\n";
+ echo $out;
+ echo "\n\n";
+ return $out;
+ }
}
\ No newline at end of file