Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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)
);
}

/// exceptionless methods
//@{
Expand Down
12 changes: 12 additions & 0 deletions core/DB/PgSQL.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,23 @@ public function isConnected()

public function obtainSequence($sequence)
{
if($sequence === 'uuid')
return $this->obtainUuid();

$res = $this->queryRaw("select nextval('{$sequence}') as seq");
$row = pg_fetch_assoc($res);
pg_free_result($res);
return $row['seq'];
}


/**
* @return string
*/
public function obtainUuid()
{
return UuidUtils::make();
}

/**
* @return PgSQL
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 @@ -362,5 +362,35 @@ public static function ipRange($name)
{
return new PrimitiveIpRange($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);
}
}
?>
4 changes: 2 additions & 2 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
33 changes: 33 additions & 0 deletions core/Form/Primitives/PrimitiveUuid.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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);
$this->setAllowedPattern(self::UUID_PATTERN);
Copy link
Member

Choose a reason for hiding this comment

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

А почему бы в этом классе вместо конструктора сразу не настроить свойство allowedPattern? А вот метод setAllowedPattern перекрыть и бросать Exception что бы не меняли эту настройку.

Copy link
Member Author

Choose a reason for hiding this comment

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

Согласен.
Готво :-)

}
}
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;
}

}
27 changes: 27 additions & 0 deletions core/Form/Primitives/PrimitiveUuidIdentifierList.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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
{

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 @@ -137,6 +137,7 @@ public static function fill(
($type == 'identifier') // obsoleted
|| ($type == 'integerIdentifier')
|| ($type == 'scalarIdentifier')
|| ($type == 'uuidIdentifier')
);

return $property;
Expand Down
41 changes: 41 additions & 0 deletions main/Utils/UuidUtils.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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
{

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

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

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 @@ -348,6 +351,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
57 changes: 57 additions & 0 deletions meta/types/UuidType.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Georgiy T. Kutusurua *
* *
* 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 Types
**/
class UuidType extends StringType
{

public function getPrimitiveName()
{
return 'uuid';
}

/**
* @throws WrongArgumentException
* @return UuidType
**/
public function setDefault($default)
{
Assert::isUuid(
$default,
"strange default value given - '{$default}'"
);

$this->default = $default;

return $this;
}

public function getDeclaration()
{
if ($this->hasDefault())
return $this->default;

return 'null';
}

public function isMeasurable()
{
return false;
}

public function toColumnType()
{
return 'DataType::create(DataType::UUID)';
}
}
?>
Loading