Skip to content

Commit

Permalink
Další věci...
Browse files Browse the repository at this point in the history
  • Loading branch information
Programátor authored and Programátor committed Jul 20, 2020
1 parent 0e0ff30 commit 2d16881
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor/
/vendor/*
composer.phar
composer.lock
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Tool for creating and changing MySQL database tables with script.

(No need to update tables with SQL commands on application update)

##Installation
## Installation
`composer require zrny/mksql`

## Requirements

nette database

9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"name": "zrny/mksql",
"description": "Creating and AutoUpdating tables in MySQL",
"version": "0.0.2",
"type": "library",
"keywords": ["sql","table","create"],
"keywords": [
"sql",
"table",
"create"
],
"license": "MIT",
"homepage": "https://github.com/ozh/empty-empty",
"homepage": "https://github.com/Zrny/MkSQL",
"authors": [
{
"name": "Štěpán Zrník",
Expand Down
51 changes: 33 additions & 18 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
namespace Zrnik\MkSQL;


use Exception;
use Nette\Database\ConstraintViolationException;

class Column
{
public $ColName = null;


public function __construct(string $ColumnName, ColumnType $type = null)
{
$this->ColName = $ColumnName;
Expand All @@ -26,61 +26,76 @@ public function __construct(string $ColumnName, ColumnType $type = null)
* @var $Type ColumnType
*/
private $Type = null;

public function setType(?ColumnType $type)
{
$this->Type = $type;
return $this;
}

/**
* @return bool|string
* @throws Exception
*/
public function getTypeString()
{
$typestr = $this->Type->getString();

if($typestr === false)
throw new \Exception("Unknown Type!");


return $typestr;
$ts = $this->Type->getString();
if ($ts === false)
throw new Exception("Unknown Type!");
return $ts;
}

public $CanBeNull = true;

public function notNull($notnull = true)
{
$this->CanBeNull = !$notnull;
return $this;
}

public $DefaultValue = null;
public function default(?string $string = null)
private $DefaultValue = null;

public function setDefault(?string $string = null)
{
if($this->RequireUnique === true)
if ($this->RequireUnique === true)
throw new ConstraintViolationException("Cannot set Default to column with Unique!");

if($this->Type === null)
if ($this->Type === null)
throw new ConstraintViolationException("Type required before setting default value!");

if(!$this->Type->canUniqueOrDefault())
if (!$this->Type->canUniqueOrDefault())
throw new ConstraintViolationException("This type cannot have default value!");

$this->DefaultValue = $string;
return $this;
}

public $RequireUnique = false;
public function unique(bool $needUnique = true)
public function getDefault()
{
return $this->DefaultValue;
}

private $RequireUnique = false;

public function setUnique(bool $needUnique = true)
{
if($this->DefaultValue !== null)
if ($this->DefaultValue !== null)
throw new ConstraintViolationException("Cannot set Unique to column with Default Value");

if($this->Type === null)
if ($this->Type === null)
throw new ConstraintViolationException("Type required before setting unique!");

if(!$this->Type->canUniqueOrDefault())
if (!$this->Type->canUniqueOrDefault())
throw new ConstraintViolationException("This type cannot be unique!");

$this->RequireUnique = $needUnique;
return $this;
}

public function getUnique()
{
return $this->RequireUnique;
}


}
91 changes: 67 additions & 24 deletions src/ColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,53 @@
* Date: 20.07.2020 9:48
*/


namespace Zrnik\MkSQL;


class ColumnType
{

CONST TINYTEXT = 100010;
CONST TEXT = 100011;
CONST MEDIUMTEXT = 100012;
CONST LONGTEXT = 100013;
//region CONST
const TINYTEXT = 100010;
const TEXT = 100011;
const MEDIUMTEXT = 100012;
const LONGTEXT = 100013;

CONST TINYBLOB = 200010;
CONST BLOB = 200011;
CONST MEDIUMBLOB = 200012;
CONST LONGBLOB = 200013;
const TINYBLOB = 200010;
const BLOB = 200011;
const MEDIUMBLOB = 200012;
const LONGBLOB = 200013;

CONST CHAR = 100;

const DECIMAL = 210010;
const DOUBLE = 210011;
const FLOAT = 210012;

const INT = 220010;
const MEDIUMINT = 220011;
const SMALLINT = 220012;
const TINYINT = 220013;

const CHAR = 1;
const VARCHAR = 2;
const BINARY = 3;
const VARBINARY = 4;

const ENUM = 1000;
const SET = 1001;

const BIT = 2000;
const BOOL = 2001;
//endregion

private $Params = [];
private $Type = -1;

public function __construct(int $type)
{
$this->Type = $type;
}

private $Params = [];

public function addParam($par)
{
$this->Params[] = $par;
Expand All @@ -49,15 +67,6 @@ public function canUniqueOrDefault()
return !in_array($this->Type,$Disallowed);
}



private function parameterized($type)
{
if(count($this->Params) === 0)
return $type;
return $type."(".implode(",",$this->Params).")";
}

public function getString()
{
switch($this->Type)
Expand All @@ -83,8 +92,38 @@ public function getString()
case self::LONGBLOB:
return "longblob";

//Sem si vygeneroval

case self::DECIMAL:
return $this->parameterized(strtolower("DECIMAL"));
case self::DOUBLE:
return $this->parameterized(strtolower("DOUBLE"));
case self::FLOAT:
return $this->parameterized(strtolower("FLOAT"));
case self::INT:
return $this->parameterized(strtolower("INT"));
case self::MEDIUMINT:
return $this->parameterized(strtolower("MEDIUMINT"));
case self::SMALLINT:
return $this->parameterized(strtolower("SMALLINT"));
case self::TINYINT:
return $this->parameterized(strtolower("TINYINT"));
case self::CHAR:
return $this->parameterized("char");
return $this->parameterized(strtolower("CHAR"));
case self::VARCHAR:
return $this->parameterized(strtolower("VARCHAR"));
case self::BINARY:
return $this->parameterized(strtolower("BINARY"));
case self::VARBINARY:
return $this->parameterized(strtolower("VARBINARY"));
case self::ENUM:
return $this->parameterized(strtolower("ENUM"));
case self::SET:
return $this->parameterized(strtolower("SET"));
case self::BIT:
return $this->parameterized(strtolower("BIT"));
case self::BOOL:
return $this->parameterized(strtolower("BOOL"));

default:
break;
Expand All @@ -93,7 +132,11 @@ public function getString()
return false;
}

private function parameterized($type)
{
if (count($this->Params) === 0)
return $type;



return $type . "(" . implode(",", $this->Params) . ")";
}
}
Loading

0 comments on commit 2d16881

Please sign in to comment.