def record(self, row=-1): if row >= 0: record = super(CollectionModel, self).record(row) else: record = super(CollectionModel, self).record() for field in [ 'obverseimg', 'reverseimg', 'edgeimg', 'photo1', 'photo2', 'photo3', 'photo4' ]: record.append(QSqlField(field + '_title')) record.append(QSqlField(field + '_id')) img_id = record.value(field) if img_id: data = self.getImage(img_id) record.setValue(field, data) record.setValue(field + '_title', self.getImageTitle(img_id)) record.setValue(field + '_id', img_id) else: record.setValue(field, None) fieldDesc = getattr(self.fields, field) record.setValue(field + '_title', fieldDesc.title) record.append(QSqlField('image_id')) img_id = record.value('image') if img_id: data = self.getPreviewImage(img_id) record.setValue('image', data) record.setValue('image_id', img_id) else: record.setValue('image', None) return record
def _constructColumnStatement(field: QSqlField) -> str: """ Constructs the statement components necessary for creating a column in a create table or alter table statement """ columnStatement = field.name() + " " + _handleFieldType(field) if field.requiredStatus() == QSqlField.Required: columnStatement += " NOT NULL " if field.defaultValue() != None: columnStatement += " DEFAULT " + field.defaultValue() return columnStatement
def initdiagUI(self): exitShortcut = QShortcut(QKeySequence('Ctrl+W'), self, qApp.quit) #=====# #=====# #=====# #=====# #=====# #=====# # Create local seperate connection to databse #=====# #=====# #=====# #=====# #=====# #=====# self.db = QSqlDatabase.database("dialog") #=====# #=====# #=====# #=====# #=====# #=====# # Create SqlRecord template with predefined fields #=====# #=====# #=====# #=====# #=====# #=====# self.record_template = QSqlRecord() self.record_template.insert(0, QSqlField('model')) self.record_template.insert(1, QSqlField('order_quantity')) self.record_template.insert(2, QSqlField('order_time')) self.record_template.insert(3, QSqlField('expected_production')) #=====# #=====# #=====# #=====# #=====# #=====# # Main widgets #=====# #=====# #=====# #=====# #=====# #=====# label_model = QLabel('Model', self) label_model.move(40, 50) self.combo = QComboBox(self) self.fillcombo() self.combo.move(180, 50) label_orderquantity = QLabel('Order Quantity', self) label_orderquantity.move(40, 100) self.oq_input = QLineEdit('', self) self.oq_input.move(180, 100) self.error_message = QLabel('', self) self.error_message.move(180, 130) btn_submit = QPushButton('Submit Order', self) btn_submit.move(180, 150) btn_submit.clicked.connect(self.submittodb) self.setFixedSize(350, 200) self.setWindowTitle('Input Dialog') self.show() self.new_record = -1
def AlterTableDropColumn(self, tableName: str, column: QSqlField): """ Executes an alter table statement to drop a column. If the column does not exist, throws a QSqlQueryException. """ statement = "ALTER TABLE {} DROP COLUMN {};".format( tableName, column.name()) self.exec(statement) if self.lastError() != None: _throwException(self.lastError().databaseText(), self.lastError().driverText())
def _createField(fieldName: str, fieldArgs: Mapping[str, Any]) -> QSqlField: ''' Creates the QSqlField object based on the field constraints. The name of the field is defined in the field constraints. Otherwise it is scraped from either the name of the get method, set method or delete method (in that order). If the name cannot be determined the function returns None ''' field = QSqlField(fieldName, QVariant(fieldName).type()) QSqlDBObject._handleFieldConstraints(fieldArgs, field) return field
def _handleFieldType(field: QSqlField) -> str: """ Returns a valid SQL type based on the type of the field. """ fieldType = field.type() if (fieldType == QVariant.Bool): return "BOOLEAN" elif (fieldType == QVariant.Char): return "VARCHAR({})".format(field.length()) elif (fieldType == QVariant.Date): return "DATE" elif (fieldType == QVariant.DateTime): return "DATETIME" elif (fieldType == QVariant.Double): return "DOUBLE({}, {})".format(field.length().field.precision()) elif (fieldType == QVariant.Int or fieldType == QVariant.UInt): return "INT({})".format(field.length()) elif (fieldType == QVariant.String or fieldType == QVariant.Url): return "TEXT" elif (fieldType == QVariant.Time): return "TIME" else: return "BLOB"
def _handleFieldConstraints(constraints: Mapping[str, Any], field: QSqlField) -> None: ''' Sets the defined field constraints on the field ''' for keyword in constraints: value = constraints[keyword] if (keyword == "defaultValue"): field.setDefaultValue(QVariant(value)) elif (keyword == "isAuto"): field.setAutoValue(value) elif (keyword == "isReadOnly"): field.setReadOnly(value) elif (keyword == "isRequired"): field.setRequired(value) elif (keyword == "length"): field.setLength(value) elif (keyword == "precision"): field.setPrecision(value) elif (keyword == "isPrimary"): # TODO: Handle primary keys pass elif (keyword == "links"): # TODO: Handle foreign keys pass