/* * Copyright (C) SapphireCode - All Rights Reserved * This file is part of Snippeteer which is released under BSD-3-Clause. * See file 'LICENSE' for full license details. * Created by Timo Hocker , May 2020 */ import { Serializable } from '../classes/Serializable'; import { PatchAction } from '../classes/PatchAction'; import { Database } from '../classes/Database'; export class RenameColumn implements Serializable, PatchAction { public table: string; public column: string; public new_name: string; public constructor ( column: string, new_name: string|null = null, table: string|null = null ) { if (new_name === null || table === null) { const regex = /(?[a-z_]+) (?[a-z_]+) (?[a-z_]+)/iu; const res = regex.exec (column); if (res === null || typeof res.groups === 'undefined') throw new Error ('invalid string to deserialize'); this.column = res.groups.column; this.new_name = res.groups.new_name; this.table = res.groups.table; return; } this.column = column; this.table = table; this.new_name = new_name; } public serialize (): string { return `${this.table} ${this.column} ${this.new_name}`; } public apply (db: Database): void { const table = db.get_table (this.table); if (typeof table === 'undefined' || table === null) { throw new Error ( `table ${this.table} not found` ); } const column = table.get_column (this.column); if (typeof column === 'undefined' || column === null) { throw new Error ( `column ${this.column} not found in table ${this.table}` ); } column.name = this.new_name; } }