142 lines
4.3 KiB
Python
142 lines
4.3 KiB
Python
"""
|
|
Column definition dialog — used for both Add Column and Rename Column operations
|
|
in the Table Designer.
|
|
"""
|
|
from PyQt6.QtWidgets import (
|
|
QDialog, QVBoxLayout, QHBoxLayout, QFormLayout,
|
|
QLineEdit, QComboBox, QCheckBox, QDialogButtonBox, QLabel,
|
|
)
|
|
from PyQt6.QtCore import Qt
|
|
|
|
|
|
# Common SQL types offered in the dropdown (user can type anything)
|
|
_COMMON_TYPES = [
|
|
"INT",
|
|
"BIGINT",
|
|
"SMALLINT",
|
|
"TINYINT",
|
|
"BOOLEAN",
|
|
"FLOAT",
|
|
"DOUBLE",
|
|
"DECIMAL(10,2)",
|
|
"VARCHAR(255)",
|
|
"VARCHAR(100)",
|
|
"CHAR(36)",
|
|
"TEXT",
|
|
"LONGTEXT",
|
|
"DATE",
|
|
"DATETIME",
|
|
"TIMESTAMP",
|
|
"TIME",
|
|
"JSON",
|
|
"BLOB",
|
|
"BINARY(16)",
|
|
]
|
|
|
|
|
|
class ColumnDialog(QDialog):
|
|
"""
|
|
Dialog for adding a new column or renaming an existing one.
|
|
|
|
In rename mode only the Name field is shown; in add mode all fields appear.
|
|
"""
|
|
|
|
def __init__(self, mode: str = "add", column_name: str = "",
|
|
db_type: str = "", parent=None):
|
|
"""
|
|
Args:
|
|
mode: "add" | "rename"
|
|
column_name: pre-filled name (for rename, the old name)
|
|
db_type: driver db_type string — used to tailor type suggestions
|
|
"""
|
|
super().__init__(parent)
|
|
self._mode = mode
|
|
self._db_type = db_type
|
|
|
|
self.setWindowTitle("Add Column" if mode == "add" else "Rename Column")
|
|
self.setModal(True)
|
|
self.setMinimumWidth(360)
|
|
self._build_ui(column_name)
|
|
|
|
# ── Properties ────────────────────────────────────────────────────────────
|
|
|
|
@property
|
|
def column_name(self) -> str:
|
|
return self._name_edit.text().strip()
|
|
|
|
@property
|
|
def column_type(self) -> str:
|
|
return self._type_combo.currentText().strip()
|
|
|
|
@property
|
|
def nullable(self) -> bool:
|
|
return self._nullable_cb.isChecked()
|
|
|
|
@property
|
|
def default_value(self) -> str:
|
|
return self._default_edit.text().strip()
|
|
|
|
# ── UI ────────────────────────────────────────────────────────────────────
|
|
|
|
def _build_ui(self, column_name: str):
|
|
root = QVBoxLayout(self)
|
|
|
|
form = QFormLayout()
|
|
form.setLabelAlignment(Qt.AlignmentFlag.AlignRight)
|
|
|
|
# Name
|
|
self._name_edit = QLineEdit(column_name)
|
|
self._name_edit.setPlaceholderText("column_name")
|
|
form.addRow("Column name:", self._name_edit)
|
|
|
|
if self._mode == "add":
|
|
# Type
|
|
self._type_combo = QComboBox()
|
|
self._type_combo.setEditable(True)
|
|
self._type_combo.addItems(_COMMON_TYPES)
|
|
self._type_combo.setCurrentText("VARCHAR(255)")
|
|
form.addRow("Data type:", self._type_combo)
|
|
|
|
# Nullable
|
|
self._nullable_cb = QCheckBox()
|
|
self._nullable_cb.setChecked(True)
|
|
form.addRow("Allow NULL:", self._nullable_cb)
|
|
|
|
# Default
|
|
self._default_edit = QLineEdit()
|
|
self._default_edit.setPlaceholderText("optional default value")
|
|
form.addRow("Default:", self._default_edit)
|
|
else:
|
|
# Stub widgets so properties don't crash in rename mode
|
|
self._type_combo = QComboBox()
|
|
self._nullable_cb = QCheckBox()
|
|
self._nullable_cb.setChecked(True)
|
|
self._default_edit = QLineEdit()
|
|
|
|
root.addLayout(form)
|
|
|
|
if self._mode == "add":
|
|
note = QLabel(
|
|
"<i>Changes are applied immediately via ALTER TABLE.</i>"
|
|
)
|
|
note.setWordWrap(True)
|
|
root.addWidget(note)
|
|
|
|
# Buttons
|
|
btns = QDialogButtonBox(
|
|
QDialogButtonBox.StandardButton.Ok |
|
|
QDialogButtonBox.StandardButton.Cancel
|
|
)
|
|
btns.accepted.connect(self._accept)
|
|
btns.rejected.connect(self.reject)
|
|
root.addWidget(btns)
|
|
|
|
def _accept(self):
|
|
if not self.column_name:
|
|
self._name_edit.setFocus()
|
|
return
|
|
if self._mode == "add" and not self.column_type:
|
|
self._type_combo.setFocus()
|
|
return
|
|
self.accept()
|