alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.persistence.jdbcimpl;

import org.netbeans.mdr.persistence.*;
import org.netbeans.mdr.util.*;

import java.util.*;
import java.io.*;

/**
 * JdbcIndex implements the MDR Index interface using JDBC.
 *
 * @author John V. Sichi
 * @version $Id: JdbcIndex.java,v 1.1 2004/02/04 06:38:11 mmatula Exp $
 */
abstract class JdbcIndex implements Index
{
    protected JdbcStorage storage;
    protected String tableName;
    protected String name;
    protected String keyColName;
    protected String valColName;
    protected Storage.EntryType keyType;
    protected Storage.EntryType valueType;
    protected boolean needSurrogate;

    protected LazyPreparedStatement sqlKeySetIterator;
    protected LazyPreparedStatement sqlKeySetSize;
    protected LazyPreparedStatement sqlKeySetContains;
    protected LazyPreparedStatement sqlInsert;
    protected LazyPreparedStatement sqlDelete;
    protected LazyPreparedStatement sqlFind;

    void init(
        JdbcStorage storage,
        String tableName,
        String name,
        String keyColName,
        String valColName,
        Storage.EntryType keyType,
        Storage.EntryType valueType,
        boolean needSurrogate)
    {
        this.storage = storage;
        this.tableName = tableName;
        this.name = name;
        this.keyColName = keyColName;
        this.valColName = valColName;
        this.keyType = keyType;
        this.valueType = valueType;
        this.needSurrogate = needSurrogate;
        defineSql();
    }

    protected void defineSql()
    {
        if (isKeyUnique()) {
            sqlKeySetIterator = new LazyPreparedStatement(
                "select " + keyColName + " from " + tableName);
            sqlKeySetSize = new LazyPreparedStatement(
                "select count(*) from " + tableName);
        } else {
            sqlKeySetIterator = new LazyPreparedStatement(
                "select distinct " + keyColName
                + " from " + tableName);
            sqlKeySetSize = new LazyPreparedStatement(
                "select count(distinct " + keyColName
                + ") from " + tableName);
        }
        
        sqlKeySetContains = new LazyPreparedStatement(
            "select count(*) from " + tableName + " where "
            + keyColName + " = ?");
        
        sqlInsert = new LazyPreparedStatement(
            "insert into " + tableName
            + " values(?,?"
            + (needSurrogate ? ",?" : "")
            + ")");
        
        sqlDelete = new LazyPreparedStatement(
            "delete from " + tableName
            + " where " + keyColName + " = ?");
        
        sqlFind = new LazyPreparedStatement(
            "select " + valColName + " from " + tableName
            + " where " + keyColName + " = ?");
    }

    protected boolean isKeyUnique()
    {
        return false;
    }

    // implement Index
    public String getName() throws StorageException
    {
        return name;
    }

    // implement Index
    public Storage.EntryType getValueType() throws StorageException
    {
        return valueType;
    }

    // implement Index
    public Storage.EntryType getKeyType() throws StorageException
    {
        return keyType;
    }

    // implement Index
    public Set keySet() throws StorageException
    {
        return new JdbcSet(
            storage,
            getKeyType(),
            sqlKeySetIterator,sqlKeySetSize,sqlKeySetContains);
    }

    // implement Index
    public void add(Object key, Object value) throws StorageException
    {
        addImpl(key,value);
    }

    protected void addImpl(Object key, Object value) throws StorageException
    {
        Object [] args;
        if (needSurrogate) {
            args = new Object[]{key,value,new Long(storage.getSerialNumber())};
        } else {
            args = new Object[]{key,value};
        }
        storage.executeUpdate(sqlInsert,args);
    }
    
    // implement Index
    public boolean remove(Object key) throws StorageException
    {
        return removeImpl(key);
    }

    protected boolean removeImpl(Object key) throws StorageException
    {
        return storage.executeUpdate(sqlDelete,new Object[]{key}) > 0;
    }
}

// End JdbcIndex.java
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.