Class DbToolBase

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    DbPorter

    public class DbToolBase
    extends java.lang.Object
    implements java.io.Closeable
    Database tool base.
    Since:
    2.0.0
    Author:
    Lijun Liao (xipki)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.String baseDir  
      protected java.sql.Connection connection  
      protected org.xipki.datasource.DataSourceWrapper datasource  
      protected java.util.concurrent.atomic.AtomicBoolean stopMe  
    • Constructor Summary

      Constructors 
      Constructor Description
      DbToolBase​(org.xipki.datasource.DataSourceWrapper datasource, java.lang.String baseDir, java.util.concurrent.atomic.AtomicBoolean stopMe)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String buildFilename​(java.lang.String prefix, java.lang.String suffix, long minIdOfCurrentFile, long maxIdOfCurrentFile, long maxId)  
      void close()  
      protected void commit​(java.lang.String task)  
      int count​(java.lang.String table)  
      boolean deleteFromTableWithLargerId​(java.lang.String table, java.lang.String idColumn, long id, org.slf4j.Logger log)  
      static void deleteTmpFiles​(java.lang.String dirName, java.lang.String prefix)  
      protected void disableAutoCommit()  
      static org.xipki.util.ConfigurableProperties getDbConfProperties​(java.nio.file.Path path)  
      static java.util.zip.ZipOutputStream getZipOutputStream​(java.io.File zipFile)  
      long max​(java.lang.String table, java.lang.String column)  
      long max​(java.lang.String table, java.lang.String column, java.lang.String condition)  
      long min​(java.lang.String table, java.lang.String column)  
      long min​(java.lang.String table, java.lang.String column, java.lang.String condition)  
      protected java.sql.PreparedStatement prepareStatement​(java.lang.String sql)  
      protected void recoverAutoCommit()  
      void releaseResources​(java.sql.PreparedStatement ps, java.sql.ResultSet rs)  
      protected void rollback()  
      protected static void setBoolean​(java.sql.PreparedStatement ps, int index, boolean value)  
      protected static void setInt​(java.sql.PreparedStatement ps, int index, java.lang.Integer value)  
      protected static void setLong​(java.sql.PreparedStatement ps, int index, java.lang.Long value)  
      protected java.sql.Savepoint setSavepoint()  
      boolean tableExists​(java.lang.String table)  
      boolean tableHasColumn​(java.lang.String table, java.lang.String column)  
      protected org.xipki.datasource.DataAccessException translate​(java.lang.String sql, java.sql.SQLException ex)  
      protected static void writeLine​(java.io.OutputStream os, java.lang.String text)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • stopMe

        protected final java.util.concurrent.atomic.AtomicBoolean stopMe
      • datasource

        protected final org.xipki.datasource.DataSourceWrapper datasource
      • baseDir

        protected final java.lang.String baseDir
      • connection

        protected java.sql.Connection connection
    • Constructor Detail

      • DbToolBase

        public DbToolBase​(org.xipki.datasource.DataSourceWrapper datasource,
                          java.lang.String baseDir,
                          java.util.concurrent.atomic.AtomicBoolean stopMe)
                   throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
    • Method Detail

      • prepareStatement

        protected java.sql.PreparedStatement prepareStatement​(java.lang.String sql)
                                                       throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • deleteFromTableWithLargerId

        public boolean deleteFromTableWithLargerId​(java.lang.String table,
                                                   java.lang.String idColumn,
                                                   long id,
                                                   org.slf4j.Logger log)
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • min

        public long min​(java.lang.String table,
                        java.lang.String column)
                 throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • min

        public long min​(java.lang.String table,
                        java.lang.String column,
                        java.lang.String condition)
                 throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • max

        public long max​(java.lang.String table,
                        java.lang.String column)
                 throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • max

        public long max​(java.lang.String table,
                        java.lang.String column,
                        java.lang.String condition)
                 throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • count

        public int count​(java.lang.String table)
                  throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • tableHasColumn

        public boolean tableHasColumn​(java.lang.String table,
                                      java.lang.String column)
                               throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • tableExists

        public boolean tableExists​(java.lang.String table)
                            throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • setSavepoint

        protected java.sql.Savepoint setSavepoint()
                                           throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • rollback

        protected void rollback()
                         throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • translate

        protected org.xipki.datasource.DataAccessException translate​(java.lang.String sql,
                                                                     java.sql.SQLException ex)
      • disableAutoCommit

        protected void disableAutoCommit()
                                  throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • recoverAutoCommit

        protected void recoverAutoCommit()
      • commit

        protected void commit​(java.lang.String task)
                       throws org.xipki.datasource.DataAccessException
        Throws:
        org.xipki.datasource.DataAccessException
      • setLong

        protected static void setLong​(java.sql.PreparedStatement ps,
                                      int index,
                                      java.lang.Long value)
                               throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • setInt

        protected static void setInt​(java.sql.PreparedStatement ps,
                                     int index,
                                     java.lang.Integer value)
                              throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • setBoolean

        protected static void setBoolean​(java.sql.PreparedStatement ps,
                                         int index,
                                         boolean value)
                                  throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getDbConfProperties

        public static org.xipki.util.ConfigurableProperties getDbConfProperties​(java.nio.file.Path path)
                                                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • deleteTmpFiles

        public static void deleteTmpFiles​(java.lang.String dirName,
                                          java.lang.String prefix)
      • writeLine

        protected static void writeLine​(java.io.OutputStream os,
                                        java.lang.String text)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • buildFilename

        public static java.lang.String buildFilename​(java.lang.String prefix,
                                                     java.lang.String suffix,
                                                     long minIdOfCurrentFile,
                                                     long maxIdOfCurrentFile,
                                                     long maxId)
      • getZipOutputStream

        public static java.util.zip.ZipOutputStream getZipOutputStream​(java.io.File zipFile)
                                                                throws java.io.IOException
        Throws:
        java.io.IOException
      • releaseResources

        public void releaseResources​(java.sql.PreparedStatement ps,
                                     java.sql.ResultSet rs)