1 module hunt.database.base.SqlConnectOptions; 2 3 import hunt.net.NetClientOptions; 4 import hunt.net.util.HttpURI; 5 6 import hunt.collection; 7 import hunt.Exceptions; 8 import hunt.logging; 9 10 import std.array; 11 12 /** 13 * Connect options for configuring {@link SqlConnection} or {@link Pool}. 14 */ 15 abstract class SqlConnectOptions : NetClientOptions { 16 enum bool DEFAULT_CACHE_PREPARED_STATEMENTS = false; 17 enum int DEFAULT_PREPARED_STATEMENT_CACHE_MAX_SIZE = 256; 18 enum int DEFAULT_PREPARED_STATEMENT_CACHE_SQL_LIMIT = 2048; 19 20 private string host; 21 private int port; 22 private string user; 23 private string password; 24 private string database; 25 private bool cachePreparedStatements = DEFAULT_CACHE_PREPARED_STATEMENTS; 26 private int preparedStatementCacheMaxSize = DEFAULT_PREPARED_STATEMENT_CACHE_MAX_SIZE; 27 private int preparedStatementCacheSqlLimit = DEFAULT_PREPARED_STATEMENT_CACHE_SQL_LIMIT; 28 private Map!(string, string) properties; 29 30 this() { 31 properties = new HashMap!(string, string)(); 32 super(); 33 initialize(); 34 } 35 36 this(HttpURI uri) { 37 version(HUNT_DEBUG) info("DB connection string: ", uri.toString()); 38 super(); 39 initialize(); 40 this.host = uri.getHost(); 41 this.port = uri.getPort(); 42 this.user = uri.getUser(); 43 this.password = uri.getPassword(); 44 string path = uri.getPath(); 45 assert(path.length >1); 46 this.database = path[1..$]; 47 } 48 49 this(SqlConnectOptions other) { 50 super(other); 51 initialize(); 52 this.host = other.host; 53 this.port = other.port; 54 this.user = other.user; 55 this.password = other.password; 56 this.database = other.database; 57 this.cachePreparedStatements = other.cachePreparedStatements; 58 this.preparedStatementCacheMaxSize = other.preparedStatementCacheMaxSize; 59 this.preparedStatementCacheSqlLimit = other.preparedStatementCacheSqlLimit; 60 this.properties = new HashMap!(string, string)(other.properties); 61 } 62 63 /** 64 * Get the host for connecting to the server. 65 * 66 * @return the host 67 */ 68 string getHost() { 69 return host; 70 } 71 72 /** 73 * Specify the host for connecting to the server. 74 * 75 * @param host the host to specify 76 * @return a reference to this, so the API can be used fluently 77 */ 78 SqlConnectOptions setHost(string host) { 79 assert(!host.empty(), "Host can not be null"); 80 this.host = host; 81 return this; 82 } 83 84 /** 85 * Get the port for connecting to the server. 86 * 87 * @return the port 88 */ 89 int getPort() { 90 return port; 91 } 92 93 /** 94 * Specify the port for connecting to the server. 95 * 96 * @param port the port to specify 97 * @return a reference to this, so the API can be used fluently 98 */ 99 SqlConnectOptions setPort(int port) { 100 if (port < 0 || port > 65535) { 101 throw new IllegalArgumentException("Port should range in 0-65535"); 102 } 103 this.port = port; 104 return this; 105 } 106 107 /** 108 * Get the user account to be used for the authentication. 109 * 110 * @return the user 111 */ 112 string getUser() { 113 return user; 114 } 115 116 /** 117 * Specify the user account to be used for the authentication. 118 * 119 * @param user the user to specify 120 * @return a reference to this, so the API can be used fluently 121 */ 122 SqlConnectOptions setUser(string user) { 123 assert(!host.empty, "User account can not be null"); 124 this.user = user; 125 return this; 126 } 127 128 /** 129 * Get the user password to be used for the authentication. 130 * 131 * @return the password 132 */ 133 string getPassword() { 134 return password; 135 } 136 137 /** 138 * Specify the user password to be used for the authentication. 139 * 140 * @param password the password to specify 141 * @return a reference to this, so the API can be used fluently 142 */ 143 SqlConnectOptions setPassword(string password) { 144 assert(!host.empty, "Password can not be null"); 145 this.password = password; 146 return this; 147 } 148 149 /** 150 * Get the default database name for the connection. 151 * 152 * @return the database name 153 */ 154 string getDatabase() { 155 return database; 156 } 157 158 /** 159 * Specify the default database for the connection. 160 * 161 * @param database the database name to specify 162 * @return a reference to this, so the API can be used fluently 163 */ 164 SqlConnectOptions setDatabase(string database) { 165 assert(!host.empty, "Database name can not be null"); 166 this.database = database; 167 return this; 168 } 169 170 /** 171 * Get whether prepared statements cache is enabled. 172 * 173 * @return the value 174 */ 175 bool getCachePreparedStatements() { 176 return cachePreparedStatements; 177 } 178 179 /** 180 * Set whether prepared statements cache should be enabled. 181 * 182 * @param cachePreparedStatements true if cache should be enabled 183 * @return a reference to this, so the API can be used fluently 184 */ 185 SqlConnectOptions setCachePreparedStatements(bool cachePreparedStatements) { 186 this.cachePreparedStatements = cachePreparedStatements; 187 return this; 188 } 189 190 /** 191 * Get the maximum number of prepared statements that the connection will cache. 192 * 193 * @return the size 194 */ 195 int getPreparedStatementCacheMaxSize() { 196 return preparedStatementCacheMaxSize; 197 } 198 199 /** 200 * Set the maximum number of prepared statements that the connection will cache. 201 * 202 * @param preparedStatementCacheMaxSize the size to set 203 * @return a reference to this, so the API can be used fluently 204 */ 205 SqlConnectOptions setPreparedStatementCacheMaxSize(int preparedStatementCacheMaxSize) { 206 this.preparedStatementCacheMaxSize = preparedStatementCacheMaxSize; 207 return this; 208 } 209 210 /** 211 * Get the maximum length of prepared statement SQL string that the connection will cache. 212 * 213 * @return the limit of maximum length 214 */ 215 int getPreparedStatementCacheSqlLimit() { 216 return preparedStatementCacheSqlLimit; 217 } 218 219 /** 220 * Set the maximum length of prepared statement SQL string that the connection will cache. 221 * 222 * @param preparedStatementCacheSqlLimit the maximum length limit of SQL string to set 223 * @return a reference to this, so the API can be used fluently 224 */ 225 SqlConnectOptions setPreparedStatementCacheSqlLimit(int preparedStatementCacheSqlLimit) { 226 this.preparedStatementCacheSqlLimit = preparedStatementCacheSqlLimit; 227 return this; 228 } 229 230 /** 231 * @return the value of current connection properties 232 */ 233 Map!(string, string) getProperties() { 234 return properties; 235 } 236 237 /** 238 * Set properties for this client, which will be sent to server at the connection start. 239 * 240 * @param properties the value of properties to specify 241 * @return a reference to this, so the API can be used fluently 242 */ 243 SqlConnectOptions setProperties(Map!(string, string) properties) { 244 assert(properties !is null, "Properties can not be null"); 245 this.properties = properties; 246 return this; 247 } 248 249 /** 250 * Add a property for this client, which will be sent to server at the connection start. 251 * 252 * @param key the value of property key 253 * @param value the value of property value 254 * @return a reference to this, so the API can be used fluently 255 */ 256 SqlConnectOptions addProperty(string key, string value) { 257 assert(!key.empty(), "Property key can not be null"); 258 assert(!value.empty(), "Property value can not be null"); 259 this.properties.put(key, value); 260 return this; 261 } 262 263 264 // override 265 // JsonObject toJson() { 266 // JsonObject json = super.toJson(); 267 // SqlConnectOptionsConverter.toJson(this, json); 268 // return json; 269 // } 270 271 /** 272 * Initialize with the default options. 273 */ 274 abstract protected void initialize(); 275 276 // protected void checkParameterNonNull(Object parameter, string message) { 277 // if (parameter is null) { 278 // throw new IllegalArgumentException(message); 279 // } 280 // } 281 }