~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/events_ms.cc

  • Committer: Marisa Plumb
  • Date: 2011-03-14 20:08:52 UTC
  • mfrom: (2232 staging)
  • mto: (2235.2.4 build)
  • mto: This revision was merged to the branch mainline in revision 2237.
  • Revision ID: marisa.plumb@gmail.com-20110314200852-luwrsw1bm2lno26p
updating and fixing slave plugin example

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 
43
43
//==================================
44
44
// My table event observers: 
45
 
#ifdef OLD_WAY
46
 
static bool insertRecord(TableEventData &data, unsigned char *new_row)
47
 
{
48
 
        Field_blob *field;
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;
54
 
        int32_t err;
55
 
        PBMSResultRec result;
56
 
        
57
 
        for (i= 0; i < data.table.sizeBlobFields(); i++) {
58
 
                field = data.table.getBlobFieldAt(i);
59
 
 
60
 
                // Get the blob record:
61
 
                blob_rec = new_row + field->offset(data.table.getInsertRecord());
62
 
                packlength = field->pack_length() - data.table.getBlobPtrSize();
63
 
 
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);
67
 
                
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);
73
 
                        if (err) {
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);
77
 
                                        
78
 
                                return true;
79
 
                        }                               
80
 
                        blob_url = blob_url_buffer.bu_data;
81
 
                } else {
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);
86
 
                                safe_url[length] = 0;
87
 
                                blob_url = safe_url;
88
 
                        } else
89
 
                                blob_url = possible_blob_url;
90
 
                }
91
 
                
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);
94
 
                if (err) {
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);
98
 
                                
99
 
                        return true;
100
 
                }
101
 
                
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.
107
 
                        
108
 
                        if (length != org_length) {
109
 
                                field->store_length(blob_rec, packlength, length);
110
 
                        }
111
 
                        
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*));
116
 
                        }                       
117
 
                        memcpy(blob, blob_url_buffer.bu_data, length);
118
 
                } 
119
 
        }
120
 
 
121
 
        return false;
122
 
}
123
 
 
124
 
//---
125
 
static bool deleteRecord(TableEventData &data, const unsigned char *old_row)
126
 
{
127
 
        Field_blob *field;
128
 
        const char *blob_rec;
129
 
        unsigned char *blob_url;
130
 
        size_t packlength, i, length;
131
 
        int32_t err;
132
 
        PBMSResultRec result;
133
 
        bool call_failed = false;
134
 
        
135
 
        for (i= 0; i < data.table.sizeBlobFields(); i++) {
136
 
                field = data.table.getBlobFieldAt(i);
137
 
 
138
 
                // Get the blob record:
139
 
                blob_rec = (char *)old_row + field->offset(data.table.getInsertRecord());
140
 
                packlength = field->pack_length() - data.table.getBlobPtrSize();
141
 
 
142
 
                length = field->get_length((unsigned char *)blob_rec);
143
 
                memcpy(&blob_url, blob_rec +packlength, sizeof(char*));
144
 
                
145
 
                // Check to see if this is a valid URL.
146
 
                if (MSEngine::couldBeURL(blob_url, length)) {
147
 
                
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;
154
 
                                blob_url = safe_url;
155
 
                        }
156
 
                        
157
 
                        // Signal PBMS to delete the reference to the BLOB.
158
 
                        err = MSEngine::dereferenceBlob(data.table.getSchemaName(), data.table.getTableName(), blob_url, &result);
159
 
                        if (err) {
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);
163
 
                                        
164
 
                                call_failed = true;
165
 
                        }
166
 
                }
167
 
        }
168
 
 
169
 
        return call_failed;
170
 
}
171
 
#endif
172
 
 
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)
175
47
{
222
94
                char *blob = possible_blob_url; // This is the BLOB as the server currently sees it.
223
95
                
224
96
                if (length != org_length) {
225
 
                        field->store_length(blob_rec, packlength);
 
97
                        field->store_length(blob_rec, length);
226
98
                }
227
99
                
228
100
                if (length > org_length) {