-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
762ab9b
commit 159db68
Showing
9 changed files
with
267 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { CreateTableAsSelect } from "./CreateTableAsSelect"; | ||
import { Cond } from "./Condition"; | ||
import { Q } from "./Query"; | ||
import { MySQLFlavor } from "./flavors/mysql"; | ||
import { MetadataOperationType } from "./interfaces"; | ||
|
||
describe("CreateTableAsSelect", () => { | ||
const initialSelectQuery = Q.select() | ||
.from("users", "u") | ||
.where(Cond.equal("u.id", 1)); | ||
const tableName = "new_users_table"; | ||
|
||
it("should clone itself correctly", () => { | ||
const ctas = Q.createTableAs(tableName, initialSelectQuery); | ||
const clone = ctas.clone(); | ||
|
||
expect(clone).not.toBe(ctas); | ||
expect(clone.toSQL(new MySQLFlavor())).toBe(ctas.toSQL(new MySQLFlavor())); | ||
}); | ||
|
||
it("should generate the correct SQL", () => { | ||
const ctas = Q.createTableAs(tableName, initialSelectQuery); | ||
const expectedSQL = `CREATE TABLE \`${tableName}\` AS SELECT * FROM \`users\` AS \`u\` WHERE \`u\`.\`id\` = 1`; | ||
expect(ctas.toSQL(new MySQLFlavor())).toBe(expectedSQL); | ||
}); | ||
|
||
it("should serialize and deserialize correctly", () => { | ||
const ctas = Q.createTableAs(tableName, initialSelectQuery); | ||
const serialized = ctas.serialize(); | ||
const deserialized = Q.deserialize(serialized); | ||
|
||
expect(deserialized).toEqual(ctas); | ||
expect(deserialized.toSQL(new MySQLFlavor())).toBe( | ||
ctas.toSQL(new MySQLFlavor()) | ||
); | ||
}); | ||
|
||
it("should fetch table names correctly", () => { | ||
const ctas = Q.createTableAs(tableName, initialSelectQuery); | ||
expect(ctas.getTableNames()).toEqual([tableName, "users"]); | ||
}); | ||
|
||
it("should return correct operation type", () => { | ||
const ctas = Q.createTableAs(tableName, initialSelectQuery); | ||
expect(ctas.getOperationType()).toEqual( | ||
MetadataOperationType.CREATE_TABLE_AS | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Condition, ConditionValue } from "./Condition"; | ||
import { ISQLFlavor } from "./Flavor"; | ||
import { Q, SelectQuery, Table } from "./Query"; | ||
import { MySQLFlavor } from "./flavors/mysql"; | ||
import { | ||
IMetadata, | ||
ISequelizable, | ||
ISerializable, | ||
MetadataOperationType, | ||
} from "./interfaces"; | ||
|
||
export class CreateTableAsSelect | ||
implements ISerializable, ISequelizable, IMetadata | ||
{ | ||
constructor(private _tableName: string, private _select: SelectQuery) {} | ||
|
||
public clone(): this { | ||
return new (this.constructor as any)(this._tableName, this._select.clone()); | ||
} | ||
|
||
getOperationType(): MetadataOperationType { | ||
return MetadataOperationType.CREATE_TABLE_AS; | ||
} | ||
|
||
getTableNames(): string[] { | ||
return [this._tableName, ...this._select.getTableNames()]; | ||
} | ||
|
||
toSQL(flavor: ISQLFlavor = new MySQLFlavor()): string { | ||
return `CREATE TABLE ${flavor.escapeTable( | ||
this._tableName | ||
)} AS ${this._select.toSQL(flavor)}`; | ||
} | ||
|
||
serialize(): string { | ||
return JSON.stringify(this.toJSON()); | ||
} | ||
|
||
toJSON() { | ||
return { | ||
type: MetadataOperationType.CREATE_TABLE_AS, | ||
select: this._select.toJSON(), | ||
tableName: this._tableName, | ||
}; | ||
} | ||
|
||
static fromJSON({ tableName, select }: any): CreateTableAsSelect { | ||
return new CreateTableAsSelect(tableName, SelectQuery.fromJSON(select)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { CreateViewAsSelect } from "./CreateViewAsSelect"; // Adjust the import path as needed | ||
import { Cond } from "./Condition"; | ||
import { Q } from "./Query"; | ||
import { MySQLFlavor } from "./flavors/mysql"; | ||
import { MetadataOperationType } from "./interfaces"; | ||
|
||
describe("CreateViewAsSelect", () => { | ||
const initialSelectQuery = Q.select() | ||
.from("users", "u") | ||
.where(Cond.equal("u.id", 1)); | ||
const viewName = "user_view"; | ||
|
||
it("should clone itself correctly", () => { | ||
const cvas = Q.createOrReaplaceViewAs(viewName, initialSelectQuery); | ||
const clone = cvas.clone(); | ||
|
||
expect(clone).not.toBe(cvas); | ||
expect(clone.toSQL(new MySQLFlavor())).toBe(cvas.toSQL(new MySQLFlavor())); | ||
}); | ||
|
||
it("should generate the correct SQL with OR REPLACE", () => { | ||
const cvas = Q.createOrReaplaceViewAs(viewName, initialSelectQuery); | ||
const expectedSQL = `CREATE OR REPLACE VIEW \`${viewName}\` AS SELECT * FROM \`users\` AS \`u\` WHERE \`u\`.\`id\` = 1`; | ||
expect(cvas.toSQL(new MySQLFlavor())).toBe(expectedSQL); | ||
}); | ||
|
||
it("should generate the correct SQL without OR REPLACE", () => { | ||
const cvas = Q.createViewAs(viewName, initialSelectQuery); | ||
const expectedSQL = `CREATE VIEW \`${viewName}\` AS SELECT * FROM \`users\` AS \`u\` WHERE \`u\`.\`id\` = 1`; | ||
expect(cvas.toSQL(new MySQLFlavor())).toBe(expectedSQL); | ||
}); | ||
|
||
it("should serialize and deserialize correctly", () => { | ||
const cvas = Q.createTableAs(viewName, initialSelectQuery); | ||
const serialized = cvas.serialize(); | ||
const deserialized = Q.deserialize(serialized); | ||
|
||
expect(deserialized.toSQL(new MySQLFlavor())).toEqual( | ||
cvas.toSQL(new MySQLFlavor()) | ||
); | ||
}); | ||
|
||
it("should fetch table names correctly", () => { | ||
const cvas = Q.createViewAs(viewName, initialSelectQuery); | ||
expect(cvas.getTableNames()).toEqual([viewName, "users"]); | ||
}); | ||
|
||
it("should return correct operation type", () => { | ||
const cvas = Q.createViewAs(viewName, initialSelectQuery); | ||
expect(cvas.getOperationType()).toEqual( | ||
MetadataOperationType.CREATE_VIEW_AS | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { SelectQuery } from "./Query"; | ||
import { ISQLFlavor } from "./Flavor"; | ||
import { MySQLFlavor } from "./flavors/mysql"; | ||
import { | ||
IMetadata, | ||
ISequelizable, | ||
ISerializable, | ||
MetadataOperationType, | ||
} from "./interfaces"; | ||
|
||
export class CreateViewAsSelect | ||
implements ISerializable, ISequelizable, IMetadata | ||
{ | ||
constructor( | ||
private _viewName: string, | ||
private _select: SelectQuery, | ||
private _orReplace: boolean = false | ||
) {} | ||
|
||
public clone(): this { | ||
return new (this.constructor as any)( | ||
this._viewName, | ||
this._select.clone(), | ||
this._orReplace | ||
); | ||
} | ||
|
||
getOperationType(): MetadataOperationType { | ||
return MetadataOperationType.CREATE_VIEW_AS; | ||
} | ||
|
||
getTableNames(): string[] { | ||
return [this._viewName, ...this._select.getTableNames()]; | ||
} | ||
|
||
toSQL(flavor: ISQLFlavor = new MySQLFlavor()): string { | ||
const orReplaceStr = this._orReplace ? "OR REPLACE " : ""; | ||
return `CREATE ${orReplaceStr}VIEW ${flavor.escapeTable( | ||
this._viewName | ||
)} AS ${this._select.toSQL(flavor)}`; | ||
} | ||
|
||
serialize(): string { | ||
return JSON.stringify(this.toJSON()); | ||
} | ||
|
||
toJSON() { | ||
return { | ||
type: MetadataOperationType.CREATE_VIEW_AS, | ||
select: this._select.toJSON(), | ||
viewName: this._viewName, | ||
orReplace: this._orReplace, | ||
}; | ||
} | ||
|
||
static fromJSON({ viewName, select, orReplace }: any): CreateViewAsSelect { | ||
return new CreateViewAsSelect( | ||
viewName, | ||
SelectQuery.fromJSON(select), | ||
orReplace | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.