~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

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
#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 NULL;
 
206
}
 
207
#endif  
 
208
 
 
209
//---------------
 
210
int32_t MSEngine::createBlob(const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url, PBMSResultPtr result)
 
211
{
 
212
 
 
213
        CSThread                *self;
 
214
        int32_t                 err = MS_OK;
 
215
        MSOpenTable             *otab;
 
216
        CSInputStream   *i_stream = NULL;
 
217
        
 
218
        CLOBBER_PROTECT(err);
 
219
 
 
220
        if ((err = enterConnectionNoThd(&self, result)))
 
221
                return err;
 
222
 
 
223
        inner_();
 
224
        try_(a) {
 
225
                otab = openTable(db_name, tab_name, true);
 
226
                frompool_(otab);
 
227
                
 
228
                if (!otab->getDB()->isRecovering()) {
 
229
                        i_stream = CSMemoryInputStream::newStream((unsigned char *)blob, blob_len);
 
230
                        otab->createBlob(blob_url, blob_len, NULL, 0, i_stream);
 
231
                } else
 
232
                        CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot create BLOBs during repository recovery.");
 
233
 
 
234
                backtopool_(otab);
 
235
        }
 
236
        catch_(a) {
 
237
                err = exceptionToResult(&self->myException, result);
 
238
        }
 
239
        cont_(a);
 
240
        return_(err);
 
241
}
 
242
 
 
243
//---------------
 
244
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)
 
245
{
 
246
 
 
247
        CSThread                *self;
 
248
        int32_t                 err = MS_OK;
 
249
        MSBlobURLRec    blob;
 
250
        MSOpenTable             *otab;
 
251
        
 
252
        CLOBBER_PROTECT(err);
 
253
 
 
254
        if ((err = enterConnectionNoThd(&self, result)))
 
255
                return err;
 
256
 
 
257
        inner_();
 
258
        try_(a) {
 
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
        }
 
275
        catch_(a) {
 
276
                err = exceptionToResult(&self->myException, result);
 
277
        }
 
278
        cont_(a);
 
279
        return_(err);
 
280
}
 
281
 
 
282
//---------------
 
283
int32_t MSEngine::dereferenceBlob(const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
 
284
{
 
285
        CSThread                *self;
 
286
        int32_t                 err = MS_OK;
 
287
        MSBlobURLRec    blob;
 
288
        MSOpenTable             *otab;
 
289
 
 
290
        CLOBBER_PROTECT(err);
 
291
 
 
292
        if ((err = enterConnectionNoThd(&self, result)))
 
293
                return err;
 
294
 
 
295
        inner_();
 
296
        try_(a) {
 
297
                if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
 
298
                        char buffer[CS_EXC_MESSAGE_SIZE];
 
299
 
 
300
                        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
 
301
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
 
302
                        CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
 
303
                }
 
304
                
 
305
                otab = openTable(db_name, tab_name, true);
 
306
                frompool_(otab);
 
307
                if (!otab->getDB()->isRecovering()) {
 
308
                        if (otab->getTableID() == blob.bu_tab_id)
 
309
                                otab->releaseReference(blob.bu_blob_id, blob.bu_blob_ref_id);
 
310
                        else {
 
311
                                char buffer[CS_EXC_MESSAGE_SIZE];
 
312
 
 
313
                                cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect table ID: ");
 
314
                                cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
 
315
                                CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
 
316
                        }
 
317
                }
 
318
                else {
 
319
                        char buffer[CS_EXC_MESSAGE_SIZE];
 
320
 
 
321
                        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
 
322
                        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
 
323
                        CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
 
324
                }
 
325
                
 
326
                backtopool_(otab);                              
 
327
        }
 
328
        catch_(a) {
 
329
                err = exceptionToResult(&self->myException, result);
 
330
        }
 
331
        cont_(a);
 
332
        return_(err);
 
333
}
 
334
 
 
335
int32_t MSEngine::dropDatabase(const char *db_name, PBMSResultPtr result)
 
336
{
 
337
        CSThread *self;
 
338
        int             err = MS_OK;;
 
339
        
 
340
        CLOBBER_PROTECT(err);
 
341
 
 
342
        if ((err = enterConnectionNoThd(&self, result)))
 
343
                return err;
 
344
        inner_();
 
345
        
 
346
        try_(a) {
 
347
                MSDatabase::dropDatabase(db_name);
 
348
        }
 
349
        catch_(a) {
 
350
                err = exceptionToResult(&self->myException, result);
 
351
        }
 
352
        cont_(a);
 
353
        return_(err);
 
354
}
 
355
//---------------
 
356
typedef struct UnDoInfo {
 
357
        bool udo_WasRename;
 
358
        CSString *udo_toDatabaseName;
 
359
        CSString *udo_fromDatabaseName;
 
360
        CSString *udo_OldName;
 
361
        CSString *udo_NewName;
 
362
} UnDoInfoRec, *UnDoInfoPtr;
 
363
 
 
364
int32_t MSEngine::dropTable(const char *db_name, const char *tab_name, PBMSResultPtr result)
 
365
{
 
366
        CSThread        *self;
 
367
        int                     err = MS_OK;
 
368
 
 
369
        CLOBBER_PROTECT(err);
 
370
 
 
371
        if ((err = enterConnectionNoThd(&self, result)))
 
372
                return err;
 
373
 
 
374
        inner_();
 
375
        try_(a) {
 
376
 
 
377
                CSPath                  *new_path;
 
378
                CSPath                  *old_path;
 
379
                MSOpenTable             *otab;
 
380
                MSOpenTablePool *tab_pool;
 
381
                MSTable                 *tab;
 
382
                UnDoInfoPtr             undo_info = NULL;
 
383
 
 
384
                otab = openTable(db_name, tab_name, false);
 
385
                if (!otab)
 
386
                        goto exit;
 
387
                
 
388
                // If we are recovering do not delete the table.
 
389
                // It is normal for MySQL recovery scripts to delete any table they aare about to
 
390
                // recover and then recreate it. If this is done after the repository has been recovered
 
391
                // then this would delete all the recovered BLOBs in the table.
 
392
                if (otab->getDB()->isRecovering()) {
 
393
                        otab->returnToPool();
 
394
                        goto exit;
 
395
                }
 
396
 
 
397
                frompool_(otab);
 
398
 
 
399
                // Before dropping the table the table ref file is renamed so that
 
400
                // it is out of the way incase a new table is created before the
 
401
                // old one is cleaned up.
 
402
                
 
403
                old_path = otab->getDBTable()->getTableFile();
 
404
                push_(old_path);
 
405
 
 
406
                new_path = otab->getDBTable()->getTableFile(tab_name, true);
 
407
 
 
408
                // Rearrage the object stack to pop the otab object
 
409
                pop_(old_path);
 
410
                pop_(otab);
 
411
 
 
412
                push_(new_path);
 
413
                push_(old_path);
 
414
                frompool_(otab);
 
415
                
 
416
                tab = otab->getDBTable();
 
417
                pop_(otab);
 
418
                push_(tab);
 
419
 
 
420
                tab_pool = MSTableList::lockTablePoolForDeletion(otab);
 
421
                frompool_(tab_pool);
 
422
 
 
423
                if (old_path->exists())
 
424
                        old_path->move(new_path);
 
425
                tab->myDatabase->dropTable(RETAIN(tab));
 
426
                
 
427
                /* Add the table to the temp delete list if we are not recovering... */
 
428
                tab->prepareToDelete();
 
429
 
 
430
                backtopool_(tab_pool);  // The will unlock and close the table pool freeing all tables in it.
 
431
                pop_(tab);                              // Returning the pool will have released this. (YUK!)
 
432
                release_(old_path);
 
433
                release_(new_path);
 
434
 
 
435
 
 
436
                undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
 
437
                
 
438
                undo_info->udo_WasRename = false;
 
439
                self->myInfo = undo_info;
 
440
                
 
441
                
 
442
exit: ;
 
443
        }
 
444
        
 
445
        catch_(a) {
 
446
                err = exceptionToResult(&self->myException, result);
 
447
        }
 
448
        cont_(a);
 
449
        outer_();
 
450
        exitConnection();
 
451
        return err;
 
452
}
 
453
 
 
454
//---------------
 
455
static void completeDeleteTable(UnDoInfoPtr info, bool ok)
 
456
{
 
457
        // TO DO: figure out a way to undo the delete.
 
458
        cs_free(info);
 
459
        if (!ok) 
 
460
                CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot undo delete table.");
 
461
}
 
462
 
 
463
//---------------
 
464
bool MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table)
 
465
{
 
466
        MSOpenTable             *otab;
 
467
        CSPath                  *from_path;
 
468
        CSPath                  *to_path;
 
469
        MSOpenTablePool *tab_pool;
 
470
        MSTable                 *tab;
 
471
 
 
472
        enter_();
 
473
        
 
474
        if (strcmp(to_db_name, from_db_name) != 0) {
 
475
                CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot rename tables containing BLOBs across databases (yet). Sorry!");
 
476
        }
 
477
        
 
478
        otab = openTable(from_db_name, from_table, false);
 
479
        if (!otab)
 
480
                return_(false);
 
481
                
 
482
        frompool_(otab);
 
483
 
 
484
        if (otab->getDB()->isRecovering()) 
 
485
                CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot rename tables during repository recovery.");
 
486
 
 
487
        from_path = otab->getDBTable()->getTableFile();
 
488
        push_(from_path);
 
489
 
 
490
        to_path = otab->getDBTable()->getTableFile(to_table, false);
 
491
 
 
492
        // Rearrage the object stack to pop the otab object
 
493
        pop_(from_path);
 
494
        pop_(otab);
 
495
 
 
496
        push_(to_path);
 
497
        push_(from_path);
 
498
        frompool_(otab);
 
499
 
 
500
        otab->openForReading();
 
501
        tab = otab->getDBTable();
 
502
        tab->retain();
 
503
        pop_(otab);
 
504
        push_(tab);
 
505
        
 
506
        tab_pool = MSTableList::lockTablePoolForDeletion(otab);
 
507
        frompool_(tab_pool);
 
508
 
 
509
        from_path->move(to_path);
 
510
        tab->myDatabase->renameTable(tab, to_table);
 
511
 
 
512
        backtopool_(tab_pool);  // The will unlock and close the table pool freeing all tables in it.
 
513
        pop_(tab);                              // Returning the pool will have released this. (YUK!)
 
514
        release_(from_path);
 
515
        release_(to_path);
 
516
        
 
517
        return_(true);
 
518
}
 
519
 
 
520
//---------------
 
521
int32_t MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table, PBMSResultPtr result)
 
522
{
 
523
        CSThread        *self;
 
524
        int err = MS_OK;
 
525
 
 
526
        CLOBBER_PROTECT(err);
 
527
 
 
528
        if ((err = enterConnectionNoThd(&self, result)))
 
529
                return err;
 
530
 
 
531
        inner_();
 
532
        try_(a) {
 
533
                UnDoInfoPtr undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
 
534
                push_ptr_(undo_info);
 
535
 
 
536
                undo_info->udo_WasRename = true;
 
537
                if (renameTable(from_db_name, from_table, to_db_name, to_table)) {              
 
538
                        undo_info->udo_fromDatabaseName = CSString::newString(from_db_name);
 
539
                        push_(undo_info->udo_fromDatabaseName);
 
540
 
 
541
                        undo_info->udo_toDatabaseName = CSString::newString(to_db_name);
 
542
                        push_(undo_info->udo_toDatabaseName);
 
543
 
 
544
                        undo_info->udo_OldName = CSString::newString(from_table);
 
545
                        push_(undo_info->udo_OldName);
 
546
 
 
547
                        undo_info->udo_NewName = CSString::newString(to_table);
 
548
                        
 
549
                        pop_(undo_info->udo_OldName);
 
550
                        pop_(undo_info->udo_toDatabaseName);
 
551
                        pop_(undo_info->udo_fromDatabaseName);
 
552
                } else {
 
553
                        undo_info->udo_fromDatabaseName = undo_info->udo_toDatabaseName = undo_info->udo_OldName = undo_info->udo_NewName = NULL;
 
554
                }
 
555
                self->myInfo = undo_info;
 
556
                pop_(undo_info);
 
557
        }
 
558
        catch_(a) {
 
559
                err = exceptionToResult(&self->myException, result);
 
560
        }
 
561
        cont_(a);
 
562
        outer_();
 
563
        exitConnection();
 
564
        return err;
 
565
}
 
566
 
 
567
//---------------
 
568
void MSEngine::completeRenameTable(UnDoInfoPtr info, bool ok)
 
569
{
 
570
        // Swap the paths around here to revers the rename.
 
571
        CSString                *from_db_name= info->udo_toDatabaseName;
 
572
        CSString                *to_db_name= info->udo_fromDatabaseName;
 
573
        CSString                *from_table= info->udo_NewName;
 
574
        CSString                *to_table= info->udo_OldName;
 
575
        
 
576
        enter_();
 
577
        
 
578
        cs_free(info);
 
579
        if (from_db_name) {
 
580
                push_(from_db_name);
 
581
                push_(from_table);
 
582
                push_(to_db_name);
 
583
                push_(to_table);
 
584
                if (!ok) 
 
585
                        renameTable(from_db_name->getCString(), from_table->getCString(), to_db_name->getCString(), to_table->getCString());
 
586
                        
 
587
                release_(to_table);
 
588
                release_(to_db_name);
 
589
                release_(from_table);
 
590
                release_(from_db_name);
 
591
        }
 
592
        exit_();
 
593
}
 
594
//---------------
 
595
void MSEngine::callCompleted(bool ok)
 
596
{
 
597
        CSThread        *self;
 
598
        PBMSResultRec   result;
 
599
        
 
600
        if (enterConnectionNoThd(&self, &result))
 
601
                return ;
 
602
 
 
603
        if (self->myInfo) {
 
604
                UnDoInfoPtr info = (UnDoInfoPtr) self->myInfo;
 
605
                if (info->udo_WasRename) 
 
606
                        completeRenameTable(info, ok);
 
607
                else 
 
608
                        completeDeleteTable(info, ok);
 
609
 
 
610
                
 
611
                self->myInfo = NULL;
 
612
        } else if (self->myTID && (self->myIsAutoCommit || !ok)) {
 
613
                inner_();
 
614
                try_(a) {
 
615
                        if (ok)
 
616
                                MSTransactionManager::commit();
 
617
                        else if (self->myIsAutoCommit)
 
618
                                MSTransactionManager::rollback();
 
619
                        else
 
620
                                MSTransactionManager::rollbackToPosition(self->myStartStmt); // Rollback the last logical statement.
 
621
                }
 
622
                catch_(a) {
 
623
                        self->logException();
 
624
                }
 
625
                cont_(a);
 
626
                outer_();
 
627
        }
 
628
        
 
629
        self->myStartStmt = self->myStmtCount;
 
630
}
 
631
 
 
632
//---------------
 
633
MSOpenTable *MSEngine::openTable(const char *db_name, const char *tab_name, bool create)
 
634
{
 
635
        MSOpenTable             *otab = NULL;
 
636
        uint32_t db_id, tab_id;
 
637
        enter_();
 
638
        
 
639
        if ( MSDatabase::convertTableAndDatabaseToIDs(db_name, tab_name, &db_id, &tab_id, create))  
 
640
                otab = MSTableList::getOpenTableByID(db_id, tab_id);
 
641
                
 
642
        return_(otab);
 
643
}
 
644
 
 
645
//---------------
 
646
bool MSEngine::couldBeURL(const char *blob_url, size_t length)
 
647
{
 
648
        MSBlobURLRec blob;
 
649
        return PBMSBlobURLTools::couldBeURL(blob_url, length, &blob);
 
650
}
 
651
 
 
652
//---------------
 
653
int MSEngine::exceptionToResult(CSException *e, PBMSResultPtr result)
 
654
{
 
655
        const char *context, *trace;
 
656
 
 
657
        result->mr_code = e->getErrorCode();
 
658
        cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, e->getMessage());
 
659
        context = e->getContext();
 
660
        trace = e->getStackTrace();
 
661
        if (context && *context) {
 
662
                cs_strcpy(MS_RESULT_STACK_SIZE, result->mr_stack, context);
 
663
                if (trace && *trace)
 
664
                        cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, "\n");
 
665
        }
 
666
        else
 
667
                *result->mr_stack = 0;
 
668
        if (trace && *trace)
 
669
                cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, trace);
 
670
        return MS_ERR_ENGINE;
 
671
}
 
672
 
 
673
//---------------
 
674
int MSEngine::errorResult(const char *func, const char *file, int line, int err, const char *message, PBMSResultPtr result)
 
675
{
 
676
        CSException e;
 
677
                
 
678
        e.initException(func, file, line, err, message);
 
679
        return exceptionToResult(&e, result);
 
680
}
 
681
 
 
682
//---------------
 
683
int MSEngine::osErrorResult(const char *func, const char *file, int line, int err, PBMSResultPtr result)
 
684
{
 
685
        CSException e;
 
686
                
 
687
        e.initOSError(func, file, line, err);
 
688
        return MSEngine::exceptionToResult(&e, result);
 
689
}
 
690
 
 
691
//---------------
 
692
int MSEngine::enterConnection(THD *thd, CSThread **r_self, PBMSResultPtr result, bool doCreate)
 
693
{
 
694
        CSThread        *self = NULL;
 
695
 
 
696
#ifndef DRIZZLED
 
697
        // In drizzle there is no 1:1 relationship between pthreads and sessions
 
698
        // so we must always get it from the session handle NOT the current pthread.
 
699
        self = CSThread::getSelf();
 
700
#endif
 
701
        if (!self) {    
 
702
                if (thd) {
 
703
                        if (!(self = pbms_getMySelf(thd))) {
 
704
                                if (!doCreate)
 
705
                                        return MS_ERR_NOT_FOUND;
 
706
                                        
 
707
                                if (!(self = CSThread::newCSThread()))
 
708
                                        return osErrorResult(CS_CONTEXT, ENOMEM, result);
 
709
                                if (!CSThread::attach(self))
 
710
                                        return MSEngine::exceptionToResult(&self->myException, result);
 
711
                                pbms_setMySelf(thd, self);
 
712
                        } else {
 
713
                                if (!CSThread::setSelf(self))
 
714
                                        return MSEngine::exceptionToResult(&self->myException, result);
 
715
                        }
 
716
                } else {
 
717
                        if (!doCreate)
 
718
                                return MS_ERR_NOT_FOUND;
 
719
                                
 
720
                        if (!(self = CSThread::newCSThread()))
 
721
                                return osErrorResult(CS_CONTEXT, ENOMEM, result);
 
722
                        if (!CSThread::attach(self))
 
723
                                return MSEngine::exceptionToResult(&self->myException, result);
 
724
                }
 
725
        }
 
726
 
 
727
        *r_self = self;
 
728
        return MS_OK;
 
729
}
 
730
 
 
731
//---------------
 
732
int MSEngine::enterConnectionNoThd(CSThread **r_self, PBMSResultPtr result)
 
733
{
 
734
        return enterConnection(current_thd, r_self, result, true);
 
735
}
 
736
 
 
737
//---------------
 
738
void MSEngine::exitConnection()
 
739
{
 
740
        THD                     *thd = (THD *) current_thd;
 
741
        CSThread        *self;
 
742
 
 
743
        self = CSThread::getSelf();
 
744
        if (self && self->pbms_api_owner)
 
745
                return;
 
746
 
 
747
 
 
748
        if (thd)
 
749
                CSThread::setSelf(NULL);
 
750
        else {
 
751
                self = CSThread::getSelf();
 
752
                CSThread::detach(self);
 
753
        }
 
754
}
 
755
 
 
756
//---------------
 
757
void MSEngine::closeConnection(THD* thd)
 
758
{
 
759
        CSThread        *self;
 
760
 
 
761
        self = CSThread::getSelf();
 
762
        if (self && self->pbms_api_owner)
 
763
                return;
 
764
 
 
765
        if (thd) {
 
766
                if ((self = pbms_getMySelf(thd))) {
 
767
                        pbms_setMySelf(thd, NULL);
 
768
                        CSThread::setSelf(self);
 
769
                        CSThread::detach(self);
 
770
                }
 
771
        }
 
772
        else {
 
773
                self = CSThread::getSelf();
 
774
                CSThread::detach(self);
 
775
        }
 
776
}
 
777
 
 
778
 
 
779
 
 
780