ShowDbStmt.java
// 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.analysis;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.InfoSchemaDb;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.UserException;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.qe.ShowResultSetMetaData;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
// Show database statement.
public class ShowDbStmt extends ShowStmt implements NotFallbackInParser {
private static final TableName TABLE_NAME = new TableName(InternalCatalog.INTERNAL_CATALOG_NAME,
InfoSchemaDb.DATABASE_NAME, "schemata");
private static final String DB_COL = "Database";
private static final ShowResultSetMetaData META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column(DB_COL, ScalarType.createVarchar(20)))
.build();
private String pattern;
private String catalogName;
private Expr where;
private SelectStmt selectStmt;
public ShowDbStmt(String pattern) {
this.pattern = pattern;
}
public ShowDbStmt(String pattern, Expr where, String catalogName) {
this.pattern = pattern;
this.where = where;
this.catalogName = catalogName;
}
public String getPattern() {
return pattern;
}
public String getCatalogName() {
return catalogName;
}
@Override
public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
super.analyze(analyzer);
this.catalogName = this.catalogName == null ? analyzer.getDefaultCatalog() : this.catalogName;
}
@Override
public SelectStmt toSelectStmt(Analyzer analyzer) {
if (where == null) {
return null;
}
if (selectStmt != null) {
return selectStmt;
}
// Columns
SelectList selectList = new SelectList();
ExprSubstitutionMap aliasMap = new ExprSubstitutionMap(false);
TableName tableName = new TableName(catalogName, InfoSchemaDb.DATABASE_NAME, "schemata");
SelectListItem item = new SelectListItem(new SlotRef(tableName, "SCHEMA_NAME"), DB_COL);
selectList.addItem(item);
aliasMap.put(new SlotRef(null, DB_COL), item.getExpr().clone(null));
where = where.substitute(aliasMap);
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(tableName, null))),
where, null, null, null, LimitElement.NO_LIMIT);
analyzer.setSchemaInfo(null, null, catalogName);
return selectStmt;
}
@Override
public String toSql() {
StringBuilder sb = new StringBuilder("SHOW DATABASES");
if (pattern != null) {
sb.append(" LIKE '").append(pattern).append("'");
}
if (!Strings.isNullOrEmpty(catalogName) && !InternalCatalog.INTERNAL_CATALOG_NAME.equals(catalogName)) {
sb.append(" FROM ").append(catalogName);
}
return sb.toString();
}
@Override
public String toString() {
return toSql();
}
@Override
public ShowResultSetMetaData getMetaData() {
return META_DATA;
}
}