Skip to content

#75 Add Uuid support #77

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions core/Base/Assert.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,30 @@ public static function isUnreachable($message = 'unreachable code reached')
{
throw new WrongArgumentException($message);
}

/**
* Checking UUID
* @see http://tools.ietf.org/html/rfc4122
* @param string $value
* @return boolean
*/
public static function checkUuid($value)
{
return (
is_string($value) &&
preg_match(PrimitiveUuid::UUID_PATTERN, $value)
);
}

public static function isUuid($variable, $message = null)
{
if(
!self::checkUuid($variable)
)
throw new WrongArgumentException(
$message.', '.self::dumpArgument($variable)
);
}

public static function isObject($object, $message = null)
{
Expand Down
4 changes: 4 additions & 0 deletions core/DB/PgSQL.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function isConnected()

public function obtainSequence($sequence)
{
if(UuidUtils::isUuidSequence($sequence))
return UuidUtils::make();

$res = $this->queryRaw("select nextval('{$sequence}') as seq");
$row = pg_fetch_assoc($res);
pg_free_result($res);
Expand Down Expand Up @@ -192,6 +195,7 @@ public function getTableInfo($table)
'int4' => DataType::INTEGER,
'int8' => DataType::BIGINT,
'numeric' => DataType::NUMERIC,
'uuid' => DataType::UUID,

'float4' => DataType::REAL,
'float8' => DataType::DOUBLE,
Expand Down
16 changes: 16 additions & 0 deletions core/Exceptions/UnsupportedExtensionException.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Georgiy T. Kutsurua *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Exceptions
**/

class UnsupportedExtensionException extends BaseException {/*_*/}
30 changes: 30 additions & 0 deletions core/Form/Primitive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,35 @@ public static function enumList($name)
{
return new PrimitiveEnumList($name);
}

/**
* @static
* @param $name
* @return PrimitiveUuid
*/
public static function uuid($name)
{
return new PrimitiveUuid($name);
}

/**
* @static
* @param $name
* @return PrimitiveUuidIdentifier
*/
public static function uuidIdentifier($name)
{
return new PrimitiveUuidIdentifier($name);
}

/**
* @static
* @param $name
* @return PrimitiveUuidIdentifierList
*/
public static function uuidIdentifierList($name)
{
return new PrimitiveUuidIdentifierList($name);
}
}
?>
22 changes: 17 additions & 5 deletions core/Form/Primitives/PrimitiveIdentifierList.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
/**
* @ingroup Primitives
**/
final class PrimitiveIdentifierList extends PrimitiveIdentifier
class PrimitiveIdentifierList extends PrimitiveIdentifier
{
protected $value = array();
private $ignoreEmpty = false;

/**
* @return PrimitiveIdentifierList
**/
Expand Down Expand Up @@ -83,7 +83,20 @@ public function importValue($value)

return parent::importValue($value);
}


/**
* Here we check a identifier
* @param $id
* @return bool
*/
protected function checkIdentifier($id)
{
return (
($this->scalar && Assert::checkScalar($id))
|| (!$this->scalar && Assert::checkInteger($id))
);
}

public function import($scope)
{
if (!$this->className)
Expand All @@ -106,8 +119,7 @@ public function import($scope)
continue;

if (
($this->scalar && !Assert::checkScalar($id))
|| (!$this->scalar && !Assert::checkInteger($id))
!$this->checkIdentifier($id)
)
return false;

Expand Down
43 changes: 43 additions & 0 deletions core/Form/Primitives/PrimitiveUuid.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Georgiy T. Kutsurua *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Primitives
**/
class PrimitiveUuid extends PrimitiveString
{
const UUID_PATTERN = '/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i';

/**
* @param string $name
* @return PrimitiveUuid
*/
public static function create($name)
{
return new self($name);
}

public function __construct($name)
{
parent::__construct($name);
parent::setAllowedPattern(self::UUID_PATTERN);
}

/**
* @param $pattern
* @throws UnsupportedMethodException
*/
public function setAllowedPattern($pattern)
{
throw new UnsupportedMethodException('this method not supported yet!');
}

}
28 changes: 28 additions & 0 deletions core/Form/Primitives/PrimitiveUuidIdentifier.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Georgiy T. Kutsurua *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Primitives
**/
class PrimitiveUuidIdentifier extends PrimitiveIdentifier
{

protected function checkNumber($number)
{
Assert::isUuid($number);
}

protected function castNumber($number)
{
return $number;
}

}
37 changes: 37 additions & 0 deletions core/Form/Primitives/PrimitiveUuidIdentifierList.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Georgiy T. Kutsurua *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Primitives
**/
class PrimitiveUuidIdentifierList extends PrimitiveIdentifierList
{
/**
* @param $id
* @return bool
*/
protected function checkIdentifier($id)
{
return (
Assert::checkUuid($id)
);
}

protected function checkNumber($number)
{
Assert::isUuid($number);
}

protected function castNumber($number)
{
return $number;
}
}
2 changes: 2 additions & 0 deletions core/OSQL/DataType.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class DataType extends Enumeration implements DialectString
const TIME = 0x000A0C;
const TIMESTAMP = 0x000A0D;
const INTERVAL = 0x00000F;
const UUID = 0x000005;

const BINARY = 0x00000E;

Expand Down Expand Up @@ -73,6 +74,7 @@ final class DataType extends Enumeration implements DialectString
self::TIME => 'TIME',
self::TIMESTAMP => 'TIMESTAMP',
self::INTERVAL => 'INTERVAL',
self::UUID => 'UUID',

self::BINARY => 'BINARY',

Expand Down
1 change: 1 addition & 0 deletions main/Base/LightMetaProperty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public static function fill(
($type == 'identifier') // obsoleted
|| ($type == 'integerIdentifier')
|| ($type == 'scalarIdentifier')
|| ($type == 'uuidIdentifier')
);

return $property;
Expand Down
56 changes: 56 additions & 0 deletions main/Utils/UuidUtils.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Georgiy T. Kutsurua *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Utils
**/
class UuidUtils extends StaticFactory
{

const SEQUENCE_NAME = 'uuid';

/**
* @static
* @param $sequence
* @return bool
*/
public static function isUuidSequence($sequence)
{
return ($sequence === self::SEQUENCE_NAME);
}

/**
* @static
* @return bool
*/
public static function isExtensionLoaded()
{
return extension_loaded('uuid');
}

/**
* @static
* @param $type
* @return string
* @throws UnsupportedExtensionException
*/
public static function make($type=null)
{
if(!static::isExtensionLoaded() )
throw new UnsupportedExtensionException('uuid is unloaded, but it needed!');

if($type===null)
$type=UUID_TYPE_TIME;

return uuid_create($type);
}

}
7 changes: 6 additions & 1 deletion meta/builders/BaseBuilder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ protected static function buildPointers(MetaClass $class)
$out = null;

if (!$class->getPattern() instanceof AbstractClassPattern) {
$sequenceName = $class->getTableName().'_id';

if($class->getIdentifier()->getType() instanceof UuidType)
$sequenceName = 'uuid';

if (
$class->getIdentifier()->getColumnName() !== 'id'
) {
Expand All @@ -49,7 +54,7 @@ public function getObjectName()

public function getSequence()
{
return '{$class->getTableName()}_id';
return '{$sequenceName}';
}
EOT;
} elseif ($class->getWithInternalProperties()) {
Expand Down
5 changes: 5 additions & 0 deletions meta/classes/MetaClassProperty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ public function toLightProperty(MetaClass $holder)
if ($this->getType() instanceof IntegerType) {
$primitiveName = 'integerIdentifier';
$className = $holder->getName();
} elseif ($this->getType() instanceof UuidType) {
$primitiveName = 'uuidIdentifier';
$className = $holder->getName();
} elseif ($this->getType() instanceof StringType) {
$primitiveName = 'scalarIdentifier';
$className = $holder->getName();
Expand All @@ -350,6 +353,8 @@ public function toLightProperty(MetaClass $holder)
) {
if ($identifier->getType() instanceof IntegerType) {
$primitiveName = 'integerIdentifier';
} elseif ($identifier->getType() instanceof UuidType) {
$primitiveName = 'uuidIdentifier';
} elseif ($identifier->getType() instanceof StringType) {
$primitiveName = 'scalarIdentifier';
} else
Expand Down
Loading