![JAR search and dependency download from the Maven repository](/logo.png)
com.viaoa.ds.jdbc.delegate.InsertDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oa-core Show documentation
Show all versions of oa-core Show documentation
Object Automation library
/* 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.ds.jdbc.delegate;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import com.viaoa.ds.jdbc.*;
import com.viaoa.ds.jdbc.db.*;
import com.viaoa.object.*;
/**
* 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) {
Class c = clazz.getSuperclass();
if (c != null && !c.equals(OAObject.class)) {
insert(ds, oaObj, c, bIncludeRefereces);
}
Column columnSkip = null;
Table table = ds.getDatabase().getTable(clazz);
if (table != null) {
Column[] columns = table.getColumns();
for (int i=0; columns != null && i < columns.length; i++) {
if (ds.getDBMetaData().supportsAutoAssign && columns[i].assignNextNumber && columns[i].assignedByDatabase) {
columnSkip = columns[i];
break;
}
}
}
Object[] objs = null;
try {
objs = getInsertSQL(ds, oaObj, clazz, columnSkip, bIncludeRefereces);
Object[] params = null;
Vector v = (Vector) objs[1];
if (v != null) {
int x = v.size();
params = new Object[x];
for (int i=0; i vecValue = null;
String value;
DBMetaData dbmd = ds.getDBMetaData();
for (int i=0; columns != null && i < columns.length; i++) {
Column column = columns[i];
if (column == columnSkip) continue;
if (column.propertyName == null || column.propertyName.length() == 0) continue;
if (column.readOnly) continue;
Object obj = oaObj.getProperty(column.propertyName);
// see if column needs to be assigned to a seq number
// support for DB generated keys
if (column.primaryKey) {
boolean b = false;
if (obj == null) b = true;
else if (obj instanceof Number) {
if (((Number) obj).intValue() == 0) b = true;
else {
// this will make sure that the assigned number does not mess up the nextNumber generator
AutonumberDelegate.verifyNumberUsed(ds, oaObj, table, column, ((Number) obj).intValue());
}
}
if (b) {
if (column.assignedByDatabase && dbmd.supportsAutoAssign) continue; // generated by DB
AutonumberDelegate.assignNumber(ds, oaObj, table, column);
obj = oaObj.getProperty(column.propertyName);
}
}
boolean bNull = (obj == null);
// 20100514
boolean byteArray = (obj instanceof byte[]);
if (byteArray) {
if (vecValue == null) vecValue = new Vector(3,3);
vecValue.addElement(obj);
value = "?";
}
else {
boolean bOver512 = (obj instanceof String && ((String)obj).length() > 512);
// this will convert to SQL string
value = ConverterDelegate.convertToString(dbmd, obj, !bOver512, Delegate.getMaxLength(column), column.decimalPlaces, column);
String origValue = value;
if (value != null && bOver512) {
if (vecValue == null) vecValue = new Vector
© 2015 - 2025 Weber Informatics LLC | Privacy Policy