Class PgCodeKeeperApi

java.lang.Object
org.pgcodekeeper.core.api.PgCodeKeeperApi

public final class PgCodeKeeperApi extends Object
Main API class for pgCodeKeeper database operations.
  • Method Details

    • createTree

      public static TreeElement createTree(ILoader oldDbLoader, ILoader newDbLoader, DiffSettings diffSettings) throws IOException, InterruptedException
      Compares two databases and generates a tree.
      Parameters:
      oldDbLoader - loader for the old database version to compare from
      newDbLoader - loader for the new database version to compare to
      diffSettings - unified context object containing settings, ignore list, and error accumulator
      Returns:
      the root element of generated tree
      Throws:
      IOException - if I/O operations fail
      InterruptedException - if the thread is interrupted during the operation
    • diff

      public static String diff(IDatabaseProvider provider, ILoader oldDbLoader, ILoader newDbLoader, DiffSettings diffSettings) throws IOException, InterruptedException
      Compares two databases and generates a migration script.
      Parameters:
      provider - the database provider determining SQL dialect
      oldDbLoader - loader for the old database version to compare from
      newDbLoader - loader for the new database version to compare to
      diffSettings - unified context object containing settings, ignore list, and error accumulator
      Returns:
      the generated migration script as a string
      Throws:
      IOException - if I/O operations fail
      InterruptedException - if the thread is interrupted during the operation
    • diff

      public static String diff(IDatabaseProvider provider, IDatabase oldDb, IDatabase newDb, DiffSettings diffSettings) throws IOException, InterruptedException
      Compares two databases and generates a migration script.
      Parameters:
      provider - the database provider determining SQL dialect
      oldDb - the old database version to compare from
      newDb - the new database version to compare to
      diffSettings - unified context object containing settings, ignore list, and error accumulator
      Returns:
      the generated migration script as a string
      Throws:
      IOException - if I/O operations fail
      InterruptedException - if the thread is interrupted during the operation
    • diff

      public static String diff(IDatabaseProvider provider, IDatabase oldDb, IDatabase newDb, DiffSettings diffSettings, TreeElement root) throws IOException
      Compares two databases and generates a migration script with a pre-built tree.
      Parameters:
      provider - the database provider determining SQL dialect
      oldDb - the old database version to compare from
      newDb - the new database version to compare to
      diffSettings - unified context object containing settings, ignore list, and error accumulator
      root - root element of tree
      Returns:
      the generated migration script as a string
      Throws:
      IOException - if I/O operations fail
    • exportToProject

      public static void exportToProject(IDatabaseProvider provider, ILoader oldDbLoader, ILoader newDbLoader, Path projectPath, DiffSettings diffSettings) throws IOException, InterruptedException
      Exports or updates project files based on database schema.

      If oldDb is null, exports newDb schema to an empty project directory. If oldDb is provided, updates the existing project with changes between oldDb and newDb.

      Parameters:
      provider - the database provider determining SQL dialect and exporter/updater implementation
      oldDbLoader - loader for the old database version (existing project state), or null for a full export
      newDbLoader - loader for the new new database version (target state)
      projectPath - path to the target project directory
      diffSettings - unified context object containing settings, monitor, ignore list, and error accumulator
      Throws:
      IOException - if I/O operations fail, if the directory does not exist, if the directory is not empty (export) or if path is a file
      InterruptedException - if the thread is interrupted during the operation
    • exportToProject

      public static void exportToProject(IDatabaseProvider provider, ILoader oldDbLoader, ILoader newDbLoader, Path projectPath, boolean overridesOnly, DiffSettings diffSettings) throws IOException, InterruptedException
      Exports or updates project or overrides files based on database schema.

      If oldDb is null, exports newDb schema to an empty project directory. If oldDb is provided, updates the existing project with changes between oldDb and newDb.

      Parameters:
      provider - the database provider determining SQL dialect and exporter/updater implementation
      oldDbLoader - loader for the old database version (existing project state), or null for a full export
      newDbLoader - loader for the new new database version (target state)
      projectPath - path to the target project directory
      overridesOnly - option to update only overrides
      diffSettings - unified context object containing settings, monitor, ignore list, and error accumulator
      Throws:
      IOException - if I/O operations fail, if the directory does not exist, if the directory is not empty (export) or if path is a file
      InterruptedException - if the thread is interrupted during the operation
    • exportToProject

      public static void exportToProject(IDatabaseProvider provider, IDatabase oldDb, IDatabase newDb, List<TreeElement> selected, Path projectPath, boolean overridesOnly, ISettings settings) throws IOException
      Exports or updates project or overrides files based on selected elements.
      Parameters:
      provider - the database provider determining SQL dialect and exporter/updater implementation
      oldDb - the old database version (existing project state), or null for a full export
      newDb - the new new database version (target state)
      selected - the selected elements
      projectPath - path to the target project directory
      overridesOnly - option to update only overrides
      settings - configuration settings
      Throws:
      IOException - if I/O operations fail, if the directory does not exist, if the directory is not empty (export) or if path is a file
    • analyzeDependencies

      public static List<String> analyzeDependencies(ILoader loader, Collection<String> objectNames, int depth, boolean reverse, Collection<DbObjType> filterTypes, boolean invertFilter) throws IOException, InterruptedException
      Analyzes database object dependencies and builds a dependency graph.
      Parameters:
      loader - loader for the the database to analyze
      objectNames - collection of object name patterns to search for (e.g., "public.users", "*.orders")
      depth - depth of dependency analysis (e.g., 10 levels)
      reverse - false = direct dependencies (what this depends on), true = reverse dependencies (what depends on this)
      filterTypes - types of objects to filter (TABLE, FUNCTION, VIEW), null = all types
      invertFilter - false = include only specified types, true = exclude specified types
      Returns:
      list of strings with dependency information
      Throws:
      IOException - if I/O operations fail
      InterruptedException - if the thread is interrupted during the operation
    • checkDangerousStatements

      public static Set<DangerStatement> checkDangerousStatements(IDatabaseProvider provider, String name, String sql, DiffSettings diffSettings, Collection<DangerStatement> allowedDangers) throws IOException, InterruptedException
      Checks SQL script for dangerous operations (DROP TABLE, ALTER COLUMN, etc.).
      Parameters:
      provider - the database provider determining SQL dialect
      name - name of the script source (used as file identifier for parsing)
      sql - the SQL script to check
      diffSettings - parsing settings
      allowedDangers - set of allowed dangerous operations
      Returns:
      set of detected dangerous operations
      Throws:
      IOException - if I/O operations fail
      InterruptedException - if the thread is interrupted during the operation
    • runSQL

      public static void runSQL(IDatabaseProvider provider, String name, String sql, String url, DiffSettings diffSettings) throws IOException, InterruptedException, SQLException
      Parses and executes SQL script against a database.
      Parameters:
      provider - the database provider determining SQL dialect and JDBC connector
      name - name of the script source (used as file identifier for parsing)
      sql - the SQL script to execute
      url - full JDBC URL of the target database
      diffSettings - parsing and execution settings
      Throws:
      IOException - if there is an error reading the script
      InterruptedException - if the thread is interrupted during the operation
      SQLException - if a database access error occurs during execution