In want to store an array in this field. [Field] public byte[] SerializedTaskPackage { get; private set; } And i get completely wrong array back (double size +1 )

It is a PostgreSQL specific problem, memory provider and sqlserver provider work. I use PostgreSQL 9.0.2 64-bit

After some research i found that the delivered data is ASCII encoded with a "\" at the beginning and two ASCII Hex Digits'0'-'9', 'a'-'f' for each byte:

byte[0]; -> {92}  "\"
{0} -> {92,48,48} "\00"
{0,1} -> {92,48,48,48,49} "\0001"

Here is the test code:

[Serializable]
[HierarchyRoot]
public class BlobEntity : Entity
    {
        [Field, Key]
        public int Id { get; private set; }

        [Field]
        public byte[] BlobData { get; set; }
    }

    static void Main(string[] args)
    {
        // var config = new DomainConfiguration("memory://localhost/DO40-Tests");
        // var config = new DomainConfiguration("sqlserver://localhost\\SQLExpress/do4test");
        var config = new DomainConfiguration("postgresql://do4test:do4test@localhost:5433/do4test");
        config.UpgradeMode = DomainUpgradeMode.Recreate;
        config.Types.Register(typeof(BlobEntity).Assembly, typeof(BlobEntity).Namespace);
        Domain domain = Domain.Build(config);

        using (Session.Open((Domain)domain))
        {
            using (var transactionScope = Transaction.Open())
            {
                var blob = new BlobEntity();

                blob.BlobData = new byte[] {0,1};

                transactionScope.Complete();
            }
        }

        using (Session.Open((Domain)domain))
        {
            using (var transactionScope = Transaction.Open())
            {
                foreach (BlobEntity blob in (from blob in Query.All<BlobEntity>() select blob ))
                {
                    var blobData = blob.BlobData;

                }
                transactionScope.Complete();
            }
        }
    }

asked Dec 17 '10 at 00:04

Thomas%20Maierhofer's gravatar image

Thomas Maierhofer
738812

edited Jan 04 '11 at 16:02

Sergey's gravatar image

Sergey
123339


3 Answers:

I have found the reason for this. There is a breaking change in 9.0 concerning bytea fields. In 9.0 they delivered byteea fields in the "hex" format instead of the legacy "escape" format for performance reasons.

Workaround for old Npgsql drivers: ALTER DATABASE foo SET bytea_output='escape';

This issue is fixed with the Npgsql2.0.11 driver (2010-11-05)

http://pgfoundry.org/frs/shownotes.php?release_id=1727

Please bind and include this driver in the next DO4 release. I think changes in DO4 code are not necessary.

answered Dec 18 '10 at 07:10

Thomas%20Maierhofer's gravatar image

Thomas Maierhofer
738812

edited Dec 18 '10 at 07:26

Thanks Thomas. Will do.

(Dec 18 '10 at 08:07) Dmitri Maximov Dmitri%20Maximov's gravatar image

DataObjects.Net 4.3.7 & 4.4 beta 2 with the last version of Npgsqlprovider is released

(Jan 29 '11 at 04:29) Dmitri Maximov Dmitri%20Maximov's gravatar image

Thanks, Thomas. We'll investigate the issue during the upcoming week. Special thanks for the test case. It will definitely help to speed up the process.

BTW, have you tried any previous version of PostgreSQL? I mean 8.x family. We haven't run our tests against PostgreSQL 9.0 yet.

answered Dec 17 '10 at 06:29

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

UPDATE: The test doesn't fail on PostgreSQL 8.x 32-bit.

(Dec 17 '10 at 11:36) Dmitri Maximov Dmitri%20Maximov's gravatar image

I want to store the byte[] on the postgreSQL , how can I do it ?

answered Sep 17 '11 at 12:09

szwhbu's gravatar image

szwhbu
5

Hello,

you should declare a byte[] field, like this:

public class BlobEntity : Entity
{
    ...

    [Field]
    public byte[] BlobData { get; set; }
}
(Sep 18 '11 at 04:59) Dmitri Maximov Dmitri%20Maximov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA