~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2010-07-06 00:44:32 UTC
  • mfrom: (1643.1.13 build)
  • Revision ID: mordred@inaugust.com-20100706004432-843uftc92rc2497l
Merged in PBMS, translation updates, a few build fixes and a few bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Original author: Paul McCullagh
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-07-20
 
23
 *
 
24
 * H&G2JCtL
 
25
 *
 
26
 * Engine interface.
 
27
 *
 
28
 */
 
29
#ifdef DRIZZLED
 
30
#include "config.h"
 
31
#include <drizzled/common.h>
 
32
#include <drizzled/session.h>
 
33
#endif
 
34
 
 
35
 
 
36
#include "cslib/CSConfig.h"
 
37
#include "cslib/CSGlobal.h"
 
38
#include "cslib/CSStrUtil.h"
 
39
#include "cslib/CSThread.h"
 
40
 
 
41
#include "engine_ms.h"
 
42
#include "connection_handler_ms.h"
 
43
#include "open_table_ms.h"
 
44
#include "network_ms.h"
 
45
#include "transaction_ms.h"
 
46
#include "mysql_ms.h"
 
47
 
 
48
 
 
49
#ifdef new
 
50
#undef new
 
51
#endif
 
52
 
 
53
// From ha-pbms.cc:
 
54
extern CSThread *pbms_getMySelf(THD *thd);
 
55
extern void pbms_setMySelf(THD *thd, CSThread *self);
 
56
 
 
57
#ifndef DRIZZLED
 
58
 
 
59
/*
 
60
 * ---------------------------------------------------------------
 
61
 * ENGINE CALL-IN INTERFACE
 
62
 */
 
63
 
 
64
static PBMS_API *StreamingEngines;
 
65
// If PBMS support is built directly into the mysql/drizzle handler code 
 
66
// then calls from all other handlers are ignored.
 
67
static bool have_handler_support = false; 
 
68
 
 
69
/*
 
70
 * ---------------------------------------------------------------
 
71
 * ENGINE CALLBACK INTERFACE
 
72
 */
 
73
 
 
74
static void ms_register_engine(PBMSEnginePtr engine)
 
75
{
 
76
        if (engine->ms_internal)
 
77
                have_handler_support = true;
 
78
}
 
79
 
 
80
static void ms_deregister_engine(PBMSEnginePtr engine)
 
81
{
 
82
        UNUSED(engine);
 
83
}
 
84
 
 
85
static int ms_create_blob(bool internal, const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url, PBMSResultPtr result)
 
86
{
 
87
        if (have_handler_support && !internal) {
 
88
                errorResult(CS_CONTEXT, MS_ERR_INVALID_OPERATION, "Invalid ms_create_blob() call", result);
 
89
                return MS_ERR_ENGINE;
 
90
        }
 
91
 
 
92
        return MSEngine::createBlob(db_name, tab_name, blob, blob_len, blob_url, result);
 
93
}
 
94
 
 
95
/*
 
96
 * ms_use_blob() may or may not alter the blob url depending on the type of URL and if the BLOB is in a
 
97
 * different database or not. It may also add a BLOB reference to the BLOB table log if the BLOB was from
 
98
 * a different table or no table was specified when the BLOB was uploaded.
 
99
 *
 
100
 * There is no need to undo this function because it will be undone automaticly if the BLOB is not retained.
 
101
 */
 
102
static int ms_retain_blob(bool internal, const char *db_name, const char *tab_name, PBMSBlobURLPtr ret_blob_url, char *blob_url, unsigned short col_index, PBMSResultPtr result)
 
103
{
 
104
        if (have_handler_support && !internal) {
 
105
                cs_strcpy(PBMS_BLOB_URL_SIZE, ret_blob_url->bu_data, blob_url); // This should have already been converted.
 
106
                return MS_OK;
 
107
        }
 
108
        
 
109
        return MSEngine::referenceBlob(db_name, tab_name, ret_blob_url, blob_url, col_index, result);   
 
110
}
 
111
 
 
112
static int ms_release_blob(bool internal, const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
 
113
{
 
114
 
 
115
        if (have_handler_support && !internal) 
 
116
                return MS_OK;
 
117
        
 
118
        return MSEngine::dereferenceBlob(db_name, tab_name, blob_url, result);  
 
119
}
 
120
 
 
121
static int ms_drop_table(bool internal, const char *db_name, const char *tab_name, PBMSResultPtr result)
 
122
{
 
123
        if (have_handler_support && !internal) 
 
124
                return MS_OK;
 
125
 
 
126
        return MSEngine::dropTable(db_name, tab_name, result);  
 
127
}
 
128
 
 
129
static int ms_rename_table(bool internal, const char * db_name, const char *from_table, const char *to_db, const char *to_table, PBMSResultPtr result)
 
130
{
 
131
        if (have_handler_support && !internal) 
 
132
                return MS_OK;
 
133
 
 
134
        return MSEngine::renameTable(db_name, from_table, to_db, to_table, result);     
 
135
}
 
136
 
 
137
static void ms_completed(bool internal, bool ok)
 
138
{
 
139
        if (have_handler_support && !internal) 
 
140
                return;
 
141
                
 
142
        MSEngine::callCompleted(ok);    
 
143
}
 
144
 
 
145
PBMSCallbacksRec engine_callbacks = {
 
146
        MS_CALLBACK_VERSION,
 
147
        ms_register_engine,
 
148
        ms_deregister_engine,
 
149
        ms_create_blob,
 
150
        ms_retain_blob,
 
151
        ms_release_blob,
 
152
        ms_drop_table,
 
153
        ms_rename_table,
 
154
        ms_completed
 
155
};
 
156
 
 
157
// =============================
 
158
int MSEngine::startUp(PBMSResultPtr result)
 
159
{
 
160
        int err = 0;
 
161
        
 
162
        StreamingEngines = new PBMS_API();
 
163
        err = StreamingEngines->PBMSStartup(&engine_callbacks, result);
 
164
        if (err)
 
165
                delete StreamingEngines;
 
166
        else { // Register the PBMS enabled engines the startup before PBMS
 
167
                PBMSSharedMemoryPtr             sh_mem = StreamingEngines->sharedMemory;
 
168
                PBMSEnginePtr                   engine;
 
169
                
 
170
                for (int i=0; i<sh_mem->sm_list_len; i++) {
 
171
                        if ((engine = sh_mem->sm_engine_list[i])) 
 
172
                                ms_register_engine(engine);
 
173
                }
 
174
        }
 
175
        return err;
 
176
}
 
177
 
 
178
void MSEngine::shutDown()
 
179
{
 
180
        StreamingEngines->PBMSShutdown();
 
181
 
 
182
        delete StreamingEngines;
 
183
}
 
184
 
 
185
const PBMSEnginePtr MSEngine::getEngineInfoAt(int indx)
 
186
{
 
187
        PBMSSharedMemoryPtr             sh_mem = StreamingEngines->sharedMemory;
 
188
        PBMSEnginePtr                   engine = NULL;
 
189
        
 
190
        if (sh_mem) {
 
191
                for (int i=0; i<sh_mem->sm_list_len; i++) {
 
192
                        if ((engine = sh_mem->sm_engine_list[i])) {
 
193
                                if (!indx)
 
194
                                        return engine;
 
195
                                indx--;
 
196
                        }
 
197
                }
 
198
        }
 
199
        
 
200
        return NULL;
 
201
}
 
202
#endif  
 
203
 
 
204
//---------------
 
205
int32_t MSEngine::createBlob(const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url, PBMSResultPtr result)
 
206
{
 
207
 
 
208
        CSThread                *self;
 
209
        int32_t                 err = MS_OK;
 
210
        MSOpenTable             *otab;
 
211
        CSInputStream   *i_stream = NULL;
 
212
        
 
213
        CLOBBER_PROTECT(err);
 
214
 
 
215
        if ((err = enterConnectionNoThd(&self, result)))
 
216
                return err;
 
217
 
 
218
        inner_();
 
219
        try_(a) {
 
220
                otab = openTable(db_name, tab_name, true);
 
221
                frompool_(otab);
 
222
                
 
223
                if (!otab->getDB()->isRecovering()) {
 
224
                        i_stream = CSMemoryInputStream::newStream((unsigned char *)blob, blob_len);
 
225
                        otab->createBlob(blob_url, blob_len, NULL, 0, i_stream);
 
226
                } else
 
227
                        CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot create BLOBs during repository recovery.");
 
228
 
 
229
                backtopool_(otab);
 
230
        }
 
231
        catch_(a) {
 
232
                err = exceptionToResult(&self->myException, result);
 
233
        }
 
234
        cont_(a);
 
235
        return_(err);
 
236
}
 
237
 
 
238
//---------------
 
239
int32_t MSEngine::referenceBlob(const char *db_name, const char *tab_name, PBMSBlobURLPtr ret_blob_url, char *blob_url, uint16_t col_index, PBMSResultPtr result)
 
240
{
 
241
 
 
242
        CSThread                *self;
 
243
        int32_t                 err = MS_OK;
 
244
        MSBlobURLRec    blob;
 
245
        MSOpenTable             *otab;
 
246
        
 
247
        CLOBBER_PROTECT(err);
 
248
 
 
249
        if ((err = enterConnectionNoThd(&self, result)))
 
250
                return err;
 
251
 
 
252
        inner_();
 
253
        try_(a) {
 
254
                
 
255
                if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
 
256
                        char buffer[CS_EXC_MESSAGE_SIZE];
 
257
                        
 
258
                        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
 
259
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
 
260
                        CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
 
261
                }
 
262
                
 
263
                otab = openTable(db_name, tab_name, true);
 
264
                frompool_(otab);
 
265
 
 
266
                otab->useBlob(blob.bu_type, blob.bu_db_id, blob.bu_tab_id, blob.bu_blob_id, blob.bu_auth_code, col_index, blob.bu_blob_size, blob.bu_blob_ref_id, ret_blob_url);
 
267
 
 
268
                backtopool_(otab);
 
269
        }
 
270
        catch_(a) {
 
271
                err = exceptionToResult(&self->myException, result);
 
272
        }
 
273
        cont_(a);
 
274
        return_(err);
 
275
}
 
276
 
 
277
//---------------
 
278
int32_t MSEngine::dereferenceBlob(const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
 
279
{
 
280
        CSThread                *self;
 
281
        int32_t                 err = MS_OK;
 
282
        MSBlobURLRec    blob;
 
283
        MSOpenTable             *otab;
 
284
 
 
285
        CLOBBER_PROTECT(err);
 
286
 
 
287
        if ((err = enterConnectionNoThd(&self, result)))
 
288
                return err;
 
289
 
 
290
        inner_();
 
291
        try_(a) {
 
292
                if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
 
293
                        char buffer[CS_EXC_MESSAGE_SIZE];
 
294
 
 
295
                        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
 
296
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
 
297
                        CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
 
298
                }
 
299
                
 
300
                otab = openTable(db_name, tab_name, true);
 
301
                frompool_(otab);
 
302
                if (!otab->getDB()->isRecovering()) {
 
303
                        if (otab->getTableID() == blob.bu_tab_id)
 
304
                                otab->releaseReference(blob.bu_blob_id, blob.bu_blob_ref_id);
 
305
                        else {
 
306
                                char buffer[CS_EXC_MESSAGE_SIZE];
 
307
 
 
308
                                cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect table ID: ");
 
309
                                cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
 
310
                                CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
 
311
                        }
 
312
                }
 
313
                else {
 
314
                        char buffer[CS_EXC_MESSAGE_SIZE];
 
315
 
 
316
                        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
 
317
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
 
318
                        CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
 
319
                }
 
320
                
 
321
                backtopool_(otab);                              
 
322
        }
 
323
        catch_(a) {
 
324
                err = exceptionToResult(&self->myException, result);
 
325
        }
 
326
        cont_(a);
 
327
        return_(err);
 
328
}
 
329
 
 
330
int32_t MSEngine::dropDatabase(const char *db_name, PBMSResultPtr result)
 
331
{
 
332
        CSThread *self;
 
333
        int             err = MS_OK;;
 
334
        
 
335
        CLOBBER_PROTECT(err);
 
336
 
 
337
        if ((err = enterConnectionNoThd(&self, result)))
 
338
                return err;
 
339
        inner_();
 
340
        
 
341
        try_(a) {
 
342
                MSDatabase::dropDatabase(db_name);
 
343
        }
 
344
        catch_(a) {
 
345
                err = exceptionToResult(&self->myException, result);
 
346
        }
 
347
        cont_(a);
 
348
        return_(err);
 
349
}
 
350
//---------------
 
351
typedef struct UnDoInfo {
 
352
        bool udo_WasRename;
 
353
        CSString *udo_toDatabaseName;
 
354
        CSString *udo_fromDatabaseName;
 
355
        CSString *udo_OldName;
 
356
        CSString *udo_NewName;
 
357
} UnDoInfoRec, *UnDoInfoPtr;
 
358
 
 
359
int32_t MSEngine::dropTable(const char *db_name, const char *tab_name, PBMSResultPtr result)
 
360
{
 
361
        CSThread        *self;
 
362
        int                     err = MS_OK;
 
363
 
 
364
        CLOBBER_PROTECT(err);
 
365
 
 
366
        if ((err = enterConnectionNoThd(&self, result)))
 
367
                return err;
 
368
 
 
369
        inner_();
 
370
        try_(a) {
 
371
 
 
372
                CSPath                  *new_path;
 
373
                CSPath                  *old_path;
 
374
                MSOpenTable             *otab;
 
375
                MSOpenTablePool *tab_pool;
 
376
                MSTable                 *tab;
 
377
                UnDoInfoPtr             undo_info = NULL;
 
378
 
 
379
                otab = openTable(db_name, tab_name, false);
 
380
                if (!otab)
 
381
                        goto exit;
 
382
                
 
383
                // If we are recovering do not delete the table.
 
384
                // It is normal for MySQL recovery scripts to delete any table they aare about to
 
385
                // recover and then recreate it. If this is done after the repository has been recovered
 
386
                // then this would delete all the recovered BLOBs in the table.
 
387
                if (otab->getDB()->isRecovering()) {
 
388
                        otab->returnToPool();
 
389
                        goto exit;
 
390
                }
 
391
 
 
392
                frompool_(otab);
 
393
 
 
394
                // Before dropping the table the table ref file is renamed so that
 
395
                // it is out of the way incase a new table is created before the
 
396
                // old one is cleaned up.
 
397
                
 
398
                old_path = otab->getDBTable()->getTableFile();
 
399
                push_(old_path);
 
400
 
 
401
                new_path = otab->getDBTable()->getTableFile(tab_name, true);
 
402
 
 
403
                // Rearrage the object stack to pop the otab object
 
404
                pop_(old_path);
 
405
                pop_(otab);
 
406
 
 
407
                push_(new_path);
 
408
                push_(old_path);
 
409
                frompool_(otab);
 
410
                
 
411
                tab = otab->getDBTable();
 
412
                pop_(otab);
 
413
                push_(tab);
 
414
 
 
415
                tab_pool = MSTableList::lockTablePoolForDeletion(otab);
 
416
                frompool_(tab_pool);
 
417
 
 
418
                if (old_path->exists())
 
419
                        old_path->move(new_path);
 
420
                tab->myDatabase->dropTable(RETAIN(tab));
 
421
                
 
422
                /* Add the table to the temp delete list if we are not recovering... */
 
423
                tab->prepareToDelete();
 
424
 
 
425
                backtopool_(tab_pool);  // The will unlock and close the table pool freeing all tables in it.
 
426
                pop_(tab);                              // Returning the pool will have released this. (YUK!)
 
427
                release_(old_path);
 
428
                release_(new_path);
 
429
 
 
430
 
 
431
                undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
 
432
                
 
433
                undo_info->udo_WasRename = false;
 
434
                self->myInfo = undo_info;
 
435
                
 
436
                
 
437
exit: ;
 
438
        }
 
439
        
 
440
        catch_(a) {
 
441
                err = exceptionToResult(&self->myException, result);
 
442
        }
 
443
        cont_(a);
 
444
        outer_();
 
445
        exitConnection();
 
446
        return err;
 
447
}
 
448
 
 
449
//---------------
 
450
static void completeDeleteTable(UnDoInfoPtr info, bool ok)
 
451
{
 
452
        // TO DO: figure out a way to undo the delete.
 
453
        cs_free(info);
 
454
        if (!ok) 
 
455
                CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot undo delete table.");
 
456
}
 
457
 
 
458
//---------------
 
459
bool MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table)
 
460
{
 
461
        MSOpenTable             *otab;
 
462
        CSPath                  *from_path;
 
463
        CSPath                  *to_path;
 
464
        MSOpenTablePool *tab_pool;
 
465
        MSTable                 *tab;
 
466
 
 
467
        enter_();
 
468
        
 
469
        if (strcmp(to_db_name, from_db_name) != 0) {
 
470
                CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot rename tables containing BLOBs across databases (yet). Sorry!");
 
471
        }
 
472
        
 
473
        otab = openTable(from_db_name, from_table, false);
 
474
        if (!otab)
 
475
                return_(false);
 
476
                
 
477
        frompool_(otab);
 
478
 
 
479
        if (otab->getDB()->isRecovering()) 
 
480
                CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot rename tables during repository recovery.");
 
481
 
 
482
        from_path = otab->getDBTable()->getTableFile();
 
483
        push_(from_path);
 
484
 
 
485
        to_path = otab->getDBTable()->getTableFile(to_table, false);
 
486
 
 
487
        // Rearrage the object stack to pop the otab object
 
488
        pop_(from_path);
 
489
        pop_(otab);
 
490
 
 
491
        push_(to_path);
 
492
        push_(from_path);
 
493
        frompool_(otab);
 
494
 
 
495
        otab->openForReading();
 
496
        tab = otab->getDBTable();
 
497
        tab->retain();
 
498
        pop_(otab);
 
499
        push_(tab);
 
500
        
 
501
        tab_pool = MSTableList::lockTablePoolForDeletion(otab);
 
502
        frompool_(tab_pool);
 
503
 
 
504
        from_path->move(to_path);
 
505
        tab->myDatabase->renameTable(tab, to_table);
 
506
 
 
507
        backtopool_(tab_pool);  // The will unlock and close the table pool freeing all tables in it.
 
508
        pop_(tab);                              // Returning the pool will have released this. (YUK!)
 
509
        release_(from_path);
 
510
        release_(to_path);
 
511
        
 
512
        return_(true);
 
513
}
 
514
 
 
515
//---------------
 
516
int32_t MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table, PBMSResultPtr result)
 
517
{
 
518
        CSThread        *self;
 
519
        int err = MS_OK;
 
520
 
 
521
        CLOBBER_PROTECT(err);
 
522
 
 
523
        if ((err = enterConnectionNoThd(&self, result)))
 
524
                return err;
 
525
 
 
526
        inner_();
 
527
        try_(a) {
 
528
                UnDoInfoPtr undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
 
529
                push_ptr_(undo_info);
 
530
 
 
531
                undo_info->udo_WasRename = true;
 
532
                if (renameTable(from_db_name, from_table, to_db_name, to_table)) {              
 
533
                        undo_info->udo_fromDatabaseName = CSString::newString(from_db_name);
 
534
                        push_(undo_info->udo_fromDatabaseName);
 
535
 
 
536
                        undo_info->udo_toDatabaseName = CSString::newString(to_db_name);
 
537
                        push_(undo_info->udo_toDatabaseName);
 
538
 
 
539
                        undo_info->udo_OldName = CSString::newString(from_table);
 
540
                        push_(undo_info->udo_OldName);
 
541
 
 
542
                        undo_info->udo_NewName = CSString::newString(to_table);
 
543
                        
 
544
                        pop_(undo_info->udo_OldName);
 
545
                        pop_(undo_info->udo_toDatabaseName);
 
546
                        pop_(undo_info->udo_fromDatabaseName);
 
547
                } else {
 
548
                        undo_info->udo_fromDatabaseName = undo_info->udo_toDatabaseName = undo_info->udo_OldName = undo_info->udo_NewName = NULL;
 
549
                }
 
550
                self->myInfo = undo_info;
 
551
                pop_(undo_info);
 
552
        }
 
553
        catch_(a) {
 
554
                err = exceptionToResult(&self->myException, result);
 
555
        }
 
556
        cont_(a);
 
557
        outer_();
 
558
        exitConnection();
 
559
        return err;
 
560
}
 
561
 
 
562
//---------------
 
563
void MSEngine::completeRenameTable(UnDoInfoPtr info, bool ok)
 
564
{
 
565
        // Swap the paths around here to revers the rename.
 
566
        CSString                *from_db_name= info->udo_toDatabaseName;
 
567
        CSString                *to_db_name= info->udo_fromDatabaseName;
 
568
        CSString                *from_table= info->udo_NewName;
 
569
        CSString                *to_table= info->udo_OldName;
 
570
        
 
571
        enter_();
 
572
        
 
573
        cs_free(info);
 
574
        if (from_db_name) {
 
575
                push_(from_db_name);
 
576
                push_(from_table);
 
577
                push_(to_db_name);
 
578
                push_(to_table);
 
579
                if (!ok) 
 
580
                        renameTable(from_db_name->getCString(), from_table->getCString(), to_db_name->getCString(), to_table->getCString());
 
581
                        
 
582
                release_(to_table);
 
583
                release_(to_db_name);
 
584
                release_(from_table);
 
585
                release_(from_db_name);
 
586
        }
 
587
        exit_();
 
588
}
 
589
//---------------
 
590
void MSEngine::callCompleted(bool ok)
 
591
{
 
592
        CSThread        *self;
 
593
        PBMSResultRec   result;
 
594
        
 
595
        if (enterConnectionNoThd(&self, &result))
 
596
                return ;
 
597
 
 
598
        if (self->myInfo) {
 
599
                UnDoInfoPtr info = (UnDoInfoPtr) self->myInfo;
 
600
                if (info->udo_WasRename) 
 
601
                        completeRenameTable(info, ok);
 
602
                else 
 
603
                        completeDeleteTable(info, ok);
 
604
 
 
605
                
 
606
                self->myInfo = NULL;
 
607
        } else if (self->myTID && (self->myIsAutoCommit || !ok)) {
 
608
                inner_();
 
609
                try_(a) {
 
610
                        if (ok)
 
611
                                MSTransactionManager::commit();
 
612
                        else if (self->myIsAutoCommit)
 
613
                                MSTransactionManager::rollback();
 
614
                        else
 
615
                                MSTransactionManager::rollbackToPosition(self->myStartStmt); // Rollback the last logical statement.
 
616
                }
 
617
                catch_(a) {
 
618
                        self->logException();
 
619
                }
 
620
                cont_(a);
 
621
                outer_();
 
622
        }
 
623
        
 
624
        self->myStartStmt = self->myStmtCount;
 
625
}
 
626
 
 
627
//---------------
 
628
MSOpenTable *MSEngine::openTable(const char *db_name, const char *tab_name, bool create)
 
629
{
 
630
        MSOpenTable             *otab = NULL;
 
631
        uint32_t db_id, tab_id;
 
632
        enter_();
 
633
        
 
634
        if ( MSDatabase::convertTableAndDatabaseToIDs(db_name, tab_name, &db_id, &tab_id, create))  
 
635
                otab = MSTableList::getOpenTableByID(db_id, tab_id);
 
636
                
 
637
        return_(otab);
 
638
}
 
639
 
 
640
//---------------
 
641
bool MSEngine::couldBeURL(const char *blob_url, size_t length)
 
642
{
 
643
        MSBlobURLRec blob;
 
644
        return PBMSBlobURLTools::couldBeURL(blob_url, length, &blob);
 
645
}
 
646
 
 
647
//---------------
 
648
int MSEngine::exceptionToResult(CSException *e, PBMSResultPtr result)
 
649
{
 
650
        const char *context, *trace;
 
651
 
 
652
        result->mr_code = e->getErrorCode();
 
653
        cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, e->getMessage());
 
654
        context = e->getContext();
 
655
        trace = e->getStackTrace();
 
656
        if (context && *context) {
 
657
                cs_strcpy(MS_RESULT_STACK_SIZE, result->mr_stack, context);
 
658
                if (trace && *trace)
 
659
                        cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, "\n");
 
660
        }
 
661
        else
 
662
                *result->mr_stack = 0;
 
663
        if (trace && *trace)
 
664
                cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, trace);
 
665
        return MS_ERR_ENGINE;
 
666
}
 
667
 
 
668
//---------------
 
669
int MSEngine::errorResult(const char *func, const char *file, int line, int err, const char *message, PBMSResultPtr result)
 
670
{
 
671
        CSException e;
 
672
                
 
673
        e.initException(func, file, line, err, message);
 
674
        return exceptionToResult(&e, result);
 
675
}
 
676
 
 
677
//---------------
 
678
int MSEngine::osErrorResult(const char *func, const char *file, int line, int err, PBMSResultPtr result)
 
679
{
 
680
        CSException e;
 
681
                
 
682
        e.initOSError(func, file, line, err);
 
683
        return MSEngine::exceptionToResult(&e, result);
 
684
}
 
685
 
 
686
//---------------
 
687
int MSEngine::enterConnection(THD *thd, CSThread **r_self, PBMSResultPtr result, bool doCreate)
 
688
{
 
689
        CSThread        *self = NULL;
 
690
 
 
691
#ifndef DRIZZLED
 
692
        // In drizzle there is no 1:1 relationship between pthreads and sessions
 
693
        // so we must always get it from the session handle NOT the current pthread.
 
694
        self = CSThread::getSelf();
 
695
#endif
 
696
        if (!self) {    
 
697
                if (thd) {
 
698
                        if (!(self = pbms_getMySelf(thd))) {
 
699
                                if (!doCreate)
 
700
                                        return MS_ERR_NOT_FOUND;
 
701
                                        
 
702
                                if (!(self = CSThread::newCSThread()))
 
703
                                        return osErrorResult(CS_CONTEXT, ENOMEM, result);
 
704
                                if (!CSThread::attach(self))
 
705
                                        return MSEngine::exceptionToResult(&self->myException, result);
 
706
                                pbms_setMySelf(thd, self);
 
707
                        } else {
 
708
                                if (!CSThread::setSelf(self))
 
709
                                        return MSEngine::exceptionToResult(&self->myException, result);
 
710
                        }
 
711
                } else {
 
712
                        if (!doCreate)
 
713
                                return MS_ERR_NOT_FOUND;
 
714
                                
 
715
                        if (!(self = CSThread::newCSThread()))
 
716
                                return osErrorResult(CS_CONTEXT, ENOMEM, result);
 
717
                        if (!CSThread::attach(self))
 
718
                                return MSEngine::exceptionToResult(&self->myException, result);
 
719
                }
 
720
        }
 
721
 
 
722
        *r_self = self;
 
723
        return MS_OK;
 
724
}
 
725
 
 
726
//---------------
 
727
int MSEngine::enterConnectionNoThd(CSThread **r_self, PBMSResultPtr result)
 
728
{
 
729
        return enterConnection(current_thd, r_self, result, true);
 
730
}
 
731
 
 
732
//---------------
 
733
void MSEngine::exitConnection()
 
734
{
 
735
        THD                     *thd = (THD *) current_thd;
 
736
        CSThread        *self;
 
737
 
 
738
        self = CSThread::getSelf();
 
739
        if (self && self->pbms_api_owner)
 
740
                return;
 
741
 
 
742
 
 
743
        if (thd)
 
744
                CSThread::setSelf(NULL);
 
745
        else {
 
746
                self = CSThread::getSelf();
 
747
                CSThread::detach(self);
 
748
        }
 
749
}
 
750
 
 
751
//---------------
 
752
void MSEngine::closeConnection(THD* thd)
 
753
{
 
754
        CSThread        *self;
 
755
 
 
756
        self = CSThread::getSelf();
 
757
        if (self && self->pbms_api_owner)
 
758
                return;
 
759
 
 
760
        if (thd) {
 
761
                if ((self = pbms_getMySelf(thd))) {
 
762
                        pbms_setMySelf(thd, NULL);
 
763
                        CSThread::setSelf(self);
 
764
                        CSThread::detach(self);
 
765
                }
 
766
        }
 
767
        else {
 
768
                self = CSThread::getSelf();
 
769
                CSThread::detach(self);
 
770
        }
 
771
}
 
772
 
 
773
 
 
774
 
 
775