~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2010-10-20 20:26:18 UTC
  • mfrom: (1859.2.13 refactor)
  • Revision ID: brian@tangent.org-20101020202618-9222n39lm329urv5
Merge for Brian 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
2
2
 *
3
3
 * PrimeBase Media Stream for MySQL
4
4
 *
202
202
                }
203
203
        }
204
204
        
205
 
        return (const PBMSEnginePtr)NULL;
 
205
        return NULL;
206
206
}
207
207
#endif  
208
208
 
209
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)
 
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
211
{
212
 
        volatile bool rtc = true;
 
212
 
 
213
        CSThread                *self;
 
214
        int32_t                 err = MS_OK;
 
215
        MSOpenTable             *otab;
 
216
        CSInputStream   *i_stream = NULL;
213
217
        
 
218
        CLOBBER_PROTECT(err);
 
219
 
 
220
        if ((err = enterConnectionNoThd(&self, result)))
 
221
                return err;
 
222
 
 
223
        inner_();
214
224
        try_(a) {
215
 
                MSOpenTable             *otab;
216
 
                CSInputStream   *i_stream = NULL;
217
 
                
218
225
                otab = openTable(db_name, tab_name, true);
219
226
                frompool_(otab);
220
227
                
225
232
                        CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot create BLOBs during repository recovery.");
226
233
 
227
234
                backtopool_(otab);
228
 
                rtc = false;                    
229
 
        }
230
 
        catch_(a);
 
235
        }
 
236
        catch_(a) {
 
237
                err = exceptionToResult(&self->myException, result);
 
238
        }
231
239
        cont_(a);
232
 
        return rtc;
 
240
        return_(err);
233
241
}
234
242
 
235
243
//---------------
236
 
int32_t MSEngine::createBlob(const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url, PBMSResultPtr result)
 
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)
237
245
{
238
246
 
239
247
        CSThread                *self;
240
248
        int32_t                 err = MS_OK;
 
249
        MSBlobURLRec    blob;
 
250
        MSOpenTable             *otab;
241
251
        
 
252
        CLOBBER_PROTECT(err);
 
253
 
242
254
        if ((err = enterConnectionNoThd(&self, result)))
243
255
                return err;
244
256
 
245
257
        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
258
        try_(a) {
257
 
                MSBlobURLRec    blob;
258
 
                MSOpenTable             *otab;
259
259
                
260
260
                if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
261
261
                        char buffer[CS_EXC_MESSAGE_SIZE];
271
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
272
 
273
273
                backtopool_(otab);
274
 
                rtc = false;                    
275
 
        }
276
 
        catch_(a);
 
274
        }
 
275
        catch_(a) {
 
276
                err = exceptionToResult(&self->myException, result);
 
277
        }
277
278
        cont_(a);
278
 
        return rtc;
 
279
        return_(err);
279
280
}
280
281
 
281
282
//---------------
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
int32_t MSEngine::dereferenceBlob(const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
283
284
{
284
 
 
285
285
        CSThread                *self;
286
286
        int32_t                 err = MS_OK;
287
 
        
 
287
        MSBlobURLRec    blob;
 
288
        MSOpenTable             *otab;
 
289
 
 
290
        CLOBBER_PROTECT(err);
 
291
 
288
292
        if ((err = enterConnectionNoThd(&self, result)))
289
293
                return err;
290
294
 
291
295
        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
296
        try_(a) {
304
 
                MSBlobURLRec    blob;
305
 
                MSOpenTable             *otab;
306
 
 
307
297
                if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
308
298
                        char buffer[CS_EXC_MESSAGE_SIZE];
309
299
 
333
323
                        CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
334
324
                }
335
325
                
336
 
                backtopool_(otab);      
337
 
                rtc = false;                    
 
326
                backtopool_(otab);                              
338
327
        }
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))
 
328
        catch_(a) {
354
329
                err = exceptionToResult(&self->myException, result);
355
 
 
 
330
        }
 
331
        cont_(a);
356
332
        return_(err);
357
333
}
358
334
 
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
335
int32_t MSEngine::dropDatabase(const char *db_name, PBMSResultPtr result)
373
336
{
374
337
        CSThread *self;
375
 
        int             err = MS_OK;
 
338
        int             err = MS_OK;;
376
339
        
 
340
        CLOBBER_PROTECT(err);
 
341
 
377
342
        if ((err = enterConnectionNoThd(&self, result)))
378
343
                return err;
379
 
 
380
344
        inner_();
381
345
        
382
 
        if (try_dropDatabase(self, db_name))
 
346
        try_(a) {
 
347
                MSDatabase::dropDatabase(db_name);
 
348
        }
 
349
        catch_(a) {
383
350
                err = exceptionToResult(&self->myException, result);
384
 
 
 
351
        }
 
352
        cont_(a);
385
353
        return_(err);
386
354
}
387
 
 
388
355
//---------------
389
356
typedef struct UnDoInfo {
390
357
        bool udo_WasRename;
394
361
        CSString *udo_NewName;
395
362
} UnDoInfoRec, *UnDoInfoPtr;
396
363
 
397
 
//---------------
398
 
bool MSEngine::try_dropTable(CSThread *self, const char *db_name, const char *tab_name)
 
364
int32_t MSEngine::dropTable(const char *db_name, const char *tab_name, PBMSResultPtr result)
399
365
{
400
 
        volatile bool rtc = true;
 
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_();
401
375
        try_(a) {
402
376
 
403
377
                CSPath                  *new_path;
408
382
                UnDoInfoPtr             undo_info = NULL;
409
383
 
410
384
                otab = openTable(db_name, tab_name, false);
411
 
                if (!otab) {
412
 
                        goto end_try;
413
 
                }
 
385
                if (!otab)
 
386
                        goto exit;
414
387
                
415
388
                // If we are recovering do not delete the table.
416
389
                // It is normal for MySQL recovery scripts to delete any table they aare about to
418
391
                // then this would delete all the recovered BLOBs in the table.
419
392
                if (otab->getDB()->isRecovering()) {
420
393
                        otab->returnToPool();
421
 
                        goto end_try;
 
394
                        goto exit;
422
395
                }
423
396
 
424
397
                frompool_(otab);
448
421
                frompool_(tab_pool);
449
422
 
450
423
                if (old_path->exists())
451
 
                        old_path->move(RETAIN(new_path));
 
424
                        old_path->move(new_path);
452
425
                tab->myDatabase->dropTable(RETAIN(tab));
453
426
                
454
427
                /* Add the table to the temp delete list if we are not recovering... */
464
437
                
465
438
                undo_info->udo_WasRename = false;
466
439
                self->myInfo = undo_info;
467
 
                                
468
 
end_try:
469
 
                rtc = false;    
470
 
        }
471
 
        catch_(a);
 
440
                
 
441
                
 
442
exit: ;
 
443
        }
 
444
        
 
445
        catch_(a) {
 
446
                err = exceptionToResult(&self->myException, result);
 
447
        }
472
448
        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
449
        outer_();
490
450
        exitConnection();
491
451
        return err;
546
506
        tab_pool = MSTableList::lockTablePoolForDeletion(otab);
547
507
        frompool_(tab_pool);
548
508
 
549
 
        from_path->move(RETAIN(to_path));
 
509
        from_path->move(to_path);
550
510
        tab->myDatabase->renameTable(tab, to_table);
551
511
 
552
512
        backtopool_(tab_pool);  // The will unlock and close the table pool freeing all tables in it.
558
518
}
559
519
 
560
520
//---------------
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)
 
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)
562
522
{
563
 
        volatile bool rtc = true;
 
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_();
564
532
        try_(a) {
565
533
                UnDoInfoPtr undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
566
534
                push_ptr_(undo_info);
586
554
                }
587
555
                self->myInfo = undo_info;
588
556
                pop_(undo_info);
589
 
                rtc = false;                    
590
 
        }
591
 
        catch_(a);
 
557
        }
 
558
        catch_(a) {
 
559
                err = exceptionToResult(&self->myException, result);
 
560
        }
592
561
        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
562
        outer_();
610
563
        exitConnection();
611
564
        return err;
638
591
        }
639
592
        exit_();
640
593
}
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
594
//---------------
662
595
void MSEngine::callCompleted(bool ok)
663
596
{
678
611
                self->myInfo = NULL;
679
612
        } else if (self->myTID && (self->myIsAutoCommit || !ok)) {
680
613
                inner_();
681
 
                if (try_CompleteTransaction(self, ok)) {
 
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) {
682
623
                        self->logException();
683
624
                }
 
625
                cont_(a);
684
626
                outer_();
685
627
        }
686
628