use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class ServerLoadDataInfileHandler method buildResultSet.
private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
statement.setLocal(true);
// druid will filter path, reset it now
SQLLiteralExpr fn = new SQLCharExpr(fileName);
statement.setFileName(fn);
// replace IGNORE X LINES in SQL to avoid IGNORING X LINE in every node.
String srcStatement = this.ignoreLinesDelete(statement.toString());
RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
rrs.setLoadData(true);
rrs.setStatement(srcStatement);
rrs.setFinishedRoute(true);
int size = routeMap.size();
RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
int index = 0;
for (Map.Entry<String, LoadData> entry : routeMap.entrySet()) {
RouteResultsetNode rrNode = new RouteResultsetNode(entry.getKey(), ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
rrNode.setStatement(srcStatement);
LoadData newLoadData = new LoadData();
ObjectUtil.copyProperties(loadData, newLoadData);
newLoadData.setLocal(true);
LoadData loadData1 = entry.getValue();
if (loadData1.getFileName() != null) {
newLoadData.setFileName(loadData1.getFileName());
} else {
newLoadData.setData(loadData1.getData());
}
rrNode.setLoadData(newLoadData);
routeResultsetNodes[index] = rrNode;
index++;
}
rrs.setNodes(routeResultsetNodes);
return rrs;
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class ServerLoadDataInfileHandler method end.
@Override
public void end(byte packId) {
isStartLoadData = false;
this.packID = packId;
// empty packet for end
saveByteOrToFile(null, true);
List<SQLExpr> columns = statement.getColumns();
String tableSimpleName = statement.getTableName().getSimpleName();
if (isHasStoreToFile) {
parseFileByLine(tempFile, loadData.getCharset(), loadData.getLineTerminatedBy());
} else {
String content = new String(tempByteBuffer.toByteArray(), Charset.forName(loadData.getCharset()));
// List<String> lines = Splitter.on(loadData.getLineTerminatedBy()).omitEmptyStrings().splitToList(content);
CsvParserSettings settings = new CsvParserSettings();
settings.setMaxColumns(65535);
settings.setMaxCharsPerColumn(65535);
settings.getFormat().setLineSeparator(loadData.getLineTerminatedBy());
settings.getFormat().setDelimiter(loadData.getFieldTerminatedBy().charAt(0));
if (loadData.getEnclose() != null) {
settings.getFormat().setQuote(loadData.getEnclose().charAt(0));
}
if (loadData.getEscape() != null) {
settings.getFormat().setQuoteEscape(loadData.getEscape().charAt(0));
}
settings.getFormat().setNormalizedNewline(loadData.getLineTerminatedBy().charAt(0));
/*
* fix bug #1074 : LOAD DATA local INFILE导入的所有Boolean类型全部变成了false
* 不可见字符将在CsvParser被当成whitespace过滤掉, 使用settings.trimValues(false)来避免被过滤掉
* FIXME : 设置trimValues(false)之后, 会引起字段值前后的空白字符无法被过滤!
*/
settings.trimValues(false);
CsvParser parser = new CsvParser(settings);
try {
parser.beginParsing(new StringReader(content));
String[] row = null;
int ignoreNumber = 0;
if (statement.getIgnoreLinesNumber() != null && !"".equals(statement.getIgnoreLinesNumber().toString())) {
ignoreNumber = Integer.parseInt(statement.getIgnoreLinesNumber().toString());
}
while ((row = parser.parseNext()) != null) {
if (ignoreNumber == 0) {
parseOneLine(columns, tableSimpleName, row, true, loadData.getLineTerminatedBy());
} else {
ignoreNumber--;
}
}
} finally {
parser.stopParsing();
}
}
RouteResultset rrs = buildResultSet(routeResultMap);
if (rrs != null) {
flushDataToFile();
serverConnection.getSession2().execute(rrs);
}
// sendOk(++packID);
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class ServerLoadDataInfileHandler method parseOneLine.
private void parseOneLine(List<SQLExpr> columns, String table, String[] line, boolean toFile, String lineEnd) {
if (loadData.getEnclose() != null && loadData.getEnclose().charAt(0) > 0x0020) {
for (int i = 0; i < line.length; i++) {
line[i] = line[i].trim();
}
}
RouteResultset rrs = tryDirectRoute(sql, line);
if (rrs == null || rrs.getNodes() == null || rrs.getNodes().length == 0) {
String insertSql = makeSimpleInsert(columns, line, table);
rrs = serverConnection.routeSQL(insertSql, ServerParse.INSERT);
}
if (rrs == null || rrs.getNodes() == null || rrs.getNodes().length == 0) {
// do nothing
} else {
for (RouteResultsetNode routeResultsetNode : rrs.getNodes()) {
String name = routeResultsetNode.getName();
LoadData data = routeResultMap.get(name);
if (data == null) {
data = new LoadData();
data.setCharset(loadData.getCharset());
data.setEnclose(loadData.getEnclose());
data.setFieldTerminatedBy(loadData.getFieldTerminatedBy());
data.setLineTerminatedBy(loadData.getLineTerminatedBy());
data.setEscape(loadData.getEscape());
routeResultMap.put(name, data);
}
String jLine = joinField(line, data);
if (data.getData() == null) {
data.setData(Lists.newArrayList(jLine));
} else {
data.getData().add(jLine);
}
if (toFile && data.getData().size() > 10000) {
// avoid OOM
saveDataToFile(data, name);
}
}
}
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class ServerConnection method routeSQL.
public RouteResultset routeSQL(String sql, int type) {
String db = this.schema;
if (db == null) {
writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "No Database selected");
return null;
}
SchemaConfig schema = DbleServer.getInstance().getConfig().getSchemas().get(db);
if (schema == null) {
writeErrMessage(ErrorCode.ERR_BAD_LOGICDB, "Unknown Database '" + db + "'");
return null;
}
RouteResultset rrs;
try {
rrs = DbleServer.getInstance().getRouterService().route(schema, type, sql, this);
} catch (Exception e) {
executeException(e, sql);
return null;
}
return rrs;
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class ServerConnection method routeEndExecuteSQL.
private void routeEndExecuteSQL(String sql, int type, SchemaConfig schema) {
RouteResultset rrs;
try {
rrs = DbleServer.getInstance().getRouterService().route(schema, type, sql, this);
if (rrs == null) {
return;
}
if (rrs.getSqlType() == ServerParse.DDL) {
addTableMetaLock(rrs);
if (DbleServer.getInstance().getTmManager().getCatalogs().get(rrs.getSchema()).getView(rrs.getTable()) != null) {
DbleServer.getInstance().getTmManager().removeMetaLock(rrs.getSchema(), rrs.getTable());
String msg = "Table '" + rrs.getTable() + "' already exists as a view";
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
}
} catch (Exception e) {
executeException(e, sql);
return;
}
session.endRoute(rrs);
session.execute(rrs);
}
Aggregations