4

我在数据库中存储了一个 240 字节(1920 位)的二进制字符串(目前是 MySQL,但我想保持数据库独立)。

我需要执行以下操作:

UPDATE `my_table` SET `data` = `data` ^ 'new_data' WHERE `id` = 'field_id'

我相信 SQL 语法按位 XOR 仅适用于高达 6b 位的值。

我在文本字段中使用十六进制编码存储数据,但如有必要,我可以将其更改为保存二进制数据的 blob 字段。

目前我正在使用 PHP 加载该字段。然后我正在做 XOR 并将其写回。

但是,此字段对所有脚本都是全局的,并且此方法存在以下风险:

Script X Loads Data0
Script Y Loads Data0
Script X Generates Data1 = Data0 ^ new_data1
Script Y Generates Data2 = Data0 ^ new_data2
Script X writes Data1
Script Y writes Data2 Error: dosen't contain new_data1

如果这是使用 SQL 语句完成的,则如下所示:

UPDATE `my_table` SET `data` = `data` ^ 'new_data1' WHERE `id` = 'field_id'
UPDATE `my_table` SET `data` = `data` ^ 'new_data2' WHERE `id` = 'field_id'

通过避免上述错误,数据库将在那里按顺序执行查询

如果我使用 64 位整数来存储这些数据,那么由于大小的原因,我必须将其分成 30 个单独的值。这个选项不合理。

有谁知道实现这一目标的数据库独立方式?

4

1 回答 1

1

Consider writing a C language User Defined Function (UDF) to do this operation. C UDF's are supported in many databases MySQL, PostgreSQL, Oracle, ....

They are fast and integrate well into SQL, as they are called like any other stored proc. Unfortunately the creation syntax and binding API all differ for each database.

Here is documentation for MySQL

http://dev.mysql.com/doc/refman/5.0/en/udf-compiling.html

and a Codeproject example for MySQL

http://www.codeproject.com/Articles/15643/MySQL-User-Defined-Functions

and PostgreSQL docs http://www.postgresql.org/docs/9.2/static/xfunc-c.html

there many examples of C UDF's in the PostgreSQL source. http://doxygen.postgresql.org/

PostgreSQL takes UDF's one step further and allows to to create C based User Define Types and Operators.

于 2013-05-25T14:48:35.030 回答