|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.statement; |
| 11 | + |
| 12 | +import java.util.function.Consumer; |
| 13 | +import net.sf.jsqlparser.expression.Expression; |
| 14 | +import net.sf.jsqlparser.expression.StringValue; |
| 15 | +import net.sf.jsqlparser.statement.create.user.UserAccount; |
| 16 | + |
| 17 | +/** |
| 18 | + * MySQL/MariaDB {@code SET PASSWORD [FOR user] {= password | TO RANDOM} |
| 19 | + * [REPLACE 'current'] [RETAIN CURRENT PASSWORD]}. The password is an expression, usually a string |
| 20 | + * literal; MariaDB also accepts {@code PASSWORD('...')}. |
| 21 | + */ |
| 22 | +public class SetPasswordStatement implements Statement { |
| 23 | + |
| 24 | + private UserAccount account; |
| 25 | + private Expression password; |
| 26 | + private boolean random; |
| 27 | + private StringValue currentCredential; |
| 28 | + private boolean retainCurrentPassword; |
| 29 | + |
| 30 | + /** The account after {@code FOR}, or null for the current user. */ |
| 31 | + public UserAccount getAccount() { |
| 32 | + return account; |
| 33 | + } |
| 34 | + |
| 35 | + public void setAccount(UserAccount account) { |
| 36 | + this.account = account; |
| 37 | + } |
| 38 | + |
| 39 | + /** The password after {@code =}; null for {@code TO RANDOM}. */ |
| 40 | + public Expression getPassword() { |
| 41 | + return password; |
| 42 | + } |
| 43 | + |
| 44 | + public void setPassword(Expression password) { |
| 45 | + this.password = password; |
| 46 | + } |
| 47 | + |
| 48 | + /** Whether the statement generates a password with {@code TO RANDOM}. */ |
| 49 | + public boolean isRandom() { |
| 50 | + return random; |
| 51 | + } |
| 52 | + |
| 53 | + public void setRandom(boolean random) { |
| 54 | + this.random = random; |
| 55 | + } |
| 56 | + |
| 57 | + /** The current password of {@code REPLACE 'current'}, or null when omitted. */ |
| 58 | + public StringValue getCurrentCredential() { |
| 59 | + return currentCredential; |
| 60 | + } |
| 61 | + |
| 62 | + public void setCurrentCredential(StringValue currentCredential) { |
| 63 | + this.currentCredential = currentCredential; |
| 64 | + } |
| 65 | + |
| 66 | + public boolean isRetainCurrentPassword() { |
| 67 | + return retainCurrentPassword; |
| 68 | + } |
| 69 | + |
| 70 | + public void setRetainCurrentPassword(boolean retainCurrentPassword) { |
| 71 | + this.retainCurrentPassword = retainCurrentPassword; |
| 72 | + } |
| 73 | + |
| 74 | + /** Shares statement rendering while letting deparsers render the password expression. */ |
| 75 | + public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressionRenderer) { |
| 76 | + builder.append("SET PASSWORD"); |
| 77 | + if (account != null) { |
| 78 | + builder.append(" FOR ").append(account); |
| 79 | + } |
| 80 | + if (random) { |
| 81 | + builder.append(" TO RANDOM"); |
| 82 | + } else { |
| 83 | + builder.append(" = "); |
| 84 | + expressionRenderer.accept(password); |
| 85 | + } |
| 86 | + if (currentCredential != null) { |
| 87 | + builder.append(" REPLACE ").append(currentCredential); |
| 88 | + } |
| 89 | + if (retainCurrentPassword) { |
| 90 | + builder.append(" RETAIN CURRENT PASSWORD"); |
| 91 | + } |
| 92 | + return builder; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) { |
| 97 | + return statementVisitor.visit(this, context); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String toString() { |
| 102 | + StringBuilder builder = new StringBuilder(); |
| 103 | + return appendTo(builder, builder::append).toString(); |
| 104 | + } |
| 105 | + |
| 106 | + public SetPasswordStatement withAccount(UserAccount account) { |
| 107 | + setAccount(account); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public SetPasswordStatement withPassword(Expression password) { |
| 112 | + setPassword(password); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public SetPasswordStatement withRandom(boolean random) { |
| 117 | + setRandom(random); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + public SetPasswordStatement withCurrentCredential(StringValue currentCredential) { |
| 122 | + setCurrentCredential(currentCredential); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public SetPasswordStatement withRetainCurrentPassword(boolean retainCurrentPassword) { |
| 127 | + setRetainCurrentPassword(retainCurrentPassword); |
| 128 | + return this; |
| 129 | + } |
| 130 | +} |
0 commit comments