Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle column type methods using __call magic method #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 21 additions & 194 deletions src/Bosnadev/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

/**
* Class PostgresGrammar
*
* Available Column Type supported dynamically:
* hstore, uuid, jsonb, point, line,
* path, box, polygon, circle,
* money, int4range, int8range, numrange,
* tsrange, tstzrange, daterange, tsvector
*
* @package Bosnadev\Database\Schema\Grammars
*/
class PostgresGrammar extends \Illuminate\Database\Schema\Grammars\PostgresGrammar
{
/**
* Check if the type is uuid, use internal guid
*
*
* @param string $type
* @return \Doctrine\DBAL\Types\Type
*/
Expand All @@ -28,47 +35,30 @@ protected function getDoctrineColumnType($type)
}

/**
* Create the column definition for a character type.
* Handle dynamic method calls.
*
* @param Fluent $column
* @return string
* @param string $method
* @param array $parameters
* @return mixed
*/
protected function typeCharacter(Fluent $column)
public function __call($method, $parameters)
{
return "character({$column->length})";
}
if(substr($method, 0, 4) === 'type') {
$type = substr($method, 4, strlen($method) - 4);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khalilst it should also use strtolower to ensure types work no matter how many capital letters are used.


/**
* Create the column definition for a hstore type.
*
* @param Fluent $column
* @return string
*/
protected function typeHstore(Fluent $column)
{
return "hstore";
}

/**
* Create the column definition for a uuid type.
*
* @param Fluent $column
* @return string
*/
protected function typeUuid(Fluent $column)
{
return "uuid";
return lcfirst($type);
}
}

/**
* Create the column definition for a jsonb type.
* Create the column definition for a character type.
*
* @param Fluent $column
* @return string
*/
protected function typeJsonb(Fluent $column)
protected function typeCharacter(Fluent $column)
{
return "jsonb";
return "character({$column->length})";
}

/**
Expand All @@ -81,6 +71,7 @@ protected function typeIpAddress(Fluent $column)
{
return 'inet';
}

/**
* Create the column definition for a CIDR notation-style netmask.
*
Expand All @@ -103,28 +94,6 @@ protected function typeMacAddress(Fluent $column)
return 'macaddr';
}

/**
* Create the column definition for a 2D geometric point (x, y), x and y are floating-point numbers.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePoint(Fluent $column)
{
return 'point';
}

/**
* Create the column definition for a line represented as a standard linear equation Ax + By + C = 0.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeLine(Fluent $column)
{
return 'line';
}

/**
* Create the column definition for a line segment represented by two points (x1, y1), (x2, y2).
*
Expand All @@ -136,148 +105,6 @@ protected function typeLineSegment(Fluent $column)
return 'lseg';
}

/**
* Create the column definition for a path represented as a list of points (x1, y1), (x2, y2), ..., (xn, yn).
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePath(Fluent $column)
{
return 'path';
}

/**
* Create the column definition for a box represented by opposite corners of the box as points (x1, y1), (x2, y2).
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeBox(Fluent $column)
{
return 'box';
}

/**
* Create the column definition for a polygon represented by a list of points (vertices of the polygon).
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typePolygon(Fluent $column)
{
return 'polygon';
}

/**
* Create the column definition for a circle represented by a center point and a radius <(x, y), r>
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeCircle(Fluent $column)
{
return 'circle';
}

/**
* Create the column definition for storing amounts of currency with a fixed fractional precision.
*
* This will store values in the range of: -92233720368547758.08 to +92233720368547758.07. (92 quadrillion).
* Output is locale-sensitive, see lc_monetary setting of PostgreSQL instance/s.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeMoney(Fluent $column)
{
return 'money';
}

/**
* Create the column definition for an int4range type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeInt4range(Fluent $column)
{
return "int4range";
}

/**
* Create the column definition for an int8range type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeInt8range(Fluent $column)
{
return "int8range";
}

/**
* Create the column definition for an numrange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeNumrange(Fluent $column)
{
return "numrange";
}

/**
* Create the column definition for an tsrange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeTsrange(Fluent $column)
{
return "tsrange";
}

/**
* Create the column definition for an tstzrange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeTstzrange(Fluent $column)
{
return "tstzrange";
}

/**
* Create the column definition for an daterange type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeDaterange(Fluent $column)
{
return "daterange";
}

/**
* Create the column definition for a Text Search Vector type.
*
* @param Fluent $column
*
* @return string
*/
protected function typeTsvector(Fluent $column)
{
return "tsvector";
}

/**
* @param mixed $value
* @return mixed|string
Expand Down