Skip to content

Commit

Permalink
修复 PostgreSQL jsonb 类型在模型类中的使用 (#610)
Browse files Browse the repository at this point in the history
* 修复 PostgreSQL jsonb 类型在模型类中的使用

* 修复

* 修复测试
  • Loading branch information
Yurunsoft authored Oct 30, 2023
1 parent fe052af commit 186defc
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 245 deletions.
192 changes: 97 additions & 95 deletions .github/pgsql.sql
Original file line number Diff line number Diff line change
@@ -1,218 +1,220 @@
-- ----------------------------
-- Table structure for tb_article
-- Table structure for tb_virtual_column
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_article";
CREATE TABLE "public"."tb_article" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
DROP TABLE IF EXISTS "public"."tb_virtual_column";
CREATE TABLE "public"."tb_virtual_column" (
"id" int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
MAXVALUE 9223372036854775807
START 1
CACHE 1
),
"title" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"content" text COLLATE "pg_catalog"."default" NOT NULL,
"time" timestamp(6) NOT NULL
"amount" int4 NOT NULL,
"virtual_amount" numeric(10,2) NOT NULL GENERATED ALWAYS AS (
((amount)::numeric / (100)::numeric)
) STORED
)
;

-- ----------------------------
-- Table structure for tb_member
-- Table structure for tb_update_time
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_member";
CREATE TABLE "public"."tb_member" (
DROP TABLE IF EXISTS "public"."tb_update_time";
CREATE TABLE "public"."tb_update_time" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1
),
"username" varchar(32) COLLATE "pg_catalog"."default" NOT NULL DEFAULT ''::character varying,
"password" varchar(255) COLLATE "pg_catalog"."default" NOT NULL
)
;
COMMENT ON COLUMN "public"."tb_member"."username" IS '用户名';
COMMENT ON COLUMN "public"."tb_member"."password" IS '密码';

-- ----------------------------
-- Table structure for tb_no_inc_pk
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_no_inc_pk";
CREATE TABLE "public"."tb_no_inc_pk" (
"a_id" int4 NOT NULL,
"b_id" int4 NOT NULL,
"value" varchar(255) COLLATE "pg_catalog"."default" NOT NULL
"date" date,
"time" time(6),
"timetz" timetz(6),
"time2" time(6),
"timetz2" timetz(6),
"timestamp" timestamp(6),
"timestamptz" timestamptz(6),
"timestamp2" timestamp(6),
"timestamptz2" timestamptz(6),
"int" int4,
"bigint" int8
)
;

-- ----------------------------
-- Table structure for tb_performance
-- Table structure for tb_tree
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_performance";
CREATE TABLE "public"."tb_performance" (
DROP TABLE IF EXISTS "public"."tb_tree";
CREATE TABLE "public"."tb_tree" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1
),
"value" varchar(255) COLLATE "pg_catalog"."default" NOT NULL
"parent_id" int4 NOT NULL,
"name" varchar(32) COLLATE "pg_catalog"."default" NOT NULL
)
;

-- ----------------------------
-- Table structure for tb_test_json
-- Records of tb_tree
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_test_json";
CREATE TABLE "public"."tb_test_json" (
INSERT INTO "public"."tb_tree" VALUES (1, 0, 'a');
INSERT INTO "public"."tb_tree" VALUES (2, 0, 'b');
INSERT INTO "public"."tb_tree" VALUES (3, 0, 'c');
INSERT INTO "public"."tb_tree" VALUES (4, 1, 'a-1');
INSERT INTO "public"."tb_tree" VALUES (5, 1, 'a-2');
INSERT INTO "public"."tb_tree" VALUES (6, 4, 'a-1-1');
INSERT INTO "public"."tb_tree" VALUES (7, 4, 'a-1-2');
INSERT INTO "public"."tb_tree" VALUES (8, 2, 'b-1');
INSERT INTO "public"."tb_tree" VALUES (9, 2, 'b-2');

-- ----------------------------
-- Table structure for tb_test_soft_delete
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_test_soft_delete";
CREATE TABLE "public"."tb_test_soft_delete" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1
),
"json_data" json NOT NULL
"title" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"delete_time" int4 NOT NULL DEFAULT 0
)
;
COMMENT ON COLUMN "public"."tb_test_json"."json_data" IS 'json数据';
COMMENT ON TABLE "public"."tb_test_json" IS 'test';

-- ----------------------------
-- Table structure for tb_test_soft_delete
-- Table structure for tb_test_json
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_test_soft_delete";
CREATE TABLE "public"."tb_test_soft_delete" (
DROP TABLE IF EXISTS "public"."tb_test_json";
CREATE TABLE "public"."tb_test_json" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1
),
"title" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"delete_time" int4 NOT NULL DEFAULT 0
"json_data" json NOT NULL,
"jsonb_data" jsonb NOT NULL
)
;
COMMENT ON COLUMN "public"."tb_test_json"."json_data" IS 'json数据';
COMMENT ON COLUMN "public"."tb_test_json"."jsonb_data" IS 'jsonb数据';
COMMENT ON TABLE "public"."tb_test_json" IS 'test';

-- ----------------------------
-- Table structure for tb_tree
-- Table structure for tb_performance
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_tree";
CREATE TABLE "public"."tb_tree" (
DROP TABLE IF EXISTS "public"."tb_performance";
CREATE TABLE "public"."tb_performance" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1
),
"parent_id" int4 NOT NULL,
"name" varchar(32) COLLATE "pg_catalog"."default" NOT NULL
"value" varchar(255) COLLATE "pg_catalog"."default" NOT NULL
)
;

-- ----------------------------
-- Records of tb_tree
-- Table structure for tb_no_inc_pk
-- ----------------------------
INSERT INTO "public"."tb_tree" VALUES (1, 0, 'a');
INSERT INTO "public"."tb_tree" VALUES (2, 0, 'b');
INSERT INTO "public"."tb_tree" VALUES (3, 0, 'c');
INSERT INTO "public"."tb_tree" VALUES (4, 1, 'a-1');
INSERT INTO "public"."tb_tree" VALUES (5, 1, 'a-2');
INSERT INTO "public"."tb_tree" VALUES (6, 4, 'a-1-1');
INSERT INTO "public"."tb_tree" VALUES (7, 4, 'a-1-2');
INSERT INTO "public"."tb_tree" VALUES (8, 2, 'b-1');
INSERT INTO "public"."tb_tree" VALUES (9, 2, 'b-2');
DROP TABLE IF EXISTS "public"."tb_no_inc_pk";
CREATE TABLE "public"."tb_no_inc_pk" (
"a_id" int4 NOT NULL,
"b_id" int4 NOT NULL,
"value" varchar(255) COLLATE "pg_catalog"."default" NOT NULL
)
;

-- ----------------------------
-- Table structure for tb_update_time
-- Table structure for tb_member
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_update_time";
CREATE TABLE "public"."tb_update_time" (
DROP TABLE IF EXISTS "public"."tb_member";
CREATE TABLE "public"."tb_member" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1
),
"date" date,
"time" time(6),
"timetz" timetz(6),
"time2" time(6),
"timetz2" timetz(6),
"timestamp" timestamp(6),
"timestamptz" timestamptz(6),
"timestamp2" timestamp(6),
"timestamptz2" timestamptz(6),
"int" int4,
"bigint" int8
"username" varchar(32) COLLATE "pg_catalog"."default" NOT NULL DEFAULT ''::character varying,
"password" varchar(255) COLLATE "pg_catalog"."default" NOT NULL
)
;
COMMENT ON COLUMN "public"."tb_member"."username" IS '用户名';
COMMENT ON COLUMN "public"."tb_member"."password" IS '密码';

-- ----------------------------
-- Table structure for tb_virtual_column
-- Table structure for tb_article
-- ----------------------------
DROP TABLE IF EXISTS "public"."tb_virtual_column";
CREATE TABLE "public"."tb_virtual_column" (
"id" int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
DROP TABLE IF EXISTS "public"."tb_article";
CREATE TABLE "public"."tb_article" (
"id" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
MAXVALUE 2147483647
START 1
CACHE 1
),
"amount" int4 NOT NULL,
"virtual_amount" numeric(10,2) NOT NULL GENERATED ALWAYS AS (
((amount)::numeric / (100)::numeric)
) STORED
"title" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"content" text COLLATE "pg_catalog"."default" NOT NULL,
"time" timestamp(6) NOT NULL
)
;

-- ----------------------------
-- Primary Key structure for table tb_article
-- Primary Key structure for table tb_virtual_column
-- ----------------------------
ALTER TABLE "public"."tb_article" ADD CONSTRAINT "tb_article_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."tb_virtual_column" ADD CONSTRAINT "tb_virtual_column_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table tb_member
-- Primary Key structure for table tb_update_time
-- ----------------------------
ALTER TABLE "public"."tb_member" ADD CONSTRAINT "tb_member_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."tb_update_time" ADD CONSTRAINT "tb_update_time_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table tb_no_inc_pk
-- Primary Key structure for table tb_tree
-- ----------------------------
ALTER TABLE "public"."tb_no_inc_pk" ADD CONSTRAINT "tb_no_inc_pk_pkey" PRIMARY KEY ("a_id", "b_id");
ALTER TABLE "public"."tb_tree" ADD CONSTRAINT "tb_tree_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table tb_performance
-- Primary Key structure for table tb_test_soft_delete
-- ----------------------------
ALTER TABLE "public"."tb_performance" ADD CONSTRAINT "tb_performance_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."tb_test_soft_delete" ADD CONSTRAINT "tb_test_soft_delete_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table tb_test_json
-- ----------------------------
ALTER TABLE "public"."tb_test_json" ADD CONSTRAINT "tb_test_json_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table tb_test_soft_delete
-- Primary Key structure for table tb_performance
-- ----------------------------
ALTER TABLE "public"."tb_test_soft_delete" ADD CONSTRAINT "tb_test_soft_delete_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."tb_performance" ADD CONSTRAINT "tb_performance_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table tb_tree
-- Primary Key structure for table tb_no_inc_pk
-- ----------------------------
ALTER TABLE "public"."tb_tree" ADD CONSTRAINT "tb_tree_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."tb_no_inc_pk" ADD CONSTRAINT "tb_no_inc_pk_pkey" PRIMARY KEY ("a_id", "b_id");

-- ----------------------------
-- Primary Key structure for table tb_update_time
-- Primary Key structure for table tb_member
-- ----------------------------
ALTER TABLE "public"."tb_update_time" ADD CONSTRAINT "tb_update_time_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."tb_member" ADD CONSTRAINT "tb_member_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Primary Key structure for table tb_virtual_column
-- Primary Key structure for table tb_article
-- ----------------------------
ALTER TABLE "public"."tb_virtual_column" ADD CONSTRAINT "tb_virtual_column_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."tb_article" ADD CONSTRAINT "tb_article_pkey" PRIMARY KEY ("id");
19 changes: 19 additions & 0 deletions src/Components/pgsql/src/Model/PgModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ class PgModel extends Model
{
public const DEFAULT_QUERY_CLASS = ModelQuery::class;

protected static array $__fieldInitParsers = [
'json' => 'parseJsonInitValue',
'jsonb' => 'parseJsonInitValue',
'list' => 'parseListInitValue',
'set' => 'parseSetInitValue',
];

protected static array $__fieldSaveParsers = [
'json' => 'parseJsonSaveValue',
'jsonb' => 'parseJsonSaveValue',
'list' => 'parseListSaveValue',
'set' => 'parseSetSaveValue',
];

protected static array $_fieldParseNullTypes = [
'json',
'jsonb',
];

/**
* @param bool|int $timeAccuracy 推荐最大精度6位(微秒),部分系统能提供9位精度(纳秒)
*
Expand Down
37 changes: 36 additions & 1 deletion src/Components/pgsql/tests/Model/Base/TestJsonBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestJson.name", default="tb_test_json"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestJson.poolName"))
*
* @property int|null $id
* @property \Imi\Util\LazyArrayObject|array|null $jsonData json数据
* @property \Imi\Util\LazyArrayObject|array|null $jsonData json数据
* @property \Imi\Util\LazyArrayObject|array|null $jsonbData jsonb数据
*/
abstract class TestJsonBase extends Model
{
Expand Down Expand Up @@ -94,4 +95,38 @@ public function setJsonData($jsonData)

return $this;
}

/**
* jsonb数据.
* jsonb_data.
*
* @Column(name="jsonb_data", type="jsonb", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false, ndims=0, virtual=false)
*
* @var \Imi\Util\LazyArrayObject|array|null
*/
protected $jsonbData = null;

/**
* 获取 jsonbData - jsonb数据.
*
* @return \Imi\Util\LazyArrayObject|array|null
*/
public function &getJsonbData()
{
return $this->jsonbData;
}

/**
* 赋值 jsonbData - jsonb数据.
*
* @param \Imi\Util\LazyArrayObject|array|null $jsonbData jsonb_data
*
* @return static
*/
public function setJsonbData($jsonbData)
{
$this->jsonbData = null === $jsonbData ? null : $jsonbData;

return $this;
}
}
Loading

0 comments on commit 186defc

Please sign in to comment.