agilord/postgres_pool

Binary Large Objects

beautybird opened this issue · 6 comments

Hello,
does your API support saving BLOB type such as images and videos ?
I know Postgresql has bytea...do you support it ?

isoos commented

@beautybird: I usually convert the blob with base64.encode() (in dart:convert), and then in the query string use decode(@parameter, 'base64'). It is possible that this could be easily added to package:postgres, but I haven't had the time for it, as the encode/decode workaround was acceptable for my use-case.

@isoos ..Thanks for the workaround. I'm able to convert the image to base64...the issue now is with the query implementation...based on the API I did as follows: -

 bool updateImage;

  Future<bool> updateImages(String base64ImageFromAppField,String email) async{

pgPool = await The_Class_Name._pgPoolConn();  // getting the pool

await pgPool.runTx((c) async {  // getting the connection

  await c.execute('update table set logoImage = @logoImage where emailDB = @emailDB',

  substitutionValues: {'logoImage':base64Decode(base64ImageFromAppField),'emailDB' : email}

  ,timeoutInSeconds: 60)
      .then((updateResult) { // I'm not getting this step, 
                                                            //the DB not updated..
    							//and program just stop here 
      
    updateImage = (updateResult > 0 ? true : false ); 
   
  }).catchError((updateError){
    updateImage = false;
    updateError.toString();
  })
      .whenComplete(() => {
    if (updateImage == true)
      {
        pgPool.close(),
      }
    else
      {
        Timer(Duration(minutes: 3), () {
          pgPool.close();
        }),
      }
  });
});
return updateImage;

}

You mentioned in query to do :
decode(@parameter, 'base64');
but the syntax of the substitutionValues not accept your suggestion ..
Is my implemention wrong ?

isoos commented

I think you need to make the following two changes:

  • update table set logoImage = decode(@logoImage, 'base64') where ...
  • substitutionValues: {'logoImage':base64ImageFromAppField, 'emailDB' : email}

unrelated, but I find the following async block easier to follow:

try {
  await pgPool.runTx((c) async {
    final updateResult = await c.execute(...);
    return (updateResult > 0 ? true : false ); 
  });
} finally {
  await pgPool.close();
}

Also, closing pgPool will close all the current connections. I wouldn't do that after each query, as it is expensive to connect to the database. The reason you want to use PgPool is to keep the connections open for a while...

@isoos ; OK Thanks for the help ...this API saved my days :)
I worked with query & execute(insert & update) and all works fine along with transactions.
one last thing though ...does the query select accept more than one parameter(i.e, I want do select on a table and use 2 or more parameters in the where clause ) ?

isoos commented

Sure, you can use as many parameters as you like. Note: the actual query execution is done via package:postgres, this package only handles the connection pooling aspects.

Yes I'm aware of that .
Thanks.