2 // C++ Implementation: dlgcolumndef
7 // Author: Fanda Vacek <fanda.vacek@volny.cz>, (C) 2005
9 // Copyright: See COPYING file that comes with this distribution
12 #include <qfmsgshowagain.h>
14 #include "mainwindow.h"
16 #include "dlgcolumndef.h"
18 //#define QF_NO_TRASH_OUTPUT
19 #include <qflogcust.h>
21 DlgColumnDef::DlgColumnDef(QWidget
* parent
, const QString
& table
)
24 QFSql::parseFullName(table
, &tableName
, &dbName
);
27 if(connection().driverName().endsWith("SQLITE")) {
28 types
<< "INTEGER" << "REAL" << "TEXT" << "BLOB";
30 else if(connection().driverName().endsWith("PSQL")) {
32 << "smallint" // 2 bytes small-range integer -32768 to +32767
33 << "integer" // 4 bytes usual choice for integer -2147483648 to +2147483647
34 << "bigint" // 8 bytes large-range integer -9223372036854775808 to 9223372036854775807
35 << "decimal" // variable user-specified precision, exact no limit
36 << "numeric" // variable user-specified precision, exact no limit
37 << "real" // 4 bytes variable-precision, inexact 6 decimal digits precision
38 << "double precision" // precision 8 bytes variable-precision, inexact 15 decimal digits precision
39 << "serial" // 4 bytes autoincrementing integer 1 to 2147483647
40 << "bigserial" //8 bytes large autoincrementing integer 1 to 9223372036854775807
41 << "varchar" //(n) variable-length with limit
42 << "character varying"
43 << "char" //(n) fixed-length, blank padded
44 << "text" // variable unlimited length
46 << "date" // 4 bytes dates only 4713 BC 32767 AD 1 day
47 << "time" // [ (p) ] [ without time zone ] 8 bytes times of day only 00:00:00 24:00:00 1 microsecond / 14 digits
48 << "timestamp" // [ (p) ] [ without time zone ] 8 bytes both date and time 4713 BC 5874897 AD 1 microsecond / 14 digits
49 << "interval" // [ (p) ] 12 bytes time intervals -178000000 years 178000000 years 1 microsecond / 14 digits
50 << "money" // 4 bytes currency amount -21474836.48 to +21474836.47;
51 << "bytea" // 4 bytes plus the actual binary string variable-length binary string
53 << "bit varying" //(n)
54 << "cidr" // 12 or 24 bytes IPv4 and IPv6 networks
55 << "inet" // 12 or 24 bytes IPv4 and IPv6 hosts and networks
56 << "macaddr" ;// 6 bytes MAC addresses
58 else if(connection().driverName().endsWith("MYSQL")) {
90 lstType
->addItems(types
);
94 DlgColumnDef::~DlgColumnDef()
98 QFSqlConnection
DlgColumnDef::connection()
100 //qfTrash() << QF_FUNC_NAME;
101 MainWindow
*w
= qfFindParent
<MainWindow
*>(this);
102 return w
->activeConnection();
105 void DlgColumnDef::enableControls(bool v
)
107 edName
->setEnabled(v
);
108 lstType
->setEnabled(v
);
109 edLength
->setEnabled(v
);
110 edDecimals
->setEnabled(v
);
111 edDefaultValue
->setEnabled(v
);
112 chkNotNull
->setEnabled(v
);
113 chkUnique
->setEnabled(v
);
114 chkPrimaryKey
->setEnabled(v
);
115 chkUnsigned
->setEnabled(v
);
116 lstRefTable
->setEnabled(v
);
117 lstRefColumn
->setEnabled(v
);
118 lstCharacterSet
->setEnabled(v
);
119 grpEnum
->setEnabled(v
);
120 txtEnum
->setEnabled(grpEnum
->isEnabled());
121 //qfInfo() << "1grpEnum enabled:" << grpEnum->isEnabled() << "txtEnum enabled:" << txtEnum->isEnabled();
124 void DlgColumnDef::clearFields()
126 qfTrash() << QF_FUNC_NAME
;
127 edName
->setText(QString());
128 lstType
->setCurrentIndex(0);
129 edLength
->setText(QString());
130 edDecimals
->setText(QString());
131 edDefaultValue
->setText(QString());
132 chkNotNull
->setChecked(false);
133 chkUnique
->setChecked(false);
134 chkPrimaryKey
->setChecked(false);
135 chkUnsigned
->setChecked(false);
136 lstRefTable
->clear();
137 if(!dbName
.isEmpty()) {
138 QFSqlDbInfo di
= connection().catalog().database(dbName
);
139 lstRefTable
->addItems(di
.tables());
141 lstRefTable
->setCurrentIndex(-1);
142 lstCharacterSet
->setCurrentIndex(0);
145 void DlgColumnDef::loadColumnDefinition(const QFSqlFieldInfo
&fi
)
147 qfTrash() << QF_FUNC_NAME
;
148 qfTrash() << fi
.toString();
150 enableControls(true);
152 edName
->setText("new_field");
154 btOk
->setEnabled(true);
155 if(!fi
.isValid()) return;
156 enableControls(false);
157 edName
->setText(fi
.fieldName());
159 lstCharacterSet
->setEnabled(false);
160 lstCharacterSet
->setCurrentIndex(-1);
161 QString s
= fi
.nativeType();
162 qfTrash() << "\tnative type:" << s
<< "length:" << fi
.length();
163 if(connection().driverName().endsWith("MYSQL")) {
164 if(s
== "tinyint" && fi
.length() == 1) s
= "boolean";
166 int ix
= lstType
->findText(s
, Qt::MatchExactly
);
167 lstType
->setCurrentIndex(ix
);
168 edLength
->setText(QString::number(fi
.length()));
169 if(fi
.type() == QVariant::String
) edDecimals
->setText(QString());
170 else edDecimals
->setText(QString::number(fi
.precision()));
171 edDefaultValue
->setText(fi
.defaultValue().toString()); edName
->setEnabled(true);
172 chkNotNull
->setChecked(!fi
.isNullable());
173 chkUnsigned
->setChecked(fi
.isUnsigned());
174 chkPrimaryKey
->setChecked(fi
.isPriKey());
175 txtComment
->setPlainText(fi
.comment());
176 if(connection().driverName().endsWith("SQLITE")) {
177 QFMsgShowAgain::show(this,"SQLite ver 3.2.2 can only rename already created fields");
178 edName
->setEnabled(true);
180 else if(connection().driverName().endsWith("PSQL")) {
181 QFMsgShowAgain::show(this,"Altering columns is not fully supported yet.");
182 edName
->setEnabled(true);
183 lstType
->setEnabled(true);
184 edDefaultValue
->setEnabled(true);
185 chkNotNull
->setEnabled(true);
186 lstRefTable
->setEnabled(true);
187 lstRefColumn
->setEnabled(true);
189 else if(connection().driverName().endsWith("MYSQL")) {
190 edName
->setEnabled(true);
191 lstType
->setEnabled(true);
192 edDefaultValue
->setEnabled(true);
193 chkNotNull
->setEnabled(true);
194 lstRefTable
->setEnabled(true);
195 lstRefColumn
->setEnabled(true);
196 chkUnique
->setEnabled(true);
197 chkPrimaryKey
->setEnabled(true);
198 chkUnsigned
->setEnabled(true);
199 edLength
->setEnabled(true);
200 edDecimals
->setEnabled(true);
201 txtComment
->setEnabled(true);
203 QString char_set
= fi
.characterSet();
204 int ix
= lstCharacterSet
->findText(char_set
, Qt::MatchFixedString
);
205 lstCharacterSet
->setCurrentIndex(ix
);
207 lstCharacterSet
->setEnabled(fi
.type() == QVariant::String
);
208 //qfInfo() << "2grpEnum enabled:" << grpEnum->isEnabled() << "txtEnum enabled:" << txtEnum->isEnabled();
209 grpEnum
->setEnabled(fi
.nativeType() == "enum" || fi
.nativeType() == "set");
210 txtEnum
->setPlainText(fi
.enumOrSetFields().join("\n"));
211 txtEnum
->setEnabled(grpEnum
->isEnabled());
212 //qfInfo() << "3grpEnum enabled:" << grpEnum->isEnabled() << "txtEnum enabled:" << txtEnum->isEnabled();
214 //edOk->setEnabled(false);
217 void DlgColumnDef::on_lstRefTable_currentIndexChanged(const QString
& text
)
219 lstRefColumn
->clear();
220 if(!text
.isEmpty()) {
221 QFSqlTableInfo ti
= connection().catalog().table(QFSql::composeFullName(text
, dbName
), !Qf::ThrowExc
);
222 lstRefColumn
->addItems(ti
.unorderedFields());
224 lstRefColumn
->setCurrentIndex(-1);
227 QString
DlgColumnDef::toString()
230 QString type
= lstType
->currentText();
232 if(type
== "enum" || type
== "set") {
233 QFString s
= txtEnum
->toPlainText();
234 QStringList sl
= s
.splitAndTrim('\n');
236 ret
+= "('" + s
+ "')";
239 QString s
= edLength
->text();
242 s
= edDecimals
->text().trimmed();
243 if(!s
.isEmpty() && s
!= "0") ret
+= "," + s
;
247 if(chkUnsigned
->isChecked()) ret
+= " UNSIGNED";
248 if(lstCharacterSet
->isEnabled() && lstCharacterSet
->currentIndex() > 0) ret
+= " CHARACTER SET " + lstCharacterSet
->currentText();
249 QString s
= edDefaultValue
->text();
251 ret
+= " DEFAULT " + s
;
253 if(chkNotNull
->isChecked()) ret
+= " NOT NULL";
254 if(chkUnique
->isChecked()) ret
+= " UNIQUE";
256 if(connection().driverName().endsWith("SQLITE")) {
257 /// sqlite umi jen prejmenovat sloupec, takze vrat nove jmeno
258 //ret = edName->text();
260 else if(connection().driverName().endsWith("PSQL") ||
261 connection().driverName().endsWith("MYSQL")) {
262 if(chkPrimaryKey
->isChecked()) ret
+= " PRIMARY KEY";
263 s
= lstRefTable
->currentText();
265 ret
+= " REFERENCES " + s
;
266 s
= lstRefColumn
->currentText();
268 ret
+= " (" + s
+ ")";
272 if(connection().driverName().endsWith("MYSQL")) {
273 s
= txtComment
->toPlainText().trimmed();
274 if(!s
.isEmpty()) ret
+= " COMMENT '" + s
+ "'";
283 bool DlgColumnDef::showCommand()
285 return chkShowCommand
->isChecked();