1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
|
/*****************************************************************************
* sqlite.c: An SQLite3 wrapper for VLC
*****************************************************************************
* Copyright (C) 2008-2009 the VideoLAN team
* $Id: 360577f447289abb69f5bd86e3323a21b8551301 $
*
* Authors: Antoine Lejeune <phytos@videolan.org>
* Jean-Philippe André <jpeg@videolan.org>
* Rémi Duraffort <ivoire@videolan.org>
* Adrien Maglo <magsoft@videolan.org>
* Srikanth Raju <srikiraju@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_sql.h>
#include <vlc_plugin.h>
#include <sqlite3.h>
#include <assert.h>
/*****************************************************************************
* Private structures
*****************************************************************************/
struct sql_sys_t
{
sqlite3 *db; /**< Database connection. */
vlc_mutex_t lock; /**< SQLite mutex. Threads are evil here. */
vlc_mutex_t trans_lock; /**< Mutex for running transactions */
};
struct sql_stmt_t
{
sqlite3_stmt* p_sqlitestmt;
};
/*****************************************************************************
* Headers
*****************************************************************************/
static int load ( vlc_object_t * );
static void unload ( vlc_object_t * );
static int OpenDatabase( sql_t * );
static int CloseDatabase (sql_t * );
static int QueryCallback( sql_t * p_sql,
const char * query,
sql_query_callback_t callback,
void *arg ); // 1st argument to callback
static int Query( sql_t * p_sql,
const char * query,
char *** result,
int * nrow,
int * ncol );
static int GetTables( sql_t * p_sql,
char *** result );
static void FreeResult( sql_t * p_sql,
char **pp_result );
static char* VMSprintf( const char* psz_fmt,
va_list args );
static int BeginTransaction( sql_t* p_sql );
static int CommitTransaction( sql_t* p_sql );
static void RollbackTransaction( sql_t* p_sql );
static sql_stmt_t* PrepareStatement( sql_t* p_sql,
const char* psz_fmt,
int i_length );
static int BindValues( sql_t* p_sql,
sql_stmt_t* p_stmt,
int i_pos,
unsigned int i_type,
const sql_value_t* p_value );
static int StatementStep( sql_t* p_sql,
sql_stmt_t* p_stmt );
static int StatementReset( sql_t* p_sql,
sql_stmt_t* p_stmt );
static int StatementFinalize( sql_t* p_sql,
sql_stmt_t* p_stmt );
static int GetColumnFromStatement( sql_t* p_sql,
sql_stmt_t* p_stmt,
int i_col,
int type,
sql_value_t *p_res );
static int GetColumnTypeFromStatement( sql_t* p_sql,
sql_stmt_t* p_stmt,
int i_col,
int* pi_type );
static int GetColumnSize( sql_t* p_sql,
sql_stmt_t* p_stmt,
int i_col );
/*****************************************************************************
* Module description
*****************************************************************************/
vlc_module_begin()
set_shortname( "SQLite" )
set_description( _("SQLite database module") )
set_capability( "sql", 1 )
set_callbacks( load, unload )
set_category( CAT_ADVANCED )
vlc_module_end()
/**
* @brief Load module
* @param obj Parent object
* @return VLC_SUCCESS or VLC_ENOMEM
*/
static int load( vlc_object_t *p_this )
{
sql_t *p_sql = (sql_t *) p_this;
/* Initialize sys_t */
p_sql->p_sys = calloc( 1, sizeof( *p_sql->p_sys ) );
if( !p_sql->p_sys )
return VLC_ENOMEM;
vlc_mutex_init( &p_sql->p_sys->lock );
vlc_mutex_init( &p_sql->p_sys->trans_lock );
/* Open Database */
if( OpenDatabase( p_sql ) == VLC_SUCCESS )
msg_Dbg( p_sql, "sqlite module loaded" );
else
{
free( p_sql->p_sys );
vlc_mutex_destroy( &p_sql->p_sys->lock );
vlc_mutex_destroy( &p_sql->p_sys->trans_lock );
return VLC_EGENERIC;
}
p_sql->pf_query_callback = QueryCallback;
p_sql->pf_get_tables = GetTables;
p_sql->pf_query = Query;
p_sql->pf_free = FreeResult;
p_sql->pf_vmprintf = VMSprintf;
p_sql->pf_begin = BeginTransaction;
p_sql->pf_commit = CommitTransaction;
p_sql->pf_rollback = RollbackTransaction;
p_sql->pf_prepare = PrepareStatement;
p_sql->pf_bind = BindValues;
p_sql->pf_run = StatementStep;
p_sql->pf_reset = StatementReset;
p_sql->pf_finalize = StatementFinalize;
p_sql->pf_gettype = GetColumnTypeFromStatement;
p_sql->pf_getcolumn = GetColumnFromStatement;
p_sql->pf_getcolumnsize = GetColumnSize;
return VLC_SUCCESS;
}
/**
* @brief Unload module
* @param obj This sql_t object
* @return Nothing
*/
static void unload( vlc_object_t *p_this )
{
sql_t *p_sql = (sql_t *)p_this;
CloseDatabase( p_sql );
vlc_mutex_destroy( &p_sql->p_sys->lock );
vlc_mutex_destroy( &p_sql->p_sys->trans_lock );
free( p_sql->p_sys );
}
/**
* @brief Sqlite Busy handler
* @param p_data sql_t object
* @param i_times Number of times busy handler has been invoked
*/
static int vlc_sqlite_busy_handler( void* p_data, int i_times )
{
if( i_times >= 10 )
{
msg_Warn( (sql_t*) p_data, "Wait limit exceeded in SQLITE_BUSY handler" );
return 0;
}
msleep( 2000000 );
return 1;
}
/**
* @brief Open current database
* @param p_sql This sql_t object
* @return VLC_SUCCESS or VLC_EGENERIC
* @note p_sql->psz_host is required
*/
static int OpenDatabase( sql_t *p_sql )
{
assert( p_sql->psz_host && *p_sql->psz_host );
if( sqlite3_threadsafe() == 0 )
{
msg_Err( p_sql, "Sqlite library on your system is not threadsafe" );
return VLC_EGENERIC;
}
if( sqlite3_open( p_sql->psz_host, &p_sql->p_sys->db ) != SQLITE_OK )
{
msg_Err( p_sql, "Can't open database : %s", p_sql->psz_host );
msg_Err( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
return VLC_EGENERIC;
}
if( sqlite3_busy_timeout( p_sql->p_sys->db, 30000 ) != SQLITE_OK )
{
msg_Err( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
return VLC_EGENERIC;
}
if( sqlite3_busy_handler( p_sql->p_sys->db, vlc_sqlite_busy_handler, p_sql )
!= SQLITE_OK )
{
msg_Err( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
/**
* @brief Close current database
* @param p_sql This sql_t object
* @return VLC_SUCCESS
* You have to set and open current database first
*/
static int CloseDatabase( sql_t *p_sql )
{
assert( p_sql->p_sys->db );
/* Close all prepared statements */
sqlite3_stmt* p_stmt;
while( ( p_stmt = sqlite3_next_stmt( p_sql->p_sys->db, NULL ) ) != NULL )
{
if( sqlite3_finalize( p_stmt ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
}
}
/* Close database */
/* TODO: We've closed all open prepared statements
* Perhaps sqlite3_close can still fail? */
sqlite3_close( p_sql->p_sys->db );
p_sql->p_sys->db = NULL;
return VLC_SUCCESS;
}
/**
* @brief SQL Query with callback
* @param p_sql This sql_t object
* @param query SQL query
* @param callback Callback function to receive results row by row
* @param arg Argument to pass to callback
* @return VLC_SUCCESS or an error code
* You have to set and open current database first
*/
static int QueryCallback( sql_t * p_sql,
const char * query,
sql_query_callback_t callback,
void *arg )
{
int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sql->p_sys->lock );
assert( p_sql->p_sys->db );
#ifndef NDEBUG
msg_Dbg( p_sql, "QueryCallback: %s", query );
#endif
sqlite3_exec( p_sql->p_sys->db, query, callback, arg, NULL );
if( sqlite3_errcode( p_sql->p_sys->db ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Direct SQL Query
* @param p_sql This sql_t object
* @param query SQL query
* @param result Return value : Array of results
* @param nrow Return value : Row number
* @param ncol Return value : Column number
* @return VLC_SUCCESS or an error code
* You have to set and open current database first
* @todo Handle transaction closing due to errors in sql query
*/
static int Query( sql_t * p_sql,
const char * query,
char *** result,
int * nrow,
int * ncol )
{
assert( p_sql->p_sys->db );
int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sql->p_sys->lock );
#ifndef NDEBUG
msg_Dbg( p_sql, "Query: %s", query );
#endif
sqlite3_get_table( p_sql->p_sys->db, query, result, nrow, ncol, NULL );
if( sqlite3_errcode( p_sql->p_sys->db ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Get tables in database
* @param p_sql This sql_t object
* @param result SQL query result
* @return Number of elements
* You have to set and open current database first
*/
static int GetTables( sql_t * p_sql,
char *** result )
{
int nrow, i_num = -1;
vlc_mutex_lock( &p_sql->p_sys->lock );
assert( p_sql->p_sys->db );
sqlite3_get_table( p_sql->p_sys->db, "SELECT * FROM sqlite_master;",
result, &nrow, &i_num, NULL );
if( sqlite3_errcode( p_sql->p_sys->db ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_num;
}
/**
* @brief Free SQL request's result
* @param p_sql This SQL object.
* @param ppsz_result SQL result to free
*/
static void FreeResult( sql_t * p_sql, char **ppsz_result )
{
VLC_UNUSED( p_sql );
if( ppsz_result != NULL )
sqlite3_free_table( ppsz_result );
}
/**
* @brief vmprintf replacement for SQLite.
* @param psz_fmt Format string
* @param args va_list of arguments
* This function implements the formats %q, %Q and %z.
*/
static char* VMSprintf( const char* psz_fmt, va_list args )
{
char *psz = sqlite3_vmprintf( psz_fmt, args );
char *ret = strdup( psz );
sqlite3_free( psz );
return ret;
}
/**
* @brief Starts a Transaction and waits if necessary
* @param p_sql The SQL object
* @note This function locks the transactions on the database.
* Within the period of the transaction, only the calling thread may
* execute sql statements provided all threads use these transaction fns.
*/
static int BeginTransaction( sql_t* p_sql )
{
int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sql->p_sys->trans_lock );
vlc_mutex_lock( &p_sql->p_sys->lock );
assert( p_sql->p_sys->db );
sqlite3_exec( p_sql->p_sys->db, "BEGIN;", NULL, NULL, NULL );
#ifndef NDEBUG
msg_Dbg( p_sql, "Transaction Query: BEGIN;" );
#endif
if( sqlite3_errcode( p_sql->p_sys->db ) != SQLITE_OK )
{
vlc_mutex_unlock( &p_sql->p_sys->trans_lock );
vlc_mutex_unlock( &p_sql->p_sys->lock );
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Commit a transaction
* @param p_sql The SQL object
* @note This function unlocks the transactions on the database
* Only the calling thread of "BeginTransaction" is allowed to call this method
* If the commit fails, the transaction lock is still held by the thread
* and this function may be retried or RollbackTransaction can be called
* @return VLC_SUCCESS or VLC_EGENERIC
*/
static int CommitTransaction( sql_t* p_sql )
{
int i_ret = VLC_SUCCESS;
assert( p_sql->p_sys->db );
vlc_mutex_lock( &p_sql->p_sys->lock );
/** This turns the auto commit on. */
sqlite3_exec( p_sql->p_sys->db, "COMMIT;", NULL, NULL, NULL );
#ifndef NDEBUG
msg_Dbg( p_sql, "Transaction Query: COMMIT;" );
#endif
if( sqlite3_errcode( p_sql->p_sys->db ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_ret = VLC_EGENERIC;
}
else
vlc_mutex_unlock( &p_sql->p_sys->trans_lock );
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Rollback a transaction, in case of failure
* @param p_sql The SQL object
* @return VLC_SUCCESS or VLC_EGENERIC
* @note This function unlocks the transactions on the database
* Only the calling thread of "BeginTransaction" is allowed to call this method
* If failed, if a statement in the transaction failed, it means that
* the transaction was automatically rolled back
* If failed otherwise, the engine is busy executing some queries and you must
* try again
*/
static void RollbackTransaction( sql_t* p_sql )
{
assert( p_sql->p_sys->db );
vlc_mutex_lock( &p_sql->p_sys->lock );
sqlite3_exec( p_sql->p_sys->db, "ROLLBACK;", NULL, NULL, NULL );
#ifndef NDEBUG
msg_Dbg( p_sql, "Transaction Query: ROLLBACK;" );
#endif
if( sqlite3_errcode( p_sql->p_sys->db ) != SQLITE_OK )
{
msg_Err( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
}
vlc_mutex_unlock( &p_sql->p_sys->trans_lock );
vlc_mutex_unlock( &p_sql->p_sys->lock );
}
/**
* Prepare an sqlite statement
* @return statement object or NULL in case of failure
*/
static sql_stmt_t* PrepareStatement( sql_t* p_sql, const char* psz_fmt, int i_length )
{
assert( p_sql->p_sys->db );
sql_stmt_t* p_stmt;
p_stmt = calloc( 1, sizeof( *p_stmt ) );
if( p_stmt == NULL )
return NULL;
vlc_mutex_lock( &p_sql->p_sys->lock );
if( sqlite3_prepare_v2( p_sql->p_sys->db, psz_fmt, i_length,
&p_stmt->p_sqlitestmt, NULL ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
vlc_mutex_unlock( &p_sql->p_sys->lock );
free( p_stmt );
return NULL;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return p_stmt;
}
/**
* @brief Bind arguments to a sql_stmt_t object
* @param p_sql The SQL object
* @param p_stmt Statement Object
* @param i_pos Position at which the parameter should be bound
* @param i_type Data type of the value
* @param p_value Value to be bound
* @return VLC_SUCCESS or VLC_EGENERIC
*/
static int BindValues( sql_t* p_sql, sql_stmt_t* p_stmt,
int i_pos, unsigned int i_type, const sql_value_t* p_value )
{
assert( p_sql->p_sys->db );
assert( p_stmt->p_sqlitestmt );
vlc_mutex_lock( &p_sql->p_sys->lock );
int i_ret, i_vlc_ret = VLC_SUCCESS;
switch( i_type )
{
case SQL_INT:
i_ret = sqlite3_bind_int( p_stmt->p_sqlitestmt, i_pos, p_value->value.i );
break;
case SQL_DOUBLE:
i_ret = sqlite3_bind_double( p_stmt->p_sqlitestmt, i_pos, p_value->value.dbl );
break;
case SQL_TEXT:
i_ret = sqlite3_bind_text( p_stmt->p_sqlitestmt, i_pos, p_value->value.psz, p_value->length, NULL );
break;
case SQL_BLOB:
i_ret = sqlite3_bind_blob( p_stmt->p_sqlitestmt, i_pos, p_value->value.ptr, p_value->length, NULL );
break;
case SQL_NULL:
i_ret = sqlite3_bind_null( p_stmt->p_sqlitestmt, i_pos );
break;
default:
msg_Warn( p_sql, "Trying to bind invalid type of value %d", i_type );
vlc_mutex_unlock( &p_sql->p_sys->lock );
return VLC_EGENERIC;
}
if( i_ret != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_vlc_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_vlc_ret;
}
/**
* @brief Run the SQL statement. If the statement fetches data, then only
* one row of the data is fetched at a time. Run this function again to
* fetch the next row.
* @param p_sql The SQL object
* @param p_stmt The statement
* @return VLC_SQL_DONE if done fetching all rows or there are no rows to fetch
* VLC_SQL_ROW if a row was fetched for this statement.
* VLC_EGENERIC if this function failed
*/
static int StatementStep( sql_t* p_sql, sql_stmt_t* p_stmt )
{
assert( p_sql->p_sys->db );
assert( p_stmt->p_sqlitestmt );
vlc_mutex_lock( &p_sql->p_sys->lock );
int i_sqlret = sqlite3_step( p_stmt->p_sqlitestmt );
int i_ret = VLC_EGENERIC;
if( i_sqlret == SQLITE_ROW )
i_ret = VLC_SQL_ROW;
else if( i_ret == SQLITE_DONE )
i_ret = VLC_SQL_DONE;
else
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Reset the SQL statement. Resetting the statement will unbind all
* the values that were bound on this statement
* @param p_sql The SQL object
* @param p_stmt The sql statement object
* @return VLC_SUCCESS or VLC_EGENERIC
*/
static int StatementReset( sql_t* p_sql, sql_stmt_t* p_stmt )
{
assert( p_sql->p_sys->db );
assert( p_stmt->p_sqlitestmt );
int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sql->p_sys->lock );
if( sqlite3_reset( p_stmt->p_sqlitestmt ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Destroy the sql statement object. This will free memory.
* @param p_sql The SQL object
* @param p_stmt The statement object
* @return VLC_SUCCESS or VLC_EGENERIC
*/
static int StatementFinalize( sql_t* p_sql, sql_stmt_t* p_stmt )
{
assert( p_sql->p_sys->db );
assert( p_stmt->p_sqlitestmt );
int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sql->p_sys->lock );
if( sqlite3_finalize( p_stmt->p_sqlitestmt ) != SQLITE_OK )
{
msg_Warn( p_sql, "sqlite3 error: %d: %s",
sqlite3_errcode( p_sql->p_sys->db ),
sqlite3_errmsg( p_sql->p_sys->db ) );
i_ret = VLC_EGENERIC;
}
free( p_stmt );
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Get the column data
* @param p_sql The SQL object
* @param p_stmt The statement object
* @param i_col The column number
* @param type Datatype of result
* @param p_res The structure which contains the value of the result
* @return VLC_SUCCESS or VLC_EGENERIC
*/
static int GetColumnFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
int type, sql_value_t *p_res )
{
assert( p_sql->p_sys->db );
assert( p_stmt->p_sqlitestmt );
int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sql->p_sys->lock );
const unsigned char* psz;
const void* ptr;
int size;
switch( type )
{
case SQL_INT:
p_res->value.i = sqlite3_column_int( p_stmt->p_sqlitestmt, i_col );
break;
case SQL_DOUBLE:
p_res->value.dbl = sqlite3_column_double( p_stmt->p_sqlitestmt, i_col );
break;
case SQL_TEXT:
psz = sqlite3_column_text( p_stmt->p_sqlitestmt, i_col );
if( psz )
p_res->value.psz = strdup( (const char* ) psz );
break;
case SQL_BLOB:
ptr = sqlite3_column_blob( p_stmt->p_sqlitestmt, i_col );
size = sqlite3_column_bytes( p_stmt->p_sqlitestmt, i_col );
if( ptr )
{
p_res->value.ptr = malloc( size );
p_res->length = size;
if( p_res->value.ptr )
memcpy( p_res->value.ptr, ptr, size );
else
i_ret = VLC_ENOMEM;
}
break;
case SQL_NULL:
default:
msg_Warn( p_sql, "Trying to bind invalid type of value %d", type );
i_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Get the datatype of the result of the column
* @param p_sql The SQL object
* @param p_stmt The sql statement object
* @param i_col The column
* @param pi_type pointer to datatype of the given column
* @return VLC_SUCCESS or VLC_EGENERIC
*/
static int GetColumnTypeFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
int* pi_type )
{
assert( p_sql->p_sys->db );
assert( p_stmt->p_sqlitestmt );
assert( pi_type );
vlc_mutex_lock( &p_sql->p_sys->lock );
int i_ret = VLC_SUCCESS;
int i_sqlret = sqlite3_column_type( p_stmt->p_sqlitestmt, i_col );
switch( i_sqlret )
{
case SQLITE_INTEGER:
*pi_type = SQL_INT;
break;
case SQLITE_FLOAT:
*pi_type= SQL_DOUBLE;
break;
case SQLITE_TEXT:
*pi_type = SQL_TEXT;
break;
case SQLITE_BLOB:
*pi_type = SQL_BLOB;
break;
case SQLITE_NULL:
*pi_type = SQL_NULL;
break;
default:
i_ret = VLC_EGENERIC;
}
vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret;
}
/**
* @brief Get the size of the column in bytes
* @param p_sql The SQL object
* @param p_stmt The sql statement object
* @param i_col The column
* @return Size of the column in bytes, undefined for invalid columns
*/
static int GetColumnSize( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col )
{
assert( p_sql->p_sys->db );
assert( p_stmt->p_sqlitestmt );
return sqlite3_column_bytes( p_stmt->p_sqlitestmt, i_col );
}
|