Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 2x 2x 2x 2x 2x 2x 2x 37x 37x 37x 4x 4x 4x 4x 29x 29x 29x 29x 4x 4x 4x 2x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 29x 37x 37x 37x 287x 287x 6x 6x 281x 270x 270x 270x 43x 227x 227x 49x 49x 49x 178x 227x 227x 270x 118x 59x 59x 59x | /* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ import { DefaultPlatform, isSet, Column, Index, Table, parseType } from '@deepkit/sql'; import { postgresSerializer } from './postgres-serializer'; import { ClassSchema, isArray, PostgresOptions, PropertySchema } from '@deepkit/type'; import { PostgresSchemaParser } from './postgres-schema-parser'; import { PostgreSQLFilterBuilder } from './sql-filter-builder'; import { isObject } from '@deepkit/core'; import sqlstring from 'sqlstring'; function escapeLiteral(value: any): string { Iif (value === null || value === undefined) return 'null'; Iif (value instanceof Date) return sqlstring.escape(value); if ('number' === typeof value || 'bigint' === typeof value) return String(value); Iif ('string' !== typeof value) return escapeLiteral(String(value)); let hasBackslash = false; let escaped = '\''; for (let i = 0; i < value.length; i++) { const c = value[i]; Iif (c === '\'') { escaped += c + c; } else Iif (c === '\\') { escaped += c + c; hasBackslash = true; } else { escaped += c; } } escaped += '\''; Iif (hasBackslash) { escaped = ' E' + escaped; } return escaped; } export class PostgresPlatform extends DefaultPlatform { protected defaultSqlType = 'text'; public readonly serializer = postgresSerializer; schemaParserType = PostgresSchemaParser; constructor() { super(); this.addType('number', 'double precision'); this.addType('date', 'timestamp'); this.addType('boolean', 'boolean'); this.addType('class', 'jsonb'); this.addType('array', 'jsonb'); this.addType('union', 'jsonb'); this.addType('partial', 'jsonb'); this.addType('map', 'jsonb'); this.addType('patch', 'jsonb'); this.addType('uuid', 'uuid'); this.addBinaryType('bytea'); } getAggregateSelect(tableName: string, property: PropertySchema, func: string) { if (func === 'group_concat') { return `array_to_string(array_agg(${tableName}.${this.quoteIdentifier(property.name)}), ',')`; } return super.getAggregateSelect(tableName, property, func); } createSqlFilterBuilder(schema: ClassSchema, tableName: string): PostgreSQLFilterBuilder { return new PostgreSQLFilterBuilder(schema, tableName, this.serializer, this.quoteValue.bind(this), this.quoteIdentifier.bind(this)); } quoteValue(value: any): string { Iif (!(value instanceof Date) && (isObject(value) || isArray(value))) return escapeLiteral(JSON.stringify(value)); Iif (value instanceof Date) return 'TIMESTAMP ' + sqlstring.escape(value); return escapeLiteral(value); } protected setColumnType(column: Column, typeProperty: PropertySchema) { const db = (typeProperty.data['postgres'] || {}) as PostgresOptions; if (db.type) { parseType(column, db.type); return; } super.setColumnType(column, typeProperty); } getColumnDDL(column: Column) { const ddl: string[] = []; ddl.push(this.getIdentifier(column)); if (column.isAutoIncrement) { ddl.push(`SERIAL`); } else { const foreignKey = column.table.getForeignKeyOfLocalColumn(column); if (foreignKey) { const [foreignPk] = foreignKey.foreignColumns; Eif (foreignPk.isAutoIncrement) { //the foreignPK has `SERIAL` type, which is basically an INTEGER. ddl.push('INTEGER'); } else { ddl.push((foreignPk.type || 'INTEGER') + column.getSizeDefinition()); } } else { ddl.push((column.type || 'INTEGER') + column.getSizeDefinition()); } ddl.push(column.isNotNull ? this.getNotNullString() : this.getNullString()); ddl.push(this.getColumnDefaultValueDDL(column)); } return ddl.filter(isSet).join(' '); } getUniqueDDL(unique: Index): string { return `CONSTRAINT ${this.getIdentifier(unique)} UNIQUE (${this.getColumnListDDL(unique.columns)})`; } getDropIndexDDL(index: Index): string { return `DROP CONSTRAINT ${this.getIdentifier(index)}`; } supportsInlineForeignKey(): boolean { return false; } getAutoIncrement() { return ''; } getDropTableDDL(table: Table): string { return `DROP TABLE IF EXISTS ${this.getIdentifier(table)} CASCADE`; } getUseSchemaDDL(table: Table) { Eif (!table.schemaName) return ''; return `SET search_path TO ${this.quoteIdentifier(table.schemaName)}`; } getResetSchemaDDL(table: Table): string { Eif (!table.schemaName) return ''; return `SET search_path TO public`; } } |