Subversion Repositories OpenInventory

Rev

Rev 39 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#include "tablefield.h"

/*TableField::TableField()
{
}
*/


/***********************************************************************************************
 ** Êîíñòðóêòîð êëàññà TableField èìååò îäèí îáÿçàòåëüíûé ïàðàìåòð - èìÿ ïîëÿ
 ** îñòàëüíûå ïàðàìåòðû èìåþò çíà÷åíèÿ ïî óìîë÷àíèþ:
 ** *parent = 0 (íåò "ðîäèòåëüñêîãî" îáúåêòà)
 ** ftype = CHAR(30) (òèï õðàíèìûõ â ïîëå äàííûõ - ñòðîêà èç 30 ñèìâîëîâ)
 ** fdefaultValue = NULL (äàííûå â ïîëå ìîãóò èìåòü "ïóñòîå" çíà÷åíèå)
 ***********************************************************************************************/


TableField::TableField(QString fname, QObject *parent, QString ftype, QString fdefaultValue):
  QObject(parent)
{
    fieldName = fname;
    fieldType = ftype;
    defaultValue = fdefaultValue;
    notNull = false;                    // ïî óìîë÷àíèþ äàííûå â ïîëå ìîãóò îòñóòñòâîâàòü (áûòü NULL)
    characterSet = "";                  // êîäèðîâêà ïî óìîë÷àíèþ (íå óêàçûâàòü êîäèðîâêó)
    autoIncrement = false;               // ñäåëàåì íàøå ïîëå àâòîèíêåìåíòíûì
    createSqlString();                  // ñôîðìèðóåì ñòðîêó Sql-çàïðîñà, êîòîðàÿ ñîçäàåò â áàçå äàííûõ ñîîòâåòñòâóþùåå ïîëå
}



/*******************************************************************
** Ôóíêöèÿ óñòàíàâëèâàò ïåðåííóþ name (èìÿ ïîëÿ),
**
*******************************************************************/




void TableField::setName(QString name){
    TableField::fieldName = name;
    TableField::createSqlString();
}


QString TableField::name(){
    return TableField::fieldName;
}

QString TableField::type(){
    return TableField::fieldType;
}

void TableField::setType(QString ftype){
TableField::fieldType = ftype;
TableField::createSqlString();
}

/*************************************************************************
** Ôóíêöèÿ isNotNull() âîçâðàùàåò true, åñëè ïîëå èìååò àòðèáóò "NOT NULL"
** Èíà÷å - âîçâðàùàåò false
*************************************************************************/


bool TableField::isNotNull(){
return TableField::notNull;
}

/*******************************************************************
** Ôóíêöèÿ óñòàíàâëèâàåò àòðèáóò ïîëÿ "NOT NULL"
** Çíà÷åíèå ïåðåäàåòñÿ  ÷åðåç ïàðàìåòð ôóíêöèè
*******************************************************************/



void TableField::setNotNull(bool fnotNull){
    if ((fnotNull) && (defaultValue == "NULL")) defaultValue = ""; // ïîëå íå ìîæåò áûòü ïóñòûì, ïîýòîìó, åñëè çíà÷åíèå ïî óìîë÷àíèþ NULL, ïîìåíÿåì åãî íà ïóñòóþ ñòðîêó
    TableField::notNull = fnotNull;
    TableField::createSqlString(); // Ãåíåðèðóåò ñòðîêó Sql-çàïðîñà ñ íîâûìè ïàðàìåòðàìè
}


/********************************************************************************************
** Ôóíêöèÿ ãåíåðèðóåò ñòðîêó Sql-çàïðîñà, êîòîðàÿ ñîçäàåò â áàçå äàííûõ ñîîòâåòñòâóþùåå ïîëå
** è ïðèñâàèâàåò ïîëó÷åííîå çíà÷åíèå ïåðåìåííîé SqlString
********************************************************************************************/


void TableField::createSqlString(){
QString str_tmp = "";
str_tmp.append(tr("`"));
str_tmp.append(fieldName);
str_tmp.append(tr("` "));
str_tmp.append(fieldType);
//str_tmp.append(tr(" "));
if (characterSet !="") {
str_tmp.append(tr(" character set"));
str_tmp.append(characterSet);
}
if (notNull) { str_tmp.append(tr(" NOT NULL")); }
if ((defaultValue !="") && (defaultValue !="NULL")) {
    str_tmp.append(tr(" default '"));
    str_tmp.append(defaultValue);
    str_tmp.append(tr("'"));}
if ((defaultValue =="NULL")) {
    str_tmp.append(tr(" default "));
    str_tmp.append(defaultValue);}

if (TableField::autoIncrement) {
     str_tmp.append(tr(" "));
     str_tmp.append(tr("AUTO_INCREMENT"));
}
TableField::SqlString = str_tmp;
}


QString TableField::sqlString(){
return TableField::SqlString;
}


void TableField::setDefaultValue(QString fdefaultValue){
 TableField::defaultValue = fdefaultValue;
 TableField::createSqlString();
}



void TableField::setAutoIncrement(bool autoInc){
    TableField::autoIncrement = autoInc;
    TableField::createSqlString();
}


bool TableField::isAutoIncrement(){
    return TableField::autoIncrement;
}