~mordred/ndb-bindings/resultsets

22.1.2 by Monty Taylor
Added the rest of the sample files.
1
// -*- mode: c++ -*- 
321.1.15 by Monty Taylor
Modified references from NDB/Connectors to NDB/Bindings
2
/*  ndb-bindings: Bindings for the NDB API
22.1.2 by Monty Taylor
Added the rest of the sample files.
3
    Copyright (C) 2006 MySQL, Inc.
4
    
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or 
8
    (at your option) any later version.
9
    
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
15
    You should have received a copy of the GNU General Public License
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
*/
19
20
%include "typemaps.i"
21
22
%{
23
#include <NdbApi.hpp> 
24
%}
25
26
%include "ndb_constants.h"
27
%include "ndb_init.h"
28
29
30
#if defined(SWIG_PYTHON)
31
%init %{
32
ndb_init();
33
%}
34
#endif
35
36
enum AbortOption {
37
  CommitIfFailFree = 0,         
38
  TryCommit = 0,
39
  AbortOnError = 0,
40
  CommitAsMuchAsPossible = 2,
41
  AO_IgnoreError = 2
42
};
43
44
enum ExecType { 
45
  NoExecTypeDef = -1,
46
  Prepare,
47
  NoCommit,
48
  Commit,
49
  Rollback
50
};
51
52
typedef unsigned long long Uint64; 
53
typedef unsigned int Uint32; 
54
typedef signed long long Int64; 
55
typedef signed int Int32;
56
57
58
/*%include "cluster.hpp"*/
59
60
class Ndb_cluster_connection {
61
62
public:
63
64
  Ndb_cluster_connection(const char * connectstring = 0);
65
  ~Ndb_cluster_connection();
66
67
  %exception connect { 
68
    $action
69
      if (result) { 
70
	const char * msg = "Connect to management server failed"; 
71
#if defined(SWIGPYTHON)
72
        PyErr_SetString(PyExc_RuntimeError, msg); 
73
        return PyInt_FromLong(-1); 
74
#endif
75
#if defined(SWIGJAVA)
76
	jclass clazz = jenv->FindClass("java/lang/RuntimeException");
77
	jenv->ThrowNew(clazz, msg); 
78
        return -1; 
79
#endif 
80
      }
81
  }
82
83
  int connect(int no_retries=0, int retry_delay_in_seconds=1, int verbose=0);
84
  int wait_until_ready(int timeout_for_first_alive,
85
		       int timeout_after_first_alive);
86
};
87
88
89
%extend Ndb_cluster_connection {
90
public:
91
  Ndb* getNdb(const char* aCatalogName="", const char* aSchemaName="def") {
92
    Ndb* myNdb = new Ndb(self,aCatalogName,aSchemaName);
93
    return myNdb; 
94
  }
95
}
96
97
class Ndb { 
98
99
public:
100
  Ndb(Ndb_cluster_connection *ndb_cluster_connection,
101
      const char* aCatalogName = "", const char* aSchemaName = "def");
102
  ~Ndb();
103
104
105
106
  int init(int maxNoOfTransactions = 4);
107
108
  // Deprcated 
109
  int waitUntilReady(int timeout = 60);
110
  const NdbError & getNdbError() const;
111
112
  %exception startTransaction { 
113
    $action
114
      if (result==NULL) { 
115
        NdbError err = arg1->getNdbError();
116
	
117
        PyErr_SetString(PyExc_RuntimeError, err.message);
118
        return NULL; 
119
      }
120
  }
121
  NdbTransaction* startTransaction(const NdbDictionary::Table *table= 0,
122
				   const char  *keyData = 0, 
123
				   Uint32       keyLen = 0);
124
public:
125
  int getAutoIncrementValue(const char* aTableName, 
126
                            Uint64 & tupleId, Uint32 cacheSize);
127
  void closeTransaction(NdbTransaction*);
128
};     
129
130
%extend Ndb {
131
public:
132
  NdbTransaction* startTransaction(const char* aTableName, 
133
				   const char *keyData) {
134
    const NdbDictionary::Dictionary *myDict = self->getDictionary();
135
    const NdbDictionary::Table *myTable = myDict->getTable(aTableName);
136
    return self->startTransaction(myTable,keyData);
137
  }
138
  NdbTransaction* startTransaction(const char* aTableName, 
139
				   int keyData) {
140
    const NdbDictionary::Dictionary *myDict = self->getDictionary();
141
    const NdbDictionary::Table *myTable = myDict->getTable(aTableName);
142
    return self->startTransaction(myTable,(const char *) &keyData);
143
  }
144
145
  Uint64 getAutoIncrementValue(const char* aTableName,
146
			       Uint32 cacheSize) {
147
    
148
    Uint64 id = 0;
149
    self->getAutoIncrementValue(aTableName,id,cacheSize);
150
    return id;
151
  };
152
};
153
154
class NdbTransaction {
155
  ~NdbTransaction();
156
  NdbTransaction(Ndb* aNdb); 
157
158
public: 
159
  NdbOperation* getNdbOperation(const char* aTableName);
160
  NdbScanOperation* getNdbScanOperation(const char* aTableName);
161
  const NdbError & getNdbError() const;
162
  int execute(ExecType execType,
163
	      AbortOption abortOption = AbortOnError,
164
	      int force = 0 );
165
};
166
167
class NdbOperation {
168
169
170
  friend class Ndb;
171
  friend class NdbTransaction;
172
  friend class NdbScanOperation;
173
  friend class NdbScanReceiver;
174
  friend class NdbScanFilter;
175
  friend class NdbScanFilterImpl;
176
  friend class NdbReceiver;
177
  friend class NdbBlob;
178
179
public:
180
181
  enum Type {
182
    PrimaryKeyAccess = 0,
183
    UniqueIndexAccess = 1,
184
    TableScan = 2,
185
    OrderedIndexScan = 3, 
186
    };
187
188
  enum LockMode {
189
    LM_Read = 0,
190
    LM_Exclusive = 1,
191
    LM_CommittedRead = 2,
192
    LM_Dirty = 2
193
  };
194
195
196
197
#if defined(SWIGPYTHON)
198
199
  %rename(setValueU) setValue(const char *, Uint32);
200
  %rename(setValueU64) setValue(const char *, Uint64);
201
202
  %rename(setVal)  setValue(const char* anAttrName, const char* aValue);
203
  %rename(setVal)  setValue(const char* anAttrName, Int32 aValue);
204
  %rename(setVal)  setValue(const char* anAttrName, Uint32 aValue);
205
  %rename(setVal)  setValue(const char* anAttrName, Uint64 aValue);
206
  %rename(setVal)  setValue(const char* anAttrName, Int64 aValue);
207
  %rename(setVal)  setValue(const char* anAttrName, float aValue);
208
  %rename(setVal)  setValue(const char* anAttrName, double aValue);
209
  %rename(setVal)  setValue(Uint32 anAttrId, const char* aValue);
210
  %rename(setVal)  setValue(Uint32 anAttrId, Int32 aValue);
211
  %rename(setVal)  setValue(Uint32 anAttrId, Uint32 aValue);
212
  %rename(setVal)  setValue(Uint32 anAttrId, Int64 aValue);
213
  %rename(setVal)  setValue(Uint32 anAttrId, Uint64 aValue);
214
  %rename(setVal)  setValue(Uint32 anAttrId, float aValue);
215
  %rename(setVal)  setValue(Uint32 anAttrId, double aValue);
216
217
218
#endif
219
220
  int  setValue(const char* anAttrName, const char* aValue);
221
  int  setValue(const char* anAttrName, Int32 aValue);
222
  int  setValue(const char* anAttrName, Uint32 aValue);
223
  int  setValue(const char* anAttrName, Uint64 aValue);
224
  int  setValue(const char* anAttrName, Int64 aValue);
225
  int  setValue(const char* anAttrName, float aValue);
226
  int  setValue(const char* anAttrName, double aValue);
227
  int  setValue(Uint32 anAttrId, const char* aValue);
228
  int  setValue(Uint32 anAttrId, Int32 aValue);
229
  int  setValue(Uint32 anAttrId, Uint32 aValue);
230
  int  setValue(Uint32 anAttrId, Int64 aValue);
231
  int  setValue(Uint32 anAttrId, Uint64 aValue);
232
  int  setValue(Uint32 anAttrId, float aValue);
233
  int  setValue(Uint32 anAttrId, double aValue);
234
235
236
  const NdbError & getNdbError() const;
237
238
  
239
  virtual int insertTuple();
240
241
#if defined(SWIGPYTHON)
242
  %rename(equalU) equal(const char*, Uint32);
243
  %rename(equalU64) equal(const char*, Uint64);
244
  %rename(equalU) equal(Uint32, Uint32);
245
  %rename(equalU64) equal(Uint32, Uint64);
246
#endif
247
248
249
  int  equal(const char* anAttrName, const char* aValue);
250
  int  equal(const char* anAttrName, Uint32 aValue);	
251
  int  equal(const char* anAttrName, Int32 aValue);	
252
  int  equal(const char* anAttrName, Int64 aValue);	
253
  int  equal(const char* anAttrName, Uint64 aValue);
254
  int  equal(Uint32 anAttrId, const char* aValue);
255
  int  equal(Uint32 anAttrId, Int32 aValue);	
256
  int  equal(Uint32 anAttrId, Uint32 aValue);	
257
  int  equal(Uint32 anAttrId, Int64 aValue);	
258
  int  equal(Uint32 anAttrId, Uint64 aValue);
259
260
261
#if defined(SWIGPYTHON)
262
  %pythoncode %{
263
def setValue(self,name,value,length=None):
264
  if isinstance(value,"".__class__):
265
    value="%c%s" % (len(value),value)
266
  if length is None:
267
    return self.setVal(name,value)
268
  return self.setVal(name,value,length)
269
      %}
270
#endif
271
272
273
protected:
274
  NdbOperation(Ndb* aNdb, Type aType = PrimaryKeyAccess);	
275
  virtual ~NdbOperation();
276
};
277
278
279
280
class NdbError {
281
  enum Status {
282
    Success = ndberror_st_success,
283
    TemporaryError = ndberror_st_temporary,
284
    PermanentError = ndberror_st_permanent,
285
    UnknownResult = ndberror_st_unknown
286
  };
287
  
288
  enum Classification {
289
    NoError = ndberror_cl_none,
290
    ApplicationError = ndberror_cl_application,
291
    NoDataFound = ndberror_cl_no_data_found,
292
    ConstraintViolation = ndberror_cl_constraint_violation,
293
    SchemaError = ndberror_cl_schema_error,
294
    UserDefinedError = ndberror_cl_user_defined,
295
    InsufficientSpace = ndberror_cl_insufficient_space,
296
    TemporaryResourceError = ndberror_cl_temporary_resource,
297
    NodeRecoveryError = ndberror_cl_node_recovery,
298
    OverloadError = ndberror_cl_overload,
299
    TimeoutExpired = ndberror_cl_timeout_expired,
300
    UnknownResultError = ndberror_cl_unknown_result,
301
    InternalError = ndberror_cl_internal_error,
302
    FunctionNotImplemented = ndberror_cl_function_not_implemented,
303
    UnknownErrorCode = ndberror_cl_unknown_error_code,
304
    NodeShutdown = ndberror_cl_node_shutdown,
305
    SchemaObjectExists = ndberror_cl_schema_object_already_exists,
306
    InternalTemporary = ndberror_cl_internal_temporary
307
  };
308
  
309
  Status status;
310
  Classification classification;
311
  int code;
312
  int mysql_code;
313
  const char * message;
314
  char * details;
315
316
317
  NdbError(){
318
    status = UnknownResult;
319
    classification = NoError;
320
    code = 0;
321
    mysql_code = 0;
322
    message = 0;
323
    details = 0;
324
  }
325
  NdbError(const ndberror_struct & ndberror){
326
    status = (NdbError::Status) ndberror.status;
327
    classification = (NdbError::Classification) ndberror.classification;
328
    code = ndberror.code;
329
    mysql_code = ndberror.mysql_code;
330
    message = ndberror.message;
331
    details = ndberror.details;
332
  }
333
  operator ndberror_struct() const {
334
    ndberror_struct ndberror;
335
    ndberror.status = (ndberror_status_enum) status;
336
    ndberror.classification = (ndberror_classification_enum) classification;
337
    ndberror.code = code;
338
    ndberror.mysql_code = mysql_code;
339
    ndberror.message = message;
340
    ndberror.details = details;
341
    return ndberror;
342
  }
343
344
};
345
346
%extend NdbError { 
347
  const char * getMessage() { 
348
   return self->message; 
349
  }
350
}
351
/* test */
352
/* -*- mode: c++ -*- */