postgres ddl code generator

Esse commit está contido em:
Felipe Nascimento de Moura
2011-03-31 01:25:25 -03:00
commit db5e744a14
5 arquivos alterados com 111 adições e 33 exclusões
+17 -1
Ver Arquivo
@@ -1,4 +1,6 @@
<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script src='scripts/jquery.js'></script>
<style tye='text/css'>
@@ -6,6 +8,20 @@
{
font-weight:bold;
}
keyword
{
font-weight:bold;
color: blue;
}
object
{
color: blue;
}
comment
{
font-style:italic;
color: green;
}
</style>
</head>
<body>
+23 -10
Ver Arquivo
@@ -47,13 +47,26 @@ class pgsql implements DBMS{
return "UNIQUE";
}
public function getHeader()
{
$author= substr($_SESSION['login'], 0, 30);
$author= str_pad($author, 30);
return"<comment>#".str_pad('', 60, '#')."
# ".str_pad("Generated by theWebMind(mind3rd release)", 58)."#
# ".str_pad("DBMS: PostgreSQL Date: ". date('d/m/Y'), 58)."#
# ".str_pad("Generator Author: Felipe Nascimento de Moura", 58)."#
# ".str_pad("Source Author: ".$author, 58)."#
#".str_pad('', 60, '#')."</comment>
";
}
public function createFK()
{
return "
<command>ALTER </command><object>TABLE</object> <tablename>
<command>ADD</command> <object>CONSTRAINT</object> <constraintname>
<object>FOREIGN KEY (<column>)</object> <command>REFERENCES </command>
<referencetablename>(<referencecolumnname>)
<keyword>ALTER </keyword><object>TABLE</object> <tablename>
<keyword>ADD</keyword> <object>CONSTRAINT</object> <constraintname>
<object>FOREIGN KEY (<propertyname>)</object> <keyword>REFERENCES </keyword>
<referencetablename>(<referencecolumnname>);
";
}
@@ -67,15 +80,15 @@ class pgsql implements DBMS{
public function createPK()
{
return "
<command>ALTER</command> <object>TABLE</object> <tablename>
<command>ADD</command> <object>PRIMARY KEY</object> (<propertienames>);
<keyword>ALTER</keyword> <object>TABLE</object> <tablename>
<keyword>ADD</keyword> <object>PRIMARY KEY</object> (<propertienames>);
";
}
public function createTable()
{
return "
<command>CREATE </command><object>TABLE</object> <tablename>
<keyword>CREATE </keyword><object>TABLE</object> <tablename>
(
<properties>
<primarykeys>
@@ -83,10 +96,10 @@ class pgsql implements DBMS{
";
}
public function getModel($command)
public function getModel($keyword)
{
if(method_exists($this, $command))
return $this->$command();
if(method_exists($this, $keyword))
return $this->$keyword();
return false;
}
}
+42 -12
Ver Arquivo
@@ -18,18 +18,22 @@ namespace DQB;
*/
class Query {
const TABLE_NAME = '/<tablename(\/)?>/';
const PROPS = '/<properties(\/)?>/';
const PROPS_NAME = '/<propertienames(\/)?>/';
const PROP_NAME = '/<propertyname(\/)?>/';
const PROP_TYPE = '/<propertytype(\/)?>/';
const PROP_SIZE = '/<propertysize(\/)?>/';
const PROP_DETAILS= '/<propertydetails(\/)?>/';
const PROP_DEFAULT= '/<defaultvalue(\/)?>/';
const PROP_OPTIONS= '/<options(\/)?>/';
const PRIMARY_KEYS= '/<primarykeys(\/)?>/';
const TABLE_NAME = '/<tablename(\/)?>/';
const PROPS = '/<properties(\/)?>/';
const PROPS_NAME = '/<propertienames(\/)?>/';
const PROP_NAME = '/<propertyname(\/)?>/';
const PROP_TYPE = '/<propertytype(\/)?>/';
const PROP_SIZE = '/<propertysize(\/)?>/';
const PROP_DETAILS = '/<propertydetails(\/)?>/';
const PROP_DEFAULT = '/<defaultvalue(\/)?>/';
const PROP_OPTIONS = '/<options(\/)?>/';
const PRIMARY_KEYS = '/<primarykeys(\/)?>/';
const CONSTRAINT_NAME= '/<constraintname(\/)?>/';
const REF_TAB_NAME = '/<referencetablename(\/)?>/';
const REF_COL_NAME = '/<referencecolumnname(\/)?>/';
const FK_NAME= '/<fkname(\/)?>/';
public $query= "";
public $closingQuery= Array();
private $pks= Array(); // temporary variable
private $fks= Array(); // temporary variable
@@ -39,6 +43,8 @@ class Query {
$this->pks= Array();
$this->fks= false;
$this->fks= Array();
$this->closingQuery= false;
$this->closingQuery= Array();
}
private function parseDetails(Array $prop, $table)
@@ -137,6 +143,27 @@ class Query {
$query= preg_replace(self::PRIMARY_KEYS, trim($tmpQuery), $query);
}
private function createForeignKeys(&$query, $table)
{
$tmplt= QueryFactory::getQueryString('createFk');
foreach($this->fks as $fk)
{
$constraintName= str_replace('.', '_', $fk['ref_to_property']);
$constraintName= "fk_".$table['name'].'_'.$constraintName;
$tb= explode('.', $fk['ref_to_property']);
$col= $tb[1];
$tb= $tb[0];
$tmpQuery= preg_replace(self::TABLE_NAME, $table['name'], $tmplt);
$tmpQuery= preg_replace(self::CONSTRAINT_NAME, $constraintName, $tmpQuery);
$tmpQuery= preg_replace(self::PROP_NAME, $fk['name'], $tmpQuery);
$tmpQuery= preg_replace(self::REF_TAB_NAME, $tb, $tmpQuery);
$tmpQuery= preg_replace(self::REF_COL_NAME, $col, $tmpQuery);
$this->closingQuery[]= $tmpQuery;
}
}
public function __construct($command, Array $table, $template)
{
$query= '';
@@ -145,11 +172,14 @@ class Query {
switch($command)
{
case 'createTable':
$query= preg_replace(self::TABLE_NAME, $table['name'], $template);
$query= preg_replace(self::TABLE_NAME,
$table['name'],
$template);
//if(preg_match(self::PROPS, $template))
self::createProperties($query, $table);
self::createPrimaryKeys($query, $table);
self::createForeignKeys($query, $table);
break;
}
@@ -159,7 +189,7 @@ class Query {
public function __toString()
{
return htmlentities($this->query);
return $this->query;
}
}
+26 -2
Ver Arquivo
@@ -18,8 +18,32 @@ namespace DQB;
*/
class QueryFactory {
public static $queries= Array();
public static $dbms= Array();
public static $queries = Array();
public static $dbms = Array();
public static $showHeader= true;
public static function showQueries($decorated=true, $raw= false)
{
$closingQueries= Array();
$outpt= "";
if(self::$showHeader)
$outpt= self::getQueryString('getHeader');
foreach(self::$queries as $qrs)
{
foreach($qrs as $qr)
{
$outpt.= $qr->query;
$closingQueries= array_merge($qr->closingQuery, $closingQueries);
}
}
$outpt.= implode("\n ", $closingQueries);
if(!$decorated)
$outpt= strip_tags($outpt);
if($raw)
echo htmlentities($outpt);
else
echo $outpt;
}
public static function addQuery($command, Array $table, $template)
{
+3 -8
Ver Arquivo
@@ -64,7 +64,7 @@ EOT
$p= new DAO\ProjectFactory(Mind::$currentProject);
$param= ($this->table=='*')? false: $this->table;
$entities= $p->getEntity($param);
//print_r($entities);
switch($this->query)
{
case 'create':
@@ -100,13 +100,8 @@ EOT
$query::build($this->query, $entity);
}
foreach($query::$queries as $qrs)
{
foreach($qrs as $qr)
{
echo $qr;
}
}
DQB\QueryFactory::$showHeader= true;
DQB\QueryFactory::showQueries();
return $this;
}