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

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. #include "dba_internal.hpp"
  14. #include "NdbSchemaCon.hpp"
  15. static bool getNdbAttr(DBA_DataTypes_t,
  16.        Size_t,
  17.        int * attrSize,
  18.        int * arraySize,
  19.        AttrType * attrType);
  20. extern "C"
  21. DBA_Error_t 
  22. DBA_CreateTable(const char* TableName, 
  23. int NbColumns, 
  24. const DBA_ColumnDesc_t Columns[] ){
  25.   
  26.   if(DBA_TableExists(TableName))
  27.     return DBA_NO_ERROR;
  28.   
  29.   NdbSchemaCon * schemaCon = NdbSchemaCon::startSchemaTrans(DBA__TheNdb);
  30.   if(schemaCon == 0){
  31.     DBA__SetLatestError(DBA_NDB_ERROR, 0,
  32. "Internal NDB error: No schema transaction");
  33.     return DBA_NDB_ERROR;
  34.   }
  35.   
  36.   NdbSchemaOp * schemaOp   = schemaCon->getNdbSchemaOp();
  37.   if(schemaOp == 0){    
  38.     NdbSchemaCon::closeSchemaTrans(schemaCon);    
  39.     DBA__SetLatestError(DBA_NDB_ERROR, 0,
  40. "Internal NDB error: No schema op");
  41.     return DBA_NDB_ERROR;
  42.   }
  43.   if(schemaOp->createTable( TableName,
  44.     8, // Data Size
  45.     TupleKey,
  46.     2, // Index size
  47.     All,
  48.     6,
  49.     78,
  50.     80,
  51.     1,
  52.     false) == -1){
  53.     NdbSchemaCon::closeSchemaTrans(schemaCon);    
  54.     DBA__SetLatestError(DBA_NDB_ERROR, 0,
  55. "Internal NDB error: Create table failed");
  56.     return DBA_NDB_ERROR;
  57.   }
  58.   
  59.   for (int i = 0; i < NbColumns; i++){
  60.     int attrSize;
  61.     int arraySize;
  62.     AttrType attrType;
  63.     
  64.     if(!getNdbAttr(Columns[i].DataType, Columns[i].Size,
  65.    &attrSize,
  66.    &arraySize,
  67.    &attrType)){
  68.       NdbSchemaCon::closeSchemaTrans(schemaCon);    
  69.       DBA__SetLatestError(DBA_APPLICATION_ERROR, 0,
  70.   "Invalid datatype/size combination");
  71.       return DBA_APPLICATION_ERROR;
  72.     }
  73.     
  74.     if(schemaOp->createAttribute( Columns[i].Name,
  75.   Columns[i].IsKey ? TupleKey : NoKey,
  76.   attrSize,
  77.   arraySize,
  78.   attrType) == -1){
  79.       NdbSchemaCon::closeSchemaTrans(schemaCon);    
  80.       DBA__SetLatestError(DBA_NDB_ERROR, 0,
  81.   "Internal NDB error: Create attribute failed");
  82.       return DBA_NDB_ERROR;
  83.     }
  84.   }
  85.   
  86.   if(schemaCon->execute() == -1){
  87.     NdbSchemaCon::closeSchemaTrans(schemaCon);    
  88.     DBA__SetLatestError(DBA_NDB_ERROR, 0,
  89. "Internal NDB error: Execute schema failed");
  90.     return DBA_NDB_ERROR;
  91.   }
  92.   
  93.   NdbSchemaCon::closeSchemaTrans(schemaCon);    
  94.     
  95.   return DBA_NO_ERROR;
  96. }
  97. DBA_Error_t 
  98. DBA_DropTable( char* TableName ){
  99.   return DBA_NOT_IMPLEMENTED;
  100. }
  101. Boolean_t 
  102. DBA_TableExists( const char* TableName ){
  103.   NdbDictionary::Dictionary * dict = DBA__TheNdb->getDictionary();
  104.   if(dict == 0){
  105.     return 0;
  106.   }
  107.   
  108.   const NdbDictionary::Table * tab = dict->getTable(TableName);
  109.   if(tab == 0){
  110.     return 0;
  111.   }
  112.   return 1;
  113. }
  114. static
  115. bool 
  116. getNdbAttr(DBA_DataTypes_t type,
  117.    Size_t size,
  118.    int * attrSize,
  119.    int * arraySize,
  120.    AttrType * attrType) {
  121.   
  122.   if(type == DBA_CHAR){
  123.     * attrType = String;
  124.     * attrSize  = 8;
  125.     * arraySize = size;
  126.     return true;
  127.   }
  128.   
  129.   * attrType = Signed;
  130.   if((size % 4) == 0){
  131.     * attrSize  = 32;
  132.     * arraySize = size / 4;
  133.     return true;
  134.   }
  135.   
  136.   * attrSize  = 8;
  137.   * arraySize = size;
  138.   
  139.   return true;
  140. }