| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.engine; |
| 7 |
|
| 8 |
import org.h2.message.DbException; |
| 9 |
|
| 10 |
/** |
| 11 |
* Modes of generated keys' gathering. |
| 12 |
*/ |
| 13 |
public final class GeneratedKeysMode { |
| 14 |
|
| 15 |
/** |
| 16 |
* Generated keys are not needed. |
| 17 |
*/ |
| 18 |
public static final int NONE = 0; |
| 19 |
|
| 20 |
/** |
| 21 |
* Generated keys should be configured automatically. |
| 22 |
*/ |
| 23 |
public static final int AUTO = 1; |
| 24 |
|
| 25 |
/** |
| 26 |
* Use specified column indices to return generated keys from. |
| 27 |
*/ |
| 28 |
public static final int COLUMN_NUMBERS = 2; |
| 29 |
|
| 30 |
/** |
| 31 |
* Use specified column names to return generated keys from. |
| 32 |
*/ |
| 33 |
public static final int COLUMN_NAMES = 3; |
| 34 |
|
| 35 |
/** |
| 36 |
* Determines mode of generated keys' gathering. |
| 37 |
* |
| 38 |
* @param generatedKeysRequest |
| 39 |
* {@code null} or {@code false} if generated keys are not |
| 40 |
* needed, {@code true} if generated keys should be configured |
| 41 |
* automatically, {@code int[]} to specify column indices to |
| 42 |
* return generated keys from, or {@code String[]} to specify |
| 43 |
* column names to return generated keys from |
| 44 |
* @return mode for the specified generated keys request |
| 45 |
*/ |
| 46 |
public static int valueOf(Object generatedKeysRequest) { |
| 47 |
if (generatedKeysRequest == null || Boolean.FALSE.equals(generatedKeysRequest)) { |
| 48 |
return NONE; |
| 49 |
} |
| 50 |
if (Boolean.TRUE.equals(generatedKeysRequest)) { |
| 51 |
return AUTO; |
| 52 |
} |
| 53 |
if (generatedKeysRequest instanceof int[]) { |
| 54 |
return ((int[]) generatedKeysRequest).length > 0 ? COLUMN_NUMBERS : NONE; |
| 55 |
} |
| 56 |
if (generatedKeysRequest instanceof String[]) { |
| 57 |
return ((String[]) generatedKeysRequest).length > 0 ? COLUMN_NAMES : NONE; |
| 58 |
} |
| 59 |
throw DbException.getInternalError(); |
| 60 |
} |
| 61 |
|
| 62 |
private GeneratedKeysMode() { |
| 63 |
} |
| 64 |
|
| 65 |
} |