GeezerVison is showing github.com — SqliteTypes: Move PK flag from table to field · feiva/sqlitebrowser@63cf871
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/EditTableDialog.cpp +13-18Lines changed: 13 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,7 +75,6 @@ void EditTableDialog::populateFields() | 75 | 75 | tbitem->setText(kName, f->name()); | 76 | 76 | QComboBox* typeBox = new QComboBox(ui->treeWidget); | 77 | 77 | typeBox->setProperty("column", f->name()); | 78 | | - connect(typeBox, SIGNAL(activated(int)), this, SLOT(updateTypes())); | 79 | 78 | typeBox->setEditable(false); | 80 | 79 | typeBox->addItems(sqlb::Field::Datatypes); | 81 | 80 | int index = typeBox->findText(f->type(), Qt::MatchExactly); | @@ -86,10 +85,11 @@ void EditTableDialog::populateFields() | 86 | 85 | index = typeBox->count() - 1; | 87 | 86 | } | 88 | 87 | typeBox->setCurrentIndex(index); | | 88 | + connect(typeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTypes())); | 89 | 89 | ui->treeWidget->setItemWidget(tbitem, kType, typeBox); | 90 | 90 |
| 91 | 91 | tbitem->setCheckState(kNotNull, f->notnull() ? Qt::Checked : Qt::Unchecked); | 92 | | - tbitem->setCheckState(kPrimaryKey, m_table.primarykey().contains(f) ? Qt::Checked : Qt::Unchecked); | | 92 | + tbitem->setCheckState(kPrimaryKey, f->primaryKey() ? Qt::Checked : Qt::Unchecked); | 93 | 93 | tbitem->setCheckState(kAutoIncrement, f->autoIncrement() ? Qt::Checked : Qt::Unchecked); | 94 | 94 | tbitem->setText(kDefault, f->defaultValue()); | 95 | 95 | tbitem->setText(kCheck, f->check()); | @@ -204,28 +204,21 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column) | 204 | 204 | break; | 205 | 205 | case kPrimaryKey: | 206 | 206 | { | 207 | | - sqlb::FieldVector pks = m_table.primarykey(); | | 207 | + field->setPrimaryKey(item->checkState(column) == Qt::Checked); | 208 | 208 | if(item->checkState(column) == Qt::Checked) | 209 | 209 | { | 210 | | - pks.append(field); | 211 | | - // this will unset any set autoincrement | | 210 | + // this will unset any other autoincrement | 212 | 211 | for(int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) | 213 | 212 | { | 214 | 213 | QTreeWidgetItem* tbitem = ui->treeWidget->topLevelItem(i); | 215 | 214 | if(tbitem != item) | 216 | | - { | 217 | 215 | tbitem->setCheckState(kAutoIncrement, Qt::Unchecked); | 218 | | - } | 219 | 216 | } | 220 | | - } | 221 | | - else | 222 | | - { | | 217 | + } else { | 223 | 218 | item->setCheckState(kAutoIncrement, Qt::Unchecked); | 224 | | - int index = pks.indexOf(field); | 225 | | - if(index != -1) | 226 | | - pks.remove(index); | 227 | 219 | } | 228 | | - m_table.setPrimaryKey(pks); | | 220 | + if(!m_bNewTable) | | 221 | + callRenameColumn = true; | 229 | 222 | } | 230 | 223 | break; | 231 | 224 | case kNotNull: | @@ -255,13 +248,14 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column) | 255 | 248 | case kAutoIncrement: | 256 | 249 | { | 257 | 250 | bool ischecked = item->checkState(column) == Qt::Checked; | 258 | | - field->setAutoIncrement(ischecked); | 259 | 251 | if(ischecked) | 260 | 252 | { | 261 | | - item->setCheckState(kPrimaryKey, Qt::Checked); | | 253 | + QComboBox* comboType = qobject_cast<QComboBox*>(ui->treeWidget->itemWidget(item, kType)); | | 254 | + comboType->setCurrentIndex(comboType->findText("INTEGER")); | | 255 | + item->setCheckState(kPrimaryKey, Qt::Checked); | 262 | 256 |
| 263 | | - // this will reset all other primary keys unset | 264 | | - // there can't be more then one autoincrement pk | | 257 | + // this will unset all other primary keys | | 258 | + // there can't be more than one autoincrement pk | 265 | 259 | for(int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) | 266 | 260 | { | 267 | 261 | QTreeWidgetItem* tbitem = ui->treeWidget->topLevelItem(i); | @@ -272,6 +266,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column) | 272 | 266 | } | 273 | 267 | } | 274 | 268 | } | | 269 | + field->setAutoIncrement(ischecked); | 275 | 270 |
| 276 | 271 | if(!m_bNewTable) | 277 | 272 | callRenameColumn = true; | |
Collapse file Copy file name to clipboardExpand all lines: src/sqlitetypes.cpp +18-51Lines changed: 18 additions & 51 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,6 @@ bool Field::isInteger() const | 55 | 55 | void Table::clear() | 56 | 56 | { | 57 | 57 | m_fields.clear(); | 58 | | - m_primarykey.clear(); | 59 | 58 | } | 60 | 59 |
| 61 | 60 | Table::~Table() | @@ -95,31 +94,6 @@ int Table::findField(const QString &sname) | 95 | 94 | return -1; | 96 | 95 | } | 97 | 96 |
| 98 | | -bool Table::setPrimaryKey(const FieldVector& pk) | 99 | | -{ | 100 | | - foreach(FieldPtr f, pk) { | 101 | | - if(!m_fields.contains(f)) | 102 | | - return false; | 103 | | - } | 104 | | - | 105 | | - m_primarykey = pk; | 106 | | - return true; | 107 | | -} | 108 | | - | 109 | | -bool Table::setPrimaryKey(FieldPtr pk, bool autoincrement) | 110 | | -{ | 111 | | - if(m_fields.contains(pk)) | 112 | | - { | 113 | | - if(pk->isInteger() && autoincrement) | 114 | | - pk->setAutoIncrement(true); | 115 | | - m_primarykey.clear(); | 116 | | - m_primarykey.append(pk); | 117 | | - return true; | 118 | | - } | 119 | | - | 120 | | - return false; | 121 | | -} | 122 | | - | 123 | 97 | QStringList Table::fieldList() const | 124 | 98 | { | 125 | 99 | QStringList sl; | @@ -175,12 +149,10 @@ QString Table::emptyInsertStmt() const | 175 | 149 | if(f->notnull()) | 176 | 150 | { | 177 | 151 | fields << f->name(); | 178 | | - if( m_primarykey.contains(f) && f->isInteger() ) | | 152 | + if( f->primaryKey() && f->isInteger() ) | 179 | 153 | { | 180 | 154 | vals << "NULL"; | 181 | | - } | 182 | | - else | 183 | | - { | | 155 | + } else { | 184 | 156 | if(f->isInteger()) | 185 | 157 | vals << "0"; | 186 | 158 | else | @@ -220,18 +192,21 @@ QString Table::sql() const | 220 | 192 | sql += fieldList().join(",\n"); | 221 | 193 |
| 222 | 194 | // primary key | 223 | | - if( m_primarykey.size() > 0 && !hasAutoIncrement()) | | 195 | + if(!hasAutoIncrement()) | 224 | 196 | { | 225 | | - sql += ",\n\tPRIMARY KEY("; | 226 | | - for(FieldVector::ConstIterator it = m_primarykey.constBegin(); | 227 | | - it != m_primarykey.constEnd(); | 228 | | - ++it) | | 197 | + QString pk = ",\n\tPRIMARY KEY("; | | 198 | + bool pks_found = false; | | 199 | + foreach(FieldPtr f, m_fields) | 229 | 200 | { | 230 | | - sql += (*it)->name(); | 231 | | - if(*it != m_primarykey.last()) | 232 | | - sql += ","; | | 201 | + if(f->primaryKey()) | | 202 | + { | | 203 | + pk += f->name() + ","; | | 204 | + pks_found = true; | | 205 | + } | 233 | 206 | } | 234 | | - sql += ")"; | | 207 | + pk.chop(1); | | 208 | + if(pks_found) | | 209 | + sql += pk + ")"; | 235 | 210 | } | 236 | 211 |
| 237 | 212 | return sql + "\n);"; | @@ -281,16 +256,12 @@ Table CreateTableWalker::table() | 281 | 256 | while(column != antlr::nullAST && column->getType() == sqlite3TokenTypes::COLUMNDEF) | 282 | 257 | { | 283 | 258 | FieldPtr f; | 284 | | - bool pk = parsecolumn(f, column->getFirstChild()); | | 259 | + parsecolumn(f, column->getFirstChild()); | 285 | 260 | tab.addField(f); | 286 | | - if(pk) | 287 | | - pks.append(FieldPtr(f)); | 288 | 261 | column = column->getNextSibling(); //COMMA or RPAREN | 289 | 262 | column = column->getNextSibling(); //null or tableconstraint | 290 | 263 | } | 291 | 264 |
| 292 | | - tab.setPrimaryKey(pks); | 293 | | - | 294 | 265 | // now we are finished or it is a tableconstraint | 295 | 266 | while(s != antlr::nullAST) | 296 | 267 | { | @@ -305,18 +276,15 @@ Table CreateTableWalker::table() | 305 | 276 | { | 306 | 277 | tc = tc->getNextSibling()->getNextSibling(); // skip primary and key | 307 | 278 | tc = tc->getNextSibling(); // skip LPAREN | 308 | | - pks.clear(); | 309 | 279 | do | 310 | 280 | { | 311 | 281 | QString col = identifier(tc); | 312 | 282 | int fieldindex = tab.findField(col); | 313 | 283 | if(fieldindex != -1) | 314 | | - pks.append(tab.fields().at(fieldindex)); | | 284 | + tab.fields().at(fieldindex)->setPrimaryKey(true); | 315 | 285 | tc = tc->getNextSibling(); // skip ident and comma | 316 | 286 | tc = tc->getNextSibling(); | 317 | 287 | } while(tc != antlr::nullAST && tc->getType() != sqlite3TokenTypes::RPAREN); | 318 | | - | 319 | | - tab.setPrimaryKey(pks); | 320 | 288 | } | 321 | 289 | break; | 322 | 290 | default: break; | @@ -330,7 +298,7 @@ Table CreateTableWalker::table() | 330 | 298 | return tab; | 331 | 299 | } | 332 | 300 |
| 333 | | -bool CreateTableWalker::parsecolumn(FieldPtr& f, antlr::RefAST c) | | 301 | +void CreateTableWalker::parsecolumn(FieldPtr& f, antlr::RefAST c) | 334 | 302 | { | 335 | 303 | QString columnname; | 336 | 304 | QString type = "TEXT"; | @@ -396,9 +364,8 @@ bool CreateTableWalker::parsecolumn(FieldPtr& f, antlr::RefAST c) | 396 | 364 | c = c->getNextSibling(); | 397 | 365 | } | 398 | 366 |
| 399 | | - f = FieldPtr( new Field(columnname, type, notnull, defaultvalue, check)); | | 367 | + f = FieldPtr( new Field(columnname, type, notnull, defaultvalue, check, primarykey)); | 400 | 368 | f->setAutoIncrement(autoincrement); | 401 | | - return primarykey; | 402 | 369 | } | 403 | 370 |
| 404 | 371 | } //namespace sqlb |
Collapse file Copy file name to clipboardExpand all lines: src/sqlitetypes.h +9-8Lines changed: 9 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,13 +18,16 @@ class Field | 18 | 18 | const QString& type, | 19 | 19 | bool notnull = false, | 20 | 20 | const QString& defaultvalue = "", | 21 | | - const QString& check = "") | | 21 | + const QString& check = "", | | 22 | + bool pk = false) | 22 | 23 | : m_name(name) | 23 | 24 | , m_type(type) | 24 | 25 | , m_notnull(notnull) | 25 | 26 | , m_check(check) | 26 | 27 | , m_defaultvalue(defaultvalue) | 27 | | - , m_autoincrement(false) {} | | 28 | + , m_autoincrement(false) | | 29 | + , m_primaryKey(pk) | | 30 | + {} | 28 | 31 |
| 29 | 32 | QString toString(const QString& indent = "\t", const QString& sep = "\t") const; | 30 | 33 |
| @@ -34,6 +37,7 @@ class Field | 34 | 37 | void setCheck(const QString& check) { m_check = check; } | 35 | 38 | void setDefaultValue(const QString& defaultvalue) { m_defaultvalue = defaultvalue; } | 36 | 39 | void setAutoIncrement(bool autoinc) { m_autoincrement = autoinc; } | | 40 | + void setPrimaryKey(bool pk) { m_primaryKey = pk; } | 37 | 41 |
| 38 | 42 | bool isText() const; | 39 | 43 | bool isInteger() const; | @@ -44,6 +48,7 @@ class Field | 44 | 48 | const QString& check() const { return m_check; } | 45 | 49 | const QString& defaultValue() const { return m_defaultvalue; } | 46 | 50 | bool autoIncrement() const { return m_autoincrement; } | | 51 | + bool primaryKey() const { return m_primaryKey; } | 47 | 52 |
| 48 | 53 | static QStringList Datatypes; | 49 | 54 | private: | @@ -53,6 +58,7 @@ class Field | 53 | 58 | QString m_check; | 54 | 59 | QString m_defaultvalue; | 55 | 60 | bool m_autoincrement; //! this is stored here for simplification | | 61 | + bool m_primaryKey; | 56 | 62 | }; | 57 | 63 |
| 58 | 64 | typedef QSharedPointer<Field> FieldPtr; | @@ -67,7 +73,6 @@ class Table | 67 | 73 |
| 68 | 74 | const QString& name() const { return m_name; } | 69 | 75 | const FieldVector& fields() const { return m_fields; } | 70 | | - const FieldVector& primarykey() const { return m_primarykey; } | 71 | 76 |
| 72 | 77 | /** | 73 | 78 | * @brief Creates an empty insert statement. | @@ -94,9 +99,6 @@ class Table | 94 | 99 | */ | 95 | 100 | int findField(const QString& sname); | 96 | 101 |
| 97 | | - bool setPrimaryKey(const FieldVector& pk); | 98 | | - bool setPrimaryKey(FieldPtr pk, bool autoincrement = false); | 99 | | - | 100 | 102 | static Table parseSQL(const QString& sSQL); | 101 | 103 | private: | 102 | 104 | QStringList fieldList() const; | @@ -105,7 +107,6 @@ class Table | 105 | 107 | private: | 106 | 108 | QString m_name; | 107 | 109 | FieldVector m_fields; | 108 | | - FieldVector m_primarykey; | 109 | 110 | }; | 110 | 111 |
| 111 | 112 | /** | @@ -121,7 +122,7 @@ class CreateTableWalker | 121 | 122 | Table table(); | 122 | 123 |
| 123 | 124 | private: | 124 | | - bool parsecolumn(FieldPtr& f, antlr::RefAST c); | | 125 | + void parsecolumn(FieldPtr& f, antlr::RefAST c); | 125 | 126 |
| 126 | 127 | private: | 127 | 128 | antlr::RefAST m_root; | |
|