提交 816461dd authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add constants for modes of generated keys' gathering

上级 d2867279
......@@ -9,6 +9,7 @@ import java.io.IOException;
import java.util.ArrayList;
import org.h2.engine.Constants;
import org.h2.engine.GeneratedKeysMode;
import org.h2.engine.SessionRemote;
import org.h2.engine.SysProperties;
import org.h2.expression.ParameterInterface;
......@@ -212,28 +213,28 @@ public class CommandRemote implements CommandInterface {
transfer.writeInt(SessionRemote.COMMAND_EXECUTE_UPDATE).writeInt(id);
sendParameters(transfer);
if (supportsGeneratedKeys) {
if (Boolean.FALSE.equals(generatedKeysRequest)) {
transfer.writeInt(0);
int mode = GeneratedKeysMode.valueOf(generatedKeysRequest);
transfer.writeInt(mode);
switch (mode) {
case GeneratedKeysMode.NONE:
readGeneratedKeys = false;
} else if (Boolean.TRUE.equals(generatedKeysRequest)) {
transfer.writeInt(1);
} else if (generatedKeysRequest instanceof int[]) {
break;
case GeneratedKeysMode.COLUMN_NUMBERS: {
int[] keys = (int[]) generatedKeysRequest;
transfer.writeInt(2);
transfer.writeInt(keys.length);
for (int key : keys) {
transfer.writeInt(key);
}
} else if (generatedKeysRequest instanceof String[]) {
break;
}
case GeneratedKeysMode.COLUMN_NAMES: {
String[] keys = (String[]) generatedKeysRequest;
transfer.writeInt(3);
transfer.writeInt(keys.length);
for (String key : keys) {
transfer.writeString(key);
}
} else {
transfer.writeInt(0);
readGeneratedKeys = false;
break;
}
}
}
session.done(transfer);
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.engine;
/**
* Modes of generated keys' gathering.
*/
public final class GeneratedKeysMode {
/**
* Generated keys are not needed.
*/
public static final int NONE = 0;
/**
* Generated keys should be configured automatically.
*/
public static final int AUTO = 1;
/**
* Use specified column indices to return generated keys from.
*/
public static final int COLUMN_NUMBERS = 2;
/**
* Use specified column names to return generated keys from.
*/
public static final int COLUMN_NAMES = 3;
/**
* Determines mode of generated keys' gathering.
*
* @param generatedKeysRequest
* {@code false} if generated keys are not needed, {@code true} if
* generated keys should be configured automatically, {@code int[]}
* to specify column indices to return generated keys from, or
* {@code String[]} to specify column names to return generated keys
* from
* @return mode for the specified generated keys request
*/
public static int valueOf(Object generatedKeysRequest) {
if (Boolean.FALSE.equals(generatedKeysRequest)) {
return NONE;
}
if (Boolean.TRUE.equals(generatedKeysRequest)) {
return AUTO;
}
if (generatedKeysRequest instanceof int[]) {
return COLUMN_NUMBERS;
}
if (generatedKeysRequest instanceof String[]) {
return COLUMN_NAMES;
}
return NONE;
}
private GeneratedKeysMode() {
}
}
......@@ -21,6 +21,7 @@ import org.h2.command.Command;
import org.h2.engine.ConnectionInfo;
import org.h2.engine.Constants;
import org.h2.engine.Engine;
import org.h2.engine.GeneratedKeysMode;
import org.h2.engine.Session;
import org.h2.engine.SessionRemote;
import org.h2.engine.SysProperties;
......@@ -358,16 +359,16 @@ public class TcpServerThread implements Runnable {
boolean writeGeneratedKeys = supportsGeneratedKeys;
Object generatedKeysRequest;
if (supportsGeneratedKeys) {
int type = transfer.readInt();
switch (type) {
default:
int mode = transfer.readInt();
switch (mode) {
case GeneratedKeysMode.NONE:
generatedKeysRequest = false;
writeGeneratedKeys = false;
break;
case 1:
case GeneratedKeysMode.AUTO:
generatedKeysRequest = true;
break;
case 2: {
case GeneratedKeysMode.COLUMN_NUMBERS: {
int len = transfer.readInt();
int[] keys = new int[len];
for (int i = 0; i < len; i++) {
......@@ -376,14 +377,18 @@ public class TcpServerThread implements Runnable {
generatedKeysRequest = keys;
break;
}
case 3: {
case GeneratedKeysMode.COLUMN_NAMES: {
int len = transfer.readInt();
String[] keys = new String[len];
for (int i = 0; i < len; i++) {
keys[i] = transfer.readString();
}
generatedKeysRequest = keys;
break;
}
default:
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,
"Unsupported generated keys' mode " + mode);
}
} else {
generatedKeysRequest = false;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论