Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_dynamic_relational.c File Reference
#include "native_dynamic_relational.h"
#include "native_dynamic_config.h"
#include "native_fs.h"
#include "native_support.h"
#include "native_yyjson_helpers.h"
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

Go to the source code of this file.

Data Structures

struct  NativeDynamicPostgresqlRuntimeConfig
 Carries resolved PostgreSQL runtime connection settings. More...
struct  NativeDynamicLibpqApi

Macros

#define PHAROS_SQLITE_BUSY_TIMEOUT_MS_RUNTIME   15000
#define PHAROS_CONN_STATUS_OK_RUNTIME   0
#define PHAROS_PGRES_COMMAND_OK_RUNTIME   1
#define PHAROS_PGRES_TUPLES_OK_RUNTIME   2
#define PHAROS_PGRES_SINGLE_TUPLE_RUNTIME   9

Typedefs

typedef struct pg_conn PGconn
typedef struct pg_result PGresult

Functions

static yyjson_valmigration_backend_record_runtime_local (yyjson_val *root, const char *backend_value)
static char * migration_fixture_path_dup_runtime_local (const char *app_root)
static char * runtime_state_export_sqlite_path_dup_runtime_local (const char *app_root)
static char * runtime_state_import_sqlite_path_dup_runtime_local (const char *app_root)
static char * runtime_state_export_postgresql_path_dup_runtime_local (const char *app_root)
static char * runtime_state_import_postgresql_path_dup_runtime_local (const char *app_root)
static int sqlite_configure_connection_runtime_local (sqlite3 *db)
static char * sqlite_exec_first_value_dup_runtime_local (const char *sqlite_path, const char *sql_text)
static int sqlite_exec_runtime_local (const char *sqlite_path, const char *sql_text)
static int sqlite_apply_file_list_runtime_local (const char *sqlite_path, const char *app_root, yyjson_val *list)
static char * checksum_string_dup_runtime_local (const char *value)
static char * migration_expected_checksum_dup_runtime_local (const char *app_root, yyjson_val *migration_record, const char *backend)
static void free_postgresql_runtime_config_runtime_local (NativeDynamicPostgresqlRuntimeConfig *config)
static int load_postgresql_runtime_config_runtime_local (const char *app_root, const char *runtime_conf_path, NativeDynamicPostgresqlRuntimeConfig *config)
static int conninfo_append_kv_runtime_local (Buffer *buffer, const char *key, const char *value, int escape_quotes)
static void * open_libpq_handle_candidate_runtime_local (const char *path)
static int load_native_libpq_api_runtime_local (NativeDynamicLibpqApi *api)
static void free_native_libpq_api_runtime_local (NativeDynamicLibpqApi *api)
static char * postgresql_conninfo_dup_runtime_local (const NativeDynamicPostgresqlRuntimeConfig *config)
static int capture_postgresql_result_output_runtime_local (const NativeDynamicLibpqApi *api, PGresult *result, char **output_out)
static int run_postgresql_sql_capture_runtime_local (const NativeDynamicPostgresqlRuntimeConfig *config, const char *sql_text, int capture_output, char **output_out)
static int postgresql_apply_file_list_runtime_local (const NativeDynamicPostgresqlRuntimeConfig *config, const char *app_root, yyjson_val *list)
static char * runtime_state_placeholder_sql_dup_runtime_local (const char *sql_template, const char *escaped_state_json)
static char * sql_literal_escape_dup_runtime_local (const char *value)
void native_dynamic_relational_request_sync_free (NativeDynamicRelationalRequestSync *sync)
 Clears heap-owned fields in a request-local relational sync record.
char * native_dynamic_relational_backend_dup (const char *app_root, const char *runtime_conf_path)
 Returns the configured relational backend when it is recognized.
char * native_dynamic_relational_sqlite_path_dup (const char *app_root, const char *runtime_conf_path, const char *app_id)
 Returns the effective sqlite database path for a dynamic app.
int native_dynamic_relational_sqlite_runtime_ready (const char *app_root, const char *runtime_conf_path, const char *app_id)
 Returns whether sqlite runtime prerequisites are satisfied.
int native_dynamic_relational_postgresql_runtime_ready (const char *app_root, const char *runtime_conf_path)
 Returns whether PostgreSQL runtime prerequisites are satisfied.
int native_dynamic_relational_postgresql_runtime_prepare (const char *app_root, const char *runtime_conf_path)
 Prepares the PostgreSQL schema, seed data, and migration ledger for one dynamic app.
int native_dynamic_relational_sqlite_runtime_prepare (const char *app_root, const char *runtime_conf_path, const char *app_id)
 Prepares the sqlite schema, seed data, and migration ledger for one dynamic app.
char * native_dynamic_relational_runtime_export_json_dup (const char *app_root, const char *runtime_conf_path, const char *app_id)
 Exports canonical runtime state JSON from the configured relational backend.
char * native_dynamic_relational_runtime_audit_count_dup (const char *app_root, const char *runtime_conf_path, const char *app_id)
 Returns the current audit-event count from the configured relational backend.
int native_dynamic_relational_runtime_import_state (const char *app_root, const char *runtime_conf_path, const char *app_id, const char *state_path, const char *expected_audit_count)
 Imports canonical runtime state JSON into the configured relational backend.
char * native_dynamic_relational_file_signature_dup (const char *path)
 Computes a stable signature for one file path.
int native_dynamic_relational_prepare_request_state (const AppRuntime *app, NativeDynamicRelationalRequestSync *sync)
 Exports relational state into the app state file before request handling when enabled.
int native_dynamic_relational_sync_back_if_changed (const AppRuntime *app, const NativeDynamicRelationalRequestSync *sync)
 Imports the app state file back into the relational backend when it changed.

Macro Definition Documentation

◆ PHAROS_CONN_STATUS_OK_RUNTIME

#define PHAROS_CONN_STATUS_OK_RUNTIME   0

◆ PHAROS_PGRES_COMMAND_OK_RUNTIME

#define PHAROS_PGRES_COMMAND_OK_RUNTIME   1

◆ PHAROS_PGRES_SINGLE_TUPLE_RUNTIME

#define PHAROS_PGRES_SINGLE_TUPLE_RUNTIME   9

◆ PHAROS_PGRES_TUPLES_OK_RUNTIME

#define PHAROS_PGRES_TUPLES_OK_RUNTIME   2

◆ PHAROS_SQLITE_BUSY_TIMEOUT_MS_RUNTIME

#define PHAROS_SQLITE_BUSY_TIMEOUT_MS_RUNTIME   15000

Typedef Documentation

◆ PGconn

typedef struct pg_conn PGconn

Definition at line 33 of file native_dynamic_relational.c.

◆ PGresult

typedef struct pg_result PGresult

Definition at line 34 of file native_dynamic_relational.c.

Function Documentation

◆ capture_postgresql_result_output_runtime_local()

int capture_postgresql_result_output_runtime_local ( const NativeDynamicLibpqApi * api,
PGresult * result,
char ** output_out )
static

◆ checksum_string_dup_runtime_local()

char * checksum_string_dup_runtime_local ( const char * value)
static

◆ conninfo_append_kv_runtime_local()

int conninfo_append_kv_runtime_local ( Buffer * buffer,
const char * key,
const char * value,
int escape_quotes )
static

Definition at line 346 of file native_dynamic_relational.c.

References buffer_append(), and Buffer::len.

Referenced by postgresql_conninfo_dup_runtime_local().

◆ free_native_libpq_api_runtime_local()

void free_native_libpq_api_runtime_local ( NativeDynamicLibpqApi * api)
static

◆ free_postgresql_runtime_config_runtime_local()

◆ load_native_libpq_api_runtime_local()

◆ load_postgresql_runtime_config_runtime_local()

◆ migration_backend_record_runtime_local()

◆ migration_expected_checksum_dup_runtime_local()

◆ migration_fixture_path_dup_runtime_local()

char * migration_fixture_path_dup_runtime_local ( const char * app_root)
static

◆ native_dynamic_relational_backend_dup()

char * native_dynamic_relational_backend_dup ( const char * app_root,
const char * runtime_conf_path )

◆ native_dynamic_relational_file_signature_dup()

char * native_dynamic_relational_file_signature_dup ( const char * path)

Computes a stable signature for one file path.

Parameters
pathFile path to hash.
Returns
Newly allocated signature text, or NULL on failure.

Definition at line 1220 of file native_dynamic_relational.c.

References path_exists(), and read_file_bytes_dup_runtime().

Referenced by native_dynamic_relational_prepare_request_state(), and native_dynamic_relational_sync_back_if_changed().

◆ native_dynamic_relational_postgresql_runtime_prepare()

◆ native_dynamic_relational_postgresql_runtime_ready()

int native_dynamic_relational_postgresql_runtime_ready ( const char * app_root,
const char * runtime_conf_path )

Returns whether PostgreSQL runtime prerequisites are satisfied.

Parameters
app_rootApp root directory.
runtime_conf_pathRuntime config path.
Returns
Non-zero when PostgreSQL runtime work can proceed, otherwise 0.

Definition at line 733 of file native_dynamic_relational.c.

References free_native_libpq_api_runtime_local(), free_postgresql_runtime_config_runtime_local(), load_native_libpq_api_runtime_local(), load_postgresql_runtime_config_runtime_local(), native_dynamic_relational_backend_dup(), and streq().

Referenced by handle_dynamic_postgresql_runtime_ready_command(), and native_dynamic_relational_prepare_request_state().

◆ native_dynamic_relational_prepare_request_state()

◆ native_dynamic_relational_request_sync_free()

void native_dynamic_relational_request_sync_free ( NativeDynamicRelationalRequestSync * sync)

Clears heap-owned fields in a request-local relational sync record.

Parameters
syncSync record to clear.

Definition at line 670 of file native_dynamic_relational.c.

References NativeDynamicRelationalRequestSync::before_signature, and NativeDynamicRelationalRequestSync::enabled.

Referenced by native_dynamic_relational_prepare_request_state(), and native_dynamic_runtime_handle_request().

◆ native_dynamic_relational_runtime_audit_count_dup()

char * native_dynamic_relational_runtime_audit_count_dup ( const char * app_root,
const char * runtime_conf_path,
const char * app_id )

Returns the current audit-event count from the configured relational backend.

Parameters
app_rootApp root directory.
runtime_conf_pathRuntime config path.
app_idApp identifier used for sqlite path resolution.
Returns
Newly allocated count text, or NULL on failure.

Definition at line 1107 of file native_dynamic_relational.c.

References free_postgresql_runtime_config_runtime_local(), load_postgresql_runtime_config_runtime_local(), native_dynamic_relational_backend_dup(), native_dynamic_relational_sqlite_path_dup(), run_postgresql_sql_capture_runtime_local(), sqlite_exec_first_value_dup_runtime_local(), and streq().

Referenced by handle_dynamic_relational_runtime_audit_count_command(), and native_dynamic_relational_runtime_import_state().

◆ native_dynamic_relational_runtime_export_json_dup()

char * native_dynamic_relational_runtime_export_json_dup ( const char * app_root,
const char * runtime_conf_path,
const char * app_id )

◆ native_dynamic_relational_runtime_import_state()

int native_dynamic_relational_runtime_import_state ( const char * app_root,
const char * runtime_conf_path,
const char * app_id,
const char * state_path,
const char * expected_audit_count )

Imports canonical runtime state JSON into the configured relational backend.

Parameters
app_rootApp root directory.
runtime_conf_pathRuntime config path.
app_idApp identifier used for sqlite path resolution.
state_pathCanonical state JSON path.
expected_audit_countOptional optimistic concurrency guard.
Returns
0 on success, otherwise a non-zero native status code.

Definition at line 1153 of file native_dynamic_relational.c.

References free_postgresql_runtime_config_runtime_local(), load_postgresql_runtime_config_runtime_local(), native_dynamic_relational_backend_dup(), native_dynamic_relational_runtime_audit_count_dup(), native_dynamic_relational_sqlite_path_dup(), read_file_text(), run_postgresql_sql_capture_runtime_local(), runtime_state_import_postgresql_path_dup_runtime_local(), runtime_state_import_sqlite_path_dup_runtime_local(), runtime_state_placeholder_sql_dup_runtime_local(), sql_literal_escape_dup_runtime_local(), sqlite_exec_runtime_local(), and streq().

Referenced by handle_dynamic_relational_runtime_import_state_command(), native_dynamic_relational_sync_back_if_changed(), and persist_app_state_text_runtime_local().

◆ native_dynamic_relational_sqlite_path_dup()

char * native_dynamic_relational_sqlite_path_dup ( const char * app_root,
const char * runtime_conf_path,
const char * app_id )

Returns the effective sqlite database path for a dynamic app.

Parameters
app_rootApp root directory.
runtime_conf_pathRuntime config path.
app_idApp identifier used for the default sqlite path.
Returns
Newly allocated sqlite path, or NULL on failure.

Definition at line 688 of file native_dynamic_relational.c.

References native_dynamic_config_merged_value_dup().

Referenced by handle_dynamic_relational_sqlite_path_command(), native_dynamic_relational_runtime_audit_count_dup(), native_dynamic_relational_runtime_export_json_dup(), native_dynamic_relational_runtime_import_state(), native_dynamic_relational_sqlite_runtime_prepare(), and native_dynamic_relational_sqlite_runtime_ready().

◆ native_dynamic_relational_sqlite_runtime_prepare()

int native_dynamic_relational_sqlite_runtime_prepare ( const char * app_root,
const char * runtime_conf_path,
const char * app_id )

◆ native_dynamic_relational_sqlite_runtime_ready()

int native_dynamic_relational_sqlite_runtime_ready ( const char * app_root,
const char * runtime_conf_path,
const char * app_id )

Returns whether sqlite runtime prerequisites are satisfied.

Parameters
app_rootApp root directory.
runtime_conf_pathRuntime config path.
app_idApp identifier used for the default sqlite path.
Returns
Non-zero when sqlite runtime work can proceed, otherwise 0.

Definition at line 709 of file native_dynamic_relational.c.

References ensure_directory(), native_dynamic_relational_backend_dup(), native_dynamic_relational_sqlite_path_dup(), path_dirname(), path_exists(), and streq().

Referenced by handle_dynamic_sqlite_runtime_ready_command(), and native_dynamic_relational_prepare_request_state().

◆ native_dynamic_relational_sync_back_if_changed()

int native_dynamic_relational_sync_back_if_changed ( const AppRuntime * app,
const NativeDynamicRelationalRequestSync * sync )

Imports the app state file back into the relational backend when it changed.

Parameters
appApp runtime being served.
syncRequest sync record populated by prepare_request_state.
Returns
0 on success, otherwise a non-zero native status code.

Definition at line 1316 of file native_dynamic_relational.c.

References AppRuntime::app_id, AppRuntime::app_root, NativeDynamicRelationalRequestSync::before_signature, NativeDynamicRelationalRequestSync::enabled, native_dynamic_relational_file_signature_dup(), native_dynamic_relational_runtime_import_state(), AppRuntime::runtime_conf_path, AppRuntime::state_path, and streq().

Referenced by native_dynamic_runtime_handle_request().

◆ open_libpq_handle_candidate_runtime_local()

void * open_libpq_handle_candidate_runtime_local ( const char * path)
static

Definition at line 373 of file native_dynamic_relational.c.

Referenced by load_native_libpq_api_runtime_local().

◆ postgresql_apply_file_list_runtime_local()

◆ postgresql_conninfo_dup_runtime_local()

◆ run_postgresql_sql_capture_runtime_local()

◆ runtime_state_export_postgresql_path_dup_runtime_local()

char * runtime_state_export_postgresql_path_dup_runtime_local ( const char * app_root)
static

◆ runtime_state_export_sqlite_path_dup_runtime_local()

char * runtime_state_export_sqlite_path_dup_runtime_local ( const char * app_root)
static

◆ runtime_state_import_postgresql_path_dup_runtime_local()

char * runtime_state_import_postgresql_path_dup_runtime_local ( const char * app_root)
static

Definition at line 89 of file native_dynamic_relational.c.

References path_join().

Referenced by native_dynamic_relational_runtime_import_state().

◆ runtime_state_import_sqlite_path_dup_runtime_local()

char * runtime_state_import_sqlite_path_dup_runtime_local ( const char * app_root)
static

Definition at line 81 of file native_dynamic_relational.c.

References path_join().

Referenced by native_dynamic_relational_runtime_import_state().

◆ runtime_state_placeholder_sql_dup_runtime_local()

char * runtime_state_placeholder_sql_dup_runtime_local ( const char * sql_template,
const char * escaped_state_json )
static

◆ sql_literal_escape_dup_runtime_local()

char * sql_literal_escape_dup_runtime_local ( const char * value)
static

◆ sqlite_apply_file_list_runtime_local()

int sqlite_apply_file_list_runtime_local ( const char * sqlite_path,
const char * app_root,
yyjson_val * list )
static

◆ sqlite_configure_connection_runtime_local()

int sqlite_configure_connection_runtime_local ( sqlite3 * db)
static

◆ sqlite_exec_first_value_dup_runtime_local()

char * sqlite_exec_first_value_dup_runtime_local ( const char * sqlite_path,
const char * sql_text )
static

◆ sqlite_exec_runtime_local()

int sqlite_exec_runtime_local ( const char * sqlite_path,
const char * sql_text )
static