www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Need help with bytea data type from PostgreSQL.

reply Suliman <evermind live.ru> writes:
I am still trying to get work any of PostgreSQL drivers. For last 
few day I had test all of drivers. All of them are _very_ buggy!
Just few of them have support of `bytea` data type, that use for 
binary blobs. But support does not mean that it's work.

Now I am trying to get https://github.com/IrenejMarc/dpq work.

```
import std.stdio;
import dpq.connection;
import dpq.query;
import dpq.attributes;
import dpq.result;
import dpq.value;

void main()
{

	auto conn = Connection("host=localhost dbname=test01 
user=postgres password='Infinity8'");
	
	string myq = `SELECT userblob FROM "USERS"`;
	auto q = Query(myq);
	Result r = q.run();
	
	ubyte [] x;
	
	foreach(row; r)
	{
		//x = row[0];
		writeln(row[0]); // need as!binary or so.
		readln;
	}
}
````

By docs binary data type should work, but I can't find way to set 
it's type. For examples for strings its doing like: .as!string

But what about binary? I have read sources and found place that 
can be sutable for it, but I do not know how to use it.
https://github.com/IrenejMarc/dpq/blob/6a5acc805a891b5cdab3f333fbae692aca042f5a/source/dpq/value.d#L237

But I have not ideas how to use it.
Apr 12 2016
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 12/04/2016 11:48 PM, Suliman wrote:
 I am still trying to get work any of PostgreSQL drivers. For last few
 day I had test all of drivers. All of them are _very_ buggy!
 Just few of them have support of `bytea` data type, that use for binary
 blobs. But support does not mean that it's work.

 Now I am trying to get https://github.com/IrenejMarc/dpq work.

 ```
 import std.stdio;
 import dpq.connection;
 import dpq.query;
 import dpq.attributes;
 import dpq.result;
 import dpq.value;

 void main()
 {

      auto conn = Connection("host=localhost dbname=test01 user=postgres
 password='Infinity8'");

      string myq = `SELECT userblob FROM "USERS"`;
      auto q = Query(myq);
      Result r = q.run();

      ubyte [] x;

      foreach(row; r)
      {
          //x = row[0];
          writeln(row[0]); // need as!binary or so.
          readln;
      }
 }
 ````

 By docs binary data type should work, but I can't find way to set it's
 type. For examples for strings its doing like: .as!string

 But what about binary? I have read sources and found place that can be
 sutable for it, but I do not know how to use it.
 https://github.com/IrenejMarc/dpq/blob/6a5acc805a891b5cdab3f333fbae692aca042f5a/source/dpq/value.d#L237


 But I have not ideas how to use it.
Have you tried .as!(ubyte[]); ?
Apr 12 2016
parent reply Suliman <evermind live.ru> writes:
With:
x = row[0].as!(ubyte[]);

I am getting error:

core.exception.AssertError C:\D\dmd2\windows\bin\..\..\src\phobos\s
d\typecons.d(1920): Called `get' on null Nullable!ubyte[].
Apr 12 2016
parent Garrick <irenej.marc gmail.com> writes:
On Tuesday, 12 April 2016 at 13:01:12 UTC, Suliman wrote:
 With:
 x = row[0].as!(ubyte[]);

 I am getting error:

 core.exception.AssertError C:\D\dmd2\windows\bin\..\..\src\phobos\s
d\typecons.d(1920): Called `get' on null Nullable!ubyte[].
I'm the author of the specific dpq library you're using. I've already answered you on GitHub, but just in case anyone stumbles upon this thread in the future. All the results returned by row[index] will be of type Nullable!Value, you must check if they're not NULL using the isNull method of Nullable, if the result can ever be NULL. What I suggest you do, whenever you are fetching a whole column, like for instance an user, is to use a User struct, which you can then fetch, in whole, using connection.findOne!User(<userId>), which will return a Nullabe!User that will only be null if no rows were selected. A bunch of examples of that are in the project's README. Specifically, I am talking about ensureSchema and findOne/findMany/... methods on Connection struct. Currently, the plan is to add a way to fetch scalar values from tables too.
Apr 12 2016