WORM 0.2
A C++ DAL/ORM code generation framework

src/sql/wsqldatatype.h

Go to the documentation of this file.
00001 /*
00002     WORM - a DAL/ORM code generation framework
00003     Copyright (C) 2011  Erik Winn <erikwinnmail@yahoo.com>
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 
00020 #ifndef WSQLDATATYPE_H
00021 #define WSQLDATATYPE_H
00022 
00023 #include <string>
00024 #include <boost/algorithm/string.hpp>
00025 #include <iostream>
00026 
00027 namespace WSql {
00028 
00061     namespace WSqlDataType {
00062     
00063     
00068     enum Type {
00069         NOTYPE = 0,
00070         TINYINT,
00071         SMALLINT,
00072         MEDIUMINT,
00073         INT,
00074         BIGINT,
00075         FLOAT,
00076         DOUBLE,
00077         DECIMAL,
00078         DATE,
00079         DATETIME,
00080         YEAR,
00081         TIME,
00082         TIMESTAMP,
00083         TIMESTAMPTZ,
00084         CHAR,
00085         VARCHAR,
00086         NCHAR,
00087         NVARCHAR,
00088         TEXT,
00089         TINYTEXT,
00090         MEDIUMTEXT,
00091         LONGTEXT,
00092         ENUM,
00093         SET,
00094         BLOB
00095     };
00100     static const char * const TypeNames[] ={
00101         "NOTYPE",
00102         "TINYINT",
00103         "SMALLINT",
00104         "MEDIUMINT",
00105         "INT",
00106         "BIGINT",
00107         "FLOAT",
00108         "DOUBLE",
00109         "DECIMAL",
00110         "DATE",
00111         "DATETIME",
00112         "TIME",
00113         "YEAR",
00114         "TIMESTAMP",
00115         "TIMESTAMPTZ",
00116         "CHAR",
00117         "VARCHAR",
00118         "NCHAR",
00119         "NVARCHAR",
00120         "TEXT",
00121         "TINYTEXT",
00122         "MEDIUMTEXT",
00123         "LONGTEXT",
00124         "ENUM",
00125         "SET",
00126         "BLOB"
00127     };
00128     
00130     static const unsigned short number_of_datatypes = 26;
00131     
00133     static std::string toString(Type type)
00134     {
00135         std::string strToReturn;
00136         if (type < 0 || type > (number_of_datatypes - 1))
00137             strToReturn = "INVALID";
00138         else
00139             strToReturn = TypeNames[type]; //careful .. dont mess this up, add types and name in order.
00140         return strToReturn;
00141     }
00142     
00144     static Type toType(std::string name)
00145     {
00146         boost::to_upper(name);
00147         boost::trim(name);
00149         int i = 0;
00150         for ( ; i < number_of_datatypes; ++i)
00151             if ( name.compare(TypeNames[i]) == 0 )
00152                 return static_cast<Type>(i);
00153         return static_cast<Type>(0);//NOTYPE
00154     }
00155     
00157     static std::string toSingular(const std::string& name)
00158     {
00159         std::string strToReturn = name;
00160         size_t size = strToReturn.size();
00161         if(!size)
00162             return strToReturn;
00163         if('s' == strToReturn[size-1] && 's' != strToReturn[size-2])//dont fix dress, address ..
00164         {
00165             strToReturn.erase(size-1);
00166             if('e' == strToReturn[size-2])
00167             {//eg. Cities to City ..
00168                 if( 'i' == strToReturn[size-3])
00169                 {
00170                     strToReturn.erase(size-3);
00171                     strToReturn.append("y");
00172                 }else if( 'h' == strToReturn[size-3])//eg. bushes .. might need fixing ..
00173                     strToReturn.erase(size-2);                
00174             }
00175             return strToReturn;
00176         }
00179         return strToReturn;
00180     }
00181     
00183     static std::string toPlural(const std::string& name)
00184     {
00185         std::string strToReturn = name;
00186         size_t sz = name.size();
00187         if(sz && 's' == strToReturn[sz - 1])
00188             strToReturn.append("es");
00189         else if(sz > 2 && 'y' == strToReturn[sz - 1]
00190             && 'o' != strToReturn[sz - 2]
00191             && 'a' != strToReturn[sz - 2])
00192         {
00193                 strToReturn.erase(sz - 1);
00194                 strToReturn.append("ies");
00195         }
00196         else
00197             strToReturn.append("s");
00199         return strToReturn;
00200     }
00201     
00212     static std::string columnNameToVariable(const std::string& columnname)
00213     {
00214         std::string strToReturn = columnname;
00215         size_t pos = 0;
00216         pos = strToReturn.find('_');
00217         while(pos != std::string::npos)
00218         {
00219             strToReturn.erase(pos,1);
00220             if((pos + 1) < strToReturn.size())
00221                 strToReturn[pos]= toupper(strToReturn[pos]);
00222             pos = strToReturn.find('_');
00223         }
00224         return strToReturn;
00225     }
00226     
00244     static std::string tableNameToClass(const std::string& tablename)
00245     {
00246         std::string strToReturn = toSingular(tablename);
00247         strToReturn[0] = toupper(strToReturn[0]);
00248         return columnNameToVariable(strToReturn);
00249     }
00250 
00251 } //namespace WSqlDataType
00252     
00253 }// namespace WSql
00254 
00255 #endif // WSQLDATATYPE_H
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Defines