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

BIGTOP-4223: Support PostgreSQL for mybatis #66

Merged
merged 13 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

@Getter
public enum DBType {
MYSQL("mysql", "MYSQL"),
MYSQL("mysql", "MySQL"),
POSTGRESQL("postgresql", "PostgreSQL"),
DM("dm", "DaMeng");

DBType(String code, String desc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ public static <Entity> String insert(TableMetaData tableMetaData, Entity entity,
}
break;
}
case POSTGRESQL: {
sql.INSERT_INTO("\"" + tableMetaData.getTableName() + "\"");
for (Map.Entry<String, String> entry : fieldColumnMap.entrySet()) {
// Ignore primary key
if (Objects.equals(entry.getKey(), tableMetaData.getPkProperty())) {
continue;
}
PropertyDescriptor ps = BeanUtils.getPropertyDescriptor(entityClass, entry.getKey());
if (ps == null || ps.getReadMethod() == null) {
continue;
}
Object value = ReflectionUtils.invokeMethod(ps.getReadMethod(), entity);
if (!ObjectUtils.isEmpty(value)) {
sql.VALUES("\"" + entry.getValue() + "\"", getTokenParam(entry.getKey()));
}
}
break;
}

default: {
log.error("Unsupported data source");
}
Expand Down Expand Up @@ -105,6 +124,25 @@ public static <Entity> String update(TableMetaData tableMetaData, Entity entity,
sql.WHERE(getEquals(tableMetaData.getPkColumn(), tableMetaData.getPkProperty()));
break;
}
case POSTGRESQL: {
sql.UPDATE("\"" + tableMetaData.getTableName() + "\"");
for (Map.Entry<String, String> entry : fieldColumnMap.entrySet()) {
// Ignore primary key
if (Objects.equals(entry.getKey(), tableMetaData.getPkProperty())) {
continue;
}
PropertyDescriptor ps = BeanUtils.getPropertyDescriptor(entityClass, entry.getKey());
if (ps == null || ps.getReadMethod() == null) {
continue;
}
Object value = ReflectionUtils.invokeMethod(ps.getReadMethod(), entity);
if (!ObjectUtils.isEmpty(value)) {
sql.SET("\"" + getEquals(entry.getValue() + "\"", entry.getKey()));
}
}
sql.WHERE(getEquals(tableMetaData.getPkColumn(), tableMetaData.getPkProperty()));
break;
}
default: {
log.error("Unsupported data source");
}
Expand All @@ -123,6 +161,16 @@ public static String selectById(TableMetaData tableMetaData, String databaseId,
sql.WHERE(tableMetaData.getPkColumn() + " = '" + id + "'");
break;
}
case POSTGRESQL: {
String baseColumns = tableMetaData.getBaseColumns();
if (baseColumns.toLowerCase().contains("user.")) {
baseColumns = baseColumns.replace("user.", "\"user\".");
}
sql.SELECT(baseColumns);
sql.FROM("\"" + tableMetaData.getTableName() + "\"");
sql.WHERE(tableMetaData.getPkColumn() + " = " + id);
break;
}
default: {
log.error("Unsupported data source");
}
Expand All @@ -143,6 +191,17 @@ public static String selectByIds(
sql.WHERE(tableMetaData.getPkColumn() + " in ('" + idsStr + "')");
break;
}
case POSTGRESQL: {
String idsStr = ids.stream().map(String::valueOf).collect(Collectors.joining(","));
String baseColumns = tableMetaData.getBaseColumns();
if (baseColumns.toLowerCase().contains("user.")) {
baseColumns = baseColumns.replace("user.", "\"user\".");
}
sql.SELECT(baseColumns);
sql.FROM("\"" + tableMetaData.getTableName() + "\"");
sql.WHERE(tableMetaData.getPkColumn() + " in (" + idsStr + ")");
break;
}
default: {
log.error("Unsupported data source");
}
Expand All @@ -155,6 +214,14 @@ public static String selectAll(TableMetaData tableMetaData, String databaseId) {

SQL sql = new SQL();
switch (DBType.toType(databaseId)) {
case POSTGRESQL:
String baseColumns = tableMetaData.getBaseColumns();
if (baseColumns.toLowerCase().contains("user.")) {
baseColumns = baseColumns.replace("user.", "\"user\".");
}
sql.SELECT(baseColumns);
sql.FROM("\"" + tableMetaData.getTableName() + "\"");
break;
case MYSQL: {
sql.SELECT(tableMetaData.getBaseColumns());
sql.FROM(tableMetaData.getTableName());
Expand All @@ -176,6 +243,11 @@ public static String deleteById(TableMetaData tableMetaData, String databaseId,
sql.WHERE(tableMetaData.getPkColumn() + " = '" + id + "'");
break;
}
case POSTGRESQL: {
sql.FROM("\"" + tableMetaData.getTableName() + "\"");
sql.WHERE(tableMetaData.getPkColumn() + " = " + id);
break;
}
default: {
log.error("Unsupported data source");
}
Expand All @@ -194,6 +266,12 @@ public static String deleteByIds(
sql.WHERE(tableMetaData.getPkColumn() + " in ('" + idsStr + "')");
break;
}
case POSTGRESQL: {
String idsStr = ids.stream().map(String::valueOf).collect(Collectors.joining(", "));
sql.DELETE_FROM("\"" + tableMetaData.getTableName() + "\"");
sql.WHERE(tableMetaData.getPkColumn() + " in (" + idsStr + ")");
break;
}
default: {
log.error("Unsupported data source");
}
Expand All @@ -208,6 +286,8 @@ public static <Condition> String findByCondition(
log.info("databaseId: {}", databaseId);
SQL sql = new SQL();
switch (DBType.toType(databaseId)) {
case POSTGRESQL:
tableName = "\"" + tableName + "\"";
case MYSQL: {
sql = mysqlCondition(condition, tableName);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.apache.bigtop.manager.dao.repository.ChatMessageDao">

<select id="findAllByThreadId" resultType="org.apache.bigtop.manager.dao.po.ChatMessagePO">
SELECT *
FROM llm_chat_message
WHERE thread_id = #{threadId}
</select>

<delete id="deleteByThreadId">
DELETE FROM llm_chat_message
WHERE thread_id = #{threadId}
</delete>

</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.apache.bigtop.manager.dao.repository.ChatThreadDao">
<sql id="baseColumns">
id, user_id, platform_id, model
</sql>

<resultMap id="ChatThreadResultMap" type="org.apache.bigtop.manager.dao.po.ChatThreadPO">
<result property="threadInfo" column="thread_info" typeHandler="org.apache.bigtop.manager.dao.handler.JsonTypeHandler"/>
</resultMap>

<select id="findAllByUserId" resultType="org.apache.bigtop.manager.dao.po.ChatThreadPO">
SELECT *
FROM llm_chat_thread
WHERE user_id = #{userId}
</select>

<select id="findAllByPlatformAuthorizedIdAndUserId" resultType="org.apache.bigtop.manager.dao.po.ChatThreadPO">
SELECT *
FROM llm_chat_thread
WHERE platform_id = #{platformId} AND user_id = #{userId}
</select>

<select id="findByThreadId" resultMap="ChatThreadResultMap">
SELECT * FROM llm_chat_thread WHERE id = #{id}
</select>

<insert id="saveWithThreadInfo" parameterType="org.apache.bigtop.manager.dao.po.ChatThreadPO" useGeneratedKeys="true" keyProperty="id">
INSERT INTO llm_chat_thread (platform_id, user_id, model, thread_info)
VALUES (#{platformId}, #{userId}, #{model}, #{threadInfo, typeHandler=org.apache.bigtop.manager.dao.handler.JsonTypeHandler})
ON DUPLICATE KEY UPDATE
platform_id = VALUES(platform_id),
user_id = VALUES(user_id),
model = VALUES(model),
thread_info = VALUES(thread_info)
</insert>

</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.apache.bigtop.manager.dao.repository.ClusterDao">

<sql id="baseColumns">
id, cluster_name, cluster_type, root, user_group, packages, repo_template, state, selected, stack_id
</sql>
<sql id="baseColumnsV2">
${alias}.id, ${alias}.cluster_name, ${alias}.cluster_type, ${alias}.root, ${alias}.user_group, ${alias}.packages, ${alias}.repo_template, ${alias}.state, ${alias}.selected, ${alias}.stack_id
</sql>

<select id="findByClusterName" resultType="org.apache.bigtop.manager.dao.po.ClusterPO">
select
<include refid="baseColumns"/>
from
cluster
<where>
<if test="clusterName!=null">
cluster_name=#{clusterName}
</if>
</where>
limit 1
</select>

<select id="count" resultType="java.lang.Integer">
select count(*) from cluster
</select>

<select id="findByIdJoin"
resultType="org.apache.bigtop.manager.dao.po.ClusterPO">
select
<include refid="baseColumnsV2">
<property name="alias" value="r"/>
</include>
, s.stack_name, s.stack_version
from
cluster r
inner join stack s
on r.stack_id = s.id
<where>
<if test="clusterId!=0">
r.id=#{clusterId}
</if>
</where>
</select>

<select id="findAllByJoin"
resultType="org.apache.bigtop.manager.dao.po.ClusterPO">
select
<include refid="baseColumnsV2">
<property name="alias" value="r"/>
</include>
, s.stack_name, s.stack_version
from
cluster r
inner join stack s
on r.stack_id = s.id
</select>

</mapper>
Loading
Loading