Skip to content
Permalink
Browse files
Add sql query api for sql editor (#20)
Add sql query api for sql editor
  • Loading branch information
songchuangyuan committed Mar 25, 2022
1 parent 004c5e4 commit cee3117f2b74901f5b31370795e7d9d9b3eee08f
Showing 8 changed files with 411 additions and 25 deletions.
@@ -19,6 +19,7 @@

import org.apache.doris.stack.controller.BaseController;
import org.apache.doris.stack.entity.CoreUserEntity;
import org.apache.doris.stack.model.request.construct.SqlQueryReq;
import org.apache.doris.stack.rest.ResponseEntityBuilder;
import org.apache.doris.stack.service.user.AuthenticationService;
import org.apache.doris.stack.service.construct.NativeQueryService;
@@ -58,4 +59,14 @@ public Object sqlQuery(
CoreUserEntity user = authenticationService.checkNewUserAuthWithCookie(request, response);
return ResponseEntityBuilder.ok(nativeQueryService.executeSql(nsId, dbId, stmtBody, user));
}

@ApiOperation(value = "Execute SQL query")
@PostMapping(value = "sql", produces = MediaType.APPLICATION_JSON_VALUE)
public Object query(HttpServletRequest request,
HttpServletResponse response,
@RequestBody SqlQueryReq queryReq) throws Exception {
log.debug("query request.");
CoreUserEntity user = authenticationService.checkNewUserAuthWithCookie(request, response);
return ResponseEntityBuilder.ok(nativeQueryService.querySql(user, queryReq));
}
}
@@ -0,0 +1,87 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.stack.driver;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class DorisFieldTypeDef {

public static final String TYPE_INTEGER = "type/Integer";

public static final String TYPE_BIGINTEGER = "type/BigInteger";

public static final String TYPE_FLOAT = "type/Float";

public static final String TYPE_DECIMAL = "type/Decimal";

public static final String TYPE_TEXT = "type/Text";

public static final String TYPE_DATETIME = "type/DateTime";

public static final String TYPE_DATE = "type/Date";

public static final String TYPE_BOOLEAN = "type/Boolean";

public static final String TYPE_ALL = "type/*";

public static Map<String, String> dorisFieldTypeMap = new HashMap<>();

// Doris engine field type definition and mapping
public static Set<String> dorisFieldType = new HashSet<>();

static {
// Doris engine field type definition and mapping with structured field types
dorisFieldTypeMap.put("TINYINT", TYPE_ALL);
dorisFieldTypeMap.put("DATE", TYPE_DATE);
dorisFieldTypeMap.put("INT", TYPE_INTEGER);
dorisFieldTypeMap.put("SMALLINT", TYPE_INTEGER);
dorisFieldTypeMap.put("VARCHAR", TYPE_TEXT);
dorisFieldTypeMap.put("DATETIME", TYPE_DATETIME);
dorisFieldTypeMap.put("BOOLEAN", TYPE_BOOLEAN);
dorisFieldTypeMap.put("BIT", TYPE_BOOLEAN);
dorisFieldTypeMap.put("DOUBLE", TYPE_FLOAT);
dorisFieldTypeMap.put("FLOAT", TYPE_FLOAT);
dorisFieldTypeMap.put("DECIMAL", TYPE_DECIMAL);
dorisFieldTypeMap.put("BIGINT", TYPE_BIGINTEGER);
dorisFieldTypeMap.put("LARGEINT", TYPE_TEXT); // TODO:At present, largeint is set as text type to solve
// the problem of precision
dorisFieldTypeMap.put("BITMAP", TYPE_ALL);
dorisFieldTypeMap.put("CHAR", TYPE_TEXT);
dorisFieldTypeMap.put("HLL", TYPE_ALL); // TODO:Currently set HLL as text type
// Field type of Doris engine
dorisFieldType.addAll(dorisFieldTypeMap.keySet());
}

/**
* Get Doris mapping data type
*
* @param dorisType dorisType
* @return dorisType
*/
public static String getDorisFieldType(String dorisType) {
String type = dorisType.replaceAll("[^a-zA-Z]", "");
String result = dorisFieldTypeMap.get(type);
if (result == null) {
return TYPE_ALL;
}
return result;
}
}
@@ -94,6 +94,26 @@ public Statement getStatement(String host, int port, String user, String passwd)
}
}

public Statement getStatement(String host, int port, String user, String passwd, String db) throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("jdbc:mysql://");
buffer.append(host);
buffer.append(":");
buffer.append(port);
buffer.append("/");
buffer.append(db);
String url = buffer.toString();
try {
log.info("Get connection by url:{}", url);
Connection myCon = DriverManager.getConnection(url, user, passwd);
Statement stmt = myCon.createStatement();
return stmt;
} catch (Exception e) {
log.error("Get doris jdbc connection error {}.", e);
throw e;
}
}

public void updateUserPassword(String user, String newPassword, Statement stmt) throws Exception {
try {
String sql = "SET PASSWORD FOR '" + user
@@ -182,24 +202,17 @@ public Set<String> getFeOrBeIps(Statement stmt, String feOrBeInfo) throws Except
}
}

// public List<String> getBeIps(Statement stmt) throws Exception {
// try {
// String sql = "SHOW PROC '/backends';";
//
// ResultSet result = stmt.executeQuery(sql);
// List<String> ips = new ArrayList<>();
// while (result.next()) {
// boolean isAlive = result.getBoolean("Alive");
// if (isAlive) {
// ips.add(result.getString("IP"));
// }
// }
// return ips;
// } catch (Exception e) {
// log.error("get be ip by jdbc error {}.", e);
// throw e;
// }
// }
// TODO: At present, only the simplest SQL query is implemented, there is no cache connection,
// and multiple SQL statements are not supported
public ResultSet executeSql(Statement stmt, String sql) throws Exception {
try {
ResultSet result = stmt.executeQuery(sql);
return result;
} catch (Exception e) {
log.error("execute sql error {}.", e);
throw e;
}
}

public void closeStatement(Statement stmt) {
try {
@@ -0,0 +1,34 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.stack.driver;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class SimpleColumn {
private String displayName;

private String name;

// baseType, The unified type of front-end structured display field
private String baseType;
}
@@ -0,0 +1,35 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.stack.driver;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SqlResultData {
// Each row of returned result data is a list < Object >, which may be of different types.
// It is determined according to each column defined in cols
private List<List<Object>> rows;

private List<SimpleColumn> cols;
}
@@ -0,0 +1,31 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.stack.model.request.construct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SqlQueryReq {
private int database;

private String query;
}

0 comments on commit cee3117

Please sign in to comment.