package com.openhis.tool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * Database field adder tool
 */
public class DatabaseFieldAdder {
    public static void main(String[] args) {
        String url = "jdbc:postgresql://192.168.110.252:15432/postgresql?currentSchema=public";
        String username = "postgresql";
        String password = "Jchl1528";
        
        try (Connection conn = DriverManager.getConnection(url, username, password);
             Statement stmt = conn.createStatement()) {
            
            // Check if field exists
            String checkSql = "SELECT column_name FROM information_schema.columns " +
                             "WHERE table_name = 'adm_healthcare_service' AND column_name = 'practitioner_id'";
            
            boolean fieldExists = stmt.executeQuery(checkSql).next();
            
            if (!fieldExists) {
                // Add field
                String addSql = "ALTER TABLE \"public\".\"adm_healthcare_service\" " +
                               "ADD COLUMN \"practitioner_id\" int8";
                stmt.execute(addSql);
                
                // Add comment
                String commentSql = "COMMENT ON COLUMN \"public\".\"adm_healthcare_service\".\"practitioner_id\" IS 'practitioner_id'";
                stmt.execute(commentSql);
                
                System.out.println("Successfully added practitioner_id field to adm_healthcare_service table");
            } else {
                System.out.println("practitioner_id field already exists");
            }
            
        } catch (Exception e) {
            System.err.println("Error executing SQL: " + e.getMessage());
            e.printStackTrace();
        }
    }
}