use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.
the class DatabaseUtil method displayQueryNode.
/**
* @param node
* @param parent
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
public static void displayQueryNode(final QueryNode node, final JFrame parent) {
final JDBCDriver dbd;
final ResultSet result;
final ResultSetMetaData rsmd;
final Vector<Object[]> data;
final JTable asTable;
final String[] columnNames;
final int cols;
try {
dbd = node.getMModel().getSketch().getDatabase().getJDBCDriver();
result = dbd.executeQuery(node.getQuery());
rsmd = result.getMetaData();
cols = rsmd.getColumnCount();
// build 2D array of data
data = new Vector();
columnNames = new String[cols];
int currRow, i;
// Populate array of column names
for (i = 0; i < cols; i++) {
columnNames[i] = rsmd.getColumnName(i + 1);
}
while (result.next()) {
currRow = result.getRow();
// if we have no rows, break
if (currRow == 0) {
break;
}
final Object[] row = new Object[cols];
for (i = 0; i < cols; i++) {
row[i] = result.getString(columnNames[i]);
}
data.add(row);
}
final Object[][] dataObj = data.toArray(new Object[0][0]);
asTable = new JTable(dataObj, columnNames);
new SelectDataDialog(parent, node.getName(), asTable);
} catch (SQLException e) {
JOptionPane.showMessageDialog(node.getMModel().getSketch().getFrame(), "Could not get record: " + e.getMessage());
}
}
use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.
the class DatabaseUtil method getRecord.
/**
* Returns a Map of column names to their value for a given table/primary
* key pair. If no record exists in the table with the given primary key, we
* return an empty Map. Can throw a Database.LoadException or SQLException
* in the event that a db driver can not be acquired.
*
* @param table
* The table from which we get the requested row.
* @param pk
* The primary id of the row which we want
* @return A mapping of column names to their corresponding value for the
* given table/id pair. If no such row exists, returns an empty Map.
*/
public static Map<String, String> getRecord(final EntityNode table, final int pk) {
final JDBCDriver dbd;
final ColumnNaming cn;
final Map<String, String> data = new HashMap<>(20);
final ResultSet result;
try {
dbd = table.getMModel().getDatabase().getJDBCDriver();
cn = new ColumnNaming(dbd);
final String select = "SELECT * FROM " + dbd.quoteId(table.getName()) + " WHERE " + cn.tablePK(table) + " = " + pk;
result = dbd.executeQuery(select);
final ResultSetMetaData rsmd = result.getMetaData();
result.next();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
data.put(dbd.quoteId(rsmd.getColumnName(i)), result.getString(i));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(table.getMModel().getFrame(), "Could not get record: " + e.getMessage());
}
return data;
}
use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.
the class ExecPreparedInsertAction method actionPerformed.
/**
* Create the new entity and set up its name
*
* @param e
* The action event
*/
@Override
public void actionPerformed(ActionEvent e) {
Object[] currentSelection = _theSketch.getSelectionCells();
if (!(currentSelection[0] instanceof EntityNode)) {
return;
}
EntityNode table = (EntityNode) currentSelection[0];
JDBCDriver dbd = null;
dbd = _theSketch.getDatabase().getJDBCDriver();
if (dbd == null) {
// The user hit Cancel
return;
}
FreeQueryDialog afqd;
String text = "INSERT INTO " + table.getName() + "() VALUES()";
while (true) {
afqd = new FreeQueryDialog(_theSketch.getFrame(), text);
if (!afqd.isAccepted()) {
return;
}
try {
String input = afqd.getInput();
dbd.executeUpdate(input);
return;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
text = afqd.getInput();
continue;
}
}
}
use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.
the class ExecPreparedDeleteAction method actionPerformed.
/**
* Create the new entity and set up its name
*
* @param e
* The action event
*/
@Override
public void actionPerformed(ActionEvent e) {
Object[] currentSelection = _theSketch.getSelectionCells();
if (!(currentSelection[0] instanceof EntityNode)) {
return;
}
EntityNode table = (EntityNode) currentSelection[0];
JDBCDriver dbd = null;
dbd = _theSketch.getDatabase().getJDBCDriver();
if (dbd == null) {
// The user hit Cancel
return;
}
FreeQueryDialog afqd;
String text = "DELETE FROM " + table.getName() + "\n WHERE()";
while (true) {
afqd = new FreeQueryDialog(_theSketch.getFrame(), text);
if (!afqd.isAccepted()) {
return;
}
try {
String input = afqd.getInput();
dbd.executeUpdate(input);
return;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
text = afqd.getInput();
continue;
}
}
}
use of easik.database.api.jdbc.JDBCDriver in project fql by CategoricalData.
the class DatabaseUtil method selectRowPKs.
/**
* Pops up a dialog for the user to select records from the table
* represented by the given entity node. The primary IDs of the records are
* returned.
*
* @param frame
* the JFrame to attach the dialog to
* @param table
* The entity node representing the table from which we wish to
* select the records
* @return An array of primary IDs representing the records we selected
*/
public static int[] selectRowPKs(final JFrame frame, final EntityNode table) {
@SuppressWarnings("unused") final JDBCDriver dbd;
final JTable tableData;
try {
dbd = table.getMModel().getDatabase().getJDBCDriver();
tableData = getTable(table);
} catch (PersistenceDriver.LoadException e) {
System.err.println("Error in DatabaseUtil: " + e.getMessage());
return null;
} catch (SQLException e) {
System.err.println("Error in DatabaseUtil: " + e.getMessage());
return null;
}
final SelectDataDialog sdd = new SelectDataDialog(frame, table.getName(), tableData);
if (sdd.isAccepted()) {
return sdd.getSelectedPKs();
}
return new int[0];
}
Aggregations