11package com .exasol .common
22
3- import scala . collection . SortedMap
4-
5- import com .exasol .{ ExaConnectionInformation , ExaMetadata }
3+ import com . exasol . ExaConnectionInformation
4+ import com . exasol . ExaMetadata
5+ import com .exasol .common . CommonConstants . _
66
77/**
8- * An abstract class that holds the user provided key-value parameters
9- * when using the user-defined-functions (UDFs).
8+ * An abstract class that holds the user provided key-value parameters when using the user-defined-functions (UDFs).
109 *
11- * This only represents the raw string key-value pairs. Specific
12- * implementations should extends this class to support required UDF
13- * key-value parameters.
10+ * This only represents the raw string key-value pairs. Specific implementations should extends this class to support
11+ * required UDF key-value parameters.
1412 */
1513abstract class AbstractProperties (private val properties : Map [String , String ]) {
1614
17- /** An optional property key name for the named connection object. */
18- final val CONNECTION_NAME_PROPERTY : String = " CONNECTION_NAME"
19-
2015 /**
2116 * Checks whether the key-value properties map is empty.
2217 */
@@ -55,37 +50,29 @@ abstract class AbstractProperties(private val properties: Map[String, String]) {
5550
5651 /** Checks if the Exasol named connection property is provided. */
5752 final def hasNamedConnection (): Boolean =
58- containsKey(CONNECTION_NAME_PROPERTY )
53+ containsKey(CONNECTION_NAME )
54+
55+ private [this ] def getPropertySeparator (): String =
56+ get(CONNECTION_PROPERTY_SEPARATOR ).fold(CONNECTION_PROPERTY_SEPARATOR_DEFAULT_VALUE )(identity)
57+
58+ private [this ] def getKeyValueAssignment (): String =
59+ get(CONNECTION_KEYVALUE_ASSIGNMENT ).fold(CONNECTION_KEYVALUE_ASSIGNMENT_DEFAULT_VALUE )(identity)
5960
6061 /**
61- * Parses the connection object password into key-value map pairs.
62+ * Parses the connection object password into key-value pairs.
63+ *
64+ * If the connection object contains the username, it is mapped to the {@code keyForUsername}
65+ * parameter. However, this value is overwritten if the provided key is available in password
66+ * string of connection object.
6267 *
63- * If the connection object contains the username, it is mapped to the
64- * {@code keyForUsername} parameter. However, this value is
65- * overwritten if the provided key is available in password string of
66- * connection object.
68+ * Users can set also property separators using {@code CONNECTION_SEPARATOR} and {@code
69+ * CONNECTION_KEYVALUE_ASSIGNMENT} parameters.
6770 */
68- final def parseConnectionInfo (
69- keyForUsername : String ,
70- exaMetadata : Option [ExaMetadata ]
71- ): Map [String , String ] = {
71+ final def parseConnectionInfo (keyForUsername : String , exaMetadata : Option [ExaMetadata ]): Map [String , String ] = {
7272 val connection = getConnectionInformation(exaMetadata)
7373 val username = connection.getUser()
7474 val password = connection.getPassword();
75- val map = password
76- .split(" ;" )
77- .map { str =>
78- val idx = str.indexOf('=' )
79- if (idx < 0 ) {
80- throw new IllegalArgumentException (
81- " Connection object password does not contain key=value pairs!"
82- )
83- }
84- str.substring(0 , idx).strip().replace(" \n " , " " ) ->
85- str.substring(idx + 1 ).strip().replace(" \n " , " " )
86- }
87- .toMap
88-
75+ val map = PropertiesParser (getPropertySeparator(), getKeyValueAssignment()).mapFromString(password)
8976 if (username.isEmpty()) {
9077 map
9178 } else {
@@ -94,15 +81,12 @@ abstract class AbstractProperties(private val properties: Map[String, String]) {
9481 }
9582
9683 /**
97- * Returns an Exasol [[ExaConnectionInformation ]] named connection
98- * information.
84+ * Returns an Exasol [[ExaConnectionInformation ]] named connection information.
9985 */
100- private [common] final def getConnectionInformation (
101- exaMetadata : Option [ExaMetadata ]
102- ): ExaConnectionInformation =
86+ private [common] final def getConnectionInformation (exaMetadata : Option [ExaMetadata ]): ExaConnectionInformation =
10387 exaMetadata.fold {
10488 throw new IllegalArgumentException (" Exasol metadata is None!" )
105- }(_.getConnection(getString(CONNECTION_NAME_PROPERTY )))
89+ }(_.getConnection(getString(CONNECTION_NAME )))
10690
10791 /**
10892 * Returns the value of the key as a String.
@@ -124,17 +108,14 @@ abstract class AbstractProperties(private val properties: Map[String, String]) {
124108 /**
125109 * Returns a string listing of all key-value property pairs.
126110 *
127- * The resulting string contains key-value pairs in a sorted order by
128- * keys.
111+ * The resulting string contains key-value pairs in a sorted order by keys.
129112 *
130- * @param keyValueSeparator The separator between each key-value pairs
131- * @param propertySeparator The separator between each key-value pair strings
132- * @return The string value of properties with provided separators
113+ * @param propertySeparator a separator between each key-value pair strings
114+ * @param keyValueAssignment an assignment between each key-value pairs
115+ * @return a string value of properties with provided separators
133116 */
134- final def mkString (keyValueSeparator : String , propertySeparator : String ): String =
135- (SortedMap .empty[String , String ] ++ properties)
136- .map { case (k, v) => s " $k$keyValueSeparator$v" }
137- .mkString(propertySeparator)
117+ final def mkString (propertySeparator : String , keyValueAssignment : String ): String =
118+ PropertiesParser (propertySeparator, keyValueAssignment).mapToString(properties)
138119
139120 @ SuppressWarnings (Array (" org.wartremover.warts.Return" ))
140121 // scalastyle:off
0 commit comments