connection-factory.vala 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. namespace InvercargillSql {
  2. /**
  3. * Static factory for creating database connections from connection strings.
  4. *
  5. * This factory maintains a registry of connection providers indexed by scheme.
  6. * The SQLite provider is automatically registered on first use.
  7. *
  8. * Example usage:
  9. * ```vala
  10. * // Simple connection
  11. * var conn = ConnectionFactory.create_and_open("sqlite:///mydb.sqlite");
  12. *
  13. * // With options
  14. * var conn = ConnectionFactory.create_and_open("sqlite:///readonly.db?mode=ro");
  15. *
  16. * // Register custom provider
  17. * ConnectionFactory.register_provider("postgresql", new PostgresProvider());
  18. * var conn = ConnectionFactory.create_and_open("postgresql://user:pass@host/db");
  19. * ```
  20. */
  21. public class ConnectionFactory : Object {
  22. private static bool _sqlite_registered = false;
  23. private static ConnectionProvider? _sqlite_provider = null;
  24. private static bool _postgres_registered = false;
  25. private static ConnectionProvider? _postgres_provider = null;
  26. /**
  27. * Creates a new database connection from a connection string.
  28. *
  29. * The connection is created but not opened. Call open() on the
  30. * returned connection to establish the database connection, or
  31. * use create_and_open() to create and open in one step.
  32. *
  33. * @param connection_string The connection string to parse
  34. * @return A new Connection instance (not opened)
  35. * @throws SqlError.INVALID_CONNECTION_STRING if the format is invalid
  36. * @throws SqlError.CONNECTION_FAILED if provider creation fails
  37. */
  38. public static Connection create(string connection_string) throws SqlError {
  39. ConnectionString cs = ConnectionString.parse(connection_string);
  40. ConnectionProvider provider = get_provider(cs.scheme);
  41. return provider.create_connection(cs);
  42. }
  43. /**
  44. * Creates and opens a database connection from a connection string.
  45. *
  46. * This is a convenience method that combines create() and open().
  47. *
  48. * @param connection_string The connection string to parse
  49. * @return A new Connection instance (opened)
  50. * @throws SqlError.INVALID_CONNECTION_STRING if the format is invalid
  51. * @throws SqlError.CONNECTION_FAILED if connection fails
  52. */
  53. public static Connection create_and_open(string connection_string) throws SqlError {
  54. Connection conn = create(connection_string);
  55. conn.open();
  56. return conn;
  57. }
  58. /**
  59. * Creates and opens a database connection asynchronously.
  60. *
  61. * @param connection_string The connection string to parse
  62. * @return A new Connection instance (opened)
  63. * @throws SqlError.INVALID_CONNECTION_STRING if the format is invalid
  64. * @throws SqlError.CONNECTION_FAILED if connection fails
  65. */
  66. public async static Connection create_and_open_async(string connection_string) throws SqlError {
  67. ConnectionString cs = ConnectionString.parse(connection_string);
  68. ConnectionProvider provider = get_provider(cs.scheme);
  69. Connection conn = yield provider.create_connection_async(cs);
  70. conn.open();
  71. return conn;
  72. }
  73. /**
  74. * Registers a connection provider for a scheme.
  75. *
  76. * If a provider is already registered for the scheme, it will be
  77. * replaced.
  78. *
  79. * @param scheme The scheme (e.g., "sqlite", "postgresql")
  80. * @param provider The provider instance
  81. */
  82. public static void register_provider(string scheme, ConnectionProvider provider) {
  83. string scheme_lower = scheme.down();
  84. if (scheme_lower == "sqlite") {
  85. _sqlite_provider = provider;
  86. _sqlite_registered = true;
  87. } else if (scheme_lower == "postgres" || scheme_lower == "postgresql") {
  88. _postgres_provider = provider;
  89. _postgres_registered = true;
  90. }
  91. // Other providers can be registered once a dictionary of
  92. // schemes is introduced; sqlite and postgres are the natives.
  93. }
  94. /**
  95. * Gets the provider for a scheme.
  96. *
  97. * Auto-registers SQLite provider on first access.
  98. *
  99. * @param scheme The scheme to look up
  100. * @return The ConnectionProvider for the scheme
  101. * @throws SqlError.INVALID_CONNECTION_STRING if no provider is registered
  102. */
  103. private static ConnectionProvider get_provider(string scheme) throws SqlError {
  104. string scheme_lower = scheme.down();
  105. if (scheme_lower == "sqlite") {
  106. if (!_sqlite_registered) {
  107. _sqlite_provider = new SqliteProvider();
  108. _sqlite_registered = true;
  109. }
  110. return _sqlite_provider;
  111. }
  112. if (scheme_lower == "postgres" || scheme_lower == "postgresql") {
  113. if (!_postgres_registered) {
  114. _postgres_provider = new PostgresProvider();
  115. _postgres_registered = true;
  116. }
  117. return _postgres_provider;
  118. }
  119. throw new SqlError.INVALID_CONNECTION_STRING(
  120. "No connection provider registered for scheme: %s".printf(scheme)
  121. );
  122. }
  123. /**
  124. * Clears all registered providers.
  125. *
  126. * Useful for testing or when reconfiguring the factory.
  127. */
  128. public static void clear_providers() {
  129. _sqlite_provider = null;
  130. _sqlite_registered = false;
  131. _postgres_provider = null;
  132. _postgres_registered = false;
  133. }
  134. /**
  135. * Checks if a provider is registered for a scheme.
  136. *
  137. * @param scheme The scheme to check
  138. * @return true if a provider is registered
  139. */
  140. public static bool has_provider(string scheme) {
  141. string scheme_lower = scheme.down();
  142. if (scheme_lower == "sqlite") {
  143. return _sqlite_registered;
  144. }
  145. if (scheme_lower == "postgres" || scheme_lower == "postgresql") {
  146. return _postgres_registered;
  147. }
  148. return false;
  149. }
  150. }
  151. }