release: v0.2.5 — 质量加固 + Bug修复 + 性能优化 + SQL扩展
This commit is contained in:
@@ -16,6 +16,8 @@ import type {
|
||||
DeleteStatement,
|
||||
CreateTableStatement,
|
||||
DropTableStatement,
|
||||
AlterTableStatement,
|
||||
TruncateTableStatement,
|
||||
ASTColumnDef,
|
||||
} from '../query/ast';
|
||||
import type { WhereCondition, OrderBy, SortDirection } from '../constants';
|
||||
@@ -52,6 +54,10 @@ export class Parser {
|
||||
return this.parseCreateTable();
|
||||
case TokenType.DROP:
|
||||
return this.parseDropTable();
|
||||
case TokenType.ALTER:
|
||||
return this.parseAlterTable();
|
||||
case TokenType.TRUNCATE:
|
||||
return this.parseTruncateTable();
|
||||
default:
|
||||
throw this.error(`Unexpected token "${this.curToken.value}"`);
|
||||
}
|
||||
@@ -426,6 +432,52 @@ export class Parser {
|
||||
return 'RESTRICT';
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// ALTER TABLE
|
||||
// ===================================================================
|
||||
|
||||
private parseAlterTable(): AlterTableStatement {
|
||||
this.expect(TokenType.ALTER);
|
||||
this.expect(TokenType.TABLE);
|
||||
const tableName = this.expectIdentifier('table name');
|
||||
|
||||
// ADD COLUMN / DROP COLUMN
|
||||
let action: 'ADD' | 'DROP';
|
||||
if (this.curTokenIs(TokenType.ADD)) {
|
||||
action = 'ADD';
|
||||
this.nextToken();
|
||||
// Optional COLUMN keyword
|
||||
if (this.curToken.type === TokenType.IDENTIFIER && this.curToken.value.toUpperCase() === 'COLUMN') {
|
||||
this.nextToken();
|
||||
}
|
||||
const col = this.parseColumnDef();
|
||||
return { type: 'ALTER_TABLE', name: tableName, action, column: col };
|
||||
} else if (this.curTokenIs(TokenType.DROP) ||
|
||||
(this._isKeywordAsIdent() && this.curToken.value.toUpperCase() === 'DROP')) {
|
||||
action = 'DROP';
|
||||
this.nextToken();
|
||||
// Optional COLUMN keyword
|
||||
if (this.curToken.type === TokenType.IDENTIFIER && this.curToken.value.toUpperCase() === 'COLUMN') {
|
||||
this.nextToken();
|
||||
}
|
||||
const colName = this.expectIdentifier('column name');
|
||||
return { type: 'ALTER_TABLE', name: tableName, action, column: { name: colName, type: 'string' } };
|
||||
} else {
|
||||
throw this.error('Expected ADD or DROP in ALTER TABLE');
|
||||
}
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// TRUNCATE TABLE
|
||||
// ===================================================================
|
||||
|
||||
private parseTruncateTable(): TruncateTableStatement {
|
||||
this.expect(TokenType.TRUNCATE);
|
||||
this.expect(TokenType.TABLE);
|
||||
const tableName = this.expectIdentifier('table name');
|
||||
return { type: 'TRUNCATE_TABLE', name: tableName };
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// DROP TABLE
|
||||
// ===================================================================
|
||||
|
||||
@@ -44,6 +44,9 @@ export enum TokenType {
|
||||
IF = 'IF',
|
||||
EXISTS = 'EXISTS',
|
||||
FALSE = 'FALSE',
|
||||
ALTER = 'ALTER',
|
||||
ADD = 'ADD',
|
||||
TRUNCATE = 'TRUNCATE',
|
||||
|
||||
// JOIN 相关
|
||||
INNER = 'INNER',
|
||||
@@ -139,6 +142,9 @@ export const KEYWORDS: Record<string, TokenType> = {
|
||||
'BETWEEN': TokenType.BETWEEN,
|
||||
'IF': TokenType.IF,
|
||||
'EXISTS': TokenType.EXISTS,
|
||||
'ALTER': TokenType.ALTER,
|
||||
'ADD': TokenType.ADD,
|
||||
'TRUNCATE': TokenType.TRUNCATE,
|
||||
|
||||
// JOIN
|
||||
'INNER': TokenType.INNER,
|
||||
|
||||
Reference in New Issue
Block a user