use of easik.database.types.Int in project fql by CategoricalData.
the class RowEntryDialog method getOptions.
/**
* @return
*/
@Override
public List<Option> getOptions() {
final List<Option> options = new LinkedList<>();
if (attsToTypes.isEmpty() && foreignKeys.isEmpty()) {
if (getTitle().startsWith("Update")) {
options.add(new Option("<html><i>(No attributes or outgoing edges)</i><br>Cannot update foreign keys involved in constraint."));
} else {
options.add(new Option("<html><i>(No attributes or outgoing edges)</i><br>Press OK to add a new row."));
}
setSize(300, 145);
return options;
}
for (final String name : attsToTypes.keySet()) {
final EasikType type = attsToTypes.get(name);
final JComponent c;
// else
if (type instanceof easik.database.types.Time) {
c = JUtils.fixHeight(new TimeChooser());
} else // else if (type instanceof easik.database.types.Timestamp)
// {
// c = JUtils.fixHeight(new DateTimeChooser());
// }
// else allow any input
{
c = JUtils.textField("");
c.setToolTipText(type.toString());
if (defaults.keySet().contains(name)) {
((JTextField) c).setText(defaults.get(name));
}
}
options.add(new Option("<html><b>" + name + "</b>", c));
dialogRows.add(new DialogRow(c, name, type));
}
for (final String fkName : foreignKeys.keySet()) {
// Grab target table name for "type"
final String targetType = foreignKeys.get(fkName).getName();
final JTextField idDisplay = JUtils.textField(5);
if (defaults.keySet().contains(fkName)) {
idDisplay.setText(defaults.get(fkName));
}
idDisplay.setEditable(false);
final JButton b = new JButton("Choose " + targetType);
b.addActionListener(new ForeignKeyListener(foreignKeys.get(fkName), idDisplay));
options.add(new Option("<html><b>" + fkName + "</b>", idDisplay, b));
dialogRows.add(new DialogRow(idDisplay, fkName, new Int()));
}
if (options.size() < 5) {
setSize(465, 275);
}
return options;
}
use of easik.database.types.Int in project fql by CategoricalData.
the class JDBCUpdateMonitor method deleteFrom.
/**
* Trys to delete from a given table. If the action will break a constraint,
* it is aborted and the user is notified.
*
* @param table
* The table from which we will attempt the delete
* @return The success of the delete
*/
@Override
public boolean deleteFrom(final EntityNode table) {
final int[] selectedPKs = DatabaseUtil.selectRowPKs(table.getMModel().getFrame(), table);
// if there was a selection
if (selectedPKs.length > 0) {
final String PKcolumn = cn.tablePK(table);
final StringBuilder sb = new StringBuilder("DELETE FROM " + dbd.quoteId(table.getName()) + " WHERE ");
// populate input set for prepared statement while adding column
// names
final Set<ColumnEntry> input = new LinkedHashSet<>(selectedPKs.length);
for (final int pk : selectedPKs) {
for (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> ea : table.getHiddenEntityAttributes()) {
try {
ResultSet result = dbd.executeQuery("SELECT * FROM " + table.getName() + " Where id=" + pk + " AND " + ea.getName() + "= 1");
// we do this by .isBeforeFirst()
if (result.isBeforeFirst()) {
JOptionPane.showMessageDialog(null, "Unable to execute DELETE: Deleting row will cause constraint inconsistency");
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sb.append(PKcolumn).append("=? OR ");
input.add(new ColumnEntry(PKcolumn, Integer.toString(pk), new Int()));
}
// remove last ',OR '
sb.delete(sb.length() - 4, sb.length());
try {
dbd.executePreparedUpdate(sb.toString(), input);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Unable to execute DELETE: " + e.getMessage());
}
}
return true;
}
use of easik.database.types.Int in project fql by CategoricalData.
the class JDBCViewUpdateMonitor method deleteFrom.
/**
* Trys to delete from a given table. If the action will break a constraint,
* it is aborted and the user is notified.
*
* @param table
* The table from which we will attempt the delete
* @return The success of the delete
*/
@Override
public boolean deleteFrom(final EntityNode table) {
final int[] selectedPKs = DatabaseUtil.selectRowPKs(table.getMModel().getFrame(), table);
// if there was a selection
if (selectedPKs.length > 0) {
final String PKcolumn = cn.tablePK(table);
final StringBuilder sb = new StringBuilder("DELETE FROM " + dbd.quoteId(table.getName()) + " WHERE ");
// populate input set for prepared statement while adding column
// names
final Set<ColumnEntry> input = new LinkedHashSet<>(selectedPKs.length);
for (final int pk : selectedPKs) {
sb.append(PKcolumn).append("=? OR ");
input.add(new ColumnEntry(PKcolumn, Integer.toString(pk), new Int()));
}
// remove last ',OR '
sb.delete(sb.length() - 4, sb.length());
try {
dbd.executePreparedUpdate(sb.toString(), input);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Unable to execute DELETE: " + e.getMessage());
}
}
return true;
}
use of easik.database.types.Int in project fql by CategoricalData.
the class JDBCViewUpdateMonitor method insert.
/**
* Determines if insertion into a given table requires special handling due
* to constraints it may be in. As of now, special cases that may result
* from being in multiple constraints are not supported.
*
* @param table
* The table into which we wish to insert data
* @return Success of the insertion
*/
@Override
public boolean insert(final EntityNode table) {
final DialogOptions dOpts = getDialogOptions(table);
final String lineSep = EasikTools.systemLineSeparator();
// a set of column-value pairs of which we wish to force a specific
// value, leaving the user out
final Set<ColumnEntry> forced = new HashSet<>(10);
// contstraint. Tighten up?
for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : table.getConstraints()) {
if (c instanceof SumConstraint) {
// of its foreign key, so remove it from the dialog's selection
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
if (sp.getDomain() == table) {
// we force the value 0 to avoid out driver to kick back
// an error for having a null fKey
final String columnName = cn.tableFK(sp.getFirstEdge());
dOpts.fKeys.remove(columnName);
forced.add(new ColumnEntry(columnName, "0", new Int()));
break;
}
}
}
if (c instanceof CommutativeDiagram) {
// commute
if (c.getPaths().get(0).getDomain() == table) {
JOptionPane.showMessageDialog(null, "Be sure that the following paths commute:" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram", JOptionPane.INFORMATION_MESSAGE);
try {
return promptAndInsert(table, dOpts, forced);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Not all of the following paths commute -- insert aborted!" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram failure", JOptionPane.ERROR_MESSAGE);
}
}
}
if (c instanceof PullbackConstraint) {
// happens, we want to let the user update the new record
if (((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getTarget() != table) {
final EntityNode pullback = ((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getSource();
try {
// get row count pre-insert
ResultSet result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
result.next();
final int preRowCount = result.getInt(1);
if (!promptAndInsert(table, dOpts)) {
return false;
}
// get row count post-insert
result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
result.next();
final int postRowCount = result.getInt(1);
// new row (the one with the highest primary ID)
if (postRowCount > preRowCount) {
result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(pullback) + ") FROM " + pullback.getName() + " X");
result.next();
final int pk = result.getInt(1);
if (JOptionPane.showConfirmDialog(null, "New record in pullback table '" + pullback.getName() + "'. Enter column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0) {
updateRow(pullback, pk);
}
}
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Could not execute update: " + e.getMessage());
}
}
}
if (c instanceof ProductConstraint) {
// inserting into the product.
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
if (sp.getCoDomain() == table) {
final EntityNode product = sp.getDomain();
try {
if (!promptAndInsert(table, dOpts)) {
return false;
}
// get the new records from the product. They are
// any record who's fk to our INSERT factor matches
// the primary id of the last insert
ResultSet result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(table) + ") FROM " + table.getName() + " X");
result.next();
final int newPK = result.getInt(1);
result = dbd.executeQuery("SELECT * FROM " + product.getName() + " WHERE " + cn.tableFK(sp.getFirstEdge()) + " = " + newPK);
// get count of new rows as result of INSERT
result.last();
final int newRows = result.getRow();
result.beforeFirst();
if ((newRows > 0) && (JOptionPane.showConfirmDialog(null, newRows + " new rows in product table '" + product.getName() + "'. Insert column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0)) {
while (result.next()) {
updateRow(product, result.getInt(1));
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
return true;
}
}
}
if (c instanceof LimitConstraint) {
// TRIANGLES TODO CF2012 Incomplete
}
}
try {
return promptAndInsert(table, dOpts, forced);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Could not execute update: " + e.getMessage());
return false;
}
}
use of easik.database.types.Int in project fql by CategoricalData.
the class AttributeUI method getTabs.
/**
* Returns the tabs for the page; we separate the options into rough
* categories: numeric, string, date/time, and other (for boolean/custom).
*
* @return
*/
@Override
public List<OptionTab> getTabs() {
LinkedList<OptionTab> tabs = new LinkedList<>();
EasikType currentType = (_att != null) ? _att.getType() : null;
JPanel pInts = new JPanel(), pFloats = new JPanel(), pChars = new JPanel(), pTS = new JPanel(), pCustom = new JPanel();
pInts.setLayout(new BoxLayout(pInts, BoxLayout.Y_AXIS));
pFloats.setLayout(new BoxLayout(pFloats, BoxLayout.Y_AXIS));
pChars.setLayout(new BoxLayout(pChars, BoxLayout.Y_AXIS));
pTS.setLayout(new BoxLayout(pTS, BoxLayout.Y_AXIS));
pCustom.setLayout(new BoxLayout(pCustom, BoxLayout.Y_AXIS));
OptionTab numeric = new OptionTab("Numeric");
OptionTab text = new OptionTab("Character/data");
OptionTab datetime = new OptionTab("Date/time");
OptionTab other = new OptionTab("Other");
types = new ButtonGroup();
types.add(tInt = new JRadioButton("INTEGER"));
tInt.setToolTipText("An integer value field (usually a 32-bit int) that stores integer values from -2147483648 to 2147483647");
if (currentType instanceof Int) {
tInt.setSelected(true);
numeric.setInitial(true);
}
types.add(tSmallInt = new JRadioButton("SMALLINT"));
tSmallInt.setToolTipText("An integer value field (usually a 16-bit int) that stores integer values from (at least) -32768 to 32767");
if (currentType instanceof SmallInt) {
tSmallInt.setSelected(true);
numeric.setInitial(true);
}
types.add(tBigInt = new JRadioButton("BIGINT"));
tBigInt.setToolTipText("<html>An integer value field (usually a 64-bit int) that stores integer values from -9223372036854775808<br>to 9223372036854775807");
if (currentType instanceof BigInt) {
tBigInt.setSelected(true);
numeric.setInitial(true);
}
pInts.add(tInt);
pInts.add(tSmallInt);
pInts.add(tBigInt);
numeric.addOption(new Option("Integers", pInts));
types.add(tDouble = new JRadioButton("DOUBLE PRECISION"));
tDouble.setToolTipText("<html>A floating point value with at least 15 digits of precision (typically a standard 64-bit<br>floating-point value with 53 bits of precision).");
if (currentType instanceof DoublePrecision) {
tDouble.setSelected(true);
numeric.setInitial(true);
}
types.add(tFloat = new JRadioButton("FLOAT"));
tFloat.setToolTipText("<html>A floating point value with at least 6 digits of precision (typically a standard 32-bit<br>floating-point value with 24 bits of precision). This is sometimes known as a REAL, but a REAL is<br>also sometimes an alias for a DOUBLE PRECISION.");
if (currentType instanceof easik.database.types.Float) {
tFloat.setSelected(true);
numeric.setInitial(true);
}
pFloats.add(tDouble);
pFloats.add(tFloat);
numeric.addOption(new Option("Floating-point", pFloats));
types.add(tDecimal = new JRadioButton("NUMERIC"));
// We allow a max precision of 38, since that seems to be the lowest
// maximum (in MS SQL and Oracle)
// Pg supports up to 1000, and MySQL supports up to 63.
// *Any* default here doesn't make much sense; 10,2 is an arbitrary
// choice.
// This default allows anything from 0.00 to 99,999,999.99
int defPrec = 10, defScale = 2;
if (currentType instanceof Decimal) {
tDecimal.setSelected(true);
numeric.setInitial(true);
Decimal d = (Decimal) currentType;
defPrec = d.getPrecision();
defScale = d.getScale();
}
_decPrec = new JSpinner(new SpinnerNumberModel(defPrec, 1, 38, 1));
// The scale limits, for most databases, are 0 <= scale <= precision.
// Oracle, however
// permits the scale to be larger than the precision. We don't.
_decScale = new JSpinner(new SpinnerNumberModel(defScale, 0, 38, 1));
// Add a change listener to update the scale maximum to the current
// precision value:
_decPrec.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int scaleMax = ((SpinnerNumberModel) _decPrec.getModel()).getNumber().intValue();
((SpinnerNumberModel) _decScale.getModel()).setMaximum(scaleMax);
if (((SpinnerNumberModel) _decScale.getModel()).getNumber().intValue() > scaleMax) {
_decScale.setValue(new Integer(scaleMax));
}
}
});
JPanel decPanel = new JPanel();
decPanel.setLayout(new BoxLayout(decPanel, BoxLayout.X_AXIS));
decPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
decPanel.add(tDecimal);
decPanel.add(new JLabel("(Precision: "));
decPanel.add(JUtils.fixWidth(JUtils.fixHeight(_decPrec)));
decPanel.add(new JLabel(", Scale: "));
decPanel.add(JUtils.fixWidth(JUtils.fixHeight(_decScale)));
decPanel.add(new JLabel(")"));
decPanel.setToolTipText("<html>A fixed-point numeric type. Also known as DECIMAL.<br><br>This type is substantially slower than integer and floating-point types, but guarantees precision<br>for the range of values it supports. The \"precision\" value is the total number of digits storable, <br>and the \"scale\" is the number of digits stored after the decimal point. '12345.67' has precision 7<br>and scale 2.");
tDecimal.setToolTipText(decPanel.getToolTipText());
numeric.addOption(new Option("Fixed-point", decPanel));
types.add(tVarchar = new JRadioButton("VARCHAR"));
int vcs = 255;
if (currentType instanceof Varchar) {
tVarchar.setSelected(true);
text.setInitial(true);
vcs = ((Varchar) currentType).getSize();
}
_varcharSize = new JSpinner(new SpinnerNumberModel(vcs, 1, 255, 1));
JPanel vcPanel = new JPanel();
vcPanel.setLayout(new BoxLayout(vcPanel, BoxLayout.X_AXIS));
vcPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
vcPanel.add(tVarchar);
vcPanel.add(new JLabel("Size: "));
vcPanel.add(JUtils.fixHeight(JUtils.fixWidth(_varcharSize)));
vcPanel.setToolTipText("<html>Stores a string of characters of up to \"size\" characters. Unlike a CHAR, a VARCHAR column is<br>typically stored using the minimum storage space required, while a CHAR field pads shorter strings<br>to always store values of \"size\" length.");
tVarchar.setToolTipText(vcPanel.getToolTipText());
pChars.add(vcPanel);
types.add(tChar = new JRadioButton("CHAR"));
int cs = 255;
if (currentType instanceof Char) {
tChar.setSelected(true);
text.setInitial(true);
cs = ((Char) currentType).getSize();
}
_charSize = new JSpinner(new SpinnerNumberModel(cs, 1, 255, 1));
JPanel cPanel = new JPanel();
cPanel.setLayout(new BoxLayout(cPanel, BoxLayout.X_AXIS));
cPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
cPanel.add(tChar);
cPanel.add(new JLabel("Size: "));
cPanel.add(JUtils.fixHeight(JUtils.fixWidth(_charSize)));
cPanel.setToolTipText("<html>Stores a string of characters of up to \"size\" characters. Unlike a VARCHAR, a CHAR column is<br>typically padded up to the specified size to make it a fixed-width column (the padding is removed on<br>retrieval). Note that some databases implicitly convert CHAR columns to VARCHAR if other<br>variable-width columns exist in the table.");
tChar.setToolTipText(cPanel.getToolTipText());
pChars.add(cPanel);
text.addOption(new Option("Characters", pChars));
types.add(tText = new JRadioButton("TEXT (stores up to 4GB)"));
tText.setToolTipText("Stores large amounts of text data. Also known as a CLOB.");
if (currentType instanceof Text) {
tText.setSelected(true);
text.setInitial(true);
}
text.addOption(new Option("Text data", tText));
types.add(tBlob = new JRadioButton("BLOB (stores up to 4GB)"));
tBlob.setToolTipText("Stores large amounts of binary data (bytes). Will result in a BYTEA under PostgreSQL.");
if (currentType instanceof Blob) {
tBlob.setSelected(true);
text.setInitial(true);
}
text.addOption(new Option("Binary data", tBlob));
types.add(tDate = new JRadioButton("DATE"));
tDate.setToolTipText("A date field that does not include a time, such as '2008/07/14'");
if (currentType instanceof Date) {
tDate.setSelected(true);
datetime.setInitial(true);
}
datetime.addOption(new Option("Date only", tDate));
types.add(tTime = new JRadioButton("TIME"));
tTime.setToolTipText("A field that stores just a time (e.g. '12:13:14')");
if (currentType instanceof Time) {
tTime.setSelected(true);
datetime.setInitial(true);
}
datetime.addOption(new Option("Time only", tTime));
types.add(tTimestamp = new JRadioButton("TIMESTAMP (date and time)"));
tTimestamp.setToolTipText("<html>A field that stores a date and time (e.g. '2008/07/14 12:13:14'). Note that this is converted to a<br>DATETIME when using MySQL.");
if (currentType instanceof Timestamp) {
tTimestamp.setSelected(true);
datetime.setInitial(true);
}
pTS.add(tTimestamp);
datetime.addOption(new Option("Date & time", pTS));
types.add(tBoolean = new JRadioButton("BOOLEAN"));
tBoolean.setToolTipText("<html>A column that stores true/false values. Note that this type may be converted to a small integer<br>type by databases (such as MySQL) that do not fully support BOOLEAN data types");
if (currentType instanceof easik.database.types.Boolean) {
tBoolean.setSelected(true);
other.setInitial(true);
}
other.addOption(new Option("Boolean", tBoolean));
types.add(tCustom = new JRadioButton("Custom type:"));
if (currentType instanceof Custom) {
tCustom.setSelected(true);
other.setInitial(true);
_custom = JUtils.textField(((Custom) currentType).getCustom());
} else {
_custom = JUtils.textField("");
}
JPanel custPanel = new JPanel();
custPanel.setLayout(new BoxLayout(custPanel, BoxLayout.X_AXIS));
custPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
custPanel.setToolTipText("<html>Any SQL type supported by the SQL db that will be exported to can be entered here. No<br>verification of this field is performed: you must ensure that what you specify here is a valid type<br>for the SQL db type you will be using!");
tCustom.setToolTipText(custPanel.getToolTipText());
_custom.setToolTipText(custPanel.getToolTipText());
custPanel.add(tCustom);
custPanel.add(_custom);
other.addOption(new Option("Custom:", custPanel));
tabs.add(numeric);
tabs.add(text);
tabs.add(datetime);
tabs.add(other);
return tabs;
}
Aggregations