43
43
//==================================
44
44
// My table event observers:
46
static bool insertRecord(TableEventData &data, unsigned char *new_row)
49
unsigned char *blob_rec;
50
char *blob_url, *possible_blob_url;
51
char safe_url[PBMS_BLOB_URL_SIZE+1];
52
PBMSBlobURLRec blob_url_buffer;
53
size_t packlength, i, length, org_length;
57
for (i= 0; i < data.table.sizeBlobFields(); i++) {
58
field = data.table.getBlobFieldAt(i);
60
// Get the blob record:
61
blob_rec = new_row + field->offset(data.table.getInsertRecord());
62
packlength = field->pack_length() - data.table.getBlobPtrSize();
64
length = field->get_length(blob_rec);
65
memcpy(&possible_blob_url, blob_rec +packlength, sizeof(char*));
66
org_length = field->get_length(blob_rec);
68
// Signal PBMS to record a new reference to the BLOB.
69
// If 'blob' is not a BLOB URL then it will be stored in the repositor as a new BLOB
70
// and a reference to it will be created.
71
if (MSEngine::couldBeURL(possible_blob_url, length) == false) {
72
err = MSEngine::createBlob(data.table.getSchemaName(), data.table.getTableName(), possible_blob_url, length, &blob_url_buffer, &result);
74
// If it fails log the error and continue to try and release any other BLOBs in the row.
75
fprintf(stderr, "PBMSEvents: createBlob(\"%s.%s\") error (%d):'%s'\n",
76
data.table.getSchemaName(), data.table.getTableName(), result.mr_code, result.mr_message);
80
blob_url = blob_url_buffer.bu_data;
82
// The BLOB URL may not be null terminate, if so
83
// then copy it to a safe buffer and terminate it.
84
if (possible_blob_url[length]) {
85
memcpy(safe_url, possible_blob_url, length);
89
blob_url = possible_blob_url;
92
// Signal PBMS to delete the reference to the BLOB.
93
err = MSEngine::referenceBlob(data.table.getSchemaName(), data.table.getTableName(), &blob_url_buffer, blob_url, field->position(), &result);
95
// If it fails log the error and continue to try and release any other BLOBs in the row.
96
fprintf(stderr, "PBMSEvents: referenceBlob(\"%s.%s\", \"%s\" ) error (%d):'%s'\n",
97
data.table.getSchemaName(), data.table.getTableName(), blob_url, result.mr_code, result.mr_message);
102
// The URL is modified on insert so if the BLOB length changed reset it.
103
// This will happen if the BLOB data was replaced with a BLOB reference.
104
length = strlen(blob_url_buffer.bu_data) +1;
105
if ((length != org_length) || memcmp(blob_url_buffer.bu_data, possible_blob_url, length)) {
106
char *blob = possible_blob_url; // This is the BLOB as the server currently sees it.
108
if (length != org_length) {
109
field->store_length(blob_rec, packlength, length);
112
if (length > org_length) {
113
// This can only happen if the BLOB URL is actually larger than the BLOB itself.
114
blob = (char *) data.session.alloc(length);
115
memcpy(blob_rec+packlength, &blob, sizeof(char*));
117
memcpy(blob, blob_url_buffer.bu_data, length);
125
static bool deleteRecord(TableEventData &data, const unsigned char *old_row)
128
const char *blob_rec;
129
unsigned char *blob_url;
130
size_t packlength, i, length;
132
PBMSResultRec result;
133
bool call_failed = false;
135
for (i= 0; i < data.table.sizeBlobFields(); i++) {
136
field = data.table.getBlobFieldAt(i);
138
// Get the blob record:
139
blob_rec = (char *)old_row + field->offset(data.table.getInsertRecord());
140
packlength = field->pack_length() - data.table.getBlobPtrSize();
142
length = field->get_length((unsigned char *)blob_rec);
143
memcpy(&blob_url, blob_rec +packlength, sizeof(char*));
145
// Check to see if this is a valid URL.
146
if (MSEngine::couldBeURL(blob_url, length)) {
148
// The BLOB URL may not be null terminate, if so
149
// then copy it to a safe buffer and terminate it.
150
char safe_url[PBMS_BLOB_URL_SIZE+1];
151
if (blob_url[length]) {
152
memcpy(safe_url, blob_url, length);
153
safe_url[length] = 0;
157
// Signal PBMS to delete the reference to the BLOB.
158
err = MSEngine::dereferenceBlob(data.table.getSchemaName(), data.table.getTableName(), blob_url, &result);
160
// If it fails log the error and continue to try and release any other BLOBs in the row.
161
fprintf(stderr, "PBMSEvents: dereferenceBlob(\"%s.%s\") error (%d):'%s'\n",
162
data.table.getSchemaName(), data.table.getTableName(), result.mr_code, result.mr_message);
173
45
static bool insertRecord(const char *db, const char *table_name, char *possible_blob_url, size_t length,
174
46
Session &session, Field_blob *field, unsigned char *blob_rec, size_t packlength)