diff --git a/lib/activeobjects-plugin-3.2.7.jar b/lib/activeobjects-plugin-3.2.7.jar new file mode 100644 index 0000000000000000000000000000000000000000..390285b5e14f139680b54bb7d4a7fbef0cc9cc76 Binary files /dev/null and b/lib/activeobjects-plugin-3.2.7.jar differ diff --git a/lib/kingbase8-8.6.0-c7.jar b/lib/kingbase8-8.6.0-c7.jar new file mode 100644 index 0000000000000000000000000000000000000000..955e208bd1e5df315d0c8395340f26098a3ec642 Binary files /dev/null and b/lib/kingbase8-8.6.0-c7.jar differ diff --git a/pom.xml b/pom.xml index 25771540e7aca1be6759cc9887e9afa767a903db..177d310e0057eb974e0080593f225f2fcd70e3b3 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,13 @@ 11 + + com.kingbase8 + kingbase8 + V008R006C007B0021 + system + ${pom.basedir}/lib/kingbase8-8.6.0-c7.jar + com.ibm.informix @@ -28,6 +35,7 @@ mysql mysql-connector-java 8.0.28 + @@ -114,6 +122,17 @@ + + org.mockito + mockito-core + + + + com.google.guava + guava + 32.1.2-jre + + @@ -122,7 +141,7 @@ org.springframework.boot spring-boot-maven-plugin - + true org.projectlombok diff --git a/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java b/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java index 2d560ddd6423855a0e8de87e66f129b2e6402289..6f9497da6c8f561dc19b27f9ecf767f608465902 100644 --- a/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java +++ b/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java @@ -11,7 +11,7 @@ public interface VccinfoMapper extends BaseMapper { @Results( @Result(column = "tblob",property = "tBlobStr") ) - @Select("SELECT * FROM cti_vccinfo_new where vccid = #{vId}") + @Select("SELECT vccid FROM cti_vccinfo_new where vccid = #{vId}") List queryList(@Param("vId") Integer vId); /** diff --git a/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java b/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java index b227d34ddf441364a1c885813b4fdd998099a425..127cf779b5bf0572fd66e4002d34a150135a1b6e 100644 --- a/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java +++ b/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java @@ -1,17 +1,38 @@ package com.beagle.informix.service.impl; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.beagle.informix.service.InformixService; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; -import org.springframework.core.io.Resource; -import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; + import java.io.*; import java.nio.charset.StandardCharsets; import java.sql.*; import java.util.Properties; + + + +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; +import java.util.ArrayList; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + /** * @author lzz * date 2023/5/29 @@ -22,12 +43,15 @@ import java.util.Properties; @Service("informixJdbcProxyService") public class InformixJdbcProxyServiceImpl implements InformixService { - private ResourceLoader resourceLoader; - public InformixJdbcProxyServiceImpl(ResourceLoader resourceLoader) { - this.resourceLoader = resourceLoader; - } + public static void main(String[] args) { + try { + new InformixJdbcProxyServiceImpl().doConnectJob(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } /** * informix.url=jdbc:informix-sqli://localhost:9088/sysadmin:INFORMIXSERVER=informix * informix.driverClassName=com.informix.jdbc.IfxDriver @@ -44,16 +68,45 @@ public class InformixJdbcProxyServiceImpl implements InformixService { // Unknown exception: Can not find JDBC type `2005` in column type Driver driver = getDriver(); Connection conn = getConnection(driver); -// insert(conn); -// update(conn); - query(conn); -// delete(conn); + conn.prepareStatement("SET character_set_results = NULL ").execute(); closeConnection(conn); deregisterDriver(driver); } + private ResultSet getImportedKeys(Connection connection, DatabaseMetaData metaData, String tableName) throws SQLException { + ResultSet schemas = connection.getMetaData().getSchemas(); + List schemasList = new ArrayList<>(); + while (schemas.next()){ + schemasList.add(schemas.getString("TABLE_SCHEM")); + } + return connection.getMetaData().getImportedKeys((String)null, "BIT7", tableName); + } + + + + private Object newForeignKey(ResultSet rs, String localTableName) throws SQLException { + String localFieldName = this.parseStringValue(rs, "FKCOLUMN_NAME"); + String foreignFieldName = this.parseStringValue(rs, "PKCOLUMN_NAME"); + String foreignTableName = this.parseStringValue(rs, "PKTABLE_NAME"); + return null; +// return new DatabaseMetaDataReaderImpl.ForeignKeyImpl(localTableName, localFieldName, foreignTableName, foreignFieldName); + } + + private String parseStringValue(ResultSet rs, String columnName) throws SQLException { + String value = rs.getString(columnName); + if (StringUtils.isBlank(value)) { + return value; + } else { + Matcher m = STRING_VALUE.matcher(value); + return m.find() ? m.group(1) : value; + } + } + + private static final Pattern STRING_VALUE = Pattern.compile("\"(.*)\""); + + private Driver getDriver() throws Exception{ // 定义informix的驱动信息 - String driverUrl = "org.postgresql.Driver"; + String driverUrl = "com.mysql.jdbc.Driver"; // 还需要加入informix的jdbc驱动jar包 Class driverClass = Class.forName(driverUrl); return (Driver) driverClass.getConstructor().newInstance(); @@ -62,48 +115,7 @@ public class InformixJdbcProxyServiceImpl implements InformixService { // 卸载驱动 DriverManager.deregisterDriver(driver); } - private void insert(Connection conn) throws Exception{ - // 创建testdb数据库、my_test_create_table表 - // 现在模拟插入、更新、删除、查询等sql - // 插入 - String insertSql = "insert into cti_vccinfo_new (vccid ,vccname, effective, agentmax,ivrmax,updatekey) values (?, ? , ?, ?, ? , ?)"; - PreparedStatement stat = conn.prepareStatement(insertSql); - stat.setObject(1, "123456"); - stat.setObject(2, "vname"); - stat.setObject(3, 1); - stat.setObject(4, 2); - stat.setObject(5, 3); - stat.setObject(6, "updatekey"); - String clobContent = "This is a very very long string"; -// // 处理clob字段 -// StringReader reader = new StringReader(clobContent); -// stat.setClob(7, reader, clobContent.length()); - // 处理blob字段 -// Resource resource = resourceLoader.getResource("classpath:EnableLoopback.exe"); -// File inputStream = resource.getFile(); -// FileInputStream fis = new FileInputStream(inputStream); -// stat.setBlob(8, fis); - stat.executeUpdate(); - } - private void update(Connection conn) throws Exception{ - // 创建testdb数据库、my_test_create_table表 - // 现在模拟插入、更新、删除、查询等sql - // 插入 - String updateSql = "update cti_vccinfo_new set vccname = 'informix_name' where vccid = '123456'"; - PreparedStatement stat = conn.prepareStatement(updateSql); - stat.executeUpdate(); - } - private void query(Connection conn) throws Exception { - String querySQL = "select vccid,vccname from cti_vccinfo_new where vccid = ? "; - PreparedStatement stat = conn.prepareStatement(querySQL); - // 设置查询参数,不拓展查询 - stat.setObject(1, "1", Types.OTHER); - ResultSet resultSet = stat.executeQuery(); - } - private void delete(Connection conn) throws Exception{ - String deleteSql = "delete from cti_vccinfo_new where vccid = '123456' "; - conn.createStatement().executeUpdate(deleteSql); - } + /** * 指定不用预编译语句 prepareThreshold=0 @@ -114,11 +126,9 @@ public class InformixJdbcProxyServiceImpl implements InformixService { private Connection getConnection(Driver driver) throws Exception{ DriverManager.registerDriver(driver); Properties properties = new Properties(); - // 组装连接数据库信息 + // 组装连接数据库信息 BITBUCKET_NEW // jdbc:informix-sqli://:/:informixserver= - StringBuilder jdbcBuilder = new StringBuilder("jdbc:postgresql://localhost:3307") - .append("/") - .append("SYSDBA").append("?prepareThreshold=0"); + StringBuilder jdbcBuilder = new StringBuilder("jdbc:mysql://localhost:3307/SYSDBA?character_set_results=utf8");// # ?useServerPrepStmts=true DriverManager.setLoginTimeout(10); properties.put("user", "root"); properties.put("password", "Apurelove9014"); diff --git a/src/main/java/com/beagle/informix/service/impl/InformixJdbcServiceImpl.java b/src/main/java/com/beagle/informix/service/impl/InformixJdbcServiceImpl.java index bc12ca4b8b524fbf3ca83fb5e827ca7511993ced..36c4d1e70058378e46bbbd404c202daffc4358f3 100644 --- a/src/main/java/com/beagle/informix/service/impl/InformixJdbcServiceImpl.java +++ b/src/main/java/com/beagle/informix/service/impl/InformixJdbcServiceImpl.java @@ -4,9 +4,12 @@ package com.beagle.informix.service.impl; import cn.hutool.core.io.FileUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.beagle.informix.service.InformixService; +import com.google.common.base.Function; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; @@ -14,7 +17,14 @@ import org.springframework.stereotype.Service; import java.io.*; import java.nio.charset.StandardCharsets; import java.sql.*; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.LinkedList; +import java.util.List; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author lzz @@ -46,13 +56,54 @@ public class InformixJdbcServiceImpl implements InformixService { public void doConnectJob() throws Exception{ Driver driver = getDriver(); Connection conn = getConnection(driver); - insert(conn); - update(conn); - query(conn); - delete(conn); - deregisterDriver(driver); +// insert(conn); +// update(conn); + // todo 使用jdbc连接proxy,对应的query(clob、blob字段)存在jdbc 类型转换mysql异常的问题 + // Unknown exception: Can not find JDBC type `2005` in column type +// insert(conn); +// update(conn); + ResultSet rs = this.getImportedKeys(conn, conn.getMetaData(), "AO_2AD648_INSIGHT_ANNOTATION"); + this.newForeignKey(rs, "AO_2AD648_INSIGHT_ANNOTATION"); + +// query(conn); +// delete(conn); +// deregisterDriver(driver); closeConnection(conn); } + private ResultSet getImportedKeys(Connection connection, DatabaseMetaData metaData, String tableName) throws SQLException { + return connection.getMetaData().getImportedKeys((String)null, connection.getSchema(), tableName); + } + + + + private Object newForeignKey(ResultSet rs, String localTableName) throws SQLException { + String localFieldName = this.parseStringValue(rs, "FKCOLUMN_NAME"); + String foreignFieldName = this.parseStringValue(rs, "PKCOLUMN_NAME"); + String foreignTableName = this.parseStringValue(rs, "PKTABLE_NAME"); + return null; +// return new DatabaseMetaDataReaderImpl.ForeignKeyImpl(localTableName, localFieldName, foreignTableName, foreignFieldName); + } + + private String parseStringValue(ResultSet rs, String columnName) throws SQLException { + String value = rs.getString(columnName); + if (StringUtils.isBlank(value)) { + return value; + } else { + Matcher m = STRING_VALUE.matcher(value); + return m.find() ? m.group(1) : value; + } + } + + private static final Pattern STRING_VALUE = Pattern.compile("\"(.*)\""); + + + public static void main(String[] args) { + try { + new InformixJdbcServiceImpl(null).doConnectJob(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } private void insert(Connection conn) throws Exception{ // 创建testdb数据库、my_test_create_table表 // 现在模拟插入、更新、删除、查询等sql @@ -85,25 +136,21 @@ public class InformixJdbcServiceImpl implements InformixService { stat.executeUpdate(); } private void query(Connection conn) throws Exception { - String querySQL = "select * from cti_vccinfo_new where vccid = '123456' "; + String querySQL = "select * from tabs "; PreparedStatement stat = conn.prepareStatement(querySQL); ResultSet resultSet = stat.executeQuery(); if (resultSet != null) { while (resultSet.next()) { - log.info("vccid =>" + resultSet.getString("vccid")); - Clob clob = resultSet.getClob("tclob"); - BufferedReader reader = new BufferedReader(clob.getCharacterStream()); - String clobStr = ""; - String finalClobStr = ""; - while ((clobStr = reader.readLine())!= null) { - finalClobStr += clobStr; + java.util.Date dateExecuted = null; + Object tmpDateExecuted = resultSet.getObject("cols"); + if (tmpDateExecuted instanceof Date) { + dateExecuted = (Date)tmpDateExecuted; + } else { + DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + dateExecuted = df.parse((String)tmpDateExecuted); + } catch (ParseException e) {} } - log.info("tclob =>" + finalClobStr); - Blob blob = resultSet.getBlob("tblob"); - BufferedInputStream bins= new BufferedInputStream(blob.getBinaryStream()); - byte [] bytes = bins.readAllBytes(); - String blobStr= new String(bytes, StandardCharsets.UTF_8); - log.info("tblob =>" + blobStr); } } } @@ -116,19 +163,20 @@ public class InformixJdbcServiceImpl implements InformixService { Properties properties = new Properties(); // 组装连接数据库信息 // jdbc:informix-sqli://:/:informixserver= - StringBuilder jdbcBuilder = new StringBuilder("jdbc:informix-sqli://") - .append("localhost:9088") +// StringBuilder jdbcBuilder = new StringBuilder("jdbc:mysql://") + StringBuilder jdbcBuilder = new StringBuilder("jdbc:kingbase8://") + .append("localhost:54321") .append("/") - .append("testdb") - .append(":INFORMIXSERVER=informix"); + .append("test"); DriverManager.setLoginTimeout(10); - properties.put("user", "informix"); - properties.put("password", "in4mix"); + properties.put("user", "root"); + properties.put("password", "123456"); return DriverManager.getConnection(jdbcBuilder.toString(), properties); } private Driver getDriver() throws Exception{ // 定义informix的驱动信息 - String driverUrl = "com.informix.jdbc.IfxDriver"; +// String driverUrl = "com.mysql.jdbc.Driver"; + String driverUrl = "com.kingbase8.Driver"; // 还需要加入informix的jdbc驱动jar包 Class driverClass = Class.forName(driverUrl); return (Driver) driverClass.getConstructor().newInstance(); diff --git a/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java b/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java index 085b5357db29e9ae0100b46d4bb19dd82a890d97..4dadce97cd9662d8f27efaf82e57d8297002651e 100644 --- a/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java +++ b/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java @@ -2,14 +2,11 @@ package com.beagle.informix.service.impl; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.beagle.informix.entity.mybatis.Vccinfo; import com.beagle.informix.mapper.VccinfoMapper; import com.beagle.informix.service.InformixService; import lombok.extern.slf4j.Slf4j; -import org.apache.ibatis.annotations.Result; -import org.apache.ibatis.annotations.Results; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; @@ -35,9 +32,9 @@ public class InformixMybatisServiceImpl extends ServiceImpl vccinfoList = vccinfoMapper.queryList(1); - log.info("queryAll:{}", JSONUtil.toJsonStr(vccinfoList)); } private void delete() { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index b540ae1e96ec7e243a23e6f5adf21107b4b4398a..da2ee8a91488e25459db38b6a8f61274c4519cac 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -3,12 +3,13 @@ spring: druid: # url: jdbc:informix-sqli://localhost:9088/testdb:INFORMIXSERVER=informix #&useServerPrepStmts=true -# url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai - url: jdbc:postgresql://localhost:3307/SYSDBA?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai -# url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true + url: jdbc:mysql://localhost:3307/SYSDBA?character_set_results=utf8 +# url: jdbc:mysql://192.168.1.55:3306/bit7 +# url: jdbc:mysql://localhost:3307/BITBUCKET7?useServerPrepStmts=true username: root # username: informix # password: in4mix +# password: 123456 password: Apurelove9014 # max-active: 20 # max-wait: 60000 @@ -22,7 +23,7 @@ spring: # test-while-idle: false # time-between-eviction-runs-millis: 60000 # driver-class-name: com.informix.jdbc.IfxDriver - driver-class-name: org.postgresql.Driver # com.mysql.cj.jdbc.Driver + driver-class-name: com.mysql.jdbc.Driver # com.mysql.cj.jdbc.Driver org.postgresql.Driver jpa: properties: hibernate: diff --git a/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java b/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java index 856ffcfc7135497324ef471c6736d911b02797d0..25beb84f66272446c0c2c1a29a907b13dc940b17 100644 --- a/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java +++ b/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java @@ -2,19 +2,24 @@ package com.beagle.informix.service.impl; import com.beagle.informix.BaseApplication; import com.beagle.informix.service.InformixService; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.ResourceLoader; +import org.springframework.stereotype.Service; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; +import java.sql.*; +import java.util.Properties; @RunWith(SpringRunner.class) @SpringBootTest(classes = BaseApplication.class) class InformixServiceTest { -// @Resource(name = "informixJdbcService") -// private InformixService informixJdbcService; + @Resource(name = "informixJdbcService") + private InformixService informixJdbcService; @Resource(name = "informixJdbcProxyService") private InformixService informixJdbcProxyService; @@ -24,14 +29,14 @@ class InformixServiceTest { // private InformixService informixIbatisService; @Resource(name = "informixMybatisService") private InformixService informixMybatisService; -// @Test -// void testJdbcInfo() { -// try { -// informixJdbcService.doConnectJob(); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } + @Test + void testJdbcInfo() { + try { + informixJdbcService.doConnectJob(); + } catch (Exception e) { + e.printStackTrace(); + } + } @Test void testJdbcProxyInfo() { try {