全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 ページ
fields.h
1 #ifndef BZS_DB_PROTOCOL_TDAP_CLIENT_FIELDS_H
2 #define BZS_DB_PROTOCOL_TDAP_CLIENT_FIELDS_H
3 /*=================================================================
4  Copyright (C) 2014 BizStation Corp All rights reserved.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (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
19  02111-1307, USA.
20 =================================================================*/
21 #include "field.h"
22 #include "table.h"
23 #include <boost/shared_ptr.hpp>
24 #include <stdio.h>
25 namespace bzs
26 {
27 namespace db
28 {
29 namespace protocol
30 {
31 namespace tdap
32 {
33 namespace client
34 {
35 
36 
37 /** @cond INTERNAL */
38 #define MEM_ALLOC_TYPE_NONE 0
39 #define MEM_ALLOC_TYPE_ONE 1
40 #define MEM_ALLOC_TYPE_ARRAY 2
41 
42 /* This class specify memory allocation type owned.
43  If copy this object do not copy menbers.
44  Because copy destination allocation type specify destination owned.
45 */
46 class refarymem
47 {
48  union
49  {
50  refarymem* m_parent;
51  int m_refCount;
52  };
53 
54  bool m_child;
55  unsigned char m_allocType;
56 
57  virtual void releaseMemory() = 0;
58 
59 protected:
60 
61  refarymem(const refarymem& r):m_parent(NULL),
62  m_child(0), m_allocType(MEM_ALLOC_TYPE_NONE){}
63 
64  virtual ~refarymem(){}
65 
66  refarymem& operator=(const refarymem& r)
67  {
68  return *this;
69  }
70 
71  inline int allocType() {return m_allocType;}
72 
73 public:
74 
75  refarymem():m_parent(NULL), m_child(0), m_allocType(MEM_ALLOC_TYPE_NONE){}
76 
77  inline void setAllocParent(refarymem* v)
78  {
79  m_parent = v;
80  m_child = (v != NULL);
81  }
82 
83  void setAllocTypeThis(int v) { m_allocType = ( unsigned char)v; }
84 
85  void addref()
86  {
87  if (m_child)
88  m_parent->addref();
89  else if (m_allocType)
90  ++m_refCount;
91  }
92 
93  void release()
94  {
95  if (m_child)
96  m_parent->release();
97  else
98  {
99  --m_refCount;
100  if (m_refCount == 0)
101  releaseMemory();
102  }
103  }
104 
105  int refcount() const { return m_refCount; }
106 
107  bool tryFastRelease(int totalRefCount)
108  {
109  if (!m_child && (m_refCount == totalRefCount))
110  {
111  m_refCount = 1;
112  release();
113  return true;
114  }
115  return false;
116  }
117 };
118 /** @endcond */
119 
120 class autoMemory;
121 
122 /* copyable */
123 class fieldsBase : public refarymem
124 {
125  friend class multiRecordAlocatorImple; // null_ptr setInvalidMemblock ...
126  friend class recordsetImple; // setRecordData setFielddefs
127  friend class recordsetQuery; // setRecordData
128  friend class groupQueryImple; // setInvalidMemblock
129  friend class recordCache; // setInvalidMemblock
130  friend class table; // setInvalidMemblock
131 
132  virtual unsigned char* ptr(int index) const = 0;
133  virtual unsigned char* nullPtr(int index) const = 0;
134  virtual int memoryBlockIndex(int index) const { return 0;}
135  virtual int memoryBlockIndexCache() const { return 0;}
136 
137 protected:
138  /** @cond INTERNAL */
139  fielddefs* m_fns;
140  unsigned int m_InvalidFlags;
141 
142  virtual table* tbptr() const { return NULL; }
143 
144  void throwIndexError(short index) const
145  {
146  if (tbptr())
147  {
148  tbptr()->setStat(STATUS_INVARID_FIELD_IDX);
149  nstable::throwError(_T("field access"), tbptr());
150  }
151  else
152  {
153  _TCHAR tmp[100];
154  _stprintf_s(tmp, 100, _T("field access index %d of %s"), index,
155  tbptr() ? tbptr()->tableDef()->tableName() : _T(""));
156  nstable::throwError(tmp, STATUS_INVARID_FIELD_IDX);
157  }
158  }
159 
160  explicit inline fieldsBase(fielddefs* fns)
161  : refarymem(), m_fns(fns), m_InvalidFlags(0)
162  {
163  }
164 
165  inline void setFielddefs(fielddefs* def) { m_fns = def; }
166 
167  virtual void removeLastMemBlock(){};
168 
169  virtual void setRecordData(autoMemory* am, unsigned char* ptr, size_t size,
170  short* endFieldIndex, bool owner = false){};
171 
172  inline void setInvalidMemblock(short index)
173  {
174  if (index == -1)
175  m_InvalidFlags = 0;
176  else
177  {
178  int num = memoryBlockIndex(index);
179  m_InvalidFlags |= ((2L << num) | 1L);
180  }
181  }
182 
183  /** @endcond */
184 public:
185 
186  virtual ~fieldsBase(){};
187 
188  inline bool isInvalidRecord() const
189  {
190  return (m_InvalidFlags & 1) != 0;
191  }
192 
193  inline void setInvalidRecord(bool v)
194  {
195  if (v)
196  m_InvalidFlags |= 1L;
197  else
198  m_InvalidFlags &= ~1L;
199  }
200 
201  inline field getFieldNoCheck(short index) const
202  {
203  unsigned char* p = ptr(index);
204  bool nullfield = (m_InvalidFlags &
205  (2L << memoryBlockIndexCache())) != 0;
206 
207  return field(p, (*m_fns)[index], m_fns, nullfield);
208  }
209 
210  inline field operator[](short index) const
211  {
212  if (m_fns->checkIndex(index))
213  return getFieldNoCheck(index);
214 
215  throwIndexError(index);
216  return field(NULL, dummyFd(), m_fns);
217  }
218 
219  inline field operator[](const _TCHAR* name) const
220  {
221  return operator[](std::_tstring(name));
222  }
223 
224  inline field operator[](const std::_tstring& name) const
225  {
226  short index = m_fns->indexByName(name);
227  return operator[](index);
228  }
229 
230  inline size_t size() const { return m_fns->size(); }
231 
232  inline field fd(short index) const { return operator[](index); }
233 
234  inline field fd(const _TCHAR* name) const
235  {
236  int index = m_fns->indexByName(name);
237  return operator[](index);
238  }
239 
240  inline short indexByName(const _TCHAR* name) const
241  {
242  return m_fns->indexByName(name);
243  }
244 
245  inline const fielddefs* fieldDefs() const { return m_fns; }
246 
247  virtual void clear() = 0;
248 };
249 
250 typedef boost::shared_ptr<database> database_ptr;
251 typedef boost::shared_ptr<table> table_ptr;
252 typedef fieldsBase row;
253 typedef row* row_ptr;
254 
255 /* copyable*/
256 class fields : public fieldsBase
257 {
258  table& m_tb;
259 
260  void releaseMemory(){}
261 
262  inline unsigned char* ptr(int index) const
263  {
264  return nullPtr(index) + (*m_fns)[index].nullbytes();
265  }
266 
267  inline unsigned char* nullPtr(int index) const
268  {
269  return ((unsigned char*)m_tb.data());
270  }
271 
272  table* tbptr() const { return &m_tb; }
273 
274  inline explicit fields() : fieldsBase(NULL), m_tb(*((table*)0))
275  {
276  }
277 
278 public:
279  inline explicit fields(table& tb) : fieldsBase(tb.m_fddefs), m_tb(tb) {}
280 
281  inline explicit fields(table_ptr tb)
282  : fieldsBase((*tb).m_fddefs), m_tb(*tb)
283  {
284  }
285 
286  inline void clear() { m_tb.clearBuffer(); }
287 
288  inline table& tb() const { return m_tb; }
289 
290  inline short inproc_size() const { return m_tb.getCurProcFieldCount(); }
291 
292  inline field inproc_fd(short index) const
293  {
294  return operator[](m_tb.getCurProcFieldIndex(index));
295  }
296 };
297 
298 } // namespace client
299 } // namespace tdap
300 } // namespace protocol
301 } // namespace db
302 } // namespace bzs
303 
304 #endif // BZS_DB_PROTOCOL_TDAP_CLIENT_FIELDS_H
bool isInvalidRecord() const
Definition: fields.h:188
const fielddefs * fieldDefs() const
Definition: fields.h:245
friend class multiRecordAlocatorImple
Definition: fields.h:125
virtual ~fieldsBase()
Definition: fields.h:186
field fd(const _TCHAR *name) const
Definition: fields.h:234
const void * data() const
Definition: nsTable.h:163
field operator[](const _TCHAR *name) const
Definition: fields.h:219
static void throwError(const _TCHAR *caption, short statusCode)
friend class table
Definition: fields.h:130
field operator[](const std::_tstring &name) const
Definition: fields.h:224
field getFieldNoCheck(short index) const
Definition: fields.h:201
field inproc_fd(short index) const
Definition: fields.h:292
friend class recordCache
Definition: fields.h:129
boost::shared_ptr< table > table_ptr
Definition: fields.h:251
friend class recordsetImple
Definition: fields.h:126
フィールドコレクションクラス
Definition: fields.h:256
row * row_ptr
Definition: fields.h:253
void clearBuffer(eNullReset resetType=defaultNull)
boost::shared_ptr< database > database_ptr
Definition: fields.h:250
field operator[](short index) const
Definition: fields.h:210
テーブルアクセスクラス (nocopyable)
Definition: table.h:91
fielddef のコレクションクラス
Definition: field.h:74
size_t size() const
Definition: fields.h:230
recordset フィルタリングクエリー
Definition: groupQuery.h:107
fields(table &tb)
Definition: fields.h:279
void setInvalidRecord(bool v)
Definition: fields.h:193
フィールドコレクションのベースクラス
Definition: fields.h:123
void clear()
Definition: fields.h:286
fieldsBase row
Definition: fields.h:252
friend class groupQueryImple
Definition: fields.h:128
short getCurProcFieldIndex(short index) const
field fd(short index) const
Definition: fields.h:232
short inproc_size() const
Definition: fields.h:290
table & tb() const
Definition: fields.h:288
short indexByName(const _TCHAR *name) const
Definition: fields.h:240
fields(table_ptr tb)
Definition: fields.h:281
フィールドアクセスクラス
Definition: field.h:118

Transactd SDK 2018年07月31日(火) 19時40分21秒 doxygen