~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/storage/mysql/mysql_storage.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2009-04-22 21:39:19 UTC
  • mto: (4.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20090422213919-52m015y6gcpv1m1g
Tags: upstream-0.12.0~svn2018
ImportĀ upstreamĀ versionĀ 0.12.0~svn2018

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
9
9
    
10
 
    Copyright (C) 2006-2008 Gena Batyan <bgeradz@mediatomb.cc>,
 
10
    Copyright (C) 2006-2009 Gena Batyan <bgeradz@mediatomb.cc>,
11
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
12
                            Leonhard Wimmer <leo@mediatomb.cc>
13
13
    
24
24
    version 2 along with MediaTomb; if not, write to the Free Software
25
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26
26
    
27
 
    $Id: mysql_storage.cc 1698 2008-02-23 20:48:30Z lww $
 
27
    $Id: mysql_storage.cc 2010 2009-01-11 19:10:43Z lww $
28
28
*/
29
29
 
30
30
/// \file mysql_storage.cc
78
78
    mysqlMutex = Ref<Mutex> (new Mutex(true));
79
79
    table_quote_begin = '`';
80
80
    table_quote_end = '`';
 
81
    insertBuffer = nil;
81
82
}
82
83
MysqlStorage::~MysqlStorage()
83
84
{
 
85
    AUTOLOCK(mysqlMutex);    // just to ensure, that we don't close while another thread 
 
86
                    // is executing a query
 
87
    
 
88
    if(mysql_connection)
 
89
    {
 
90
        mysql_close(&db);
 
91
        mysql_connection = false;
 
92
    }
 
93
    log_debug("calling mysql_server_end...\n");
 
94
    mysql_server_end();
 
95
    log_debug("...ok\n");
84
96
}
85
97
 
86
98
void MysqlStorage::checkMysqlThreadInit()
98
110
 
99
111
void MysqlStorage::threadCleanup()
100
112
{
 
113
    log_info("thread cleanup; thread_id=%d\n", pthread_self());
101
114
    if (pthread_getspecific(mysql_init_key) != NULL)
102
115
    {
103
116
        mysql_thread_end();
124
137
        throw _Exception(_("could not create pthread_key"));
125
138
    }
126
139
    mysql_server_init(0, NULL, NULL);
127
 
    my_init();
128
140
    pthread_setspecific(mysql_init_key, (void *) 1);
129
141
    
130
142
    Ref<ConfigManager> config = ConfigManager::getInstance();
278
290
    AUTOUNLOCK();
279
291
    
280
292
    log_debug("end\n");
 
293
    
 
294
    dbReady();
281
295
}
282
296
 
283
297
String MysqlStorage::quote(String value)
356
370
    
357
371
}
358
372
 
359
 
void MysqlStorage::shutdown()
 
373
void MysqlStorage::shutdownDriver()
360
374
{
361
 
    AUTOLOCK(mysqlMutex);    // just to ensure, that we don't close while another thread 
362
 
                    // is executing a query
363
 
    
364
 
    if(mysql_connection)
365
 
    {
366
 
        mysql_close(&db);
367
 
        mysql_connection = false;
368
 
    }
369
 
    mysql_server_end();
370
375
}
371
376
 
372
377
void MysqlStorage::storeInternalSetting(String key, String value)
379
384
    SQLStorage::exec(q);
380
385
}
381
386
 
382
 
void MysqlStorage::_exec(const char *query)
 
387
void MysqlStorage::_exec(const char *query, int length)
383
388
{
384
 
    if (mysql_real_query(&db, query, strlen(query)))
 
389
    if (mysql_real_query(&db, query, (length > 0 ? length : strlen(query))))
385
390
    {
386
391
        String myError = getError(&db);
387
392
        throw _StorageException(myError, _("Mysql: error while updating db: ") + myError);
388
393
    }
389
394
}
390
395
 
 
396
void MysqlStorage::_addToInsertBuffer(Ref<StringBuffer> query)
 
397
{
 
398
    if (insertBuffer == nil)
 
399
    {
 
400
        insertBuffer = Ref<Array<StringBase> >(new Array<StringBase>());
 
401
        insertBuffer->append(_("BEGIN"));
 
402
    }
 
403
    Ref<StringBase> sb (new StringBase(query->c_str()));
 
404
    insertBuffer->append(sb);
 
405
}
 
406
 
 
407
void MysqlStorage::_flushInsertBuffer()
 
408
{
 
409
    if (insertBuffer == nil)
 
410
        return;
 
411
    insertBuffer->append(_("COMMIT"));
 
412
    
 
413
    checkMysqlThreadInit();
 
414
    AUTOLOCK(mysqlMutex);
 
415
    for (int i=0; i < insertBuffer->size(); i++)
 
416
    {
 
417
        _exec(insertBuffer->get(i)->data, insertBuffer->get(i)->len);
 
418
    }
 
419
    AUTOUNLOCK();
 
420
    insertBuffer->clear();
 
421
    insertBuffer->append(_("BEGIN"));
 
422
}
 
423
 
 
424
 
391
425
/* MysqlResult */
392
426
 
393
427
MysqlResult::MysqlResult(MYSQL_RES *mysql_res) : SQLResult()