1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
#include <NdbDictionary.hpp>
21
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
26
* NOTE! AttrType is deprecated, use NdbDictionary::Column::Type instead!
29
Signed, ///< Attributes of this type can be read with:
30
///< NdbRecAttr::int64_value,
31
///< NdbRecAttr::int32_value,
32
///< NdbRecAttr::short_value,
33
///< NdbRecAttr::char_value
34
UnSigned, ///< Attributes of this type can be read with:
35
///< NdbRecAttr::u_64_value,
36
///< NdbRecAttr::u_32_value,
37
///< NdbRecAttr::u_short_value,
38
///< NdbRecAttr::u_char_value
39
Float, ///< Attributes of this type can be read with:
40
///< NdbRecAttr::float_value and
41
///< NdbRecAttr::double_value
42
String, ///< Attributes of this type can be read with:
43
///< NdbRecAttr::aRef,
44
///< NdbRecAttr::getAttributeObject
45
NoAttrTypeDef ///< Used for debugging only
52
enum NullAttributeType {
53
NoNullTypeDefined = -1,
59
* Indicates whether the attribute is part of a primary key or not
62
Undefined = -1, ///< Used for debugging only
63
NoKey, ///< Attribute is not part of primary key
64
///< or tuple identity
65
TupleKey, ///< Attribute is part of primary key
66
TupleId ///< Attribute is part of tuple identity
67
///< (This type of attribute is created
68
///< internally, and should not be
69
///< manually created.)
72
* Indicate whether the attribute should be stored on disk or not
73
* Only for legacy createAttribute().
76
MMBased = NDB_STORAGETYPE_MEMORY,
77
DiskBased = NDB_STORAGETYPE_DISK
81
* Type of fragmentation used for a table
84
Default = 0, ///< (All is default!)
85
Single = 1, ///< Only one fragment
86
All = 2, ///< Default value. One fragment per node group
87
DistributionGroup = 3, ///< Distribution Group used for fragmentation.
88
///< One fragment per node group
89
DistributionKey = 4, ///< Distribution Key used for fragmentation.
90
///< One fragment per node group.
91
AllLarge = 5, ///< Sixten fragments per node group.
92
DGroupLarge = 6, ///< Distribution Group used for fragmentation.
93
///< Sixten fragments per node group
94
DKeyLarge = 7 ///< Distribution Key used for fragmentation.
95
///< Sixten fragments per node group
99
* Type of table or index.
103
SystemTable = 1, ///< Internal.Table cannot be updated by user
104
UserTable = 2, ///< Normal application table
105
UniqueHashIndex = 3, ///< Unique un-ordered hash index
106
HashIndex = 4, ///< Non-unique un-ordered hash index
107
UniqueOrderedIndex = 5, ///< Unique ordered index
108
OrderedIndex = 6 ///< Non-unique ordered index
118
* @brief Represents various operations for use in schema transactions
120
* This class is used for schema operations, e.g. creating tables and
123
* The NdbSchemaOp object is created using NdbSchemaCon::getNdbSchemaOp.
125
* @note This class is depricated and is now replaced with the class
131
friend class NdbSchemaCon;
137
* Create a new table in the database.
139
* @note The NdbSchemaCon should be closed with
140
* Ndb::closeSchemaTransaction, even if this method fails.
142
* @param aTableName Table name. Should not be NULL.
143
* @param aTableSize (Performance parameter.)
144
* Initial size of the data part of the table
145
* expressed in kByte.
146
* The database handles
147
* bad parameter setting but at a certain
148
* loss in performance.
149
* The size given here is
150
* the initial size allocated for the table
151
* storage (the data part).
152
* When calculating the data storage one should
153
* add the size of all attributes (each attribute
154
* consumes at least 4 bytes) and also an overhead
156
* Variable size attributes (not supported yet)
157
* will have a size of 12 bytes plus the actual
158
* data storage parts where there is an
159
* additional overhead based on the size of the
162
* An example table with 5 attributes:
163
* one 64 bit attribute, one 32 bit attribute,
164
* two 16 bit attributes and one array of 64 8 bits.
165
* This table will consume
166
* 12 (overhead) + 8 + 4 + 2*4 (4 is minimum) + 64 =
167
* 96 bytes per record.
168
* Additionally an overhead of about 2 % as page
169
* headers and waste should be allocated.
170
* Thus, 1 million records should consume 96 MBytes
171
* plus the overhead 2 MByte and rounded up to
174
* This parameter is currently not used.
176
* @param aTupleKey Indicates if the table has a primary key or not.
178
* <b>TupleKey</b> means that a <em>primary key</em>
179
* consisting of one to four attributes
180
* (at most one of variable size)
181
* uniquely identifies each record in the created
184
* <b>TupleId</b> means that a <em>tuple identity</em>
185
* is used. The tuple identity is
186
* a unique key indentifying each record of the
188
* The tuple identity is a (non-stored)
189
* 64 bit attribute named <b>NDB$TID</b>.
191
* When inserting a record (tuple), the method
192
* NdbOperation::setTupleId
193
* will generate a unique tuple identity
194
* and return it to the user.
196
* When reading, updating or deleting a record
197
* in a table with <b>TupleId</b>,
198
* NdbOperation::equal("NDB$TID", value_Uint64)
199
* can be used to identify the record.
201
* Legal values: TupleKey or TupleId.
202
* @param aNrOfPages (Performance parameter.)
203
* Specifies the initial size of the index storage.
204
* When calculating the index storage,
205
* each key has approximately 14 byte of
206
* overhead plus the size of the key.
207
* Each key attribute takes up at least 4 bytes
209
* Thus a mixed key consisting of a
210
* 64 bit attribute, a 32 bit attribute
211
* and a 16 bit attribute will
212
* consume approx. 30 bytes per key.
213
* Thus, the if initial size is to be 1 million rows,
214
* then aNrOfPages should be set to
215
* 30 M / 8k = 2670 pages.
217
* This parameter is currently not used.
219
* @param aFragmentType Type of fragmentation.<br>
220
* <b>All</b> (default) means that the
221
* table fragments are automatically
222
* distributed on all nodes in the system.<br>
223
* <b>DistributionGroup</b> and
224
* <b>DistributionKey</b> are
225
* also supported. For further details about
226
* these types see the documentation of
227
* Ndb::startTransaction.
228
* @param aKValue (Hash parameter.)
229
* Only allowed value is 6.
230
* Later implementations might add flexibility
232
* @param aMinLoadFactor (Hash parameter.)
233
* This value specifies the load factor when
234
* starting to shrink the hash table.
235
* It must be smaller than aMaxLoadFactor.
236
* Both these factors are given in percentage.
237
* @param aMaxLoadFactor (Hash parameter.)
238
* This value specifies the load factor when
239
* starting to split the containers in the local
240
* hash tables. 100 is the maximum which will
241
* optimize memory usage (this is the figure
242
* used for the above calculations).
243
* A lower figure will store less information in
244
* each container and thus
245
* find the key faster but consume more memory.
246
* @param aMemoryType Currently only 1 is allowed which specifies
247
* storage of table in main memory.
248
* Later 2 will be added where the table is stored
250
* and 3 where the index is in main memory but
252
* If 1 is chosen an individual attribute can
253
* still be specified as a disk attribute.
254
* @param aStoredTable If set to false it indicates that the table is
255
* a temporary table and should not be logged
257
* In case of a system restart the table will still
258
* be defined and exist but will be empty.
259
* Thus no checkpointing and
260
* no logging is performed on the table.
261
* The default value is true and indicates a
262
* normal table with full checkpointing and
264
* @return Returns 0 when successful and returns -1 otherwise.
266
int createTable( const char* aTableName,
267
Uint32 aTableSize = 8,
268
KeyType aTupleKey = TupleKey,
270
FragmentType aFragmentType = All,
272
int aMinLoadFactor = 78,
273
int aMaxLoadFactor = 80,
275
bool aStoredTable = true);
278
* This is the old function declaration, don't use.
280
* @deprecated do not use!
283
inline int createTable( const char* aTableName,
287
FragmentType aFragmentType,
293
return createTable(aTableName,
302
(aStoredTable == 1 ? true : false));
307
* Add a new attribute to a database table.
309
* Attributes can only be added to a table in the same transaction
310
* as the transaction creating the table.
312
* @note The NdbSchemaCon transaction should be closed with
313
* Ndb::closeSchemaTransaction, even if this method fails.
315
* Example creating an unsigned int attribute belonging to the primary key
316
* of the table it is created in:
318
* MySchemaOp->createAttribute("Attr1", // Attribute name
319
* TupleKey, // Belongs to primary key
321
* 1, // Not an array attribute
322
* UnSigned, // Unsigned type
326
* Example creating a string attribute belonging to the primary key
327
* of the table it is created in:
329
* MySchemaOp->createAttribute("Attr1", // Attribute name
330
* TupleKey, // Belongs to primary key
331
* 8, // Each character is 8 bits
332
* 12, // Max 12 chars in string
333
* String, // Attribute if of type string
337
* A <em>distribution key</em> is a set of attributes which are used
338
* to distribute the tuples onto the NDB nodes.
339
* A <em>distribution group</em> is a part (currently 16 bits)
340
* of an attribute used to distribute the tuples onto the NDB nodes.
341
* The distribution key uses the NDB Cluster hashing function,
342
* while the distribution group uses a simpler function.
344
* @param aAttrName Attribute name. Should not be NULL.
345
* @param aTupleKey This parameter specifies whether the
346
* attribute is part of the primary key or not.
347
* Floats are not allowed in the primary key.
349
* Legal values: NoKey, TupleKey
350
* @param aAttrSize Specifies the size of the elements of the
351
* attribute. (An attribute can consist
352
* of an array of elements.)
354
* Legal values: 8, 16, 32, 64 and 128 bits.
355
* @param aArraySize Size of array.
358
* 0 = variable-sized array,
360
* 2- = fixed size array.
363
* Variable-sized array attributes are
367
* There is no upper limit of the array size
368
* for a single attribute.
369
* @param aAttrType The attribute type.
370
* This is only of interest if calculations are
373
* Legal values: UnSigned, Signed, Float, String
374
* @param aStorageMode Main memory based or disk based attribute.<br>
375
* Legal values: MMBased, DiskBased
378
* Disk-based attributes are not yet supported.
380
* @param nullable Set to true if NULL is a correct value for
383
* Legal values: true, false
384
* @param aStType Obsolete since wl-2066
385
* @param aDistributionKey Sometimes it is preferable to use a subset
386
* of the primary key as the distribution key.
387
* An example is TPC-C where it might be
388
* good to use the warehouse id and district id
389
* as the distribution key.
391
* Locally in the fragments the full primary key
392
* will still be used with the hashing algorithm.
393
* Set to 1 if this attribute is part of the
395
* All distribution key attributes must be
397
* any other attributes are defined.
398
* @param aDistributionGroup In other applications it is desirable to use
399
* only a part of an attribute to create the
401
* This is applicable for some telecom
404
* In these situations one must provide how many
405
* bits of the attribute that is to
406
* be used as the distribution hash value.
408
* This provides some control to the
409
* application of the distribution.
410
* It still needs to be part of a primary key
411
* the attribute and must be defined as the
413
* @param aDistributionGroupNoOfBits
414
* Number of bits to use of the
415
* distribution group attribute in the
416
* distribution hash value.
418
* Currently, only 16 bits is supported. It will
419
* always be the last 16 bits in the attribute
420
* which is used for the distribution group.
421
* @param aAutoIncrement Set to autoincrement attribute.
422
* @param aDefaultValue Set a default value of attribute.
424
* @return Returns 0 when successful and returns -1 otherwise.
425
****************************************************************************/
426
int createAttribute(const char* aAttrName,
427
KeyType aTupleKey = NoKey,
430
AttrType aAttrType = UnSigned,
431
StorageMode aStorageMode = MMBased,
432
bool nullable = false,
433
int aStType= 0, // obsolete
434
int aDistributionKey = 0,
435
int aDistributionGroup = 0,
436
int aDistributionGroupNoOfBits = 16,
437
bool aAutoIncrement = false,
438
const char* aDefaultValue = 0);
441
* @deprecated do not use!
443
int createAttribute(const char* aAttrName,
448
StorageMode aStorageMode,
449
NullAttributeType aNullAttr,
450
int aStType, // obsolete
451
int aDistributionKey = 0,
452
int aDistributionGroup = 0,
453
int aDistributionGroupNoOfBits = 16){
454
return createAttribute(aAttrName,
460
aNullAttr == NullAttribute,
464
aDistributionGroupNoOfBits);
467
const NdbError & getNdbError() const;
471
/*****************************************************************************
472
* These are the methods used to create and delete the NdbOperation objects.
473
****************************************************************************/
474
NdbSchemaOp(Ndb* aNdb);
478
/******************************************************************************
479
* These methods are service routines used by the other NDBAPI classes.
480
*****************************************************************************/
482
void release(); // Release all memory connected
483
// to the operations object.
485
/****************************************************************************
486
* The methods below is the execution part of the NdbSchemaOp class.
487
*****************************************************************************/
490
int sendSignals(Uint32 aNodeId, bool HaveMutex);
492
int init(NdbSchemaCon* aSchemaCon);
494
/**************************************************************************
495
* These are the private variables that are defined in the operation
497
**************************************************************************/
498
Ndb* theNdb; // Point back to the Ndb object.
499
NdbSchemaCon* theSchemaCon; // Point back to the connection object.
502
class NdbDictionary::Table * m_currentTable;
507
* Get old attribute type from new type
509
* NOTE! attrType is deprecated, use getType instead!
511
* @return Type of attribute: { Signed, UnSigned, Float,a String }
515
convertColumnTypeToAttrType(NdbDictionary::Column::Type _type)
519
case NdbDictionary::Column::Bigint:
520
case NdbDictionary::Column::Int:
522
case NdbDictionary::Column::Bigunsigned:
523
case NdbDictionary::Column::Unsigned:
525
case NdbDictionary::Column::Float:
526
case NdbDictionary::Column::Olddecimal:
527
case NdbDictionary::Column::Olddecimalunsigned:
528
case NdbDictionary::Column::Decimal:
529
case NdbDictionary::Column::Decimalunsigned:
530
case NdbDictionary::Column::Double:
532
case NdbDictionary::Column::Char:
533
case NdbDictionary::Column::Varchar:
534
case NdbDictionary::Column::Binary:
535
case NdbDictionary::Column::Varbinary:
538
return NoAttrTypeDef;