use of com.actiontech.dble.route.parser.util.Pair in project dble by actiontech.
the class MySQLConnection method getSetSQL.
private String getSetSQL(Map<String, String> usrVars, Map<String, String> sysVars, Set<String> toResetSys) {
// new final var
List<Pair<String, String>> setVars = new ArrayList<>();
Map<String, String> tmpSysVars = new HashMap<>();
// tmp add all backend sysVariables
tmpSysVars.putAll(sysVariables);
// for all front end sysVariables
for (Map.Entry<String, String> entry : sysVars.entrySet()) {
if (!tmpSysVars.containsKey(entry.getKey())) {
setVars.add(new Pair<>(entry.getKey(), entry.getValue()));
} else {
String value = tmpSysVars.remove(entry.getKey());
// if backend is not equal frontend, need to reset
if (!StringUtil.equalsIgnoreCase(entry.getValue(), value)) {
setVars.add(new Pair<>(entry.getKey(), entry.getValue()));
}
}
}
// tmp now = backend -(backend &&frontend)
for (Map.Entry<String, String> entry : tmpSysVars.entrySet()) {
String value = DbleServer.getInstance().getSystemVariables().getDefaultValue(entry.getKey());
try {
BigDecimal vl = new BigDecimal(value);
} catch (NumberFormatException e) {
value = "`" + value + "`";
}
setVars.add(new Pair<>(entry.getKey(), value));
toResetSys.add(entry.getKey());
}
for (Map.Entry<String, String> entry : usrVars.entrySet()) {
if (!usrVariables.containsKey(entry.getKey())) {
setVars.add(new Pair<>(entry.getKey(), entry.getValue()));
} else {
if (!StringUtil.equalsIgnoreCase(entry.getValue(), usrVariables.get(entry.getKey()))) {
setVars.add(new Pair<>(entry.getKey(), entry.getValue()));
}
}
}
if (setVars.size() == 0)
return null;
StringBuilder sb = new StringBuilder("set ");
int cnt = 0;
for (Pair<String, String> var : setVars) {
if (cnt > 0) {
sb.append(",");
}
sb.append(var.getKey());
sb.append("=");
sb.append(var.getValue());
cnt++;
}
sb.append(";");
return sb.toString();
}
use of com.actiontech.dble.route.parser.util.Pair in project dble by actiontech.
the class ShowDataNode method execute.
public static void execute(ManagerConnection c, String name) {
ByteBuffer buffer = c.allocate();
// write header
buffer = HEADER.write(buffer, c, true);
// write fields
for (FieldPacket field : FIELDS) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = EOF.write(buffer, c, true);
// write rows
byte packetId = EOF.getPacketId();
ServerConfig conf = DbleServer.getInstance().getConfig();
Map<String, PhysicalDBNode> dataNodes = conf.getDataNodes();
List<String> keys = new ArrayList<>();
if (StringUtil.isEmpty(name)) {
keys.addAll(dataNodes.keySet());
} else {
SchemaConfig sc = conf.getSchemas().get(name);
if (null != sc) {
keys.addAll(sc.getAllDataNodes());
}
}
Collections.sort(keys, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
Pair<String, Integer> p1 = PairUtil.splitIndex(o1, '[', ']');
Pair<String, Integer> p2 = PairUtil.splitIndex(o2, '[', ']');
if (p1.getKey().compareTo(p2.getKey()) == 0) {
return p1.getValue() - p2.getValue();
} else {
return p1.getKey().compareTo(p2.getKey());
}
}
});
for (String key : keys) {
RowDataPacket row = getRow(dataNodes.get(key), c.getCharset().getResults());
if (row != null) {
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.setPacketId(++packetId);
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
use of com.actiontech.dble.route.parser.util.Pair in project dble by actiontech.
the class SetHandler method handleSingleVariable.
private static boolean handleSingleVariable(String stmt, SQLAssignItem assignItem, ServerConnection c, List<Pair<KeyType, Pair<String, String>>> contextTask) {
String key = handleSetKey(assignItem, c);
if (key == null)
return false;
SQLExpr valueExpr = assignItem.getValue();
KeyType keyType = parseKeyType(key, true, KeyType.SYSTEM_VARIABLES);
if (!checkValue(valueExpr, keyType)) {
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, "setting target is not supported for '" + SQLUtils.toMySqlString(assignItem.getValue()) + "'");
return false;
}
switch(keyType) {
case XA:
return handleSingleXA(c, valueExpr);
case AUTOCOMMIT:
return handleSingleAutocommit(stmt, c, valueExpr);
case CHARACTER_SET_CLIENT:
return handleSingleCharsetClient(c, valueExpr);
case CHARACTER_SET_CONNECTION:
return handleSingleCharsetConnection(c, valueExpr);
case CHARACTER_SET_RESULTS:
return handleSingleCharsetResults(c, valueExpr);
case COLLATION_CONNECTION:
return handleCollationConnection(c, valueExpr);
case TX_READ_ONLY:
if (!stmt.toLowerCase().contains("session")) {
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, "setting transaction without any SESSION or GLOBAL keyword is not supported now");
return false;
}
return handleTxReadOnly(c, valueExpr);
case TX_ISOLATION:
if (!stmt.toLowerCase().contains("session")) {
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, "setting transaction without any SESSION or GLOBAL keyword is not supported now");
return false;
}
return handleTxIsolation(c, valueExpr);
case SYSTEM_VARIABLES:
if (key.startsWith("@@")) {
key = key.substring(2);
}
if (DbleServer.getInstance().getSystemVariables().getDefaultValue(key) == null) {
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, "system variable " + key + " is not supported");
return false;
}
contextTask.add(new Pair<>(KeyType.SYSTEM_VARIABLES, new Pair<>(key, parseVariablesValue(valueExpr))));
return true;
case USER_VARIABLES:
contextTask.add(new Pair<>(KeyType.USER_VARIABLES, new Pair<>(key.toUpperCase(), parseVariablesValue(valueExpr))));
return true;
default:
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, stmt + " is not supported");
return false;
}
}
use of com.actiontech.dble.route.parser.util.Pair in project dble by actiontech.
the class SetHandler method handleVariableInMultiStmt.
private static boolean handleVariableInMultiStmt(SQLAssignItem assignItem, ServerConnection c, List<Pair<KeyType, Pair<String, String>>> contextTask) {
String key = handleSetKey(assignItem, c);
if (key == null) {
return false;
}
SQLExpr valueExpr = assignItem.getValue();
KeyType keyType = parseKeyType(key, true, KeyType.SYSTEM_VARIABLES);
if (!checkValue(valueExpr, keyType)) {
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, "setting target is not supported for '" + assignItem.getValue() + "'");
return false;
}
switch(keyType) {
case XA:
c.writeErrMessage(ErrorCode.ERR_WRONG_USED, "set xa cmd can't used in multi-set statement");
return false;
case AUTOCOMMIT:
c.writeErrMessage(ErrorCode.ERR_WRONG_USED, "set autocommit cmd can't used in multi-set statement");
return false;
case NAMES:
{
String charset = parseStringValue(valueExpr);
// TODO:druid lost collation info
if (!handleSetNamesInMultiStmt(c, "SET NAMES " + charset, false, charset, null, contextTask))
return false;
break;
}
case CHARSET:
{
String charset = parseStringValue(valueExpr);
if (!handleCharsetInMultiStmt(c, false, charset, contextTask))
return false;
break;
}
case CHARACTER_SET_CLIENT:
if (!handleCharsetClientInMultiStmt(c, contextTask, valueExpr))
return false;
break;
case CHARACTER_SET_CONNECTION:
if (!handleCharsetConnInMultiStmt(c, contextTask, valueExpr))
return false;
break;
case CHARACTER_SET_RESULTS:
if (!handleCharsetResultsInMultiStmt(c, contextTask, valueExpr))
return false;
break;
case COLLATION_CONNECTION:
if (!handleCollationConnInMultiStmt(c, contextTask, valueExpr))
return false;
break;
case TX_READ_ONLY:
if (!handleReadOnlyInMultiStmt(c, contextTask, valueExpr))
return false;
break;
case TX_ISOLATION:
if (!handleTxIsolationInMultiStmt(c, contextTask, valueExpr))
return false;
break;
case SYSTEM_VARIABLES:
if (key.startsWith("@@")) {
key = key.substring(2);
}
if (DbleServer.getInstance().getSystemVariables().getDefaultValue(key) == null) {
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, "system variable " + key + " is not supported");
}
contextTask.add(new Pair<>(KeyType.SYSTEM_VARIABLES, new Pair<>(key, parseVariablesValue(valueExpr))));
break;
case USER_VARIABLES:
contextTask.add(new Pair<>(KeyType.USER_VARIABLES, new Pair<>(key.toUpperCase(), parseVariablesValue(valueExpr))));
break;
default:
c.writeErrMessage(ErrorCode.ERR_NOT_SUPPORTED, key + " is not supported");
return false;
}
return true;
}
Aggregations