/*! * \file mg_db_gd_pg.c * \brief A capsule around postgresql database access * * \version $Revision: 1.2 $ * \date $Date: 2005-04-13 17:42:54 +0100 (Thu, 13 Apr 2005) $ * \author Wolfgang Rohdewald * \author Responsible author: $Author: wolfgang61 $ */ #include #include #include #include #include #include #include #include #include #include "mg_setup.h" #include "mg_item_gd.h" #include "mg_db_gd_pg.h" #include #include using namespace std; mgQueryPG::mgQueryPG(void* db,string sql,mgQueryNoise noise) : mgQueryImp(db,sql,noise) { m_db = (PGconn*)m_db_handle; m_cursor = 0; m_table = PQexec(m_db,m_optsql); switch ((m_rc=PQresultStatus(m_table))) { case PGRES_COMMAND_OK: m_rows = atol(PQcmdTuples(m_table)); break;; case PGRES_TUPLES_OK: m_rows = PQntuples(m_table); m_columns = PQnfields(m_table); break; default: m_errormessage = PQresultErrorMessage (m_table); break; } HandleErrors(); } mgQueryPG::~mgQueryPG() { PQclear(m_table); } char ** mgQueryPG::Next() { if (m_cursor>=Rows()) return 0; assert(Columns()<100); memset(m_rowpointers,0,sizeof(m_rowpointers)); for (int idx=0;idx0) sprintf(port," port = %d ",the_setup.DbPort); else port[0]=0; if (notempty(the_setup.DbHost)) snprintf(host,199," host = %s ",the_setup.DbHost); else if (notempty(the_setup.DbSocket)) snprintf(host,199," host = %s ",the_setup.DbSocket); else host[0]=0; if (the_setup.DbUser==0) user=getenv("LOGNAME"); else user=the_setup.DbUser; snprintf(conninfo,499,"%s %s dbname = %s user = %s ", host,port,the_setup.DbName,user); m_db = PQconnectdb(conninfo); if (PQstatus(m_db) != CONNECTION_OK) { mgWarning("Failed to connect to postgres server using %s:%s",conninfo,PQerrorMessage(m_db)); return false; } return SetCharset(); } bool mgDbGd::NeedGenre2() { if (!needGenre2_set && Connect()) { needGenre2_set=true; needGenre2=exec_count("SELECT COUNT(DISTINCT genre2) FROM tracks")>1; } return needGenre2; } bool mgDbGd::FieldExists(string table, string field) { char *b; msprintf(&b,"SELECT COUNT(*) FROM information_schema.columns WHERE table_name='%s' AND column_name='%s'", table.c_str(),field.c_str()); bool result = exec_count(b)==1; free(b); return result; } const char* mgDbGd::DecadeExpr() { return "substring(cast(10 * floor(tracks.year/10) as char(4)),3)"; }