- connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
- @db_name, @db_user, @db_pass)
-
- sql_query = '(SELECT username FROM prefs)'
- sql_query += 'UNION'
- sql_query += '(SELECT user_from FROM shared);'
-
- connection.query(sql_query) do |result|
- users = result.field_values('username')
+ connection = PG::Connection.new(@db_hash)
+
+ # There are also "owner" and "with" fields in the "shares" table,
+ # but they contains principal URLs and not a bare username. Thus
+ # their format depends on the CalDAV server configuration, and
+ # isn't predictable.
+ sql_query = 'SELECT username FROM prefs;'
+
+ begin
+ connection.query(sql_query) do |result|
+ users = result.field_values('username')
+ end
+ ensure
+ # Make sure the connection gets closed even if the query explodes.
+ connection.close()