Access Keys:
Skip to content (Access Key - 0)

Release Notes

Getting Started

Concepts

Developer's Guide

Tools and Utilities

Reference

Toggle Sidebar
You are viewing information for a product in a pre-release state. The released product and the documentation may differ significantly from the version described here

ExampleRowLoader

The purpose of the ExampleRowLoader class is to provide sample code that demonstrates RowLoaders working and provide a shell for developing a RowLoader that uses a non-relational archive data store. For relational archive data stores, please see the JDBCRowLoader example.

The ExampleRowLoader is designed to work on a simple SQL table that is defined as follows:

CREATE TABLE LoaderTest 
  (id INTEGER, name CHAR(10), result CHAR(10), primary key (id, name))
  LOADER (example.gemstone.sqlfabric.loader.ExampleRowLoader.create 'init param 1', 2)

You can invoke the ExampleRowLoader using a simple SELECT call. Either of the following will work:

  • SELECT * FROM LoaderTest WHERE id=1 and name='test'
  • SELECT result FROM LoaderTest WHERE id=1 and name='test'

The following is the Java code for ExampleRowLoader. You will need the sqlfabric.jar and gemfire.jar in your classpath to compile and use this loader.

ExampleRowLoader.java

package example.gemstone.sqlfabric.callbacks;

import java.sql.*;
import javax.sql.*;
import javax.sql.rowset.*;

import com.gemstone.gemfire.LogWriter;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.sqlfabric.callbacks.RowLoader;
import com.sun.rowset.CachedRowSetImpl;

public class ExampleRowLoader implements RowLoader{

    private final LogWriter log = CacheFactory.getAnyInstance().getLogger();

    public static ExampleRowLoader create() {
        return new ExampleRowLoader();
    }

    public Object getRow(String schema, String table, Object[] params) {
        log.entering("example.gemstone.sqlfabric.callbacks.ExampleLoader", "init()");
        if (log.infoEnabled()) {
            log.info("ExampleRowLoader invoked to fetch from schema <" + schema + "> on table <" + table + ">.");
            for(int i = 0; i < params.length; i++) {
                log.info("  primary key element " + i + ": " + params[i]);
            }
        }
		
        switch ((Integer)params[0]){
            case 1:
                Object[] result1 = new Object[3];
                result1[0] = params[0];
                result1[1] = params[1];
                result1[2] =  "Success!";	
                return result1;

            case 2:
                ResultSet result2 = simulatedResultSet(params);
                return result2;
			
            default:
                // No entry found.
                return null;
        }
    }

    @Override
    public void init(Object[] arg0) {
        log.entering("example.gemstone.sqlfabric.callbacks.ExampleLoader", "init()");
        if (log.infoEnabled()) {
            log.info("ExampleRowLoader initialized.");
            for (int i = 0; i < arg0.length; i++) {
                log.info("  initialization arg " + i + ": " + arg0[i] );
            }
        }	
    }

    protected ResultSet simulatedResultSet(Object[] params) {
        try {
            CachedRowSet crs = new CachedRowSetImpl();
            crs.setType(RowSet.TYPE_SCROLL_INSENSITIVE);
	
            RowSetMetaDataImpl rsmdi = new RowSetMetaDataImpl();
            rsmdi.setColumnCount(3);
		        
            rsmdi.setColumnName(1, "id");
            rsmdi.setColumnName(2, "name");
            rsmdi.setColumnName(3, "result");

            rsmdi.setNullable(1, ResultSetMetaData.columnNoNulls);
            rsmdi.setNullable(2, ResultSetMetaData.columnNoNulls);        
            rsmdi.setNullable(3, ResultSetMetaData.columnNullable);
	        
            rsmdi.setColumnType(1, Types.INTEGER);
            rsmdi.setColumnType(2, Types.VARCHAR);
            rsmdi.setColumnType(3, Types.VARCHAR);

            crs.setMetaData(rsmdi);

            // ========== add a row
            crs.moveToInsertRow();
            crs.updateInt(1, (Integer) params[0]);
            crs.updateString(2, (String) params[1]);
            crs.updateString(3, "Sucess!");

            crs.insertRow();
            crs.moveToCurrentRow();
            crs.beforeFirst();
   
            return crs;

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
	
    public static void main(String[] args) {	
        ExampleRowLoader loader = ExampleRowLoader.create();
        loader.init(new Object[0]);
        Object[] params = new Object[2];
        params[0] = 2;
        params[1] = "test";
        loader.getRow("Schema","Table", params);	
    }
}

Child Pages

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
Adaptavist Theme Builder (3.4.0-M2-conf210) Powered by Atlassian Confluence 2.10.3, the Enterprise Wiki.