xwords/xwords4/linux/gamesdb.c

91 lines
2.8 KiB
C
Raw Normal View History

2013-01-06 01:08:47 +01:00
/* -*-mode: compile-command: "make MEMDEBUG=TRUE -j3"; -*- */
/*
2013-01-06 01:08:47 +01:00
* Copyright 2000-2013 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "gamesdb.h"
2013-01-06 01:08:47 +01:00
#include "main.h"
#define DB_NAME "games.db"
sqlite3*
openGamesDB( void )
{
sqlite3* pDb = NULL;
int result = sqlite3_open( DB_NAME, &pDb );
XP_ASSERT( SQLITE_OK == result );
const char* createStr =
"CREATE TABLE games ( "
"game BLOB"
",room VARCHAR(32)"
")";
result = sqlite3_exec( pDb, createStr, NULL, NULL, NULL );
XP_LOGF( "sqlite3_exec=>%d", result );
// XP_ASSERT( SQLITE_OK == result );
return pDb;
}
void
closeGamesDB( sqlite3* pDb )
{
sqlite3_close( pDb );
XP_LOGF( "%s finished", __func__ );
}
2013-01-06 01:08:47 +01:00
void
writeToDB( XWStreamCtxt* stream, void* closure )
{
int result;
CommonGlobals* cGlobals = (CommonGlobals*)closure;
sqlite3_int64 rowid = cGlobals->rowid;
sqlite3* pDb = cGlobals->pDb;
XP_U16 len = stream_getSize( stream );
sqlite3_stmt* stmt = NULL;
if ( 0 == rowid ) { /* new row; need to insert blob first */
const char* txt = "INSERT INTO games (game) VALUES (?)";
result = sqlite3_prepare_v2( pDb, txt, -1, &stmt, NULL );
XP_ASSERT( SQLITE_OK == result );
result = sqlite3_bind_zeroblob( stmt, 1 /*col 0 ??*/, len );
XP_ASSERT( SQLITE_OK == result );
result = sqlite3_step( stmt );
XP_ASSERT( SQLITE_DONE == result );
rowid = sqlite3_last_insert_rowid( pDb );
XP_LOGF( "%s: new rowid: %lld", __func__, rowid );
cGlobals->rowid = rowid;
sqlite3_finalize( stmt );
}
sqlite3_blob* blob;
result = sqlite3_blob_open( pDb, "main", "games", "game",
rowid, 1 /*flags: writeable*/, &blob );
XP_ASSERT( SQLITE_OK == result );
const XP_U8* ptr = stream_getPtr( stream );
result = sqlite3_blob_write( blob, ptr, len, 0 );
XP_ASSERT( SQLITE_OK == result );
result = sqlite3_blob_close( blob );
XP_ASSERT( SQLITE_OK == result );
if ( !!stmt ) {
sqlite3_finalize( stmt );
}
}