-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
462 additions
and
109 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
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,3 @@ | ||
export * from './mysql-query-transformer' | ||
export * from './postgres-query-transformer' | ||
export * from './query-transformer' |
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,70 @@ | ||
import { QueryTransformer } from './query-transformer' | ||
|
||
export class MysqlQueryTransformer extends QueryTransformer { | ||
protected transformQuery(query: string, parameters: any[]): string { | ||
const quoteCharacters = ["'", '"'] | ||
let newQueryString = '' | ||
let currentQuote = null | ||
let srcIndex = 0 | ||
let destIndex = 0 | ||
|
||
for (let i = 0; i < query.length; i += 1) { | ||
const currentCharacter = query[i] | ||
const currentCharacterEscaped = i !== 0 && query[i - 1] === '\\' | ||
|
||
if (currentCharacter === '?' && !currentQuote) { | ||
const parameter = parameters![srcIndex] | ||
|
||
if (Array.isArray(parameter)) { | ||
const additionalParameters = parameter.map((_, index) => | ||
`:param_${destIndex + index}`) | ||
|
||
newQueryString += additionalParameters.join(', ') | ||
destIndex += additionalParameters.length | ||
} else { | ||
newQueryString += `:param_${destIndex}` | ||
destIndex += 1 | ||
} | ||
srcIndex += 1 | ||
} else { | ||
newQueryString += currentCharacter | ||
|
||
if (quoteCharacters.includes(currentCharacter) && !currentCharacterEscaped) { | ||
if (!currentQuote) { | ||
currentQuote = currentCharacter | ||
} else if (currentQuote === currentCharacter) { | ||
currentQuote = null | ||
} | ||
} | ||
} | ||
} | ||
|
||
return newQueryString | ||
} | ||
|
||
protected expandArrayParameters(parameters: any[]): any[] { | ||
return parameters.reduce( | ||
(expandedParameters, parameter) => { | ||
if (Array.isArray(parameter)) { | ||
expandedParameters.push(...parameter) | ||
} else { | ||
expandedParameters.push(parameter) | ||
} | ||
return expandedParameters | ||
}, []) | ||
} | ||
|
||
protected transformParameters(parameters?: any[]) { | ||
if (!parameters) { | ||
return parameters | ||
} | ||
|
||
const expandedParameters = this.expandArrayParameters(parameters) | ||
|
||
return [expandedParameters.reduce( | ||
(params, parameter, index) => { | ||
params[`param_${index}`] = parameter | ||
return params | ||
}, {})] | ||
} | ||
} |
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,42 @@ | ||
import { QueryTransformer } from './query-transformer' | ||
|
||
export class PostgresQueryTransformer extends QueryTransformer { | ||
protected transformQuery(query: string) { | ||
const quoteCharacters = ["'", '"'] | ||
let newQueryString = '' | ||
let currentQuote = null | ||
|
||
for (let i = 0; i < query.length; i += 1) { | ||
const currentCharacter = query[i] | ||
const currentCharacterEscaped = i !== 0 && query[i - 1] === '\\' | ||
|
||
if (currentCharacter === '$' && !currentQuote) { | ||
newQueryString += ':param_' | ||
} else { | ||
newQueryString += currentCharacter | ||
|
||
if (quoteCharacters.includes(currentCharacter) && !currentCharacterEscaped) { | ||
if (!currentQuote) { | ||
currentQuote = currentCharacter | ||
} else if (currentQuote === currentCharacter) { | ||
currentQuote = null | ||
} | ||
} | ||
} | ||
} | ||
|
||
return newQueryString | ||
} | ||
|
||
protected transformParameters(parameters?: any[]) { | ||
if (!parameters) { | ||
return parameters | ||
} | ||
|
||
return [parameters.reduce( | ||
(params, parameter, index) => { | ||
params[`param_${index + 1}`] = parameter | ||
return params | ||
}, {})] | ||
} | ||
} |
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,20 @@ | ||
export interface QueryTransformationResult { | ||
queryString: string, | ||
parameters: any[], | ||
} | ||
|
||
export abstract class QueryTransformer { | ||
public transformQueryAndParameters(query: string, srcParameters: any[] = []) { | ||
if (!srcParameters.length) { | ||
return { queryString: query, parameters: [] } | ||
} | ||
|
||
const queryString = this.transformQuery(query, srcParameters) | ||
const parameters = this.transformParameters(srcParameters) | ||
return { queryString, parameters } | ||
} | ||
|
||
protected abstract transformQuery(query: string, srcParameters: any[]): string | ||
|
||
protected abstract transformParameters(srcParameters?: any[]): any[] | undefined | ||
} |
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Column, Entity, Generated, PrimaryColumn } from 'typeorm' | ||
|
||
@Entity('category', { schema: 'test1' }) | ||
export class Category { | ||
@PrimaryColumn() | ||
@Generated() | ||
public id!: number | ||
|
||
@Column() | ||
public name!: string | ||
} |
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,28 @@ | ||
import { Column, Entity, Generated, JoinTable, ManyToMany, PrimaryColumn } from 'typeorm' | ||
import { Category } from './Category' | ||
|
||
@Entity('post', { schema: 'test1' }) | ||
export class Post { | ||
@PrimaryColumn() | ||
@Generated() | ||
public id!: number | ||
|
||
@Column() | ||
public title!: string | ||
|
||
@Column() | ||
public text!: string | ||
|
||
@Column({ nullable: false }) | ||
public likesCount!: number | ||
|
||
@Column({ nullable: false, type: 'timestamp', default: () => 'now()' }) | ||
public publishedAt!: Date | ||
|
||
@Column({ nullable: true, type: 'timestamp' }) | ||
public updatedAt?: Date | ||
|
||
@ManyToMany(type => Category) | ||
@JoinTable() | ||
public categories!: Category[] | ||
} |
Oops, something went wrong.