Skip to content
Merged
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
15 changes: 12 additions & 3 deletions lib/Adapter/PgsqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ActiveRecord\Column;
use ActiveRecord\Connection;
use ActiveRecord\Inflector;
use ActiveRecord\Table;

/**
* Adapter for Postgres (not completed yet)
Expand All @@ -22,14 +23,22 @@ public function supports_sequences(): bool
return true;
}

public function get_sequence_name(string $table, string $column_name): string
public function next_sequence_value(string $sequence_name): ?string
{
return "nextval('" . str_replace("'", "\\'", $sequence_name) . "')";
}

private function get_sequence_name(string $table, string $column_name): string
{
return "{$table}_{$column_name}_seq";
}

public function next_sequence_value(string $sequence_name): ?string
public function init_sequence_name(Table $table): string
{
return "nextval('" . str_replace("'", "\\'", $sequence_name) . "')";
$table->sequence = $table->class->getStaticPropertyValue('sequence') ??
$this->get_sequence_name($table->table, $table->pk[0] ?? '');

return $table->sequence;
}

public function limit(string $sql, int $offset = 0, int $limit = 0): string
Expand Down
11 changes: 3 additions & 8 deletions lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,7 @@ public function tables(): array
*/
public function transaction(): void
{
if (!$this->connection->beginTransaction()) {
throw new DatabaseException();
}
assert($this->connection->beginTransaction(), new DatabaseException());
}

/**
Expand Down Expand Up @@ -469,14 +467,11 @@ public function supports_sequences(): bool
/**
* Return a default sequence name for the specified table.
*
* @param string $table Name of a table
* @param string $column_name Name of column sequence is for
*
* @return string sequence name or null if not supported
*/
public function get_sequence_name(string $table, string $column_name): string
public function init_sequence_name(Table $table): string
{
return "{$table}_seq";
return '';
}

/**
Expand Down
49 changes: 16 additions & 33 deletions lib/Serialize/Serialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ActiveRecord\Serialize;

use ActiveRecord\DateTimeInterface;
use ActiveRecord\Exception\UndefinedPropertyException;
use ActiveRecord\Model;
use ActiveRecord\Types;

Expand Down Expand Up @@ -187,33 +186,27 @@ private function check_include(): void
}
assert(is_string($association));

try {
$assoc = $this->model->$association;
$assoc = $this->model->$association;

if (null === $assoc) {
unset($this->attributes[$association]);
} elseif (!is_array($assoc)) {
$serialized = new $serializer_class($assoc, $options);
$this->attributes[$association] = $serialized->to_a();
} else {
$includes = [];
if (!is_array($assoc)) {
$serialized = new $serializer_class($assoc, $options);
$this->attributes[$association] = $serialized->to_a();
} else {
$includes = [];

foreach ($assoc as $a) {
$serialized = new $serializer_class($a, $options);
foreach ($assoc as $a) {
$serialized = new $serializer_class($a, $options);

if ($this->includes_with_class_name_element) {
$className = get_class($a);
assert(is_string($className));
$includes[strtolower($className)][] = $serialized->to_a();
} else {
$includes[] = $serialized->to_a();
}
if ($this->includes_with_class_name_element) {
$className = get_class($a);
assert(is_string($className));
$includes[strtolower($className)][] = $serialized->to_a();
} else {
$includes[] = $serialized->to_a();
}

$this->attributes[$association] = $includes;
}
} catch (UndefinedPropertyException $e) {
// move along

$this->attributes[$association] = $includes;
}
}
}
Expand Down Expand Up @@ -247,16 +240,6 @@ final public function to_a(): array
return $this->attributes;
}

/**
* Returns the serialized object as a string.
*
* @see to_s
*/
final public function __toString(): string
{
return $this->to_s();
}

/**
* Performs the serialization.
*/
Expand Down
12 changes: 1 addition & 11 deletions lib/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function __construct(string $class_name)
$this->set_table_name();
$this->get_meta_data();
$this->set_primary_key();
$this->set_sequence_name();
$this->conn->init_sequence_name($this);
$this->set_cache();

$this->callback = new CallBack($class_name);
Expand Down Expand Up @@ -535,16 +535,6 @@ private function set_cache(): void
$this->cache_model_expire = $model_class_name::$cache_expire ?? Cache::$options['expire'] ?? 0;
}

private function set_sequence_name(): void
{
if (!$this->conn->supports_sequences()) {
return;
}

$this->sequence = $this->class->getStaticPropertyValue('sequence') ??
$this->conn->get_sequence_name($this->table, $this->pk[0] ?? '');
}

private function set_associations(): void
{
$namespace = $this->class->getNamespaceName();
Expand Down
7 changes: 4 additions & 3 deletions lib/Validations.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,10 @@ public function validates_length_of(array $attrs): void
if ('within' == $range_options[0] || 'in' == $range_options[0]) {
$range = $options[$range_options[0]];

if (!$this->is_range($range)) {
throw new ValidationsArgumentError("$range_options[0] must be an array composing a range of numbers with key [0] being less than key [1]");
}
assert(
$this->is_range($range),
new ValidationsArgumentError("$range_options[0] must be an array composing a range of numbers with key [0] being less than key [1]")
);
$range_options = ['minimum', 'maximum'];
$options['minimum'] = $range[0];
$options['maximum'] = $range[1];
Expand Down
3 changes: 2 additions & 1 deletion test/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use test\models\AwesomePerson;
use test\models\Book;
use test\models\BookAttrAccessible;
use test\models\BookAttrProtected;
use test\models\Event;
use test\models\RmBldg;
use test\models\Venue;
Expand Down Expand Up @@ -221,7 +222,7 @@ public function testAttrAccessible()

public function testAttrProtected()
{
$book = new BookAttrAccessible(['book_id' => 999]);
$book = new BookAttrProtected(['book_id' => 999]);
$this->assertNull($book->book_id);
$book->book_id = 999;
$this->assertEquals(999, $book->book_id);
Expand Down
12 changes: 0 additions & 12 deletions test/ActiveRecordWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,6 @@ public function testFullyQualifiedNameWithExplicitDbName()
Course::$db = '';
}

public function testSequenceWasSet()
{
if (ConnectionManager::get_connection()->supports_sequences()) {
$this->assertEquals(
ConnectionManager::get_connection()->get_sequence_name('authors', 'author_id'),
Author::table()->sequence
);
} else {
$this->assertNull(Author::table()->sequence);
}
}

public function testSequenceWasExplicitlySet()
{
if (ConnectionManager::get_connection()->supports_sequences()) {
Expand Down
9 changes: 9 additions & 0 deletions test/PgsqlAdapterTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use ActiveRecord\ConnectionManager;
use test\models\Author;

class PgsqlAdapterTest extends AdapterTestCase
{
Expand All @@ -16,6 +17,14 @@ public function testInsertId()
$this->assertTrue(ConnectionManager::get_connection()->insert_id('authors_author_id_seq') > 0);
}

public function testSequenceWasSet()
{
$this->assertEquals(
ConnectionManager::get_connection()->init_sequence_name(Author::table()),
Author::table()->sequence
);
}

public function testToSql(): void
{
$this->assert_sql_includes(
Expand Down
10 changes: 10 additions & 0 deletions test/ValidatesNumericalityOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ public function testGreaterThan()
$this->assert_invalid([5, '5'], 'must be greater than 5');
}

public function testEqualTo()
{
BookNumericality::$validates_numericality_of = [
'numeric_test' => ['equal_to' => 5]
];

$this->assert_valid([5, '5']);
$this->assert_invalid([6, '6'], 'must be equal to 5');
}

public function testGreaterThanOrEqualTo()
{
BookNumericality::$validates_numericality_of = [
Expand Down
1 change: 0 additions & 1 deletion test/models/BookAttrAccessible.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ class BookAttrAccessible extends Model
public static string $table_name = 'books';

public static array $attr_accessible = ['author_id'];
public static array $attr_protected = ['book_id'];
}
2 changes: 1 addition & 1 deletion test/models/BookAttrProtected.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class BookAttrProtected extends Model
];

// No attributes should be accessible
public static array $attr_accessible = [null];
public static array $attr_protected = ['book_id'];
}