001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.jackcess; 025 026 import java.io.Closeable; 027 import java.io.File; 028 import java.io.IOException; 029 import java.sql.SQLException; 030 import java.sql.Types; 031 import java.util.List; 032 033 import org.ujmp.core.Coordinates; 034 import org.ujmp.core.Matrix; 035 import org.ujmp.core.exceptions.MatrixException; 036 import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix2D; 037 038 import com.healthmarketscience.jackcess.Column; 039 import com.healthmarketscience.jackcess.ColumnBuilder; 040 import com.healthmarketscience.jackcess.Cursor; 041 import com.healthmarketscience.jackcess.Database; 042 import com.healthmarketscience.jackcess.Table; 043 import com.healthmarketscience.jackcess.TableBuilder; 044 045 public class JackcessDenseObjectMatrix2D extends AbstractDenseObjectMatrix2D implements Closeable { 046 private static final long serialVersionUID = -6342663672866315180L; 047 048 private Database database = null; 049 050 private Table table = null; 051 052 private List<Column> columns = null; 053 054 Cursor cursor = null; 055 056 public JackcessDenseObjectMatrix2D(File file, String tablename) throws IOException { 057 database = Database.open(file); 058 table = database.getTable(tablename); 059 columns = table.getColumns(); 060 cursor = Cursor.createCursor(table); 061 } 062 063 public JackcessDenseObjectMatrix2D(File file, Matrix matrix) throws IOException { 064 this(file, "ujmp-matrix", matrix); 065 } 066 067 public JackcessDenseObjectMatrix2D(File file, String tablename, Matrix matrix) throws IOException { 068 try { 069 database = Database.create(file); 070 071 TableBuilder tb = new TableBuilder(tablename); 072 073 for (int i = 0; i < matrix.getColumnCount(); i++) { 074 ColumnBuilder cb = new ColumnBuilder("Column" + i); 075 switch (matrix.getValueType()) { 076 case DOUBLE: 077 cb.setSQLType(Types.DOUBLE); 078 break; 079 case INT: 080 cb.setSQLType(Types.INTEGER); 081 break; 082 default: 083 cb.setSQLType(Types.VARCHAR); 084 break; 085 } 086 tb.addColumn(cb.toColumn()); 087 } 088 089 table = tb.toTable(database); 090 091 for (int r = 0; r < matrix.getRowCount(); r++) { 092 Object[] data = new Object[(int) matrix.getColumnCount()]; 093 for (int c = 0; c < matrix.getColumnCount(); c++) { 094 data[c] = matrix.getAsObject(r, c); 095 } 096 table.addRow(data); 097 } 098 } catch (SQLException e) { 099 throw new MatrixException(e); 100 } 101 } 102 103 public synchronized Object getObject(long row, long column) throws MatrixException { 104 return getObject((int) row, (int) column); 105 } 106 107 108 public synchronized Object getObject(int row, int column) throws MatrixException { 109 if (columns == null || cursor == null) { 110 return null; 111 } 112 try { 113 Column c = columns.get(column); 114 cursor.reset(); 115 cursor.moveNextRows(row + 1); 116 return cursor.getCurrentRowValue(c); 117 } catch (IOException e) { 118 throw new MatrixException(e); 119 } 120 } 121 122 123 public void setObject(Object value, long row, long column) { 124 } 125 126 127 public void setObject(Object value, int row, int column) { 128 } 129 130 131 public long[] getSize() { 132 if (table == null) { 133 return Coordinates.ZERO2D; 134 } else { 135 return new long[] { table.getRowCount(), table.getColumnCount() }; 136 } 137 } 138 139 public void close() throws IOException { 140 database.close(); 141 } 142 }