Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feature: 增加了一个新的关键词:show tables,可以查看当前数据库中所有的表!
Browse files Browse the repository at this point in the history
  • Loading branch information
01Petard committed Sep 16, 2024
1 parent b8c8f4d commit ece32cb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/main/java/top/guoziyang/mydb/backend/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ public static Object Parse(byte[] statement) throws Exception {
return stat;
}

private static Show parseShow(Tokenizer tokenizer) throws Exception {
tokenizer.pop();
if ("tables".equals(tokenizer.peek())) {
tokenizer.pop();
private static Object parseShow(Tokenizer tokenizer) throws Exception {
if ("".equals(tokenizer.peek())) {
return new Show();
} else if ("tables".equals(tokenizer.peek())) {
tokenizer.pop();
return new ShowTables();
}
throw Error.InvalidCommandException;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package top.guoziyang.mydb.backend.parser.statement;

public class ShowTables {
}
2 changes: 2 additions & 0 deletions src/main/java/top/guoziyang/mydb/backend/server/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ private byte[] execute2(Object stat) throws Exception {
byte[] res = null;
if (stat instanceof Show) {
res = tbm.show(xid);
} else if (stat instanceof ShowTables) {
res = tbm.showTables(xid);
} else if (stat instanceof Create) {
res = tbm.create(xid, (Create) stat);
} else if (stat instanceof Select) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/top/guoziyang/mydb/backend/tbm/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;

/**
* 表示字段信息
* 管理表和字段的结构数据,例如表名、表字段信息和字段索引等
* 二进制格式为:
* [FieldName][TypeName][IndexUid]
* 如果field无索引,IndexUid为0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import top.guoziyang.mydb.backend.utils.Parser;
import top.guoziyang.mydb.backend.vm.VersionManager;

/**
* 表管理器接口
*/
public interface TableManager {
/**
* 创建一个新的表管理器实例
Expand Down Expand Up @@ -43,6 +46,9 @@ static TableManager open(String path, VersionManager vm, DataManager dm) {
// 显示事务的状态
byte[] show(long xid);

// 查看数据库中的所有表
byte[] showTables(long xid) throws Exception;

// 创建一个新的表
byte[] create(long xid, Create create) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* 直接被最外层 Server 调用,直接返回执行的结果
*/
public class TableManagerImpl implements TableManager {
VersionManager vm;
DataManager dm;
Expand All @@ -32,7 +35,7 @@ public class TableManagerImpl implements TableManager {
}

/**
* 启动时
* 启动时
*/
private void loadTables() {
long uid = firstTableUid();
Expand Down Expand Up @@ -95,6 +98,24 @@ public byte[] show(long xid) {
}
}

@Override
public byte[] showTables(long xid) throws Exception {
lock.lock();
try {
StringBuilder sb = new StringBuilder();
if (tableCache.isEmpty()) {
return "No tables found.".getBytes(); // 如果没有表,返回提示信息
}
for (String tableName : tableCache.keySet()) {
sb.append(tableName).append("\n"); // 将每个表名追加到字符串
}
return sb.toString().getBytes(); // 返回字节数组
} finally {
lock.unlock();
}
}


@Override
public byte[] create(long xid, Create create) throws Exception {
lock.lock();
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/top/guoziyang/mydb/backend/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public void testDelete() throws Exception {

@Test
public void testShow() throws Exception {
String stat = "show tables";
String stat = "show";
Object res = Parser.Parse(stat.getBytes());
Show show = (Show)res;
Gson gson = new Gson();
System.out.println("Show Tables");
System.out.println("Show");
System.out.println(gson.toJson(show));
System.out.println("======================");
}
Expand All @@ -98,6 +98,17 @@ public void testUpdate() throws Exception {
System.out.println("======================");
}

@Test
public void testShowTable() throws Exception {
String stat = "show tables";
Object res = Parser.Parse(stat.getBytes());
ShowTables showTables = (ShowTables)res;
Gson gson = new Gson();
System.out.println("Show Tables");
System.out.println(gson.toJson(showTables));
System.out.println("======================");
}

@Test
public void testHelp() throws Exception {
String stat = "help";
Expand Down

0 comments on commit ece32cb

Please sign in to comment.