PageRenderTime 77ms CodeModel.GetById 69ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 1ms

/TGame/TUtil/Config/ConfigWriterSqlite.cpp

http://awoe.googlecode.com/
C++ | 74 lines | 56 code | 8 blank | 10 comment | 5 complexity | 67ff9798dac1795c62ec7b5160ae7870 MD5 | raw file
 1#include "stdafx.h"
 2#include "ConfigWriterSqlite.h"
 3#include "SQLite\DB_SQLite.h"
 4#include "Log\Log.h"
 5
 6namespace woe
 7{
 8	ConfigWriterSqlite*	ConfigWriterSqlite::m_Instance = NULL;
 9
10	ConfigWriterSqlite::ConfigWriterSqlite()
11	{
12		//InitializeCriticalSection( &m_csWriteSqlite );
13	}
14
15	ConfigWriterSqlite::~ConfigWriterSqlite()
16	{
17		//DeleteCriticalSection( &m_csWriteSqlite );
18		if ( m_Instance )
19		{
20			delete m_Instance;
21			m_Instance = NULL;
22		}
23	}
24
25	bool ConfigWriterSqlite::DoSave(const std::string& strFileName, IConfigOperator* config)
26	{
27		if ( !config )
28		{
29			LOG_DEBUG("Failed to , ConfigWriterSqlite::DoSave, the param config is NULL\n");
30			return false;
31		}
32		CSQLiteBase* pSQLiteBase = new CSQLiteBase;
33		if ( !pSQLiteBase )
34		{
35			LOG_DEBUG("Failed to , ConfigWriterSqlite::DoSave, new CSQLiteBase failed\n");
36			return false;
37		}
38
39		//const char* s = ".db";
40		//std::string newdb = strFileName;
41		//newdb.append(s);
42		//
43		//remove( newdb.c_str() );
44		//::CreateFile( newdb.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
45		//::EnterCriticalSection( &m_csWriteSqlite );
46		if ( !pSQLiteBase->Open( strFileName.c_str() ) )
47		{
48			LOG_DEBUG_V("Failed to Open db %s ,ConfigWriterSqlite::DoSave\n",strFileName.c_str());
49			return false;		
50		}
51
52		char szSQL[256] = "delete from tbl ";
53		CppSQLite3DB* db = pSQLiteBase->GetDB();
54		db->execDML( szSQL );
55
56		std::string key , value;
57		while( config->GetNext(key,value) )
58		{
59			sprintf( szSQL, "insert into tbl values('%s','%s');",key.c_str(),value.c_str() );
60			db->execDML( szSQL );
61		}
62		//::LeaveCriticalSection( &m_csWriteSqlite );
63		return true;
64	}
65
66	ConfigWriterSqlite* ConfigWriterSqlite::GetInstance()
67	{
68		if ( !m_Instance )
69		{
70			m_Instance = new ConfigWriterSqlite;
71		}
72		return m_Instance;
73	}
74}