All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.ds.jdbc.delegate.VerifyDelegate Maven / Gradle / Ivy

There is a newer version: 3.7.10
Show newest version
/*  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.sql.*;
import java.util.ArrayList;
import java.util.logging.Logger;

import com.viaoa.ds.jdbc.db.*;
import com.viaoa.ds.jdbc.*;
import com.viaoa.util.OAString;

/**
 * Methods to validate that OAObject database metadata matches database scheme.  
 * @author vvia
 *
 */
public class VerifyDelegate {
    private static Logger LOG = Logger.getLogger(VerifyDelegate.class.getName());
    OADataSourceJDBC ds;
    
    public static boolean verify(OADataSourceJDBC ds) throws Exception {
        final ArrayList alError = new ArrayList<>();
        Connection connection = null;
        try {
            connection = ds.getConnection(true);
            boolean b = _verify(ds, connection, alError);

            for (String s : alError) {
                System.out.println(s);
            }
            return b;
        }
        catch (Exception e) {
            return false;
        }
        finally {
            if (connection != null) {
                ds.releaseConnection(connection);
            }
        }
    }
    /** 
	    Verifies Tables, Columns and Indexes.  Prints to console window.
	    @returns true if all tables, columns and indexes exist, else returns false if any are missing.
	*/
	private static boolean _verify(OADataSourceJDBC ds, Connection connection, final ArrayList alError) throws Exception {
	    DatabaseMetaData dbmd = connection.getMetaData();
	    ResultSet rs;
	    boolean bResult = true;
	    Table[] tables = ds.getDatabase().getTables();
	    for (int i=0; i iSize) {
		    	        			String s = DDLDelegate.getAlterColumnSQL(ds.getDBMetaData(), t.name, c.columnName, "VARCHAR(" + c.maxLength + ")");
		    	        	        ds.execute(s);
		    	        	        LOG.warning("-- resized Column: "+t.name+"."+c.columnName+" from " + iSize + " to " + c.maxLength+ " : " + s);
		    	        		}
		    	        		else {
		    	        			c.maxLength = iSize;
		    	        			LOG.warning("-- will use column size of " + c.maxLength);
		    	        		}
		    	        		*/
		    	        		continue;
	    	        		}
	    	        	}
	    	        }
	    	        else {
	    	        	b = false;
	    	        	// see if CLOB or LONGVARCHAR match
	    	        	if (iType == java.sql.Types.LONGVARCHAR || iType == java.sql.Types.CLOB) {
		    	        	if (c.getSqlType() == java.sql.Types.LONGVARCHAR || c.getSqlType() == java.sql.Types.CLOB) {
		    	        		b = true;
		    	        	}
	    	        	}
                        if (!b && iType == java.sql.Types.VARCHAR && dbx != null && dbx.databaseType == dbx.SQLSERVER) {
                            if (c.getSqlType() == java.sql.Types.CLOB && iSize > Math.pow(10, 9)) {
                                b = true;
                            }
                        }
                        if (!b && iType == java.sql.Types.VARBINARY && dbx != null && dbx.databaseType == dbx.SQLSERVER) {
                            if (c.getSqlType() == java.sql.Types.BLOB) {
                                b = true;
                            }
                        }
                        
	    	        	if (iType == java.sql.Types.BIT || iType == java.sql.Types.BOOLEAN) {
		    	        	if (c.getSqlType() == java.sql.Types.BIT || c.getSqlType() == java.sql.Types.BOOLEAN) {
		    	        		b = true;
		    	        	}
	    	        	}
	    	        	if (iType == java.sql.Types.SMALLINT || iType == java.sql.Types.BOOLEAN || iType == java.sql.Types.CHAR || iType == java.sql.Types.BIT) {
		    	        	if (c.getSqlType() == java.sql.Types.BIT || c.getSqlType() == java.sql.Types.BOOLEAN) {
		    	        		b = true;
		    	        	}
	    	        	}
	    	        	if (!b && (iType == java.sql.Types.REAL || iType == java.sql.Types.DOUBLE)) {
		    	        	if (c.getSqlType() == java.sql.Types.REAL || c.getSqlType() == java.sql.Types.DOUBLE) {
		    	        		b = true;
		    	        	}
	    	        	}
	    	        	if (!b && (iType == java.sql.Types.TIMESTAMP)) {
		    	        	if (c.getSqlType() == java.sql.Types.DATE || c.getSqlType() == java.sql.Types.TIME) {
		    	        		b = true;
		    	        	}
	    	        	}
	    	        	if (!b) {
	    	        	    String s = "DB ERROR: Column TYPE mismatch: "+t.name+"."+c.columnName+" ds:"+c.getSqlType()+" != db:"+iType;
	    	        	    LOG.warning(s);
                            alError.add(s);
	    	        		continue;
	    	        	}
	    	        }
	            }
	            rs.close();
	            if (!b) {
	                bResult = false;
	                String s = "DB ERROR: Column not found: "+t.name+"."+c.columnName;
	                LOG.warning(s);
                    alError.add(s);
	                continue;
	            }
	
	            if (c.columnLowerName != null && c.columnLowerName.length() > 0) {
		            rs = dbmd.getColumns(null,null,t.name.toUpperCase(),c.columnLowerName.toUpperCase());
		            b = rs.next();
		            rs.close();
		            if (!b) {
		                bResult = false;
		                String s = "DB ERROR: Column not found: "+t.name+"."+c.columnLowerName;
		                LOG.warning(s);
	                    alError.add(s);
		                continue;
		            }
	            }
	
	            if (c.primaryKey) {
	            	rs = dbmd.getPrimaryKeys(null, null, t.name.toUpperCase());
		            b = rs.next();
                    String colname=null;
                    String pkname=null;
		            if (b) {
		            	colname = rs.getString(4); // column name
		            	pkname = rs.getString(6); // pk name
	            		b = pkname.equalsIgnoreCase("PK"+t.name);
		            }	            	
		            rs.close();
		            if (!b) {
		                String s = "DB ERROR: PK missing: "+" PK" + t.name+" "+t.name+"."+c.columnName + " - FOUND: pkName="+pkname+", colname="+colname;
		                LOG.warning(s);
                        alError.add(s);
		            }
	            }
	        	// public static String convert(DBMetaData dbmd, Column column, Object value) {
	            try {
	            	ConverterDelegate.convert(ds.getDBMetaData(), c, "1");
	            }
	            catch (Exception e) {
	                String s = "DB ERROR: ConverterDelegate wont be able to convert column type: Table:" + t.name + " Column:" + c.columnName;
	                LOG.warning(s);
                    alError.add(s);
	            	bResult = false;
	            }
	        }
            Index[] indexes = t.getIndexes();

	        for (int j=0; j© 2015 - 2025 Weber Informatics LLC | Privacy Policy