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

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

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

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