Fixed bug that database does not support bit.

This commit is contained in:
李铭昕 2022-03-09 20:10:15 +08:00
parent d7e5f336c4
commit f727897e39
5 changed files with 129 additions and 12 deletions

View File

@ -1,7 +1,5 @@
SET NAMES utf8mb4;
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
@ -17,8 +15,6 @@ VALUES
(2,1,'Hyperf Guide 2019','2018-01-02 00:00:00','2018-01-02 00:00:00'),
(3,2,'Hyperf Component Guide','2018-01-02 00:00:00','2018-01-02 00:00:00');
DROP TABLE IF EXISTS `images`;
CREATE TABLE `images` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(128) NOT NULL DEFAULT '',
@ -38,8 +34,6 @@ VALUES
(5,'https://avatars2.githubusercontent.com/u/44228082?s=200&v=4',3,'book','2018-01-01 00:00:00','2018-01-01 00:00:00'),
(6,'https://avatars2.githubusercontent.com/u/44228082?s=200&v=4',0,'','2018-01-01 00:00:00','2018-01-01 00:00:00');
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL DEFAULT '',
@ -52,8 +46,6 @@ INSERT INTO `role` (`id`, `name`, `created_at`, `updated_at`)
VALUES
(1,'author','2018-01-01 00:00:00','2018-01-01 00:00:00');
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL DEFAULT '' COMMENT 'user name',
@ -70,7 +62,17 @@ VALUES
(3,'Hidden',0,'2019-01-01 00:00:00','2019-02-16 09:59:36'),
(100,'John',0,NULL,NULL);
DROP TABLE IF EXISTS `user_ext`;
CREATE TABLE `user_bit` (
`id` bigint(20) unsigned NOT NULL,
`bit` bit(1) NOT NULL DEFAULT b'0',
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `user_bit` (`id`, `bit`, `created_at`, `updated_at`)
VALUES
(1,b'0','2022-01-01 00:00:00','2022-01-01 00:00:00');
CREATE TABLE `user_ext` (
`id` bigint(20) unsigned NOT NULL,
@ -88,8 +90,6 @@ VALUES
(1,0,1.20,'','{"id": 1}','2019-03-13 02:38:04','2019-03-13 02:38:04'),
(2,0,0.00,NULL,NULL,'2019-02-07 16:24:02','2019-02-17 04:44:41');
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
@ -103,4 +103,3 @@ INSERT INTO `user_role` (`id`, `user_id`, `role_id`, `created_at`, `updated_at`)
VALUES
(1,1,1,'2018-01-01 00:00:00','2018-01-01 00:00:00'),
(2,2,1,'2018-01-01 00:00:00','2018-01-01 00:00:00');

View File

@ -2,6 +2,7 @@
## Fixed
- [#4588](https://github.com/hyperf/hyperf/pull/4588) Fixed bug that `database` does not support `bit`.
- [#4589](https://github.com/hyperf/hyperf/pull/4589) Fixed bug that ephemeral instance register failed when using nacos.
## Added

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database;
use PDO;
use PDOStatement;
class MySqlBitConnection extends MySqlConnection
{
public function bindValues(PDOStatement $statement, array $bindings): void
{
foreach ($bindings as $key => $value) {
$type = PDO::PARAM_STR;
if (in_array($value, [0, 1], true)) {
$type = PDO::PARAM_INT;
}
$statement->bindValue(
is_string($key) ? $key : $key + 1,
$value,
$type
);
}
}
}

View File

@ -14,17 +14,25 @@ namespace HyperfTest\Database;
use Carbon\Carbon;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Contract\PaginatorInterface;
use Hyperf\Database\Connection;
use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\ConnectionResolver;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Connectors\ConnectionFactory;
use Hyperf\Database\Connectors\MySqlConnector;
use Hyperf\Database\Events\QueryExecuted;
use Hyperf\Database\Model\Events\Saved;
use Hyperf\Database\MySqlBitConnection;
use Hyperf\Database\Schema\Column;
use Hyperf\Database\Schema\MySqlBuilder;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Container;
use Hyperf\Paginator\LengthAwarePaginator;
use Hyperf\Paginator\Paginator;
use Hyperf\Utils\ApplicationContext;
use HyperfTest\Database\Stubs\ContainerStub;
use HyperfTest\Database\Stubs\Model\User;
use HyperfTest\Database\Stubs\Model\UserBit;
use HyperfTest\Database\Stubs\Model\UserExt;
use HyperfTest\Database\Stubs\Model\UserExtCamel;
use HyperfTest\Database\Stubs\Model\UserRole;
@ -394,6 +402,46 @@ class ModelRealBuilderTest extends TestCase
}
}
public function testSaveBitValue()
{
$container = Mockery::mock(Container::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('has')->andReturn(true);
$container->shouldReceive('get')->with('db.connector.mysql.bit')->andReturn(new MySqlConnector());
$connector = new ConnectionFactory($container);
Connection::resolverFor('mysql.bit', static function ($connection, $database, $prefix, $config) {
return new MySqlBitConnection($connection, $database, $prefix, $config);
});
$dbConfig = [
'driver' => 'mysql.bit',
'host' => '127.0.0.1',
'database' => 'hyperf',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
];
$connection = $connector->make($dbConfig);
$resolver = new ConnectionResolver(['default' => $connection]);
$container->shouldReceive('get')->with(ConnectionResolverInterface::class)->andReturn($resolver);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(null);
/** @var UserBit $model */
$model = UserBit::query()->find(1);
$model->bit = (int) (! $model->bit);
$this->assertTrue($model->save());
$model->bit = ! $model->bit;
$this->assertTrue($model->save());
}
protected function getContainer()
{
$dispatcher = Mockery::mock(EventDispatcherInterface::class);

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database\Stubs\Model;
/**
* @property int $id
* @property string $bit
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class UserBit extends Model
{
/**
* The table associated with the model.
*/
protected $table = 'user_bit';
/**
* The attributes that are mass assignable.
*/
protected $fillable = ['id', 'bit', 'created_at', 'updated_at'];
/**
* The attributes that should be cast to native types.
*/
protected $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
}