Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* Copyright 1999 Vince Via [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.viaoa.datasource.jdbc.delegate;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Logger;
import com.viaoa.datasource.jdbc.OADataSourceJDBC;
import com.viaoa.datasource.jdbc.db.Column;
import com.viaoa.datasource.jdbc.db.DBMetaData;
import com.viaoa.datasource.jdbc.db.Link;
import com.viaoa.datasource.jdbc.db.Table;
import com.viaoa.object.OAObject;
import com.viaoa.object.OAObjectDSDelegate;
import com.viaoa.object.OAObjectKey;
import com.viaoa.object.OAObjectReflectDelegate;
import com.viaoa.util.OAString;
/**
* Manages inserts for JDBC datasource.
*
* @author vvia
*/
public class InsertDelegate {
private static Logger LOG = Logger.getLogger(InsertDelegate.class.getName());
public static void insertWithoutReferences(OADataSourceJDBC ds, OAObject obj) {
if (obj == null) {
return;
}
insert(ds, obj, obj.getClass(), false);
}
public static void insert(OADataSourceJDBC ds, OAObject object) {
if (object == null) {
return;
}
insert(ds, object, object.getClass(), true);
}
private static void insert(OADataSourceJDBC ds, OAObject oaObj, Class clazz, boolean bIncludeRefereces) {
if (ds.getIgnoreWrites()) {
return;
}
if (ds.getReadOnly()) {
throw new RuntimeException("datasource is set to readOnly=true");
}
Class c = clazz.getSuperclass();
if (c != null && !c.equals(OAObject.class)) {
insert(ds, oaObj, c, bIncludeRefereces);
}
Column columnSkip = null;
if (!ds.getAssignIdOnCreate() && ds.getDBMetaData().supportsAutoAssign) {
Table table = ds.getDatabase().getTable(clazz);
if (table != null) {
Column[] columns = table.getColumns();
for (int i = 0; columns != null && i < columns.length; i++) {
if (columns[i].assignNextNumber) {
if (oaObj.isNull(columns[i].propertyName)) {
columnSkip = columns[i]; // assigned by DS
}
break;
}
}
}
}
Object[] objs = null;
try {
objs = getInsertSQL(ds, oaObj, clazz, columnSkip, bIncludeRefereces);
Object[] params = null;
ArrayList al = (ArrayList) objs[1];
if (al != null) {
int x = al.size();
params = new Object[x];
for (int i = 0; i < x; i++) {
params[i] = al.get(i);
}
}
performInsert(ds, oaObj, (String) objs[0], params, columnSkip);
} catch (Exception e) {
if (objs == null || objs.length == 0) {
objs = new String[] { "no sql generated" };
}
//LOG.log(Level.WARNING, "insert(), sql="+objs[0], e);
throw new RuntimeException("Error on insert, sql=" + objs[0], e);
}
}
private static Object[] getInsertSQL(OADataSourceJDBC ds, OAObject oaObj, Class clazz, Column columnSkip, boolean bIncludeRefereces)
throws Exception {
Table table = ds.getDatabase().getTable(clazz);
if (table == null) {
throw new Exception("cant find table for Class " + clazz.getName());
}
Column[] columns = table.getColumns();
StringBuffer str = new StringBuffer(128);
StringBuffer values = new StringBuffer(128);
ArrayList