/*
 * EH_DB.h
 *
 *  Created on: 2013-9-10
 *      Author: sj
  usage:
	 try {
		 StartTransaction();
		 ExecuteQuery
		 ExecuteUpdate
		 .....
		 CommitTransaction();
	 } catch (...) {
		ExceptionInTransaction();//NOTE!!

		....//custom process code
	 }
	 modify by zgm at 2016-5-6
 */

#ifdef NOT_X86

#ifndef EH_DB_H_
#define EH_DB_H_

#include <iostream>
#include <string>
#include <map>
#include <vector>

#include "ace/Recursive_Thread_Mutex.h"
#include "ace/Process_Mutex.h"

#include "EH_Sqlite3.h"

using namespace std;

typedef vector<map<string, string>*> 		DBRESVEC;
typedef vector<map<string, string>*>* 		DBRES;
typedef map<string, string> 				DBROWMAP;
typedef map<string, string>* 				DBROW;

class EH_DB {
public:
    EH_DB(string username,string passwd,string addr,string port,string dbname);
    ~EH_DB();

    /*
     * If already have a connection,don't get a new connection.
     * if success return 0, else are other values.
     */
    int OpenConnection();
    /*
     * close the db connection
     */
    void CloseConnection();
    /*
     *  if success return 0, else are other values.
     */
    int ResetConnection();
    /*
	 *  if success return 0, else are other values.
	 */
    int StartTransaction();
    /*
	 *  if success return 0, else are other values.
	 */
    int CommitTransaction();
    /*
	 *  if success return 0, else are other values.
	 */
    int AbortTransaction();
    /* must be called when exception occur when in executing a transaction.
     * if success return 0, else are other values.
     */
    int ExceptionInTransaction();
    /*
     * release the DBRES is the application's responsibility
     */
    DBRES ExecuteQuery(string sql);
    void ExecuteUpdate(string sql);

    static void ReleaseResultSet(DBRES& res);

private:


private:
    void* 							m_pConn;
    string 							m_strConnectionInfo;
    int		m_iTransactionFlag;
    int 	m_iErrCode;
};

#endif /* EH_DB_H_ */

#endif
