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

【建议】统一sql使用规范 #1881

Closed
Adol1111 opened this issue Jan 16, 2019 · 15 comments
Closed

【建议】统一sql使用规范 #1881

Adol1111 opened this issue Jan 16, 2019 · 15 comments
Labels

Comments

@Adol1111
Copy link

目前apollo里主要用的数据库查询使用的hibernate 和 原生sql两种,但原生sql他无法使用提供的hibernate的命名策略,对数据库迁移的时候会非常的麻烦,比如这种https://github.com/ctripcorp/apollo/blob/b794f38a8f80df4448e8354da915ff0911e89ed1/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/entity/ConsumerRole.java#L16-L17

我们这边用的是pg,但是pg是大小写不敏感的,所以我们改成了consumer_role,原本只用调整一下命名策略就可以的,但是使用了native sql的地方,命名策略就无效了,只能手动调整代码。

如果apollo更新的话,我们就不能做简单的rebase,还要去对比sql的差异,工作量比较大。建议统一一下sql的使用,要么全部hibernate 或者 全部native sql,最好对pr也限制一下

@Adol1111
Copy link
Author

里面还混合了一些spring的注解,spring的注解名称对应的是hibernate的类名,但是SQLDelete对应的是真实的表名,维护起来就特别的麻烦,也不能做全局的replace

@Adol1111
Copy link
Author

@nobodyiam
Copy link
Member

应该只有native sql的地方会不兼容吧,其它的都还是走的hibernate的?

@Adol1111
Copy link
Author

Adol1111 commented Jan 17, 2019

不兼容的确实是native sql,但是其他地方也有使用不规范的地方,比如上面的那个例子

https://github.com/ctripcorp/apollo/blob/b794f38a8f80df4448e8354da915ff0911e89ed1/apollo-common/src/main/java/com/ctrip/framework/apollo/common/entity/BaseEntity.java#L37-L38

https://github.com/ctripcorp/apollo/blob/b794f38a8f80df4448e8354da915ff0911e89ed1/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/repository/ItemRepository.java#L25-L26

我的意思是,如果改了@Column(name = "DataChange_LastModifiedBy") ,比如改成@Column(name = "data_change_last_modified_by")

那么这里就要改成 @Query(sql = "update Item set isdeleted=1,data_change_last_modified_by = ?2 where namespaceId = ?1)

如果都用字段名的话,应该就没这个问题了。我不太熟悉hibernate,应该是可以的吧。
@Query("update Item set isdeleted=1,dataChangeLastModifiedBy = ?2 where namespaceId = ?1")

@YogurtOlderMan
Copy link

mysql设置了不区分大小写 , apollo的数据库全都不能访问了

@nobodyiam
Copy link
Member

mysql不管是不是设置了区分大小写,我们测试都是可以访问的

@stale
Copy link

stale bot commented Dec 6, 2019

This issue has been automatically marked as stale because it has not had activity in the last 90 days. It will be closed in 14 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 6, 2019
@stale
Copy link

stale bot commented Dec 20, 2019

This issue has been automatically closed because it has not had activity in the last 14 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted". Thank you for your contributions.

@stale stale bot closed this as completed Dec 20, 2019
@liutao5121
Copy link

是的 我在适配达梦数据库(大小写敏感)时,也遇到了一些sql解析问题,比如ServerConfig类的@where注解,按道理hibernate生成的sql里默认字段都带有双引号的,但是@where注解的条件语句字段,没有自动加上双引号(该注解默认不加双引号的),导致程序异常,hibernate自动生成的sql如下:
select serverconf0_."Id" as Id1_15_, serverconf0_."DataChange_CreatedBy" as DataChan2_15_, serverconf0_."DataChange_CreatedTime" as DataChan3_15_, serverconf0_."DataChange_LastModifiedBy" as DataChan4_15_, serverconf0_."DataChange_LastTime" as DataChan5_15_, serverconf0_."IsDeleted" as IsDelete6_15_, serverconf0_."Cluster" as Cluster7_15_, serverconf0_."Comment" as Comment8_15_, serverconf0_."Key" as Key9_15_, serverconf0_."Value" as Value10_15_ from "ServerConfig" serverconf0_ where ( serverconf0_.isDeleted = 0)
可以看到,where 条件的isDeleted没有加上双引号,在apollo源码里,如下截图:
3AEC208A-165E-4d69-9109-E6762CF3F1EB
ps: 在大小写敏感的数据库环境下,这里是不是一个bug??@nobodyiam

@nobodyiam
Copy link
Member

@liutao5121 this was fixed by @shoothzj in #4780 as an attempt to support multiple databases.

@liutao5121
Copy link

@liutao5121 this was fixed by @shoothzj in #4780 as an attempt to support multiple databases.

我看了下 #4780 的更新代码,发现这样更改(硬编码方式)只能对mysql使用,如果是其他类型的数据,如达梦,就不能使用了。我自己后来开发了拦截器(通过maven引入该自定义jar),这样很少的源码改动,不同数据库类型都可兼容(目前兼容了mysql、postgresql、达梦、opengauss、OceanBase、tdsql),也可以支持大小写敏感情况

@shoothzj
Copy link
Member

shoothzj commented Aug 9, 2023

@liutao5121 I think we need a unify way to keep the consistent behavior. How did you implement your interceptor? Like java agent?

@long1318737396
Copy link

@liutao5121 this was fixed by @shoothzj in #4780 as an attempt to support multiple databases.

我看了下 #4780 的更新代码,发现这样更改(硬编码方式)只能对mysql使用,如果是其他类型的数据,如达梦,就不能使用了。我自己后来开发了拦截器(通过maven引入该自定义jar),这样很少的源码改动,不同数据库类型都可兼容(目前兼容了mysql、postgresql、达梦、opengauss、OceanBase、tdsql),也可以支持大小写敏感情况

具体怎么操作,我现在是OceanBase的数据库,使用的是apollo 2.0.0版本,容器化环境
发布的时候会报错
image

@nobodyiam
Copy link
Member

@long1318737396 It would be beneficial if you could share the specific error messages from the server logs.

@zzsoszz
Copy link

zzsoszz commented Oct 22, 2024

@liutao5121 大佬可以分享一下拦截器的源码吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants