|
| 1 | +package biz.chenxu.tutorial; |
| 2 | + |
| 3 | +import org.apache.commons.cli.CommandLine; |
| 4 | +import org.apache.commons.lang.StringUtils; |
| 5 | + |
| 6 | +public class ConfigBuilder { |
| 7 | + |
| 8 | + private String host; |
| 9 | + private int port; |
| 10 | + private String schema; |
| 11 | + private String username; |
| 12 | + private String password; |
| 13 | + private boolean useUnicode = true; |
| 14 | + private String characterEncoding = "autodetect"; |
| 15 | + private String serverTimezone; |
| 16 | + private boolean useFastDateParsing = true; |
| 17 | + private boolean cacheDefaultTimezone = true; |
| 18 | + private boolean noTimezoneConversionForDateType = true; |
| 19 | + private boolean noTimezoneConversionForTimeType = false; |
| 20 | + private boolean useGmtMillisForDatetimes = false; |
| 21 | + private boolean useJDBCCompliantTimezoneShift = false; |
| 22 | + private boolean useLegacyDatetimeCode = true; |
| 23 | + private boolean useSSPSCompatibleTimezoneShift = false; |
| 24 | + private boolean useTimezone = false; |
| 25 | + |
| 26 | + private ConfigBuilder() { |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + public ConfigBuilder host(String host) { |
| 31 | + this.host = host; |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + public ConfigBuilder port(int port) { |
| 36 | + this.port = port; |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + public ConfigBuilder schema(String schema) { |
| 41 | + this.schema = schema; |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + public ConfigBuilder username(String username) { |
| 46 | + this.username = username; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public ConfigBuilder password(String password) { |
| 51 | + this.password = password; |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * useUnicode |
| 57 | + * |
| 58 | + * Should the driver use Unicode character encodings when handling strings? Should only be used when |
| 59 | + * the driver can't determine the character set mapping, or you are trying to 'force' the driver to |
| 60 | + * use a character set that MySQL either doesn't natively support (such as UTF-8), true/false, defaults to 'true' |
| 61 | + * |
| 62 | + * Default: true |
| 63 | + * |
| 64 | + * Since version: 1.1g |
| 65 | + * |
| 66 | + * @param useUnicode |
| 67 | + * @return |
| 68 | + */ |
| 69 | + public ConfigBuilder useUnicode(boolean useUnicode) { |
| 70 | + this.useUnicode = useUnicode; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * characterEncoding |
| 76 | + * |
| 77 | + * If 'useUnicode' is set to true, what character encoding should the driver use when dealing with strings? |
| 78 | + * (defaults is to 'autodetect') |
| 79 | + * |
| 80 | + * Since version: 1.1g |
| 81 | + * |
| 82 | + * @param characterEncoding |
| 83 | + * @return |
| 84 | + */ |
| 85 | + public ConfigBuilder characterEncoding(String characterEncoding) { |
| 86 | + this.characterEncoding = characterEncoding; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * serverTimezone |
| 92 | + * |
| 93 | + * Override detection/mapping of time zone. Used when time zone from server doesn't map to Java time zone |
| 94 | + * |
| 95 | + * Since version: 3.0.2 |
| 96 | + * |
| 97 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 98 | + * |
| 99 | + * @param serverTimezone |
| 100 | + * @return |
| 101 | + */ |
| 102 | + public ConfigBuilder serverTimezone(String serverTimezone) { |
| 103 | + this.serverTimezone = serverTimezone; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * useFastDateParsing |
| 109 | + * |
| 110 | + * Use internal String->Date/Time/Timestamp conversion routines to avoid excessive object creation? |
| 111 | + * This is part of the legacy date-time code, thus the property has an effect only when "useLegacyDatetimeCode=true." |
| 112 | + * |
| 113 | + * Default: true |
| 114 | + * |
| 115 | + * Since version: 5.0.5 |
| 116 | + * |
| 117 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 118 | + * |
| 119 | + * @param useFastDateParsing |
| 120 | + * @return |
| 121 | + */ |
| 122 | + public ConfigBuilder useFastDateParsing(boolean useFastDateParsing) { |
| 123 | + this.useFastDateParsing = useFastDateParsing; |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * cacheDefaultTimezone |
| 129 | + * |
| 130 | + * Caches client's default time zone. This results in better performance when dealing with time zone |
| 131 | + * conversions in Date and Time data types, however it won't be aware of time zone changes if they happen at runtime. |
| 132 | + * |
| 133 | + * Default: true |
| 134 | + * |
| 135 | + * Since version: 5.1.35 |
| 136 | + * |
| 137 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 138 | + * |
| 139 | + * @param cacheDefaultTimezone |
| 140 | + * @return |
| 141 | + */ |
| 142 | + public ConfigBuilder cacheDefaultTimezone(boolean cacheDefaultTimezone) { |
| 143 | + this.cacheDefaultTimezone = cacheDefaultTimezone; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * noTimezoneConversionForDateType |
| 149 | + * |
| 150 | + * Don't convert DATE values using the server time zone if 'useTimezone'='true' or 'useLegacyDatetimeCode'='false' |
| 151 | + * |
| 152 | + * Default: true |
| 153 | + * |
| 154 | + * Since version: 5.1.35 |
| 155 | + * |
| 156 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 157 | + * |
| 158 | + * @param noTimezoneConversionForDateType |
| 159 | + * @return |
| 160 | + */ |
| 161 | + public ConfigBuilder noTimezoneConversionForDateType(boolean noTimezoneConversionForDateType) { |
| 162 | + this.noTimezoneConversionForDateType = noTimezoneConversionForDateType; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * noTimezoneConversionForTimeType |
| 168 | + * |
| 169 | + * Don't convert TIME values using the server time zone if 'useTimezone'='true' |
| 170 | + * |
| 171 | + * Default: false |
| 172 | + * |
| 173 | + * Since version: 5.0.0 |
| 174 | + * |
| 175 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 176 | + * |
| 177 | + * @param noTimezoneConversionForTimeType |
| 178 | + * @return |
| 179 | + */ |
| 180 | + public ConfigBuilder noTimezoneConversionForTimeType(boolean noTimezoneConversionForTimeType) { |
| 181 | + this.noTimezoneConversionForTimeType = noTimezoneConversionForTimeType; |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * useGmtMillisForDatetimes |
| 187 | + * |
| 188 | + * Convert between session time zone and GMT before creating Date and Timestamp instances |
| 189 | + * (value of 'false' leads to legacy behavior, 'true' leads to more JDBC-compliant behavior)? |
| 190 | + * This is part of the legacy date-time code, thus the property has an effect only when "useLegacyDatetimeCode=true." |
| 191 | + * |
| 192 | + * Default: false |
| 193 | + * |
| 194 | + * Since version: 3.1.12 |
| 195 | + * |
| 196 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 197 | + * |
| 198 | + * @param useGmtMillisForDatetimes |
| 199 | + * @return |
| 200 | + */ |
| 201 | + public ConfigBuilder useGmtMillisForDatetimes(boolean useGmtMillisForDatetimes) { |
| 202 | + this.useGmtMillisForDatetimes = useGmtMillisForDatetimes; |
| 203 | + return this; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * useJDBCCompliantTimezoneShift |
| 208 | + * |
| 209 | + * Should the driver use JDBC-compliant rules when converting TIME/TIMESTAMP/DATETIME values' time zone information |
| 210 | + * for those JDBC arguments which take a java.util.Calendar argument? This is part of the legacy date-time code, |
| 211 | + * thus the property has an effect only when "useLegacyDatetimeCode=true." |
| 212 | + * |
| 213 | + * Default: false |
| 214 | + * |
| 215 | + * Since version: 5.0.0 |
| 216 | + * |
| 217 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 218 | + * |
| 219 | + * @param useJDBCCompliantTimezoneShift |
| 220 | + * @return |
| 221 | + */ |
| 222 | + public ConfigBuilder useJDBCCompliantTimezoneShift(boolean useJDBCCompliantTimezoneShift) { |
| 223 | + this.useJDBCCompliantTimezoneShift = useJDBCCompliantTimezoneShift; |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * useLegacyDatetimeCode |
| 229 | + * |
| 230 | + * Use code for DATE/TIME/DATETIME/TIMESTAMP handling in result sets and statements that consistently handles |
| 231 | + * time zone conversions from client to server and back again, or use the legacy code for these datatypes that |
| 232 | + * has been in the driver for backwards-compatibility? Setting this property to 'false' voids the effects |
| 233 | + * of "useTimezone," "useJDBCCompliantTimezoneShift," "useGmtMillisForDatetimes," and "useFastDateParsing." |
| 234 | + * |
| 235 | + * Default: true |
| 236 | + * |
| 237 | + * Since version: 5.1.6 |
| 238 | + * |
| 239 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 240 | + * |
| 241 | + * @param useLegacyDatetimeCode |
| 242 | + * @return |
| 243 | + */ |
| 244 | + public ConfigBuilder useLegacyDatetimeCode(boolean useLegacyDatetimeCode) { |
| 245 | + this.useLegacyDatetimeCode = useLegacyDatetimeCode; |
| 246 | + return this; |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * useSSPSCompatibleTimezoneShift |
| 251 | + * |
| 252 | + * If migrating from an environment that was using server-side prepared statements, and the configuration |
| 253 | + * property "useJDBCCompliantTimeZoneShift" set to "true", use compatible behavior when not using server-side |
| 254 | + * prepared statements when sending TIMESTAMP values to the MySQL server. |
| 255 | + * |
| 256 | + * Default: false |
| 257 | + * |
| 258 | + * Since version: 5.0.5 |
| 259 | + * |
| 260 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 261 | + * |
| 262 | + * @param useSSPSCompatibleTimezoneShift |
| 263 | + * @return |
| 264 | + */ |
| 265 | + public ConfigBuilder useSSPSCompatibleTimezoneShift(boolean useSSPSCompatibleTimezoneShift) { |
| 266 | + this.useSSPSCompatibleTimezoneShift = useSSPSCompatibleTimezoneShift; |
| 267 | + return this; |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * useTimezone |
| 272 | + * |
| 273 | + * Convert time/date types between client and server time zones (true/false, defaults to 'false')? |
| 274 | + * This is part of the legacy date-time code, thus the property has an effect only when "useLegacyDatetimeCode=true." |
| 275 | + * |
| 276 | + * Default: false |
| 277 | + * |
| 278 | + * Since version: 3.0.2 |
| 279 | + * |
| 280 | + * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
| 281 | + * |
| 282 | + * @param useTimezone |
| 283 | + * @return |
| 284 | + */ |
| 285 | + public ConfigBuilder useTimezone(boolean useTimezone) { |
| 286 | + this.useTimezone = useTimezone; |
| 287 | + return this; |
| 288 | + } |
| 289 | + |
| 290 | + public static ConfigBuilder parse(CommandLine cmd) { |
| 291 | + return new ConfigBuilder().host(cmd.getOptionValue("host", "127.0.0.1")) |
| 292 | + .port(Integer.parseInt(cmd.getOptionValue("port", "3306"))) |
| 293 | + .schema(cmd.getOptionValue("schema", "test")) |
| 294 | + .username(cmd.getOptionValue("username", "root")) |
| 295 | + .password(cmd.getOptionValue("password", "root")); |
| 296 | + } |
| 297 | + |
| 298 | + public Config build() { |
| 299 | + Config config = new Config(); |
| 300 | + config.setJdbcUrl(toJdbcUrl()); |
| 301 | + config.setUsername(username); |
| 302 | + config.setPassword(password); |
| 303 | + return config; |
| 304 | + } |
| 305 | + |
| 306 | + private String toJdbcUrl() { |
| 307 | + String protocol = String.format("jdbc:mysql://%s:%s/%s", host, port, schema); |
| 308 | + StringBuilder arguments = new StringBuilder(); |
| 309 | + |
| 310 | + arguments.append("?").append("useUnicode").append("=").append(useUnicode); |
| 311 | + arguments.append("&").append("useTimezone").append("=").append(useTimezone); |
| 312 | + arguments.append("&").append("cacheDefaultTimezone").append("=").append(cacheDefaultTimezone); |
| 313 | + |
| 314 | + if (StringUtils.isNotBlank(characterEncoding)) { |
| 315 | + arguments.append("&").append("characterEncoding").append("=").append(characterEncoding); |
| 316 | + } |
| 317 | + if (StringUtils.isNotBlank(serverTimezone)) { |
| 318 | + arguments.append("&").append("serverTimezone").append("=").append(serverTimezone); |
| 319 | + } |
| 320 | + |
| 321 | + return String.format("%s%s", protocol, arguments); |
| 322 | + } |
| 323 | +} |
0 commit comments