~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-04-22 02:46:23 UTC
  • mto: (1497.3.4 enable-dtrace)
  • mto: This revision was merged to the branch mainline in revision 1527.
  • Revision ID: mordred@inaugust.com-20100422024623-4urw8fi8eraci08p
Don't overwrite the pandora_vc_revinfo file if we don't have new
authoratative information.

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