/*
|
* BaseLink - Generic object relational mapping
|
* Copyright (C) 2024 Ulrich Hilger, http://uhilger.de
|
*
|
* This program is free software: you can redistribute it and/or modify
|
* it under the terms of the GNU General Public License as published by
|
* the Free Software Foundation, either version 3 of the License, or
|
* (at your option) any later version.
|
*
|
* This program is distributed in the hope that it will be useful,
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* GNU General Public License for more details.
|
*
|
* You should have received a copy of the GNU General Public License
|
* along with this program. If not, see http://www.gnu.org/licenses/
|
*/
|
package de.uhilger.baselink;
|
|
import java.sql.Connection;
|
import java.sql.PreparedStatement;
|
import java.sql.SQLException;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
|
/**
|
* Methoden zum Ausfuehren von SQL INSERT-Anweisungen
|
*
|
* @author Copyright (c) Ulrich Hilger, <a href="http://uhilger.de">http://uhilger.de</a>
|
* @author Published under the terms and conditions of
|
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
|
* @version 1, January 22, 2024
|
*/
|
public class Inserter extends DBActor {
|
|
private static final Logger logger = Logger.getLogger(Inserter.class.getName());
|
|
public Inserter(PersistenceManager pm) {
|
this.pm = pm;
|
}
|
|
/**
|
* Add an object to the database
|
*
|
* <p>Use this method for single inserts. In cases where
|
* several subsequent inserts for objects of the same class
|
* are required the use of method <code>insert(Object, Record)</code>
|
* is recommended instead to minimise instantiation
|
* overhead.</p>
|
*
|
* @param o the object to add
|
* @return the added object
|
*/
|
public Object insert(Object o) {
|
return insert(o, new GenericRecord(o.getClass()));
|
}
|
|
/**
|
* Add an object to the database
|
*
|
* <p>Use this method for single inserts. In cases where
|
* several subsequent inserts for objects of the same class
|
* are required the use of method <code>insert(Connection, Object, Record)</code>
|
* is recommended instead to minimise instantiation
|
* overhead.</p>
|
*
|
* @param c the connection to use, expected to be open and established
|
* @param o the object to add
|
* @return the object added to the database
|
*/
|
public Object insert(Connection c, Object o) {
|
return insert(c, o, new GenericRecord(o.getClass()));
|
}
|
|
/**
|
* Add an object to the database
|
* @param o object to add
|
* @param record reference to object to use to map between object and database
|
* @return the added object
|
*/
|
public Object insert(Object o, Record record) {
|
Connection c = null;
|
try {
|
c = pm.getConnection();
|
o = insert(c, o, record);
|
c.close();
|
c = null;
|
} catch (SQLException ex) {
|
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
} finally {
|
pm.closeConnectionFinally(c);
|
}
|
return o;
|
}
|
|
/**
|
* Add an object to the database
|
* @param c the connection to use, expected to be open and established
|
* @param o object to add
|
* @param record reference to object to use to map between object and database
|
* @return the object that was added
|
*/
|
public Object insert(Connection c, Object o, Record record) {
|
Object addedObject = null;
|
PreparedStatement ps = null;
|
try {
|
ps = record.getInsertStatment(c, o);
|
ps.executeUpdate();
|
ps.close();
|
ps = null;
|
addedObject = o;
|
} catch (Exception ex) {
|
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
} finally {
|
pm.closeStatementFinally(ps);
|
}
|
return addedObject;
|
}
|
|
|
}
|