提交 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; ...@@ -9,6 +9,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.GeneratedKeysMode;
import org.h2.engine.SessionRemote; import org.h2.engine.SessionRemote;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.expression.ParameterInterface; import org.h2.expression.ParameterInterface;
...@@ -212,28 +213,28 @@ public class CommandRemote implements CommandInterface { ...@@ -212,28 +213,28 @@ public class CommandRemote implements CommandInterface {
transfer.writeInt(SessionRemote.COMMAND_EXECUTE_UPDATE).writeInt(id); transfer.writeInt(SessionRemote.COMMAND_EXECUTE_UPDATE).writeInt(id);
sendParameters(transfer); sendParameters(transfer);
if (supportsGeneratedKeys) { if (supportsGeneratedKeys) {
if (Boolean.FALSE.equals(generatedKeysRequest)) { int mode = GeneratedKeysMode.valueOf(generatedKeysRequest);
transfer.writeInt(0); transfer.writeInt(mode);
switch (mode) {
case GeneratedKeysMode.NONE:
readGeneratedKeys = false; readGeneratedKeys = false;
} else if (Boolean.TRUE.equals(generatedKeysRequest)) { break;
transfer.writeInt(1); case GeneratedKeysMode.COLUMN_NUMBERS: {
} else if (generatedKeysRequest instanceof int[]) {
int[] keys = (int[]) generatedKeysRequest; int[] keys = (int[]) generatedKeysRequest;
transfer.writeInt(2);
transfer.writeInt(keys.length); transfer.writeInt(keys.length);
for (int key : keys) { for (int key : keys) {
transfer.writeInt(key); transfer.writeInt(key);
} }
} else if (generatedKeysRequest instanceof String[]) { break;
}
case GeneratedKeysMode.COLUMN_NAMES: {
String[] keys = (String[]) generatedKeysRequest; String[] keys = (String[]) generatedKeysRequest;
transfer.writeInt(3);
transfer.writeInt(keys.length); transfer.writeInt(keys.length);
for (String key : keys) { for (String key : keys) {
transfer.writeString(key); transfer.writeString(key);
} }
} else { break;
transfer.writeInt(0); }
readGeneratedKeys = false;
} }
} }
session.done(transfer); 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; ...@@ -21,6 +21,7 @@ import org.h2.command.Command;
import org.h2.engine.ConnectionInfo; import org.h2.engine.ConnectionInfo;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.Engine; import org.h2.engine.Engine;
import org.h2.engine.GeneratedKeysMode;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.engine.SessionRemote; import org.h2.engine.SessionRemote;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
...@@ -358,16 +359,16 @@ public class TcpServerThread implements Runnable { ...@@ -358,16 +359,16 @@ public class TcpServerThread implements Runnable {
boolean writeGeneratedKeys = supportsGeneratedKeys; boolean writeGeneratedKeys = supportsGeneratedKeys;
Object generatedKeysRequest; Object generatedKeysRequest;
if (supportsGeneratedKeys) { if (supportsGeneratedKeys) {
int type = transfer.readInt(); int mode = transfer.readInt();
switch (type) { switch (mode) {
default: case GeneratedKeysMode.NONE:
generatedKeysRequest = false; generatedKeysRequest = false;
writeGeneratedKeys = false; writeGeneratedKeys = false;
break; break;
case 1: case GeneratedKeysMode.AUTO:
generatedKeysRequest = true; generatedKeysRequest = true;
break; break;
case 2: { case GeneratedKeysMode.COLUMN_NUMBERS: {
int len = transfer.readInt(); int len = transfer.readInt();
int[] keys = new int[len]; int[] keys = new int[len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
...@@ -376,14 +377,18 @@ public class TcpServerThread implements Runnable { ...@@ -376,14 +377,18 @@ public class TcpServerThread implements Runnable {
generatedKeysRequest = keys; generatedKeysRequest = keys;
break; break;
} }
case 3: { case GeneratedKeysMode.COLUMN_NAMES: {
int len = transfer.readInt(); int len = transfer.readInt();
String[] keys = new String[len]; String[] keys = new String[len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
keys[i] = transfer.readString(); keys[i] = transfer.readString();
} }
generatedKeysRequest = keys; generatedKeysRequest = keys;
break;
} }
default:
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,
"Unsupported generated keys' mode " + mode);
} }
} else { } else {
generatedKeysRequest = false; generatedKeysRequest = false;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论