Skip to content

Commit de0571b

Browse files
committed
Adding Binary, Blob and Boolean Column Types. Also enables String columns as primary keys
1 parent af298fc commit de0571b

7 files changed

Lines changed: 235 additions & 7 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* The interface for the binary column.
4+
*
5+
* @since 3.0.0
6+
*
7+
* @package StellarWP\Schema\Columns
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
namespace StellarWP\Schema\Columns;
13+
14+
use StellarWP\Schema\Columns\PHP_Types;
15+
use StellarWP\Schema\Columns\Column_Types;
16+
17+
/**
18+
* Class Binary_Column
19+
*
20+
* @since 3.0.0
21+
*
22+
* @package StellarWP\Schema\Columns
23+
*/
24+
class Binary_Column extends String_Column {
25+
/**
26+
* The type of the column.
27+
*
28+
* @var string
29+
*/
30+
protected string $type = Column_Types::VARBINARY;
31+
32+
/**
33+
* The PHP type of the column.
34+
*
35+
* @var string
36+
*/
37+
protected string $php_type = PHP_Types::STRING;
38+
39+
/**
40+
* Get the supported column types.
41+
*
42+
* @return string[] The supported column types.
43+
*/
44+
protected function get_supported_column_types(): array {
45+
return Column_Types::SUPPORTED_BINARY;
46+
}
47+
48+
/**
49+
* Get the supported PHP types.
50+
*
51+
* @return string[] The supported PHP types.
52+
*/
53+
protected function get_supported_php_types(): array {
54+
return [
55+
PHP_Types::STRING,
56+
PHP_Types::BLOB,
57+
];
58+
}
59+
}

src/Schema/Columns/Blob_Column.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* The class for the blob column.
4+
*
5+
* @since 3.0.0
6+
*
7+
* @package StellarWP\Schema\Columns
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
namespace StellarWP\Schema\Columns;
13+
14+
use StellarWP\Schema\Columns\Contracts\Column;
15+
use StellarWP\Schema\Columns\PHP_Types;
16+
use StellarWP\Schema\Columns\Column_Types;
17+
18+
/**
19+
* Class Blob_Column
20+
*
21+
* @since 3.0.0
22+
*
23+
* @package StellarWP\Schema\Columns
24+
*/
25+
class Blob_Column extends Column {
26+
/**
27+
* The type of the column.
28+
*
29+
* @var string
30+
*/
31+
protected string $type = Column_Types::BLOB;
32+
33+
/**
34+
* The PHP type of the column.
35+
*
36+
* @var string
37+
*/
38+
protected string $php_type = PHP_Types::BLOB;
39+
40+
/**
41+
* Get the supported column types.
42+
*
43+
* @return string[] The supported column types.
44+
*/
45+
protected function get_supported_column_types(): array {
46+
return Column_Types::SUPPORTED_BLOB;
47+
}
48+
49+
/**
50+
* Get the supported PHP types.
51+
*
52+
* @return string[] The supported PHP types.
53+
*/
54+
protected function get_supported_php_types(): array {
55+
return [
56+
PHP_Types::BLOB,
57+
PHP_Types::STRING,
58+
PHP_Types::JSON,
59+
];
60+
}
61+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* The class for the boolean column.
4+
*
5+
* @since 3.0.0
6+
*
7+
* @package StellarWP\Schema\Columns
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
namespace StellarWP\Schema\Columns;
13+
14+
use StellarWP\Schema\Columns\Contracts\Column;
15+
use StellarWP\Schema\Columns\PHP_Types;
16+
use StellarWP\Schema\Columns\Column_Types;
17+
18+
/**
19+
* Class Boolean_Column
20+
*
21+
* @since 3.0.0
22+
*
23+
* @package StellarWP\Schema\Columns
24+
*/
25+
class Boolean_Column extends Column {
26+
/**
27+
* The type of the column.
28+
* Boolean is typically stored as TINYINT(1) in MySQL.
29+
*
30+
* @var string
31+
*/
32+
protected string $type = Column_Types::BOOLEAN;
33+
34+
/**
35+
* The PHP type of the column.
36+
*
37+
* @var string
38+
*/
39+
protected string $php_type = PHP_Types::BOOL;
40+
41+
/**
42+
* Get the supported column types.
43+
*
44+
* @return string[] The supported column types.
45+
*/
46+
protected function get_supported_column_types(): array {
47+
return Column_Types::SUPPORTED_BOOLEAN;
48+
}
49+
50+
/**
51+
* Get the supported PHP types.
52+
*
53+
* @return string[] The supported PHP types.
54+
*/
55+
protected function get_supported_php_types(): array {
56+
return [
57+
PHP_Types::BOOL,
58+
];
59+
}
60+
}

src/Schema/Columns/Column_Types.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,21 +432,31 @@ final class Column_Types {
432432
];
433433

434434
/**
435-
* The column types that are supported for binary columns.
435+
* The column types that are supported for blob columns.
436436
*
437437
* @since 3.0.0
438438
*
439439
* @var string[]
440440
*/
441-
public const SUPPORTED_BINARY = [
442-
self::BINARY,
443-
self::VARBINARY,
441+
public const SUPPORTED_BLOB = [
444442
self::TINYBLOB,
445443
self::BLOB,
446444
self::MEDIUMBLOB,
447445
self::LONGBLOB,
448446
];
449447

448+
/**
449+
* The column types that are supported for binary columns.
450+
*
451+
* @since 3.0.0
452+
*
453+
* @var string[]
454+
*/
455+
public const SUPPORTED_BINARY = [
456+
self::BINARY,
457+
self::VARBINARY,
458+
];
459+
450460
/**
451461
* The column types that are supported for enum columns.
452462
*

src/Schema/Columns/PHP_Types.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ final class PHP_Types {
7575
*/
7676
public const DATETIME = DateTimeInterface::class;
7777

78+
/**
79+
* The PHP type for a blob.
80+
*
81+
* @since 3.0.0
82+
*
83+
* @var string
84+
*/
85+
public const BLOB = 'blob';
86+
7887
/**
7988
* The PHP type for a datetime.
8089
*
@@ -89,5 +98,6 @@ final class PHP_Types {
8998
self::FLOAT,
9099
self::BOOL,
91100
self::DATETIME,
101+
self::BLOB,
92102
];
93103
}

src/Schema/Columns/String_Column.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use StellarWP\Schema\Columns\Contracts\Column;
1515
use StellarWP\Schema\Columns\Contracts\Lengthable;
1616
use StellarWP\Schema\Columns\Contracts\Uniquable;
17+
use StellarWP\Schema\Columns\Contracts\Primarable;
1718
use StellarWP\Schema\Columns\PHP_Types;
1819
use StellarWP\Schema\Columns\Column_Types;
1920

@@ -24,7 +25,7 @@
2425
*
2526
* @package StellarWP\Schema\Columns
2627
*/
27-
class String_Column extends Column implements Lengthable, Uniquable {
28+
class String_Column extends Column implements Lengthable, Uniquable, Primarable {
2829
/**
2930
* The length of the column.
3031
*

src/Schema/Traits/Custom_Table_Query_Methods.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ function ( $entry ) use ( $columns ) {
248248
case PHP_Types::JSON:
249249
$entry[ $column->get_name() ] = wp_json_encode( $entry[ $column->get_name() ] );
250250
break;
251+
case PHP_Types::BLOB:
252+
$value = $entry[ $column->get_name() ];
253+
// Only encode if not already base64 encoded.
254+
$entry[ $column->get_name() ] = is_string( $value ) && base64_decode( $value, true ) !== false ? $value : base64_encode( (string) $value );
255+
break;
251256
default:
252257
break;
253258
}
@@ -472,7 +477,11 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
472477
$output
473478
);
474479

475-
$results = array_map( fn( $result ) => static::transform_from_array( self::amend_value_types( $result ) ), $results );
480+
$results = array_map( fn( $result ) => self::amend_value_types( $result ), $results );
481+
482+
if ( [ '*' === $columns ] ) {
483+
$results = array_map( fn( $result ) => static::transform_from_array( $result ), $results );
484+
}
476485

477486
/**
478487
* Fires after the results of the query are fetched.
@@ -731,10 +740,13 @@ private static function prepare_value_for_query( string $column, $value ): array
731740

732741
switch ( $column->get_php_type() ) {
733742
case PHP_Types::INT:
734-
case PHP_Types::BOOL:
735743
$value = is_array( $value ) ? array_map( fn( $v ) => (int) $v, $value ) : (int) $value;
736744
$placeholder = '%d';
737745
break;
746+
case PHP_Types::BOOL:
747+
$value = is_array( $value ) ? array_map( fn( $v ) => (int) (bool) $v, $value ) : (int) (bool) $value;
748+
$placeholder = '%d';
749+
break;
738750
case PHP_Types::STRING:
739751
case PHP_Types::DATETIME:
740752
$value = is_array( $value ) ?
@@ -750,6 +762,15 @@ private static function prepare_value_for_query( string $column, $value ): array
750762
$value = is_array( $value ) ? array_map( fn( $v ) => (float) $v, $value ) : (float) $value;
751763
$placeholder = '%f';
752764
break;
765+
case PHP_Types::BLOB:
766+
// For blob, we store as base64 encoded string.
767+
if ( is_array( $value ) ) {
768+
$value = array_map( fn( $v ) => is_string( $v ) ? $v : base64_encode( (string) $v ), $value );
769+
} else {
770+
$value = is_string( $value ) && base64_decode( $value, true ) !== false ? $value : base64_encode( (string) $value );
771+
}
772+
$placeholder = '%s';
773+
break;
753774
default:
754775
throw new InvalidArgumentException( "Unsupported column type: $column_type." );
755776
}
@@ -870,6 +891,12 @@ public static function cast_value_based_on_type( string $type, $value ) {
870891
}
871892

872893
return $new_value;
894+
case PHP_Types::BLOB:
895+
// Decode base64 encoded blob data.
896+
if ( is_string( $value ) && base64_decode( $value, true ) !== false ) {
897+
return base64_decode( $value );
898+
}
899+
return (string) $value;
873900
default:
874901
throw new InvalidArgumentException( "Unsupported column type: {$type}." );
875902
}

0 commit comments

Comments
 (0)