|
| 1 | +import { DataType, Field } from '@apache-arrow/esnext-esm'; |
| 2 | + |
| 3 | +import * as arrow from './arrow.js'; |
| 4 | + |
| 5 | +export class Column { |
| 6 | + name: string; |
| 7 | + type: DataType; |
| 8 | + description: string; |
| 9 | + primary_key: boolean; |
| 10 | + not_null: boolean; |
| 11 | + incremental_key: boolean; |
| 12 | + unique: boolean; |
| 13 | + |
| 14 | + constructor( |
| 15 | + name: string, |
| 16 | + type: DataType, |
| 17 | + description: string = '', |
| 18 | + primary_key: boolean = false, |
| 19 | + not_null: boolean = false, |
| 20 | + incremental_key: boolean = false, |
| 21 | + unique: boolean = false, |
| 22 | + ) { |
| 23 | + this.name = name; |
| 24 | + this.type = type; |
| 25 | + this.description = description; |
| 26 | + this.primary_key = primary_key; |
| 27 | + this.not_null = not_null; |
| 28 | + this.incremental_key = incremental_key; |
| 29 | + this.unique = unique; |
| 30 | + } |
| 31 | + |
| 32 | + toString(): string { |
| 33 | + return `Column(name=${this.name}, type=${this.type}, description=${this.description}, primary_key=${this.primary_key}, not_null=${this.not_null}, incremental_key=${this.incremental_key}, unique=${this.unique})`; |
| 34 | + } |
| 35 | + |
| 36 | + // JavaScript (and TypeScript) uses a single method for both string representation and debugging output |
| 37 | + toJSON(): string { |
| 38 | + return this.toString(); |
| 39 | + } |
| 40 | + |
| 41 | + equals(value: object): boolean { |
| 42 | + if (value instanceof Column) { |
| 43 | + return ( |
| 44 | + this.name === value.name && |
| 45 | + this.type === value.type && |
| 46 | + this.description === value.description && |
| 47 | + this.primary_key === value.primary_key && |
| 48 | + this.not_null === value.not_null && |
| 49 | + this.incremental_key === value.incremental_key && |
| 50 | + this.unique === value.unique |
| 51 | + ); |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + toArrowField(): Field { |
| 57 | + const metadataMap = new Map<string, string>(); |
| 58 | + metadataMap.set(arrow.METADATA_PRIMARY_KEY, this.primary_key ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
| 59 | + metadataMap.set(arrow.METADATA_UNIQUE, this.unique ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
| 60 | + metadataMap.set(arrow.METADATA_INCREMENTAL, this.incremental_key ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
| 61 | + |
| 62 | + return new Field(this.name, this.type, /*nullable=*/ !this.not_null, metadataMap); |
| 63 | + } |
| 64 | + |
| 65 | + static fromArrowField(field: Field): Column { |
| 66 | + const metadata = field.metadata; |
| 67 | + const primary_key = metadata.get(arrow.METADATA_PRIMARY_KEY) === arrow.METADATA_TRUE; |
| 68 | + const unique = metadata.get(arrow.METADATA_UNIQUE) === arrow.METADATA_TRUE; |
| 69 | + const incremental_key = metadata.get(arrow.METADATA_INCREMENTAL) === arrow.METADATA_TRUE; |
| 70 | + |
| 71 | + return new Column(field.name, field.type, '', primary_key, !field.nullable, unique, incremental_key); |
| 72 | + } |
| 73 | +} |
0 commit comments