类 DataConnection

java.lang.Object
com.gdxsoft.easyweb.datasource.DataConnection

public class DataConnection extends Object
  • 构造器详细资料

    • DataConnection

      public DataConnection()
    • DataConnection

      public DataConnection(RequestValue rv)
    • DataConnection

      public DataConnection(String configName, RequestValue rv)
  • 方法详细资料

    • getTimeDiffMinutes

      public int getTimeDiffMinutes()
      用户和系统的时差(分钟)
      返回:
    • setTimeDiffMinutes

      public void setTimeDiffMinutes(int timeDiffMinutes)
      用户和系统的时差(分钟)
      参数:
      timeDiffMinutes - 分钟
    • updateAndClose

      public static String updateAndClose(String sql, String configName, RequestValue rv)
      执行更新并关闭连接
      参数:
      sql -
      configName -
      rv -
      返回:
      返回是否错误
    • insertAndReturnAutoIdLong

      public static long insertAndReturnAutoIdLong(String sql, String configName, RequestValue rv)
      新建记录并返回自增字段,数据库连接自动关闭
      参数:
      sql -
      configName -
      rv -
      返回:
    • insertAndReturnAutoIdInt

      public static int insertAndReturnAutoIdInt(String sql, String configName, RequestValue rv)
      新建记录并返回自增字段,数据库连接自动关闭
      参数:
      sql -
      configName -
      rv -
      返回:
    • updateAndClose

      public static String updateAndClose(StringBuilder sb, String configName, RequestValue rv)
    • queryCount

      public static int queryCount(String tableName, String where, String configName, RequestValue rv)
      获取数据量
      参数:
      tableName - 表名
      where - 查询条件
      configName -
      rv -
      返回:
      =0 无数据,>0有数据,-1 执行错误
    • queryExists

      public static boolean queryExists(String tableName, String where, String configName, RequestValue rv)
      检查数据是否存在
      参数:
      tableName - 表名
      where - 查询条件
      configName -
      rv -
      返回:
      有数据true,无数据和执行错误false
    • updateBatchAndClose

      public static String updateBatchAndClose(String sqls, String configName, RequestValue rv)
      批量执行更新并关闭连接
      参数:
      sqls - 用;分割的sql字符串
      configName -
      rv -
      返回:
      返回是否错误
    • updateBatchAndClose

      public static String updateBatchAndClose(StringBuilder sb, String configName, RequestValue rv)
      批量执行更新并关闭连接
      参数:
      sb - 用;分割的sql字符串
      configName -
      rv -
      返回:
    • updateBatchAndClose

      public static String updateBatchAndClose(List<String> sqls, String configName, RequestValue rv)
      批量执行更新并关闭连接
      参数:
      sqls - sql列表
      configName -
      rv -
      返回:
      返回是否错误
    • updateBatchAndCloseTransaction

      public static String updateBatchAndCloseTransaction(List<String> sqls, String configName, RequestValue rv)
      批量执行更新并关闭连接 (Transaction)
      参数:
      sqls - sql列表
      configName -
      rv -
      返回:
      返回是否错误
    • runMultiSqlsAndClose

      public static List<DTTable> runMultiSqlsAndClose(String sqls, String configName, RequestValue rv) throws Exception
      执行混合SQL语句(查询,更新(包含返回自增),存储过程)
      参数:
      sqls -
      configName -
      rv -
      返回:
      返回的所有表
      抛出:
      Exception
    • runMultiSqlsAndClose

      public static List<DTTable> runMultiSqlsAndClose(List<String> listSqls, String configName, RequestValue rv) throws Exception
      执行混合SQL语句(查询,更新(包含返回自增),存储过程)
      参数:
      listSqls -
      configName -
      rv -
      返回:
      返回的所有表
      抛出:
      Exception
    • executeQueryAndReturnTables

      public static List<DTTable> executeQueryAndReturnTables(String sql, String configName, RequestValue rv) throws SQLException
      执行SQL查询返回多条结果集
      参数:
      sql -
      configName -
      rv -
      返回:
      结果集
      抛出:
      SQLException
    • getAutoField

      public static String getAutoField(String sql)
      查找自增的sql的返回字段, 例如 -- auto MEMO_ID
      参数:
      sql -
      返回:
    • checkIsProcdure

      public static boolean checkIsProcdure(String sql)
      检查是否为 存储过程,例如 CALL pr_batAdd(@a)
      参数:
      sql -
      返回:
    • checkIsSelect

      public static boolean checkIsSelect(String sql)
      检查是否为 select语句
      或标记为-- EWA_IS_SELECT
      或with block语句
      参数:
      sql -
      返回:
    • checkStartWord

      public static boolean checkStartWord(String sql, String word)
      判断 特定字符出现在非注释的SQL的0位置,多行的SQL只进行第一次判断
      参数:
      sql -
      word - 关键单词,例如SELECT
      返回:
    • removeSqlMuitiComment

      public static String removeSqlMuitiComment(String sql)
      删除sql 的多行备注
      参数:
      sql -
      返回:
    • isComparativeChanges

      public static boolean isComparativeChanges(String sql)
      是否比较更新前和更新后字段的变化, 方式:
      SQL 添加 -- COMPARATIVE_CHANGES
      参数:
      sql -
      返回:
    • getSqls

      public static List<String> getSqls(String sqls)
      将sql混合语句,通过“;”符号,分解为sql列表
      参数:
      sqls -
      返回:
      sql列表
    • getDataBaseName

      public String getDataBaseName()
      数据库名称
      返回:
    • setDataBaseName

      public void setDataBaseName(String dataBaseName)
      数据库名称
      参数:
      dataBaseName -
    • transBegin

      public boolean transBegin()
      开始事务处理
      返回:
    • transCommit

      public boolean transCommit()
      提交事务
      抛出:
      SQLException
    • transRollback

      public void transRollback()
      事务回滚
      抛出:
      SQLException
    • transClose

      public void transClose()
      关闭事务连接
    • getCurrentConfig

      public ConnectionConfig getCurrentConfig()
    • setConfigName

      public void setConfigName(String configName)
    • setConfigName

      public void setConfigName(String configName, String dataBaseName)
      设置数据库连接池句柄
      参数:
      configName -
      dataBaseName -
    • connect

      public boolean connect()
    • executeQueryNoParameter

      public boolean executeQueryNoParameter(String sql)
    • createSqlByEwaBlockTest

      public String createSqlByEwaBlockTest(String orignalSql)
      根据逻辑判断组合SQL, 判断条件是 "-- ewa_block_test"
      参数:
      orignalSql -
      返回:
    • createSqlByEwaTest

      public String createSqlByEwaTest(String orignalSql)
      根据逻辑判断组合SQL, 判断条件是 "-- ewa_test"
      SELECT A.*, B.GRP
         FROM TABLE1 A
      INNER JOIN TABLE2 B ON A.ID = B.ID
      WHERE A.STATUS = 'USED'
      -- ewa_test @tag is not null
         and A.tag = @tag
      -- ewa_test @abc is not null
         and A.tag = 'TAG_CAR'
         and B.GRP = 'CAR0'
      -- ewa_test
      order by A.ID, B.GRP desc
      参数:
      orignalSql -
      返回:
    • replaceSqlCommentAtSymbol

      public String replaceSqlCommentAtSymbol(String orignalSql)
      替换SQL注释行中的@符号,防止被当作参数处理
      参数:
      orignalSql -
      返回:
    • getMoreResults

      public List<DataResult> getMoreResults() throws SQLException
      从执行完的sql中获取更多的结果集
      返回:
      抛出:
      SQLException
    • executeQueryCount

      public int executeQueryCount(String tableName, String where)
      获取数据量
      参数:
      tableName - 表名
      where - 查询条件
      返回:
      =0 无数据,>0有数据,-1 执行错误
    • executeQueryExists

      public boolean executeQueryExists(String tableName, String where)
      检查数据是否存在
      参数:
      tableName - 表名
      where - 查询条件
      返回:
      有数据true,无数据和执行错误false
    • executeQuery

      public boolean executeQuery(String sql)
      执行查询
      参数:
      sql -
      requestValue -
      返回:
    • executeQueryPage

      public boolean executeQueryPage(String sql, String pkFieldName, int currentPage, int pageSize)
      执行分页查询
      参数:
      sql -
      pkFieldName - 主键名称
      currentPage - 当前页码
      pageSize - 每页记录数
      返回:
    • closeStatment

      public String closeStatment(Statement stmt)
    • batchUpdate

      public void batchUpdate(List<String> sqls, boolean transcation)
      批处理导入数据
      参数:
      sqls -
      transcation -
    • executeMultipleUpdate

      public boolean executeMultipleUpdate(String sql)
      执行多个更新 sql语句,语句用“;”分割,不能在拼接的Sql中有“;”出现
      参数:
      sql -
      返回:
    • executeUpdateBatch

      public int executeUpdateBatch(List<String> sqls)
      批量执行sql语句,混合update和select的语句集合
      参数:
      sqls -
      返回:
    • executeUpdateBatch

      public int executeUpdateBatch(String sqls)
      批处理执行sql语句,sql语句用 ; 分割,getUpdateBatchTables获取执行国产中的所有表
      参数:
      sqls - sql语句用 ; 分割
      返回:
      -1 有错误
    • convertToList

      public List<String> convertToList(String[] sqlArray)
    • runMultiSqls

      public List<DTTable> runMultiSqls(String[] sqlArray) throws Exception
      执行混合SQL语句(查询,更新(包含返回自增),存储过程)
      1. 如果是select返回数据的话,回将第一行数据放到 rv中
      2. 自动创建变量 --auto xxx 会放到rv中
      3. 存储过程返回的结果,带 _OUT参数,例如 ID_OUT 会放到rv中
      参数:
      sqlArray -
      返回:
      select 返回 DTTable集合
      抛出:
      Exception
    • executeAutoFieldReturnName

      public String executeAutoFieldReturnName(String sql)
      执行带自动返回值的SQL,返回执行后的自动字段的名称
      如果成功,则在_RequestValue里保存
      如果失败,则清除_RequestValue里的Form,Query,Cookie的值,避免外部攻击
      参数:
      sql - 自增的sql, 例如 -- auto MEMO_ID
      返回:
      返回值的字段名称,保存在_RequestValue里, null无自动字段
    • runMultiSqls

      public List<DTTable> runMultiSqls(List<String> listSqls) throws Exception
      执行混合SQL语句(查询,更新(包含返回自增),存储过程)
      1. 如果是select返回数据的话,回将第一行数据放到 rv中
      2. 自动创建变量 --auto xxx 会放到rv中
      3. 存储过程返回的结果,带 _OUT参数,例如 ID_OUT 会放到rv中
      参数:
      listSqls -
      返回:
      抛出:
      Exception
    • executeUpdateReturnAutoIncrementObject

      public Object executeUpdateReturnAutoIncrementObject(String sql)
      执行查询并返回 自动增加值(返回为对象,需要自己判断是int还是long)
      参数:
      sql -
      返回:
    • executeUpdateReturnAutoIncrement

      public int executeUpdateReturnAutoIncrement(String sql)
      执行自增的插入
      参数:
      sql -
      返回:
    • executeUpdate

      public boolean executeUpdate(String sql)
      执行更新
      参数:
      sql -
    • useDatabase

      public boolean useDatabase() throws Exception
      sqlserver, mysql 更换数据库
      参数:
      sql -
      返回:
      抛出:
      Exception
      SQLException
    • executeUpdateAndReturnChanges

      public UpdateChanges executeUpdateAndReturnChanges(String sql)
      执行更新并返回数据的前后差异
      参数:
      sql -
      返回:
    • executeUpdateNoParameter

      public boolean executeUpdateNoParameter(String sql)
    • getRecordCount

      public int getRecordCount(String sql)
      重新组合SQL,获取记录数,创建出SELECT COUNT(*) GDX FROM ...
      参数:
      sql -
      返回:
    • skipReplaceParameter

      public boolean skipReplaceParameter(String paramName)
      特殊的不需要替换为参数的参数名称,例如rownum
      select user_id, @rownum := @rownum+1 from users b
      ,(select @rownum :=0) c
      参数:
      paramName -
      返回:
    • rebuildSql

      public String rebuildSql(String sql) throws Exception
      1. 合成SQL语句,如果参数名为XX_SPLIT,为分割参数,
      例如 select * from users where id in(1,2,3)
      2. SQL语句预替换,即在SQL语句执行前,替换SQL语句本身的参数
      例如 SELECT * FROM ~TB, 如参数TB是 USERS 则替换成 SELECT * FROM USERS
      参数:
      sql -
      requestValue -
      返回:
      抛出:
      Exception
    • replaceSqlParameters

      public String replaceSqlParameters(String sql)
      替换SQL中的参数名为“?”,不替换rownum开头的参数,mysql使用
      参数:
      sql - sql表达式
      返回:
      替换后的sql
    • replaceSqlSelectParameters

      public String replaceSqlSelectParameters(String sql)
      替换select 查询中的参数名为具体值,原因是sqlserver在查询中出现参数表达式,会进行全表扫描
      郭磊 2016-11-02
      参数:
      sql - sql表达式
      返回:
      替换后的sql
    • replaceSqlSelectParameters

      public String replaceSqlSelectParameters(String sql, String databaseType)
      替换select 查询中的参数名为具体值,原因是sqlserver在查询中出现参数表达式,会进行全表扫描
      郭磊 2016-11-02
      参数:
      sql - sql表达式
      返回:
      替换后的sql
    • getReplaceParameterValueExp

      public String getReplaceParameterValueExp(String paramName)
      获取参数值表达式,用于select替换
      参数:
      paramName - 参数名称
      返回:
    • getReplaceParameterValueExp

      public String getReplaceParameterValueExp(String paramName, String databaseType)
      获取参数值表达式,用于select替换
      参数:
      paramName - 参数名称
      databaseType - 数据库类型
      返回:
    • addSqlParameter

      public void addSqlParameter(com.gdxsoft.easyweb.utils.msnet.MListStr parameters, PreparedStatement pst) throws SQLException
      设置SQL参数
      参数:
      parameters -
      pst -
      抛出:
      SQLException
    • getParameterByEndWithType

      public PageValue getParameterByEndWithType(String parameterName)
    • addStatementParameter

      public void addStatementParameter(PreparedStatement cst, String parameterName, int index) throws SQLException
      抛出:
      SQLException
    • clearResultSets

      public void clearResultSets()
      清除所有返回的 resultSet,当长期执行查询的时,会造成内存占用过高 关闭连接会自动执行清除
    • close

      public void close()
      关闭所有连接,包括ResultSet,Cst,Pst
    • getErrorMsg

      public String getErrorMsg()
      Return the error with SQL
      返回:
    • setErrorMsg

      public void setErrorMsg(String errorMessage)
      Manual set conn error message
      参数:
      errorMessage -
    • clearErrorMsg

      public void clearErrorMsg()
      Clear the error
    • executeProcdure

      public HashMap<String,Object> executeProcdure(String callProcduceSql)
      执行存储过程
      参数:
      callProcduceSql - 存储过程调用方法: call pr_some(@name, @id_int_output)
      结尾为_output,表示是输出参数
      _int_output,表示输出参数为整形
      _smallint_output,表示输出参数为小整形
      _tinyint_output,表示输出参数为短整形
      _number_output,_money_output,_numeric_output,_decimal_output表示输出参数为bcd数值
      _long_output/_bigint_output,表示输出参数为长整形
      _text_output/_clob_output,表示输出参数为长文本
      _image_output/_blob_output,表示输出参数为长二进制
      _binary_output/_byte_output,表示输出参数为二进制(varbinary)
      _date_output/_datetime_output/_timestamp_output,表示输出参数为日期时间
      _time_output,表示输出参数为时间
      默认,为varchar字符串
      返回:
      输出参数Map,key转换为大写
    • executeProcdureReturnResults

      public HashMap<String,Object> executeProcdureReturnResults(String callProcduceSql)
      执行存储过程
      参数:
      callProcduceSql - 存储过程调用方法: call pr_some(@name, @id_int_output)
      结尾为_output,表示是输出参数
      _int_output,表示输出参数为整形
      _smallint_output,表示输出参数为小整形
      _tinyint_output,表示输出参数为短整形
      _number_output,_money_output,_numeric_output,_decimal_output表示输出参数为bcd数值
      _long_output/_bigint_output,表示输出参数为长整形
      _text_output/_clob_output,表示输出参数为长文本
      _image_output/_blob_output,表示输出参数为长二进制
      _binary_output/_byte_output,表示输出参数为二进制(varbinary)
      _date_output/_datetime_output/_timestamp_output,表示输出参数为日期时间
      _time_output,表示输出参数为时间
      默认,为varchar字符串
      返回:
      输出参数Map,key转换为大写,
      RS_SIZE 表示返回的结果集数量
      返回的结果集转换为DTTable,名字以RS开头,RS0,RS1,RS2 ...
    • executeProcdureNoParameter

      public void executeProcdureNoParameter(String sql)
      执行存储过程, 不带参数
      参数:
      sql -
      requestValue -
    • sqlParameterStringExp

      public String sqlParameterStringExp(String parameter)
      SQL字符串参数值表达式,替换'号和\转译符号(mysql)
      参数:
      parameter - 字符串参数值
      返回:
    • sqlParameterStringExp

      public String sqlParameterStringExp(String parameter, String databaseType)
      SQL字符串参数值表达式,替换'号和\转译符号(mysql)
      参数:
      parameter - 字符串参数值
      返回:
    • sqlFieldOrTableExp

      public String sqlFieldOrTableExp(String fieldOrTable)
      处理字段或表名的字符串表达式,带上[(sqlserver)或`(mysql)
      参数:
      fieldOrTable - 字段或表名
      返回:
      带上[(sqlserver)或`(mysql)的字段或表名
    • getConnectionString

      public String getConnectionString()
    • getDatabaseType

      public String getDatabaseType()
    • getDateTimePara

      public String getDateTimePara(String s1)
      获取ListFrame查询的日期表达式
      参数:
      s1 -
      返回:
    • getDateTimePara

      public String getDateTimePara(Timestamp dt)
      获取ListFrame查询的日期表达式,ISO 8601 格式(YYYY-MM-DD HH:MI:SS),
      oracle 使用to_date('YYYY-MM-DD HH24:MI:SS')
      参数:
      dt -
      返回:
    • getTimestamp

      public Timestamp getTimestamp(String s1)
      获取sql的timestamp数据
      参数:
      s1 -
      返回:
    • getSchemaName

      public String getSchemaName()
    • getConnection

      public Connection getConnection()
    • setConnection

      public void setConnection(Connection cnn)
    • setCurrentConfig

      public void setCurrentConfig(ConnectionConfig currentConfig)
      参数:
      currentConfig - the _CurrentConfig to set
    • getDebugFrames

      public DebugFrames getDebugFrames()
      返回:
      the _DebugFrames
    • setDebugFrames

      public void setDebugFrames(DebugFrames debugFrames)
      参数:
      debugFrames - the _DebugFrames to set
    • getRequestValue

      public RequestValue getRequestValue()
      返回:
      the _RequestValue
    • setRequestValue

      public void setRequestValue(RequestValue requestValue)
      参数:
      requestValue - the _RequestValue to set
    • getResultSetList

      public com.gdxsoft.easyweb.utils.msnet.MList getResultSetList()
      返回:
      the _ResultSetList
    • getLastResult

      public DataResult getLastResult()
    • isTrans

      public boolean isTrans()
      是否使用事物处理
      返回:
      the _IsTrans
    • getErrorMsgOnly

      public String getErrorMsgOnly()
      Return the error without sql
      返回:
    • getDataHelper

      public DataHelper getDataHelper()
      返回:
      the DataHelper
    • setDataHelper

      public void setDataHelper(DataHelper dataHelper)
      参数:
      dataHelper - the DataHelper to set
    • getUpdateBatchTables

      public List<DTTable> getUpdateBatchTables()
      executeUpdateBatch 批处理更新后产生的所有表
      返回:
      the updateBatchTables