diff --git a/plugins/data-studio/src/main/java/com/nctigba/datastudio/controller/SqlCodeController.java b/plugins/data-studio/src/main/java/com/nctigba/datastudio/controller/SqlCodeController.java new file mode 100644 index 0000000000000000000000000000000000000000..fbf5845d5f956735638ba8c464351ce9649b258c --- /dev/null +++ b/plugins/data-studio/src/main/java/com/nctigba/datastudio/controller/SqlCodeController.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) GBA-NCTI-ISDC. 2024-2024. + * + * openGauss DataKit is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * ------------------------------------------------------------------------- + * + * SqlCodeController.java + * + * IDENTIFICATION + * plugins/data-studio/src/main/java/com/nctigba/datastudio/controller/SqlCodeController.java + * + * ------------------------------------------------------------------------- + */ + +package com.nctigba.datastudio.controller; + +import com.nctigba.datastudio.dao.SqlCodeDAO; +import com.nctigba.datastudio.model.dto.SqlCodeDTO; +import com.nctigba.datastudio.model.entity.SqlCodeDo; +import com.nctigba.datastudio.model.query.SqlCodeQuery; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 功能描述 + * + * @author liupengfei + * @since 2024/11/25 + */ +@RestController +@RequestMapping(value = "/dataStudio/web/v1/sqlCode") +public class SqlCodeController { + @Autowired + private SqlCodeDAO sqlCodeDAO; + + /** + * createSqlCode + * + * @param sqlCodeDo SqlCodeDo + */ + @PostMapping("/create") + public void createSqlCode(@RequestBody SqlCodeDo sqlCodeDo) { + sqlCodeDAO.insert(sqlCodeDo); + } + + /** + * list + * + * @param query SqlCodeQuery + * @return SqlCodeDTO + */ + @GetMapping("/list") + public SqlCodeDTO list(SqlCodeQuery query) { + return sqlCodeDAO.list(query); + } + + /** + * list + * + * @param id Integer + * @return SqlCodeDo + */ + @GetMapping("/get") + public SqlCodeDo list(Integer id) { + return sqlCodeDAO.listById(id); + } + + /** + * update + * + * @param sqlCodeDo SqlCodeDo + */ + @PostMapping("/update") + public void update(@RequestBody SqlCodeDo sqlCodeDo) { + sqlCodeDAO.update(sqlCodeDo); + } + + /** + * delete + * + * @param id Integer + */ + @DeleteMapping("/delete") + public void delete(Integer id) { + sqlCodeDAO.delete(id); + } +} diff --git a/plugins/data-studio/src/main/java/com/nctigba/datastudio/dao/SqlCodeDAO.java b/plugins/data-studio/src/main/java/com/nctigba/datastudio/dao/SqlCodeDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..63358e39650e942e7bf4cb5966060f36831e6cb8 --- /dev/null +++ b/plugins/data-studio/src/main/java/com/nctigba/datastudio/dao/SqlCodeDAO.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) GBA-NCTI-ISDC. 2024-2024. + * + * openGauss DataKit is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * ------------------------------------------------------------------------- + * + * SqlCodeDAO.java + * + * IDENTIFICATION + * plugins/data-studio/src/main/java/com/nctigba/datastudio/dao/SqlCodeDAO.java + * + * ------------------------------------------------------------------------- + */ + +package com.nctigba.datastudio.dao; + +import com.nctigba.datastudio.model.dto.SqlCodeDTO; +import com.nctigba.datastudio.model.entity.SqlCodeDo; +import com.nctigba.datastudio.model.query.SqlCodeQuery; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; + +import static java.lang.Math.ceil; + +/** + * SqlCodeDAO + * + * @author liupengfei + * @since 2024/11/25 + */ +@Slf4j +@Repository +public class SqlCodeDAO implements ApplicationRunner { + @Autowired + private JdbcTemplate jdbcTemplate; + + @Override + public void run(ApplicationArguments args) throws Exception { + jdbcTemplate.execute( + "create table if not exists sqlCode (" + + " id integer primary key AUTOINCREMENT," + + " name varchar(128) not null," + + " code clob not null," + + " description varchar(512)" + + ");"); + } + + /** + * insert + * + * @param sqlCodeDo SqlCodeDo + */ + public void insert(SqlCodeDo sqlCodeDo) { + String sql = "insert into sqlCode(name,code,description) values('" + sqlCodeDo.getName() + + "','" + sqlCodeDo.getCode() + "','" + sqlCodeDo.getDescription() + "');"; + jdbcTemplate.execute(sql); + } + + /** + * list + * + * @param query SqlCodeQuery + * @return SqlCodeDTO + */ + public SqlCodeDTO list(SqlCodeQuery query) { + String sql = "select * from sqlCode"; + sql = sql + where(query) + " limit " + query.getPageSize() + " offset " + + ((query.getPageNum() - 1) * query.getPageSize()) + ";"; + List sqlCodeDoList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SqlCodeDo.class)); + String countSql = "select count(*) from sqlCode"; + Long total = jdbcTemplate.queryForObject(countSql + where(query), Long.class); + SqlCodeDTO dto = new SqlCodeDTO(); + dto.setData(sqlCodeDoList); + dto.setPageSize(query.getPageSize()); + dto.setPageNum(query.getPageNum()); + dto.setPageTotal((int) ceil((double) total / query.getPageSize())); + dto.setDataSize(total); + return dto; + } + + /** + * listById + * + * @param id Integer + * @return SqlCodeDo + */ + public SqlCodeDo listById(Integer id) { + String sql = "select * from sqlCode where id = " + id; + return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SqlCodeDo.class)); + } + + private static String where(SqlCodeQuery query) { + String where; + if (StringUtils.isNotEmpty(query.getName()) && StringUtils.isEmpty(query.getCode())) { + where = " where name like '%" + query.getName() + "%'"; + } else if (StringUtils.isEmpty(query.getName()) && StringUtils.isNotEmpty(query.getCode())) { + where = " where code like '%" + query.getCode() + "%'"; + } else if (StringUtils.isNotEmpty(query.getName()) && StringUtils.isNotEmpty(query.getCode())) { + where = " where name like '%" + query.getName() + "%' and code like '%" + query.getCode() + "%'"; + } else if (query.getId() != null) { + where = " where id = " + query.getId(); + } else { + where = ""; + } + return where; + } + + /** + * update + * + * @param sqlCodeDo SqlCodeDo + */ + public void update(SqlCodeDo sqlCodeDo) { + String sql = "update sqlCode set name = ?, code = ?, description = ? where id = ?"; + ArrayList objects = new ArrayList<>(); + objects.add(sqlCodeDo.getName()); + objects.add(sqlCodeDo.getCode()); + objects.add(sqlCodeDo.getDescription()); + objects.add(sqlCodeDo.getId()); + jdbcTemplate.update(sql, objects.toArray()); + } + + /** + * delete + * + * @param id Integer + */ + public void delete(Integer id) { + String sql = "delete from sqlCode where id = ?"; + jdbcTemplate.update(sql, id); + } +} diff --git a/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/dto/SqlCodeDTO.java b/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/dto/SqlCodeDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..27318ee371e6fe286c2b3174ff54424780110d53 --- /dev/null +++ b/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/dto/SqlCodeDTO.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) GBA-NCTI-ISDC. 2024-2024. + * + * openGauss DataKit is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * ------------------------------------------------------------------------- + * + * SqlCodeDTO.java + * + * IDENTIFICATION + * plugins/data-studio/src/main/java/com/nctigba/datastudio/model/dto/SqlCodeDTO.java + * + * ------------------------------------------------------------------------- + */ + +package com.nctigba.datastudio.model.dto; + +import com.nctigba.datastudio.model.entity.SqlCodeDo; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * SqlCodeDTO + * + * @author liupengfei + * @since 2024/11/25 + */ +@Data +@NoArgsConstructor +public class SqlCodeDTO { + private List data; + private Integer pageTotal; + private Long dataSize; + private Integer pageNum; + private Integer pageSize; +} diff --git a/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/entity/SqlCodeDo.java b/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/entity/SqlCodeDo.java new file mode 100644 index 0000000000000000000000000000000000000000..e9e2fc774dcd997b7930be830811df260454d280 --- /dev/null +++ b/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/entity/SqlCodeDo.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) GBA-NCTI-ISDC. 2024-2024. + * + * openGauss DataKit is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * ------------------------------------------------------------------------- + * + * SqlCodeDo.java + * + * IDENTIFICATION + * plugins/data-studio/src/main/java/com/nctigba/datastudio/model/entity/SqlCodeDo.java + * + * ------------------------------------------------------------------------- + */ + +package com.nctigba.datastudio.model.entity; + +import lombok.Data; +import lombok.Generated; +import lombok.NoArgsConstructor; + +/** + * SqlCodeDo + * + * @author liupengfei + * @since 2024/11/25 + */ +@NoArgsConstructor +@Data +@Generated +public class SqlCodeDo { + private Integer id; + private String name; + private String code; + private String description; +} diff --git a/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/query/SqlCodeQuery.java b/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/query/SqlCodeQuery.java new file mode 100644 index 0000000000000000000000000000000000000000..d92120f9c4c8d48714a383fc8570c72583bae431 --- /dev/null +++ b/plugins/data-studio/src/main/java/com/nctigba/datastudio/model/query/SqlCodeQuery.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) GBA-NCTI-ISDC. 2024-2024. + * + * openGauss DataKit is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * ------------------------------------------------------------------------- + * + * SqlCodeQuery.java + * + * IDENTIFICATION + * plugins/data-studio/src/main/java/com/nctigba/datastudio/model/query/SqlCodeQuery.java + * + * ------------------------------------------------------------------------- + */ + +package com.nctigba.datastudio.model.query; + +import lombok.Data; +import lombok.Generated; + +/** + * SqlCodeQuery + * + * @author liupengfei + * @since 2024/11/25 + */ +@Data +@Generated +public class SqlCodeQuery { + private Integer id; + private String name; + private String code; + private Integer pageNum; + private Integer pageSize; +}