-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaddtable.cpp
140 lines (113 loc) · 4.7 KB
/
addtable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "addtable.h"
#include "mainwindow.h"
#include "ui_addtable.h"
#include <QStringList>
#include <QComboBox>
#include <QCheckBox>
#include <QSqlQuery>
#include <QSqlError>
#include <QLineEdit>
#include <QDebug>
AddTable::AddTable(int theColumnNumber, QString theTableName, QWidget *parent) :
QDialog(parent),
ui(new Ui::AddTable)
{
ui->setupUi(this);
ui->labelError->setVisible(false);
tableName = theTableName;
columnCount = theColumnNumber;
//Table initialization
QStringList tableLabels;
tableLabels << "Name" << "Type" << "Lenght/Values" << "Default" << "Attributes" << "Null" << "Index" << "A.I";
ui->tableWidgetAddTable->clear();
ui->tableWidgetAddTable->setHorizontalHeaderLabels(tableLabels);
ui->tableWidgetAddTable->setRowCount(theColumnNumber);
ui->tableWidgetAddTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
for(int i=0 ; i<=theColumnNumber ; i++)
{
QLineEdit* nameLineEdit = new QLineEdit();
ui->tableWidgetAddTable->setCellWidget(i,0,nameLineEdit);
QComboBox* typeComboBox = new QComboBox();
QStringList typeLabels;
typeLabels << "INT" << "VARCHAR" << "BOOL" << "DATE" << "DATETIME" << "TIME";
typeComboBox->addItems(typeLabels);
ui -> tableWidgetAddTable -> setCellWidget(i,1,typeComboBox);
QLineEdit* valueLineEdit = new QLineEdit();
ui->tableWidgetAddTable->setCellWidget(i,2,valueLineEdit);
QComboBox* defaultComboBox = new QComboBox();
QStringList defaultLabels;
defaultLabels << "" << "As defined" << "NULL" << "CURRENT_TIMESTAMP";
defaultComboBox->addItems(defaultLabels);
ui -> tableWidgetAddTable -> setCellWidget(i,3,defaultComboBox);
QComboBox* attributesComboBox = new QComboBox();
QStringList attributesLabels;
attributesLabels << "" << "BINARY" << "UNSIGNED" << "UNSIGNED ZEROFILL" << "On update CURRENT_TIMESTAMP";
attributesComboBox->addItems(attributesLabels);
ui -> tableWidgetAddTable -> setCellWidget(i,4,attributesComboBox);
QCheckBox* nullCheckBox = new QCheckBox();
ui -> tableWidgetAddTable -> setCellWidget(i,5,nullCheckBox);
QComboBox* indexComboBox = new QComboBox();
QStringList indexLabels;
indexLabels << "" << "PRIMARY KEY" << "UNIQUE" << "INDEX" << "FULLTEXT" << "SPATIAL";
indexComboBox->addItems(indexLabels);
ui -> tableWidgetAddTable -> setCellWidget(i,6,indexComboBox);
QCheckBox* aiCheckBox = new QCheckBox();
ui -> tableWidgetAddTable -> setCellWidget(i,7,aiCheckBox);
}
}
AddTable::~AddTable()
{
delete ui;
}
void AddTable::on_pushButtonCreateTable_clicked()
{
//QSqlQuery createTableRequest;
QString request = "CREATE TABLE "+tableName+" (";
for(int i=0 ; i<columnCount ; i++)
{
if(i != 0)
{
request += ",";
}
request += ((QLineEdit *)(ui->tableWidgetAddTable->cellWidget(i,0)))->text()+" "+((QComboBox *)(ui->tableWidgetAddTable->cellWidget(i,1)))->currentText();
if(!(((QLineEdit *)(ui->tableWidgetAddTable->cellWidget(i,2)))->text().isEmpty()))
{
request += "("+((QLineEdit *)(ui->tableWidgetAddTable->cellWidget(i,2)))->text()+")";
}
if(!(((QComboBox *)(ui->tableWidgetAddTable->cellWidget(i,4)))->currentText().isEmpty()))
{
request += " " + ((QComboBox *)(ui->tableWidgetAddTable->cellWidget(i,4)))->currentText();
}
if(((QCheckBox *)(ui->tableWidgetAddTable->cellWidget(i,5)))->isChecked())
{
request += " NULL";
} else{
request += " NOT NULL";
}
if(!(((QComboBox *)(ui->tableWidgetAddTable->cellWidget(i,3)))->currentText().isEmpty()))
{
request += " DEFAULT " + ((QComboBox *)(ui->tableWidgetAddTable->cellWidget(i,3)))->currentText();
}
if(((QCheckBox *)(ui->tableWidgetAddTable->cellWidget(i,7)))->isChecked())
{
request += " AUTO_INCREMENT";
}
}
for(int i=0 ; i<columnCount ; i++)
{
if(!(((QComboBox *)(ui->tableWidgetAddTable->cellWidget(i,6)))->currentText().isEmpty()))
{
request += " ," + ((QComboBox *)(ui->tableWidgetAddTable->cellWidget(i,6)))->currentText() + " (" + ((QLineEdit *)(ui->tableWidgetAddTable->cellWidget(i,0)))->text() + ")";
}
}
request += ")";
QSqlQuery addTableRequest;
if(addTableRequest.exec(request)) {
((MainWindow*)parent())->afficherTable();
close();
} else{
ui->labelError->setWordWrap(true);
ui->labelError->setText(addTableRequest.lastError().text());
ui->labelError->setVisible(true);
}
}