001package gu.sql2java;
002
003import java.util.HashMap;
004import java.util.concurrent.TimeUnit;
005
006/**
007 * constant declare
008 * @author guyadong
009 */
010public interface Constant {    
011    public static final int STATE_BIT_NUM = 32;
012    public static final int STATE_BIT_SHIFT = 5;
013    public static final int STATE_BIT_MASK = 0x1f;
014
015    public static final String SQL_LIKE_WILDCARD = "%";
016    /** set =QUERY for loadUsingTemplate */
017    public static final int SEARCH_EXACT = 0;
018    /** set %QUERY% for loadLikeTemplate */
019    public static final int SEARCH_LIKE = 1;
020    /** set %QUERY for loadLikeTemplate */
021    public static final int SEARCH_STARTING_LIKE = 2;
022    /** set QUERY% for loadLikeTemplate */
023    public static final int SEARCH_ENDING_LIKE = 3;
024
025    /** JDBC property name definition */
026    public static enum JdbcProperty{
027        /** debug status */DEBUG("isDebug"),
028        /** JDBC driver class name */JDBC_DRIVER("jdbc.driver"),
029        /** JDBC connection url */JDBC_URL("jdbc.url"),
030        /** JDBC user name */JDBC_USERNAME("jdbc.username"),
031        /** JDBC password */JDBC_PASSWORD("jdbc.password"),
032        /** Retrieval type of auto generated key:auto,before,after */GENERATEDKEY_RETRIEVE("generatedkey.retrieve"),
033        /** Retrieval statement of auto generated key */GENERATEDKEY_STATEMENT("generatedkey.statement"),
034        /** data source type, c3p0 supported only now */DATASOURCE("datasource"),
035        /** c3p0 property */C3P0_MINPOOLSIZE("c3p0.minPoolSize"),
036        /** c3p0 property */C3P0_MAXPOOLSIZE("c3p0.maxPoolSize"),
037        /** c3p0 property */C3P0_MAXIDLETIME("c3p0.maxIdleTime"),
038        /** c3p0 property */C3P0_IDLECONNECTIONTESTPERIOD("c3p0.idleConnectionTestPeriod");
039        /** JDBC property name */
040        public final String key;
041        
042        JdbcProperty(String key){
043            this.key = key;
044        }
045        /** return {@link #key} with {@code prefix} */
046        public String withPrefix(String prefix){
047            return new StringBuffer().append(prefix).append(key).toString();
048        }
049        private static final HashMap<String, JdbcProperty> KEYS = new HashMap<>();
050        static{         
051                for(JdbcProperty p: values()){
052                KEYS.put(p.key, p);
053            }
054        }
055        /** 
056         * cast {@code key} to {@link JdbcProperty} instance if {@link #key} field equal the argument {@code key},
057         * otherwise return {@code null} 
058         * @param key JDBC property name
059         * @return JdbcProperty
060         */
061        public static final JdbcProperty fromKey(String key){
062            return KEYS.get(key);
063        }
064        /** 
065         * cast {@code key} to {@link JdbcProperty} instance if {@link #key} field equal the argument {@code key},
066         * otherwise return {@code null} <br>
067         *  be equivalent to {@link #fromKey(String)} if {@code prefix} be {@code null}
068         * @param key JDBC property name
069         * @param prefix the prefix of key,that will be striped
070         * @return JdbcProperty
071         */
072        public static final JdbcProperty fromKey(String key,String prefix){
073                if(key == null){
074                        return null;
075                }
076                int idx;
077                if(prefix != null && (idx = key.indexOf(prefix)) >=0){
078                        key = key.substring(idx);
079                }
080            return KEYS.get(key);
081        }
082    }
083
084        /** 
085         * Update strategy for cache
086         */
087        enum UpdateStrategy{        
088            /** update no matter whether key exists */
089            always,
090            /** update only if key exists */
091            replace,
092            /** remove key  */
093            remove,
094            /** reload data form database if key exists,and update  */
095            refresh
096        }
097        /** 默认缓存更新策略 */
098        UpdateStrategy DEFAULT_STRATEGY = UpdateStrategy.always;
099        /** 默认缓存最大缓存容量 */
100        long DEFAULT_CACHE_MAXIMUMSIZE = 10000;
101        /** 默认缓存失败时间 */
102        long DEFAULT_DURATION = 10;
103        /** 默认缓存失败时间单位 */
104        TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MINUTES;
105        public static final String RETRIEVE_SUFFIX = "generatedkey.retrieve";
106        public static final String STATEMENT_SUFFIX = "generatedkey.statement";
107}