Skip to content

Instantly share code, notes, and snippets.

@travisfont
Last active December 30, 2015 07:29
Show Gist options
  • Select an option

  • Save travisfont/7796237 to your computer and use it in GitHub Desktop.

Select an option

Save travisfont/7796237 to your computer and use it in GitHub Desktop.
Generates some SQL structure code using PHP objects
<?php
class create_table
{
private $query;
private $increments = array(NULL, NULL, NULL);
private $unsigned;
private $nullable = " NOT NULL";
private $engine = array(FALSE, "MyISAM");
function __construct($table)
{
$this->query = "CREATE TABLE `".$table."` (";
}
function increments($id)
{
$this->increments[0] = "\n"."`".$id."` int(11)";
$this->increments[1] = " AUTO_INCREMENT";
return $this;
}
function unsigned()
{
$this->unsigned = " unsigned";
return $this;
}
function nullable()
{
$this->nullable = "";
return $this;
}
public function engine($type)
{
if (!$this->engine[0])
{
$this->engine[0] = TRUE;
$this->engine[1] = $type;
return $this->engine;
}
}
public function execute()
{
if (count($this->increments) > 0)
{
$this->increments[3] = " AUTO_INCREMENT=0";
}
return
(
$this->query.
$this->increments[0].
$this->unsigned.
$this->nullable.
$this->increments[1].
"\n) ENGINE=".
$this->engine[1].
$this->increments[2].
" DEFAULT CHARSET=utf-8;"
);
}
}
$table= new create_table('bikini_entries');
$table->engine('InnoDB');
$table->increments('id')->unsigned();
//$table->increments('id')->unsigned()->nullable();
$table->engine('Tss'); // already set so won't work
echo '<pre>';
echo $table->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment