Search in sources :

Example 11 with FileConnection

use of org.teiid.translator.FileConnection in project teiid by teiid.

the class TestExcelExecution method testExecutionNoDataNumberXLSX.

@Test
public void testExecutionNoDataNumberXLSX() throws Exception {
    String ddl = "CREATE FOREIGN TABLE Sheet1 (\n" + "	ROW_ID integer OPTIONS (SEARCHABLE 'All_Except_Like', \"teiid_excel:CELL_NUMBER\" 'ROW_ID'),\n" + "	column1 string OPTIONS (SEARCHABLE 'Unsearchable', \"teiid_excel:CELL_NUMBER\" '1'),\n" + "	column2 string OPTIONS (SEARCHABLE 'Unsearchable', \"teiid_excel:CELL_NUMBER\" '2'),\n" + "	column3 string OPTIONS (SEARCHABLE 'Unsearchable', \"teiid_excel:CELL_NUMBER\" '3'),\n" + "	CONSTRAINT PK0 PRIMARY KEY(ROW_ID)\n" + ") OPTIONS (\"teiid_excel:FILE\" 'names.xlsx');";
    FileConnection connection = Mockito.mock(FileConnection.class);
    Mockito.stub(connection.getFile("names.xlsx")).toReturn(UnitTestUtil.getTestDataFile("names.xlsx"));
    ArrayList results = helpExecute(ddl, connection, "select * from Sheet1");
    assertEquals("[[1, FirstName, LastName, Age], [2, John, Doe, null], [3, Jane, Smith, 40.0], [4, Matt, Liek, 13.0], [5, Sarah, Byne, 10.0], [6, Rocky, Dog, 3.0]]", results.toString());
}
Also used : ArrayList(java.util.ArrayList) FileConnection(org.teiid.translator.FileConnection) Test(org.junit.Test)

Example 12 with FileConnection

use of org.teiid.translator.FileConnection in project teiid by teiid.

the class TestExcelMetadataProcessor method getDDL.

static String getDDL(Properties props, String filename) throws TranslatorException, ResourceException {
    ExcelExecutionFactory translator = new ExcelExecutionFactory();
    translator.start();
    String xlsName = props.getProperty("importer.excelFileName");
    MetadataFactory mf = new MetadataFactory("vdb", 1, "people", SystemMetadata.getInstance().getRuntimeTypeMap(), props, null);
    FileConnection connection = Mockito.mock(FileConnection.class);
    if (xlsName.contains("*.")) {
        Mockito.stub(connection.getFile(xlsName)).toReturn(UnitTestUtil.getTestDataFile(xlsName));
        File f = Mockito.mock(File.class);
        Mockito.stub(f.isDirectory()).toReturn(true);
        Mockito.stub(f.listFiles()).toReturn(new File[] { UnitTestUtil.getTestDataFile(filename) });
        Mockito.stub(connection.getFile(xlsName)).toReturn(f);
    } else {
        Mockito.stub(connection.getFile(xlsName)).toReturn(UnitTestUtil.getTestDataFile(xlsName));
    }
    translator.getMetadata(mf, connection);
    TransformationMetadata metadata = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "vdb", new FunctionTree("foo", new UDFSource(translator.getPushDownFunctions())));
    ValidatorReport report = new MetadataValidator().validate(metadata.getVdbMetaData(), metadata.getMetadataStore());
    if (report.hasItems()) {
        throw new RuntimeException(report.getFailureMessage());
    }
    String ddl = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    return ddl;
}
Also used : UDFSource(org.teiid.query.function.UDFSource) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) FunctionTree(org.teiid.query.function.FunctionTree) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) MetadataValidator(org.teiid.query.metadata.MetadataValidator) File(java.io.File) ValidatorReport(org.teiid.query.validator.ValidatorReport) FileConnection(org.teiid.translator.FileConnection)

Example 13 with FileConnection

use of org.teiid.translator.FileConnection in project teiid by teiid.

the class FileExecutionFactory method createProcedureExecution.

// @Override
public ProcedureExecution createProcedureExecution(final Call command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Connection conn) throws TranslatorException {
    if (conn instanceof VirtualFileConnection) {
        return new VirtualFileProcedureExecution(command, (VirtualFileConnection) conn);
    }
    final FileConnection fc = (FileConnection) conn;
    if (command.getProcedureName().equalsIgnoreCase(SAVEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                Object file = command.getArguments().get(1).getArgumentValue().getValue();
                if (file == null || filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Saving", filePath);
                InputStream is = null;
                try {
                    if (file instanceof SQLXML) {
                        is = ((SQLXML) file).getBinaryStream();
                    } else if (file instanceof Clob) {
                        is = new ReaderInputStream(((Clob) file).getCharacterStream(), encoding);
                    } else if (file instanceof Blob) {
                        is = ((Blob) file).getBinaryStream();
                    } else {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("unknown_type"));
                    }
                    ObjectConverterUtil.write(is, fc.getFile(filePath));
                } catch (IOException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (SQLException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    } else if (command.getProcedureName().equalsIgnoreCase(DELETEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                if (filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Deleting", filePath);
                try {
                    File f = fc.getFile(filePath);
                    if (!f.exists()) {
                        if (exceptionIfFileNotFound) {
                            // $NON-NLS-1$
                            throw new TranslatorException(DataPlugin.Util.gs("file_not_found", filePath));
                        }
                    } else if (!f.delete()) {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("error_deleting"));
                    }
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_deleting"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    }
    return new FileProcedureExecution(command, fc);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SQLXML(java.sql.SQLXML) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ProcedureExecution(org.teiid.translator.ProcedureExecution) TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Clob(java.sql.Clob) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) VirtualFileConnection(org.teiid.file.VirtualFileConnection) VirtualFileConnection(org.teiid.file.VirtualFileConnection) FileConnection(org.teiid.translator.FileConnection)

Example 14 with FileConnection

use of org.teiid.translator.FileConnection in project teiid by teiid.

the class TestFileConnection method testFileMapping.

@Test
public void testFileMapping() throws Exception {
    FileManagedConnectionFactory fmcf = new FileManagedConnectionFactory();
    fmcf.setParentDirectory("foo");
    fmcf.setFileMapping("x=y,z=a");
    BasicConnectionFactory bcf = fmcf.createConnectionFactory();
    FileConnection fc = (FileConnection) bcf.getConnection();
    File f = fc.getFile("x");
    assertEquals("foo" + File.separator + "y", f.getPath());
    f = fc.getFile("n");
    assertEquals("foo" + File.separator + "n", f.getPath());
}
Also used : BasicConnectionFactory(org.teiid.resource.spi.BasicConnectionFactory) File(java.io.File) FileConnection(org.teiid.translator.FileConnection) Test(org.junit.Test)

Example 15 with FileConnection

use of org.teiid.translator.FileConnection in project teiid by teiid.

the class TestFileConnection method testParentPaths1.

@Test
public void testParentPaths1() throws Exception {
    FileManagedConnectionFactory fmcf = new FileManagedConnectionFactory();
    fmcf.setParentDirectory("foo");
    fmcf.setAllowParentPaths(true);
    BasicConnectionFactory bcf = fmcf.createConnectionFactory();
    FileConnection fc = (FileConnection) bcf.getConnection();
    fc.getFile(".." + File.separator + "x");
}
Also used : BasicConnectionFactory(org.teiid.resource.spi.BasicConnectionFactory) FileConnection(org.teiid.translator.FileConnection) Test(org.junit.Test)

Aggregations

FileConnection (org.teiid.translator.FileConnection)27 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)21 File (java.io.File)6 BasicConnectionFactory (org.teiid.resource.spi.BasicConnectionFactory)3 MetadataFactory (org.teiid.metadata.MetadataFactory)2 ProcedureExecution (org.teiid.translator.ProcedureExecution)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 SQLXML (java.sql.SQLXML)1 Timestamp (java.sql.Timestamp)1 List (java.util.List)1 Properties (java.util.Properties)1 ResourceException (javax.resource.ResourceException)1 VirtualFile (org.jboss.vfs.VirtualFile)1 ReaderInputStream (org.teiid.core.util.ReaderInputStream)1 VirtualFileConnection (org.teiid.file.VirtualFileConnection)1