NdbScanOperation.hpp 6.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*****************************************************************************
 * Name:          NdbScanOperation.hpp
 * Include:
 * Link:
 * Author:        Martin Skld
 * Date:          2002-04-01
 * Version:       0.1
 * Description:   Table scan support
 * Documentation:
 * Adjust:  2002-04-01  Martin Skld   First version.
 ****************************************************************************/

#ifndef NdbScanOperation_H
#define NdbScanOperation_H


#include <NdbOperation.hpp>
#include <NdbCursorOperation.hpp>

/**
 * @class NdbScanOperation
 * @brief Class of scan operations for use in transactions.  
 */
class NdbScanOperation : public NdbCursorOperation
{
  friend class Ndb;
  friend class NdbConnection;
  friend class NdbResultSet;
  friend class NdbOperation;

public:
  /**
   * readTuples returns a NdbResultSet where tuples are stored.
   * Tuples are not stored in NdbResultSet until execute(NoCommit) 
   * has been executed and nextResult has been called.
   * 
   * @param parallel  Scan parallelism
   * @param LockMode  Scan lock handling   
   * @returns NdbResultSet.
   */ 
  virtual NdbResultSet* readTuples(unsigned parallel = 0, 
				   LockMode = LM_Read );
  
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL

  int updateTuples();
  int updateTuples(Uint32 parallelism);

  int deleteTuples();
  int deleteTuples(Uint32 parallelism);

  // Overload setValue for updateTuples
  int setValue(const char* anAttrName, const char* aValue, Uint32 len = 0);
  int setValue(const char* anAttrName, Int32 aValue);
  int setValue(const char* anAttrName, Uint32 aValue);
  int setValue(const char* anAttrName, Int64 aValue);
  int setValue(const char* anAttrName, Uint64 aValue);
  int setValue(const char* anAttrName, float aValue);
  int setValue(const char* anAttrName, double aValue);

  int setValue(Uint32 anAttrId, const char* aValue, Uint32 len = 0);
  int setValue(Uint32 anAttrId, Int32 aValue);
  int setValue(Uint32 anAttrId, Uint32 aValue);
  int setValue(Uint32 anAttrId, Int64 aValue);
  int setValue(Uint32 anAttrId, Uint64 aValue);
  int setValue(Uint32 anAttrId, float aValue);
  int setValue(Uint32 anAttrId, double aValue);
#endif
private:
  NdbScanOperation(Ndb* aNdb);

  ~NdbScanOperation();

  NdbCursorOperation::CursorType cursorType();

  virtual int nextResult(bool fetchAllowed = true);
  virtual void release();
  
  void closeScan();

  // Overloaded methods from NdbCursorOperation
  int executeCursor(int ProcessorId);

  // Overloaded private methods from NdbOperation
  int init(NdbTableImpl* tab, NdbConnection* myConnection);
  int prepareSend(Uint32  TC_ConnectPtr, Uint64  TransactionId);
  int doSend(int ProcessorId);

  virtual void setErrorCode(int aErrorCode);
  virtual void setErrorCodeAbort(int aErrorCode);

  virtual int equal_impl(const NdbColumnImpl* anAttrObject, 
                         const char* aValue, 
                         Uint32 len);
private:
  NdbConnection *m_transConnection;
  bool m_autoExecute;
  bool m_updateOp;
  bool m_writeOp;
  bool m_deleteOp;
  class SetValueRecList* m_setValueList;
};

#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
class AttrInfo;
class SetValueRecList;

class SetValueRec {
  friend class SetValueRecList;
public:
  SetValueRec();
  ~SetValueRec();

  enum SetValueType {
    SET_STRING_ATTR1 = 0,
    SET_INT32_ATTR1  = 1,
    SET_UINT32_ATTR1 = 2,
    SET_INT64_ATTR1  = 3,
    SET_UINT64_ATTR1 = 4,
    SET_FLOAT_ATTR1  = 5,
    SET_DOUBLE_ATTR1 = 6,
    SET_STRING_ATTR2 = 7,
    SET_INT32_ATTR2  = 8,
    SET_UINT32_ATTR2 = 9,
    SET_INT64_ATTR2  = 10,
    SET_UINT64_ATTR2 = 11,
    SET_FLOAT_ATTR2  = 12,
    SET_DOUBLE_ATTR2 = 13
  };

  SetValueType stype;
  union {
    char* anAttrName;
    Uint32 anAttrId;
  };
  typedef struct String {
    char* aStringValue;
    Uint32 len;
  };
  union {
    String stringStruct;
    Int32 anInt32Value;
    Uint32 anUint32Value;
    Int64 anInt64Value;
    Uint64 anUint64Value;
    float aFloatValue;
    double aDoubleValue;
  };
private:
  SetValueRec* next;
};

inline 
SetValueRec::SetValueRec() :
  next(0) 
{
}

inline
SetValueRec::~SetValueRec() 
{
  if ((stype == SET_STRING_ATTR1) ||
      (stype == SET_INT32_ATTR1)  ||
      (stype == SET_UINT32_ATTR1) ||
      (stype == SET_INT64_ATTR1) ||
      (stype == SET_UINT64_ATTR1) ||
      (stype == SET_FLOAT_ATTR1) ||
      (stype == SET_DOUBLE_ATTR1))
    free(anAttrName);

  if ((stype == SET_STRING_ATTR1) ||
      (stype == SET_STRING_ATTR2))
    free(stringStruct.aStringValue);
  if (next) delete next;
  next = 0;
}

class SetValueRecList {
public:
  SetValueRecList();
  ~SetValueRecList();

  void add(const char* anAttrName, const char* aValue, Uint32 len = 0);
  void add(const char* anAttrName, Int32 aValue);
  void add(const char* anAttrName, Uint32 aValue);
  void add(const char* anAttrName, Int64 aValue);
  void add(const char* anAttrName, Uint64 aValue);
  void add(const char* anAttrName, float aValue);
  void add(const char* anAttrName, double aValue);
  void add(Uint32 anAttrId, const char* aValue, Uint32 len = 0);
  void add(Uint32 anAttrId, Int32 aValue);
  void add(Uint32 anAttrId, Uint32 aValue);
  void add(Uint32 anAttrId, Int64 aValue);
  void add(Uint32 anAttrId, Uint64 aValue);
  void add(Uint32 anAttrId, float aValue);
  void add(Uint32 anAttrId, double aValue);

  typedef void(* IterateFn)(SetValueRec&, NdbOperation&);
  static void callSetValueFn(SetValueRec&, NdbOperation&);
  void iterate(IterateFn nextfn, NdbOperation&);
private:
  SetValueRec* first;    
  SetValueRec* last;    
};

inline
SetValueRecList::SetValueRecList() : 
  first(0),
  last(0) 
{
}
  
inline
SetValueRecList::~SetValueRecList() {
  if (first) delete first;
  first = last = 0;
}


inline
void SetValueRecList::iterate(SetValueRecList::IterateFn nextfn, NdbOperation& oper) 
{
  SetValueRec* recPtr = first;
  while(recPtr) {
    (*nextfn)(*recPtr, oper);
    recPtr = recPtr->next; // Move to next in list - MASV
  }
}

#endif

#endif