////////////////////////////////////////////////////////////////////////
//
// $Id: DBInt.h,v 1.4 2001/11/28 21:25:31 bviren Exp $
//
// DBInt
//
// Package: elbo
//
// Interface to MySQL database.
//
// Contact: bv@bnl.gov
//
// Created on: Sun Nov 18 12:51:06 2001
//
////////////////////////////////////////////////////////////////////////

#ifndef ELBO_DBINT_H
#define ELBO_DBINT_H

#include <mysql/mysql.h>
#include <string>
#include <vector>

class DBInt
{

public:

    DBInt();
    DBInt(const char* db,  const char* host=0, const char* user=0, 
          const char* password=0);
    ~DBInt();

    // connect to MySQL, return false if fails.  Second constructor
    // calls this. Host == 0, use "localhost", user = 0, use $USER
    bool Init(const char* db, const char* host=0, const char* user=0, 
              const char* password = 0);

    // Call to break connection with MySQL.  Done for you in destructor.
    void Close();

    // Get nth row (counting from 0) of fields as listed in "what"
    // from table table_name with id id.  Don't use this for iterating
    // over nth, it is expensive.
    bool GetNthData(int id, int nth,
                    string table_name, string what, string order_by, 
                    vector<string>& data);

    // Get all of "what" in order "order_by" from table "table_name"
    // with given id.  If id < 0, don't select on id.
    bool GetData(int id, string table_name, string what, string order_by,
                 vector<vector<string> >& data);

    // Set multiple CSV data in table using id. what holds CSV of
    // field names corresponding to the CSV in the data vector.
    bool SetData(int id, string table_name, string what, 
                 vector<string>& data);

    // Return the fields for given table
    bool GetFields(string table_name, vector<string>& fields);

    // Return ID associated with query or 0
    int GetId(string table_name, string query);

private:

    DBInt(const DBInt& rhs); // copy constructor
    DBInt& operator=(const DBInt& rhs); // assignment

    MYSQL* fMysql;
};                              // end of class DBInt

// util func returning a query sub string which matches name within
// +/- 1% of value
string bracket_double(const char* name, double value);

#endif  // ELBO_DBINT_H
