Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Return substring from start, instead from the middle of the string to avoid invalid characters being used at the start of the string.

Reformat code.

[#264][resolves #265]

Signed-off-by: Mark Paluch <mpaluch@vmware.com>
  • Loading branch information
mp911de committed Mar 28, 2023
1 parent f40d131 commit 98d5cbd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 108 deletions.
23 changes: 10 additions & 13 deletions src/main/java/io/r2dbc/mssql/MssqlConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
*
* @author Mark Paluch
* @author Hebert Coelho
* @author Nayan Hajratwala
* @see MssqlConnection
* @see DefaultMssqlResult
* @see ErrorDetails
Expand Down Expand Up @@ -117,7 +118,7 @@ public Mono<Void> beginTransaction(TransactionDefinition transactionDefinition)
if (mark != null) {
String markToUse = sanitize(mark, 128);
Assert.isTrue(IDENTIFIER128_PATTERN.matcher(markToUse.substring(0, Math.min(128, markToUse.length()))).matches(), "Transaction names must contain only characters and numbers and" +
" must not exceed 128 characters");
" must not exceed 128 characters");
builder.append(' ').append("WITH MARK '").append(markToUse).append("'");
}
}
Expand Down Expand Up @@ -412,29 +413,25 @@ private static String renderSetIsolationLevel(IsolationLevel isolationLevel) {
return "SET TRANSACTION ISOLATION LEVEL " + isolationLevel.asSql();
}

static String sanitize(final String identifier, final int maxLength) {
String sanitized = identifier
.replace('-', '_')
.replace('.', '_')
.substring(Math.max(0, identifier.length() - maxLength));
static String sanitize(String identifier, int maxLength) {

if (!Character.isLetterOrDigit(sanitized.charAt(0))) {
sanitized = sanitized.substring(1);
}
return sanitized;
return identifier
.replace('-', '_')
.replace('.', '_')
.substring(0, Math.min(identifier.length(), maxLength));
}

private Mono<Void> exchange(String sql) {

ExceptionFactory factory = ExceptionFactory.withSql(sql);
return QueryMessageFlow.exchange(this.client, sql)
.handle(factory::handleErrorResponse)
.then();
.handle(factory::handleErrorResponse)
.then();
}

private Mono<Void> useTransactionStatus(Function<TransactionStatus, Publisher<?>> function) {
return Flux.defer(() -> function.apply(this.client.getTransactionStatus()))
.then();
.then();
}

enum EmptyTransactionDefinition implements TransactionDefinition {
Expand Down

0 comments on commit 98d5cbd

Please sign in to comment.