NdbSchemaOp.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. /*****************************************************************************
  14. Name:          NdbSchemaOp.cpp
  15. Include:
  16. Link:
  17. Author:        UABMNST Mona Natterkvist UAB/B/SD
  18.                EMIKRON Mikael Ronstrom                         
  19. Date:          040524
  20. Version:       3.0
  21. Description:   Interface between application and NDB
  22. Documentation: Handles createTable and createAttribute calls
  23. Adjust:  980125  UABMNST   First version.
  24.          020826  EMIKRON   New version for new DICT
  25.          040524  Magnus Svensson - Adapted to not be included in public NdbApi
  26.                                    unless the user wants to use it.
  27.   NOTE: This file is only used as a compatibility layer for old test programs,
  28.         New programs should use NdbDictionary.hpp
  29. *****************************************************************************/
  30. #include <ndb_global.h>
  31. #include <NdbApi.hpp>
  32. #include <NdbSchemaOp.hpp>
  33. #include <NdbSchemaCon.hpp>
  34. /*****************************************************************************
  35. NdbSchemaOp(Ndb* aNdb, Table* aTable);
  36. Return Value:  None
  37. Parameters:    aNdb: Pointers to the Ndb object.
  38.                aTable: Pointers to the Table object
  39. Remark:        Creat an object of NdbSchemaOp. 
  40. *****************************************************************************/
  41. NdbSchemaOp::NdbSchemaOp(Ndb* aNdb) : 
  42.   theNdb(aNdb),
  43.   theSchemaCon(NULL),
  44.   m_currentTable(NULL)
  45. {
  46. }//NdbSchemaOp::NdbSchemaOp()
  47. /*****************************************************************************
  48. ~NdbSchemaOp();
  49. Remark:         Delete tables for connection pointers (id).
  50. *****************************************************************************/
  51. NdbSchemaOp::~NdbSchemaOp( )
  52. {
  53. }//~NdbSchemaOp::NdbSchemaOp()
  54.      
  55. /*****************************************************************************
  56. int createTable( const char* tableName )
  57. *****************************************************************************/
  58. int
  59. NdbSchemaOp::createTable(const char* aTableName, 
  60.                          Uint32 aTableSize, 
  61.                          KeyType aTupleKey,
  62.                          int aNrOfPages, 
  63.                          FragmentType aFragmentType, 
  64.                          int aKValue,
  65.                          int aMinLoadFactor,
  66.                          int aMaxLoadFactor,
  67.                          int aMemoryType,
  68.                          bool aStoredTable)
  69. {
  70.   if(m_currentTable != 0){
  71.     return -1;
  72.   }
  73.   m_currentTable = new NdbDictionary::Table(aTableName);
  74.   m_currentTable->setKValue(aKValue);
  75.   m_currentTable->setMinLoadFactor(aMinLoadFactor);
  76.   m_currentTable->setMaxLoadFactor(aMaxLoadFactor);
  77.   m_currentTable->setLogging(aStoredTable);
  78.   m_currentTable->setFragmentType(NdbDictionary::Object::FragAllMedium);
  79.   return 0;
  80. }//NdbSchemaOp::createTable()
  81. /******************************************************************************
  82. int createAttribute( const char* anAttrName,            
  83.                          KeyType aTupleyKey,            
  84.                              int anAttrSize,                    
  85.                              int anArraySize,                           
  86.                         AttrType anAttrType,
  87.                         SafeType aSafeType,             
  88.                      StorageMode aStorageMode,
  89.                              int aNullAttr,
  90.                              int aStorageAttr );
  91. ******************************************************************************/
  92. int     
  93. NdbSchemaOp::createAttribute( const char* anAttrName,                   
  94.                               KeyType aTupleKey,                        
  95.                               int anAttrSize,                   
  96.                               int anArraySize,                          
  97.                               AttrType anAttrType,      
  98.                               StorageMode aStorageMode,
  99.                               bool nullable,
  100.                               StorageAttributeType aStorageAttr,
  101.       int aDistributionKeyFlag,
  102.       int aDistributionGroupFlag,
  103.       int aDistributionGroupNoOfBits,
  104.                               bool aAutoIncrement,
  105.                               const char* aDefaultValue)
  106. {
  107.   if (m_currentTable == 0){
  108.     return -1;
  109.   }//if
  110.   
  111.   NdbDictionary::Column col(anAttrName);
  112.   switch(anAttrType){
  113.   case Signed:
  114.     if(anAttrSize == 64)
  115.       col.setType(NdbDictionary::Column::Bigint);
  116.     else
  117.       col.setType(NdbDictionary::Column::Int);
  118.     break;
  119.   case UnSigned:
  120.     if(anAttrSize == 64)
  121.       col.setType(NdbDictionary::Column::Bigunsigned);
  122.     else
  123.       col.setType(NdbDictionary::Column::Unsigned);
  124.     break;
  125.   case Float:
  126.     if(anAttrSize == 64)
  127.       col.setType(NdbDictionary::Column::Double);
  128.     else
  129.       col.setType(NdbDictionary::Column::Float);
  130.     break;
  131.   case String:
  132.     col.setType(NdbDictionary::Column::Char);
  133.     break;
  134.   case NoAttrTypeDef:
  135.     abort();
  136.   }
  137.   col.setLength(anArraySize);
  138.   col.setNullable(nullable);
  139.   if(aTupleKey != NoKey)
  140.     col.setPrimaryKey(true);
  141.   else
  142.     col.setPrimaryKey(false);
  143.   col.setDistributionKey(aDistributionKeyFlag);
  144.   col.setDistributionGroup(aDistributionGroupFlag,aDistributionGroupNoOfBits);
  145.   col.setAutoIncrement(aAutoIncrement);
  146.   col.setDefaultValue(aDefaultValue != 0 ? aDefaultValue : "");
  147.   
  148.   m_currentTable->addColumn(col);
  149.   return 0;      
  150. }
  151. /******************************************************************************
  152. void release();
  153. Remark:        Release all objects connected to the schemaop object.
  154. ******************************************************************************/
  155. void
  156. NdbSchemaOp::release(){
  157. }//NdbSchemaOp::release()
  158. /******************************************************************************
  159. int sendRec()
  160. Return Value:   Return 0 : send was succesful.
  161.                 Return -1: In all other case.   
  162. Parameters:
  163. Remark:         Send and receive signals for schema transaction based on state
  164. ******************************************************************************/
  165. int
  166. NdbSchemaOp::sendRec(){
  167.   int retVal = 0;
  168.   if(m_currentTable == 0){
  169.     retVal = -1;
  170.   } else {
  171.     retVal = theNdb->getDictionary()->createTable(* m_currentTable);
  172.     delete m_currentTable;
  173.     theSchemaCon->theError.code = theNdb->getDictionary()->getNdbError().code;
  174.   }
  175.   
  176.   return retVal;
  177. }//NdbSchemaOp::sendRec()
  178. /******************************************************************************
  179. int init();
  180. Return Value:  Return 0 : init was successful.
  181.                Return -1: In all other case.  
  182. Remark:        Initiates SchemaOp record after allocation.
  183. ******************************************************************************/
  184. int
  185. NdbSchemaOp::init(NdbSchemaCon* aSchemaCon)
  186. {
  187.   theSchemaCon = aSchemaCon;
  188.   return 0;
  189. }//NdbSchemaOp::init()
  190. const NdbError &
  191. NdbSchemaOp::getNdbError() const
  192. {
  193.    return theSchemaCon->getNdbError();
  194. }