Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: recognize json_valid constraint as json field type for database mariadb #2746 #3309

Merged
merged 29 commits into from Mar 6, 2024

Conversation

oldme-git
Copy link
Member

@oldme-git oldme-git commented Feb 7, 2024

fixed #2746

Dear reviewer:
修复 mariaDb gf gen daojson 字段识别为 string 问题

实现说明

mysql driver 中增加对 mariaDb 的识别,如果是,则使用 mariaDb 特有的 json 字段类型的判断方式,用以和 mysql json 做出区分

# Conflicts:
#	cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go
@oldme-git oldme-git requested a review from gqcn February 7, 2024 15:12
@oldme-git
Copy link
Member Author

oldme-git commented Feb 7, 2024

@gqcn First, please upload loads/mariadb:10.4

contrib/drivers/mysql/mysql_table_fields.go Outdated Show resolved Hide resolved
contrib/drivers/mysql/mysql_table_fields.go Outdated Show resolved Hide resolved
contrib/drivers/mysql/mysql_table_fields.go Outdated Show resolved Hide resolved
@houseme
Copy link
Member

houseme commented Feb 29, 2024

fixed #2746

Dear reviewer: 修复 mariaDb gf gen daojson 字段识别为 string 问题

实现说明

mysql driver 中增加对 mariaDb 的识别,如果是,则使用 mariaDb 特有的 json 字段类型的判断方式,用以和 mysql json 做出区分

MariaDB 中json字段存的就是longtext类型,不是json

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


fixed #2746

Dear reviewer: Fix the problem that the json field of mariaDb gf gen dao is recognized as string

Implementation instructions

Add recognition of mariaDb in mysql driver. If yes, use mariaDb’s unique json field type judgment method to distinguish it from mysql json

The json field in MariaDB stores the longtext type, not json

@oldme-git
Copy link
Member Author

fixed #2746
Dear reviewer: 修复 mariaDb gf gen daojson 字段识别为 string 问题

实现说明

mysql driver 中增加对 mariaDb 的识别,如果是,则使用 mariaDb 特有的 json 字段类型的判断方式,用以和 mysql json 做出区分

MariaDB 中json字段存的就是longtext类型,不是json

MariaDB 存 json 字段的确时 longtext,但是会在其上加上 json 约束:https://mariadb.com/resources/blog/using-json-in-mariadb/

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


fixed #2746
Dear reviewer: Fixed the problem that the json field of mariaDb gf gen dao is recognized as string

Implementation instructions

Add recognition of mariaDb in mysql driver, If so, use mariaDb's unique json field type judgment method to distinguish it from mysql json

The json field in MariaDB stores the longtext type, not json

MariaDB does store json fields as longtext, but will add json constraints to it: https://mariadb.com/resources/blog/using-json-in-mariadb/

@houseme
Copy link
Member

houseme commented Feb 29, 2024

CREATE TABLE locations (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,  
    type CHAR(1) NOT NULL,
    latitude DECIMAL(9,6) NOT NULL,
    longitude DECIMAL(9,6) NOT NULL,
    attr LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL 
    CHECK (JSON_VALID(`attr`)
     PRIMARY KEY (id)
);

请注意, attr 列的数据类型是 LONGTEXT 。除此之外,您会发现字符集和排序规则字段中添加了一些约束。然而,最重要的是 CHECK 约束,它指示当 attr 中的数据通过插入或更新进行修改时将执行的函数。
JSON_VALID() 函数是一个预定义函数,用于接收 JSON 数据(字符串形式)并验证其是否有效。有效含义正确形成的 JSON 数据。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


CREATE TABLE locations (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    type CHAR(1) NOT NULL,
    latitude DECIMAL(9,6) NOT NULL,
    longitude DECIMAL(9,6) NOT NULL,
    attr LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL
    CHECK (JSON_VALID(`attr`)
     PRIMARY KEY (id)
);

Note that the data type of the attr column is LONGTEXT . In addition to this, you'll find some constraints added to the character set and collation fields. The most important, however, is the CHECK constraint, which indicates the function that will be executed when the data in attr is modified by insert or update.
The JSON_VALID() function is a predefined function that receives JSON data (as a string) and verifies that it is valid. Valid meaning Correctly formed JSON data.

contrib/drivers/mysql/mysql_table_fields.go Outdated Show resolved Hide resolved
contrib/drivers/mysql/mysql_table_fields.go Outdated Show resolved Hide resolved
@oldme-git oldme-git requested a review from gqcn March 1, 2024 06:33
@gqcn gqcn changed the title fix #2746 fix: recognize json_valid constraint as json field type for database mariadb #2746 Mar 4, 2024
contrib/drivers/mysql/mysql_table_fields.go Outdated Show resolved Hide resolved
contrib/drivers/mysql/mysql.go Outdated Show resolved Hide resolved
@oldme-git oldme-git requested a review from gqcn March 5, 2024 14:06
@gqcn gqcn merged commit 290f4a3 into gogf:master Mar 6, 2024
23 checks passed
@oldme-git oldme-git deleted the fix/#2746 branch March 6, 2024 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The gJsonSupport parameter of gf gen dao does not take effect, and the generated entity model is still string
5 participants