/*! * \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 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; snprintf(conninfo,499,"%s %s dbname = %s user = %s ", host,port,the_setup.DbName,the_setup.DbUser); 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; } m_database_found = true; // otherwise we get into a recursion m_database_found = exec_count("SELECT COUNT(*) FROM information_schema.tables WHERE table_name='album'"); if (m_database_found) return true; if (time(0)1; } return needGenre2; } bool mgDbGd::FieldExists(string table, string field) { if (!Connect()) return false; char *b; asprintf(&b,"SELECT COUNT(*) FROM information_schema.columns WHERE table_name='album' AND column_name='%s'",field.c_str()); bool result = exec_count(b)==1; free(b); return result; } const char* mgDbGd::DecadeExpr() { return "substring(10 * floor(tracks.year/10),3)"; }