All files / server/executors postgres-executor.ts

50% Statements 41/82
54.11% Branches 46/85
58.33% Functions 7/12
51.42% Lines 36/70

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                            7x   7x 2x 2x 2x               5x 1x   4x     4x 3x     1x             5x   5x 5x     9x 5x   4x     5x               33x     31x     27x     25x   10x 10x 10x 8x         2x                 2x       15x   15x 7x       8x 6x         2x       1x                                                                                                                                                                                                                                       31x  
/**
 * PostgreSQL database executor
 * Works with postgres.js and Neon drivers
 */
 
import type { SQL } from 'drizzle-orm'
import { sql } from 'drizzle-orm'
import type { DrizzleDatabase, ExplainOptions, ExplainResult, IndexInfo } from '../types'
import { BaseDatabaseExecutor } from './base-executor'
import { parsePostgresExplain } from '../explain/postgres-parser'
 
export class PostgresExecutor extends BaseDatabaseExecutor {
  async execute<T = any[]>(query: SQL | any, numericFields?: string[]): Promise<T> {
    // Handle Drizzle query objects directly
    Eif (query && typeof query === 'object') {
      // Check for various execution methods that Drizzle queries might have
      if (typeof query.execute === 'function') {
        const result = await query.execute()
        Eif (Array.isArray(result)) {
          return result.map(row => this.convertNumericFields(row, numericFields)) as T
        }
        return result as T
      }
      
    }
    
    // Handle raw SQL objects
    if (!this.db.execute) {
      throw new Error('PostgreSQL database instance must have an execute method')
    }
    const result = await this.db.execute(query)
    
    // Convert numeric strings to numbers for PostgreSQL results
    if (Array.isArray(result)) {
      return result.map(row => this.convertNumericFields(row, numericFields)) as T
    }
    
    return result as T
  }
 
  /**
   * Convert numeric string fields to numbers (only for measure fields)
   */
  private convertNumericFields(row: any, numericFields?: string[]): any {
    Iif (!row || typeof row !== 'object') return row
    
    const converted: any = {}
    for (const [key, value] of Object.entries(row)) {
      // Only convert measure fields to numbers
      // Dimensions and time dimensions should keep their original types
      if (numericFields && numericFields.includes(key)) {
        converted[key] = this.coerceToNumber(value)
      } else {
        converted[key] = value
      }
    }
    return converted
  }
 
  /**
   * Coerce a value to a number if it represents a numeric type
   */
  private coerceToNumber(value: any): any {
    // Handle null/undefined - preserve null values for aggregations
    if (value == null) return value
    
    // Already a number
    if (typeof value === 'number') return value
    
    // Handle BigInt values from COUNT operations
    if (typeof value === 'bigint') return Number(value)
    
    // Handle PostgreSQL-specific types (numeric, decimal objects)
    if (value && typeof value === 'object') {
      // Check if it has a numeric toString() method
      Eif (typeof value.toString === 'function') {
        const stringValue = value.toString()
        if (/^-?\d+(\.\d+)?$/.test(stringValue)) {
          return stringValue.includes('.') ? parseFloat(stringValue) : parseInt(stringValue, 10)
        }
      }
      
      // Check for common PostgreSQL numeric type properties
      Iif (value.constructor?.name === 'Numeric' || 
          value.constructor?.name === 'Decimal' ||
          'digits' in value || 
          'sign' in value) {
        const stringValue = value.toString()
        return parseFloat(stringValue)
      }
      
      // If it's an object but doesn't look numeric, return as-is
      return value
    }
    
    // Handle string representations of numbers
    Eif (typeof value === 'string') {
      // Check for exact numeric strings
      if (/^-?\d+(\.\d+)?$/.test(value)) {
        return value.includes('.') ? parseFloat(value) : parseInt(value, 10)
      }
      
      // Check for other numeric formats (scientific notation, etc.)
      if (!isNaN(parseFloat(value)) && isFinite(parseFloat(value))) {
        return parseFloat(value)
      }
    }
    
    // Return as-is for non-numeric values
    return value
  }
 
  getEngineType(): 'postgres' {
    return 'postgres'
  }
 
  /**
   * Execute EXPLAIN on a SQL query to get the execution plan
   */
  async explainQuery(
    sqlString: string,
    params: unknown[],
    options?: ExplainOptions
  ): Promise<ExplainResult> {
    // Build EXPLAIN command
    const explainPrefix = options?.analyze ? 'EXPLAIN ANALYZE' : 'EXPLAIN'
 
    // Execute EXPLAIN with parameters
    if (!this.db.execute) {
      throw new Error('PostgreSQL database instance must have an execute method')
    }
 
    // For postgres.js, we need to pass parameters separately
    // The sql string already has $1, $2 placeholders from Drizzle
    const result = await this.db.execute(
      sql`${sql.raw(explainPrefix)} ${sql.raw(sqlString.replace(/\$(\d+)/g, (_, n) => {
        const paramIndex = parseInt(n, 10) - 1
        const value = params[paramIndex]
        // Escape and quote the value appropriately
        if (value === null) return 'NULL'
        if (typeof value === 'number') return String(value)
        if (typeof value === 'boolean') return value ? 'TRUE' : 'FALSE'
        if (value instanceof Date) return `'${value.toISOString()}'`
        // String: escape single quotes
        return `'${String(value).replace(/'/g, "''")}'`
      }))}`
    )
 
    // PostgreSQL returns EXPLAIN output as rows with 'QUERY PLAN' column
    const rawLines: string[] = []
    if (Array.isArray(result)) {
      for (const row of result) {
        if (row && typeof row === 'object') {
          // Handle different column name cases
          const planLine =
            (row as Record<string, unknown>)['QUERY PLAN'] ||
            (row as Record<string, unknown>)['query plan'] ||
            (row as Record<string, unknown>)['queryplan']
          if (typeof planLine === 'string') {
            rawLines.push(planLine)
          }
        }
      }
    }
 
    // Parse the output using the PostgreSQL parser
    return parsePostgresExplain(rawLines, { sql: sqlString, params })
  }
 
  /**
   * Get existing indexes for the specified tables
   */
  async getTableIndexes(tableNames: string[]): Promise<IndexInfo[]> {
    if (!tableNames || tableNames.length === 0) {
      return []
    }
 
    if (!this.db.execute) {
      throw new Error('PostgreSQL database instance must have an execute method')
    }
 
    try {
      // Build table list for SQL IN clause
      const tableList = tableNames.map(t => `'${t.toLowerCase()}'`).join(',')
 
      const result = await this.db.execute(sql`
        SELECT
          t.relname as table_name,
          i.relname as index_name,
          array_to_string(array_agg(a.attname ORDER BY k.n), ',') as columns,
          ix.indisunique as is_unique,
          ix.indisprimary as is_primary
        FROM pg_index ix
        JOIN pg_class t ON t.oid = ix.indrelid
        JOIN pg_class i ON i.oid = ix.indexrelid
        JOIN pg_namespace n ON n.oid = t.relnamespace
        JOIN LATERAL unnest(ix.indkey) WITH ORDINALITY AS k(attnum, n) ON true
        JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
        WHERE n.nspname = 'public'
          AND t.relname IN (${sql.raw(tableList)})
        GROUP BY t.relname, i.relname, ix.indisunique, ix.indisprimary
        ORDER BY t.relname, i.relname
      `)
 
      if (!Array.isArray(result)) {
        return []
      }
 
      return result.map((row: any) => ({
        table_name: row.table_name,
        index_name: row.index_name,
        columns: row.columns.split(','),
        is_unique: row.is_unique,
        is_primary: row.is_primary
      }))
    } catch (err) {
      console.warn('Failed to get table indexes:', err)
      return []
    }
  }
}
 
/**
 * Factory function for creating PostgreSQL executors
 */
export function createPostgresExecutor(
  db: DrizzleDatabase,
  schema?: any
): PostgresExecutor {
  return new PostgresExecutor(db, schema, 'postgres')
}