You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB

import {TableRef, QuerySource} from '../types.ts'
export class TableRefBuilder {
resolve_table_name(from: string): TableRef {
const parts = from.split('.')
const ref: any = {}
if ( parts.length > 1 ) {
ref.database = parts[0]
ref.table = parts[1]
} else {
ref.table = parts[0]
}
const alias_parts = ref.table.split(/\s+/)
if ( alias_parts.length > 1 ) {
ref.table = alias_parts[0]
ref.alias = alias_parts[1]
}
return ref as TableRef
}
serialize_table_ref(ref: TableRef): string {
return `${ref.database ? ref.database+'.' : ''}${ref.table}${ref.alias ? ' '+ref.alias : ''}`
}
source_alias_to_table_ref(source: QuerySource, alias?: string) {
let string = ''
if ( typeof source === 'string' ) {
string = source
if ( typeof alias === 'string' ) string += ` ${alias}`
}
else if ( typeof source === 'object' ) {
string = `${source.ref}`
if ( source.alias ) string += ` ${source.alias}`
else if ( typeof alias === 'string' ) string += ` ${alias}`
}
return this.resolve_table_name(string)
}
}