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

src/sql/wsqlcolumn.cpp

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 
00020 // also, tests ..
00022 
00023 #include "wsqlcolumn.h"
00024 
00025 namespace WSql{
00026 
00054 WSqlColumn::WSqlColumn()
00055 {
00056     _columnName = std::string();
00057     _variableName = std::string();
00058     _isAutoIncremented = false;
00059     _isPrimaryKey = false;
00060     _isUnsigned = false;
00061     _canBeNull = true; //most dbms default .. 
00062     _precision = 2;
00063     _maxLength = -1;
00064     _type = WSqlDataType::NOTYPE;
00065 }
00066 
00067 WSqlColumn::WSqlColumn( const WSqlColumn& other )
00068 {
00069     _columnName = other._columnName;
00070     _variableName = other._variableName;
00071     _isAutoIncremented = other._isAutoIncremented;
00072     _isPrimaryKey = other._isPrimaryKey;
00073     _isUnsigned = other._isUnsigned;
00074     _canBeNull = other._canBeNull;
00075     _precision = other._precision;
00076     _maxLength = other._maxLength;
00077     _type = other._type;
00079     _default = other._default;
00080 }
00081 
00082 WSqlColumn::~WSqlColumn()
00083 {
00084 
00085 }
00089 WSqlColumn& WSqlColumn::operator=( const WSqlColumn & other )
00090 {
00091     _columnName = other._columnName;
00092     _variableName = other._variableName;
00093     _isAutoIncremented = other._isAutoIncremented;
00094     _isPrimaryKey = other._isPrimaryKey;
00095     _isUnsigned = other._isUnsigned;
00096     _canBeNull = other._canBeNull;
00097     _precision = other._precision;
00098     _maxLength = other._maxLength;
00099     _type = other._type;
00100     _default = other._default;
00101     return *this;
00102 }
00103 
00113 bool WSqlColumn::operator==( const WSqlColumn& other ) const
00114 {
00115     return ( _columnName == other._columnName
00116         && _variableName == other._variableName
00117         && _isAutoIncremented == other._isAutoIncremented
00118         && _isPrimaryKey == other._isPrimaryKey
00119         && _isUnsigned == other._isUnsigned
00120         && _canBeNull == other._canBeNull
00121         && _maxLength == other._maxLength
00122         && _precision == other._precision
00123         && _default == other._default
00124         && _type == other._type
00125     );
00126 }
00127 
00136 void WSqlColumn::setPrecision(int precision)
00137 {
00138     _precision = precision;
00139 }
00140 
00154 void WSqlColumn::setDataType(WSqlDataType::Type type)
00155 {
00156     _type = type;
00157 }
00158 
00159 
00171 void WSqlColumn::setColumnName(const std::string& name)
00172 {
00173     _columnName = name;
00174     _variableName = WSqlDataType::columnNameToVariable(name);
00175 }
00191 void WSqlColumn::setVariableName(const std::string& name)
00192 {
00193     _variableName = name;
00194 }
00195 
00200 const std::string& WSqlColumn::columnName() const
00201 {
00202     return _columnName;
00203 }
00208 const std::string& WSqlColumn::variableName() const
00209 {
00210     return _variableName;
00211 }
00212 
00220 const WSqlDataType::Type WSqlColumn::type() const
00221 {
00222     return _type;
00223 }
00224 
00232 bool WSqlColumn::canBeNull() const
00233 {
00234     return( _isAutoIncremented || _canBeNull);
00235 }
00236 
00244 const int WSqlColumn::maxLength() const
00245 {
00246     return _maxLength;
00247 }
00248 
00254 const int WSqlColumn::precision() const
00255 {
00256     return _precision;
00257 }
00261 std::string WSqlColumn::typeDeclaration() const
00262 {
00263     std::string strToReturn;
00264     switch(_type){
00265         case WSqlDataType::NCHAR:
00266         case WSqlDataType::CHAR:
00267             strToReturn = "char";
00268             break;
00269         case WSqlDataType::TEXT:
00270         case WSqlDataType::TINYTEXT:
00271         case WSqlDataType::LONGTEXT:
00272         case WSqlDataType::MEDIUMTEXT:
00273         case WSqlDataType::VARCHAR:
00274         case WSqlDataType::NVARCHAR:
00275         case WSqlDataType::DATE:
00276         case WSqlDataType::DATETIME:
00277         case WSqlDataType::YEAR:
00278         case WSqlDataType::TIME:
00279         case WSqlDataType::TIMESTAMP:
00280         case WSqlDataType::TIMESTAMPTZ:
00281             strToReturn = "std::string";
00282             break;
00283         case WSqlDataType::TINYINT:
00284             strToReturn = "short";
00285             break;
00286         case WSqlDataType::SMALLINT:
00287         case WSqlDataType::MEDIUMINT:
00288         case WSqlDataType::INT:
00289             strToReturn = "int";
00290             break;
00291         case WSqlDataType::BIGINT:
00292             strToReturn = "long";
00293             break;
00294         case WSqlDataType::FLOAT:
00295             strToReturn = "float";
00296             break;
00297         case WSqlDataType::DECIMAL:
00298         case WSqlDataType::DOUBLE:
00299             strToReturn = "double";
00300             break;
00301         default:
00302             strToReturn = WSqlDataType::toString(_type);
00303     }
00304     return strToReturn;
00305 }
00308 bool WSqlColumn::typeIsSupported() const
00309 {
00310     switch(_type){
00311         case WSqlDataType::NCHAR:
00312         case WSqlDataType::CHAR:
00313         case WSqlDataType::TEXT:
00314         case WSqlDataType::TINYTEXT:
00315         case WSqlDataType::LONGTEXT:
00316         case WSqlDataType::MEDIUMTEXT:
00317         case WSqlDataType::VARCHAR:
00318         case WSqlDataType::NVARCHAR:
00319         case WSqlDataType::DATE:
00320         case WSqlDataType::DATETIME:
00321         case WSqlDataType::YEAR:
00322         case WSqlDataType::TIME:
00323         case WSqlDataType::TIMESTAMP:
00324         case WSqlDataType::TIMESTAMPTZ:
00325         case WSqlDataType::TINYINT:
00326         case WSqlDataType::SMALLINT:
00327         case WSqlDataType::MEDIUMINT:
00328         case WSqlDataType::INT:
00329         case WSqlDataType::BIGINT:
00330         case WSqlDataType::FLOAT:
00331         case WSqlDataType::DECIMAL:
00332         case WSqlDataType::DOUBLE:
00333             return true;
00334         default:
00335             return false;
00336     }
00337 }
00338 
00339 // template<> std::string WSqlColumn::defaultValue<std::string>()
00340 // {
00341 //     return _default.data<std::string>();
00342 // };
00343 
00355 }//namespace WSql
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Defines