COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/ColumnInfo.cpp
Go to the documentation of this file.
00001 #include <coopy/ColumnInfo.h>
00002 #include <coopy/Dbg.h>
00003 
00004 #include <stdio.h>
00005 #include <ctype.h>
00006 
00007 using namespace coopy::store;
00008 
00009 bool ColumnType::setType(const std::string& name, 
00010                          const std::string& lang) {
00011   reset();
00012   src_name = name;
00013   src_lang = lang;
00014   std::string _name = name;
00015   for (int i=0; i<(int)_name.length(); i++) {
00016     _name[i] = tolower(_name[i]);
00017     if (_name[i]=='(') {
00018       _name = _name.substr(0,i);
00019       break;
00020     }
00021   }
00022   if (_name=="int"||_name=="integer"||_name=="tinyint"||_name=="byte") {
00023     family = COLUMN_FAMILY_INTEGER;
00024   } else if (_name=="text") {
00025     family = COLUMN_FAMILY_TEXT;
00026   } else if (_name == "long integer") {
00027     family = COLUMN_FAMILY_INTEGER;
00028   } else if (_name == "float" || _name=="single" || _name=="double" || _name == "real") {
00029     family = COLUMN_FAMILY_REAL;
00030   } else if (_name == "datetime (short)"||_name=="datetime"||_name=="date"||_name=="timestamp") {
00031     family = COLUMN_FAMILY_DATETIME;
00032   } else if (_name=="text"||_name=="varchar"||_name.substr(0,7)=="varchar") {
00033     family = COLUMN_FAMILY_TEXT;
00034   } else if (_name=="memo"||_name=="hyperlink"||_name=="memo/hyperlink") {
00035     family = COLUMN_FAMILY_TEXT;
00036   } else if (_name=="boolean"||_name=="bool") {
00037     family = COLUMN_FAMILY_BOOLEAN;
00038   } else if (_name=="currency") {
00039     family = COLUMN_FAMILY_CURRENCY;
00040   } else if (_name=="blob"||_name=="clob") {
00041     family = COLUMN_FAMILY_BLOB;
00042   }
00043   if (family==COLUMN_FAMILY_NONE) {
00044     if (name!="") {
00045       fprintf(stderr,"Unrecognized type \'%s\'; update src/libcoopy_core/ColumnInfo.cpp\n", name.c_str());
00046       if (name=="INTEGER REFERENCE blob") {
00047         fprintf(stderr,"Seems like a typo? To be confirmed...\n");
00048       } else {
00049         if (coopy_is_strict()) {
00050           exit(1);
00051         }
00052       }
00053     }
00054   }
00055 
00056   return family!=COLUMN_FAMILY_NONE;
00057 }
00058 
00059 
00060 std::string ColumnType::asSqlite(bool addPrimaryKey) const {
00061   std::string result = "";
00062   switch (family) {
00063   case COLUMN_FAMILY_INTEGER:
00064     result = "INTEGER";
00065     break;
00066   case COLUMN_FAMILY_REAL:
00067     result = "REAL";
00068     break;
00069   case COLUMN_FAMILY_TEXT:
00070     result = "TEXT";
00071     break;
00072   case COLUMN_FAMILY_DATETIME:
00073     result = "DATETIME"; // for documentation purposes only
00074     break;
00075   case COLUMN_FAMILY_BOOLEAN:
00076     result = "BOOLEAN"; // for documentation purposes only
00077     break;
00078   case COLUMN_FAMILY_BLOB:
00079     result = "BLOB"; // for documentation purposes only
00080     break;
00081   }
00082   bool pk = false;
00083   if (primaryKeySet&&addPrimaryKey) {
00084     if (primaryKey) {
00085       if (result!="") {
00086         result += " ";
00087       }
00088       result += "PRIMARY KEY";
00089       pk = true;
00090     }
00091   }
00092   if (!allowNull) {
00093     if (!pk) {
00094       if (result!="") {
00095         result += " ";
00096       }
00097       result += "NOT NULL";
00098     }
00099   }
00100   return result;
00101 }
00102 
00103 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines