- 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's also an "owner" field in the "shares" table, but it
+ # contains a principal URL and not a bare username. Thus its
+ # 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()