Recent

Author Topic: Check if a database exists - PostGres  (Read 8933 times)

kapibara

  • Hero Member
  • *****
  • Posts: 610
Check if a database exists - PostGres
« on: February 21, 2017, 05:53:24 pm »
To check if a database exist in Postgres, I wrote this function. When run from the IDE it raises an exception if the database doesn't exist. (Need to press continue) I know its possible to disable the exception in the IDE. But maybe there is a another way to check if a database exist that doesnt raise an exception?

Code: Pascal  [Select][+][-]
  1. function DBExists(const ADBName: string; AConn: TSQLConnection): Boolean;
  2. begin
  3.   Result:= False;
  4.   AConn.DatabaseName:=ADBName;
  5.   try
  6.     try
  7.       AConn.Open;
  8.       Result:= AConn.Connected;
  9.     except
  10.       ;
  11.     end;
  12.   finally
  13.     AConn.Close;
  14.   end;
  15. end;
  16.  

Would it be useful to have a similar function within the TSQLConnection? To call like this:

Code: Pascal  [Select][+][-]
  1. if Conn.DBExists('dbname') then ..
« Last Edit: February 21, 2017, 06:08:01 pm by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Check if a database exists - PostGres
« Reply #1 on: February 21, 2017, 06:06:01 pm »
You could try FileExists function:

http://www.freepascal.org/docs-html/rtl/sysutils/fileexists.html
http://stackoverflow.com/questions/1137060/where-does-postgresql-store-the-database

Code: Pascal  [Select][+][-]
  1. Program Example38;
  2.  
  3. { This program demonstrates the FileExists function }
  4.  
  5. Uses SysUtils;
  6.  
  7. begin
  8.   if FileExists(ParamStr(0)) then
  9.     WriteLn ('All is well, I seem to exist.');
  10. end.
  11.  

balazsszekely

  • Guest
Re: Check if a database exists - PostGres
« Reply #2 on: February 21, 2017, 06:17:08 pm »
Quote
@valdir.marcos
You could try FileExists function:
What if the database is on a remote server? In my opinion the only reliable way to check if a db exists is a successful connect. 

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Check if a database exists - PostGres
« Reply #3 on: February 21, 2017, 06:27:57 pm »
Pardon my intrusion but afaik something is not quite right with the flow of the shown code as well.

In shown case, finally is always called. Also in case of an exception. afaik closing a not open connection will in itself raise an exception ? Either use force or add exception handler ? (or better yet, use another flow)

And to answer the question what to add to the exception part:
Code: [Select]
    except
      On E: Exception do
      begin
        WriteLn('Exception');
        Result := False;
      end;

Still requires to ignore the (specific) exception in the ide though as it will still trigger by default.
« Last Edit: February 21, 2017, 06:32:51 pm by molly »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Check if a database exists - PostGres
« Reply #4 on: February 21, 2017, 06:33:24 pm »
Quote
@valdir.marcos
You could try FileExists function:

What if the database is on a remote server? In my opinion the only reliable way to check if a db exists is a successful connect. 

I agree with you and also use the "try ... except" solution, but @kapibara is searching for other alternatives.

balazsszekely

  • Guest
Re: Check if a database exists - PostGres
« Reply #5 on: February 21, 2017, 06:36:57 pm »
Good catch @molly:
Code: Pascal  [Select][+][-]
  1. function DBExists(const ADBName: string; AConn: TSQLConnection): Boolean;
  2. begin
  3.   Result:= False;
  4.   AConn.DatabaseName:=ADBName;
  5.   try
  6.     AConn.Open;
  7.     Result:= AConn.Connected;
  8.     if Result then
  9.       AConn.Close;
  10.   except    
  11.   end;    
  12. end;
  13.  

But even the modified code won't prevent the IDE to raise an exception if the connection fails.

paweld

  • Hero Member
  • *****
  • Posts: 991
Re: Check if a database exists - PostGres
« Reply #6 on: February 21, 2017, 06:47:46 pm »
Code: Pascal  [Select][+][-]
  1. function DBExists(const ADBName: string; AConn: TSQLConnection): Boolean;
  2. begin
  3.   Result:= False;
  4.   AConn.DatabaseName:='postgres';
  5.   try
  6.     AConn.Open;
  7.     Query1.SQL.Text:=' select 1 from pg_database where name=:dbname ';
  8.     Query1.ParamByName('dbname').AsString:=ADBName;
  9.     Query1.Open;
  10.     Result:=Query1.RecordCount>0;
  11.     AConn.Close;
  12.   except    
  13.   end;    
  14. end;
Best regards / Pozdrawiam
paweld

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Check if a database exists - PostGres
« Reply #7 on: February 21, 2017, 06:59:47 pm »
But even the modified code won't prevent the IDE to raise an exception if the connection fails.
Doesn't ide options debugger ignore language exception work for you ? You have to be explicit about the exception though.

Once the exception triggered the ide even ask if it is allowed to ignore the specific exception. Or does opening a non existent database causes an external sigv error ?

balazsszekely

  • Guest
Re: Check if a database exists - PostGres
« Reply #8 on: February 21, 2017, 07:40:39 pm »
Quote
@molly
Doesn't ide options debugger ignore language exception work for you ? You have to be explicit about the exception though.
Sure, it works, I only respond to OPs first post:
Quote
When run from the IDE it raises an exception if the database doesn't exist. (Need to press continue) I know its possible to disable the exception in the IDE. But maybe there is a another way to check if a database exist that doesnt raise an exception?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Check if a database exists - PostGres
« Reply #9 on: February 21, 2017, 07:44:17 pm »
Sorry GetMem. I realized too late that you was and i did not wanted to delete my post (i could have edited it though but instead left it).

 

TinyPortal © 2005-2018