root/trunk/src/shared/Database/QueryResultPostgre.cpp @ 37

Revision 2, 4.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#ifdef DO_POSTGRESQL
20
21#include "DatabaseEnv.h"
22
23QueryResultPostgre::QueryResultPostgre(PGresult *result, uint64 rowCount, uint32 fieldCount) :
24QueryResult(rowCount, fieldCount), mResult(result),  mTableIndex(0)
25{
26
27    mCurrentRow = new Field[mFieldCount];
28    ASSERT(mCurrentRow);
29
30    for (uint32 i = 0; i < mFieldCount; i++)
31    {
32        mFieldNames[i] = PQfname(result, i);
33        mCurrentRow[i].SetType(ConvertNativeType(PQftype( result, i )));
34    }
35}
36
37QueryResultPostgre::~QueryResultPostgre()
38{
39    EndQuery();
40}
41
42bool QueryResultPostgre::NextRow()
43{
44    if (!mResult)
45        return false;
46
47    if (mTableIndex >= mRowCount)
48    {
49        EndQuery();
50        return false;
51    }
52
53    char* pPQgetvalue;
54    for (int j = 0; j < mFieldCount; j++)
55    {
56        pPQgetvalue = PQgetvalue(mResult, mTableIndex, j);
57        if(pPQgetvalue && !(*pPQgetvalue))
58            pPQgetvalue = NULL;
59
60        mCurrentRow[j].SetValue(pPQgetvalue);
61    }
62    ++mTableIndex;
63
64    return true;
65}
66
67void QueryResultPostgre::EndQuery()
68{
69    if (mCurrentRow)
70    {
71        delete [] mCurrentRow;
72        mCurrentRow = 0;
73    }
74
75    if (mResult)
76    {
77        PQclear(mResult);
78        mResult = 0;
79    }
80}
81
82// see types in #include <postgre/pg_type.h>
83enum Field::DataTypes QueryResultPostgre::ConvertNativeType(Oid  pOid ) const
84{
85    switch (pOid)
86    {
87        case BPCHAROID:
88        case CIDOID:
89        case CIDROID:
90        case CIRCLEOID:
91        case INETOID:
92        case NAMEOID:
93        case TEXTOID:
94        case VARCHAROID:
95            return Field::DB_TYPE_STRING;
96        case CASHOID:
97        case FLOAT4OID:
98        case FLOAT8OID:
99        case NUMERICOID:
100            return Field::DB_TYPE_FLOAT;
101        case DATEOID:                                       // Date
102        case RELTIMEOID:                                    // Date
103        case TIMEOID:                                       // Time
104        case TIMETZOID:                                     // Time
105        case ABSTIMEOID:                                    // DateTime
106        case INTERVALOID:                                   // DateTime
107        case TIMESTAMPOID:                                  // DateTime
108        case TIMESTAMPTZOID:                                // DateTime
109        case INT2OID:                                       // Int
110        case INT2VECTOROID:                                 // Int
111        case INT4OID:                                       // Int
112        case OIDOID:                                        // Int
113        case CHAROID:                                       // UInt
114        case INT8OID:                                       // LongLong
115            return Field::DB_TYPE_INTEGER;
116        case BOOLOID:
117            return Field::DB_TYPE_BOOL;                     // Bool
118/*
119        case BOXOID:    Rect;
120        case LINEOID:   Rect;
121        case VARBITOID: BitArray;
122        case BYTEAOID:  ByteArray;
123*/
124        case LSEGOID:
125        case OIDVECTOROID:
126        case PATHOID:
127        case POINTOID:
128        case POLYGONOID:
129        case REGPROCOID:
130        case TIDOID:
131        case TINTERVALOID:
132        case UNKNOWNOID:
133        case XIDOID:
134        default:
135            return Field::DB_TYPE_UNKNOWN;
136    }
137    return Field::DB_TYPE_UNKNOWN;
138}
139#endif
Note: See TracBrowser for help on using the browser.