1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
<?php
namespace CzProject\SqlGenerator;
class SqlDocument
{
/** @var IStatement[] */
private $statements = array();
/**
* @return static
*/
public function addStatement(IStatement $statement)
{
$this->statements[] = $statement;
return $this;
}
/**
* @return bool
*/
public function isEmpty()
{
return empty($this->statements);
}
/**
* @return string[]
*/
public function getSqlQueries(IDriver $driver)
{
$output = array();
foreach ($this->statements as $statement) {
$output[] = $statement->toSql($driver);
}
return $output;
}
/**
* @return string
*/
public function toSql(IDriver $driver)
{
$output = '';
$first = TRUE;
foreach ($this->statements as $statement) {
if ($first) {
$first = FALSE;
} else {
$output .= "\n";
}
$output .= $statement->toSql($driver);
$output .= "\n";
}
return $output;
}
/**
* @param string
* @param IDriver
* @return void
* @throws IOException
*/
public function save($file, IDriver $driver)
{
// create directory
$dir = dirname($file);
if (!is_dir($dir) && !@mkdir($dir, 0777, TRUE) && !is_dir($dir)) { // @ - dir may already exist
throw new IOException("Unable to create directory '$dir'.");
}
// write file
$content = $this->toSql($driver);
if (@file_put_contents($file, $content) === FALSE) { // @ is escalated to exception
throw new IOException("Unable to write file '$file'.");
}
}
/**
* @param string
* @param array
* @return Statements\Insert
*/
public function insert($tableName, array $data)
{
$statement = new Statements\Insert($tableName, $data);
$this->addStatement($statement);
return $statement;
}
/**
* @param string
* @return Statements\CreateTable
*/
public function createTable($tableName)
{
$statement = new Statements\CreateTable($tableName);
$this->addStatement($statement);
return $statement;
}
/**
* @param string
* @return Statements\DropTable
*/
public function dropTable($tableName)
{
$statement = new Statements\DropTable($tableName);
$this->addStatement($statement);
return $statement;
}
/**
* @param string
* @param string
* @return Statements\RenameTable
*/
public function renameTable($oldTable, $newTable)
{
$statement = new Statements\RenameTable($oldTable, $newTable);
$this->addStatement($statement);
return $statement;
}
/**
* @param string
* @return Statements\AlterTable
*/
public function alterTable($tableName)
{
$statement = new Statements\AlterTable($tableName);
$this->addStatement($statement);
return $statement;
}
/**
* @param string
* @return Statements\SqlCommand
*/
public function command($command)
{
$statement = new Statements\SqlCommand($command);
$this->addStatement($statement);
return $statement;
}
/**
* @param string
* @return Statements\Comment
*/
public function comment($comment)
{
$statement = new Statements\Comment($comment);
$this->addStatement($statement);
return $statement;
}
}