~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

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