Search in sources :

Example 11 with NVPair

use of org.eclipse.scout.rt.platform.holders.NVPair in project scout.rt by eclipse.

the class StatementProcessor method createInput.

private IBindInput createInput(IToken bindToken, Object[] bindBases) {
    if (bindToken instanceof ValueInputToken) {
        final ValueInputToken valueInputToken = (ValueInputToken) bindToken;
        final String[] path = REGEX_DOT.split(valueInputToken.getName());
        IBindInput result = null;
        for (final Object bindBase : bindBases) {
            Class nullType = null;
            if (bindBase instanceof NVPair) {
                nullType = ((NVPair) bindBase).getNullType();
            }
            final IBindInput in = createInputRec(valueInputToken, path, bindBase, nullType);
            if (in != null) {
                // bind found
                if (isBindDuplicateCheckEnabled()) {
                    if (result == null) {
                        // first match found for the bind -> remember
                        result = in;
                    } else {
                        // second match found
                        onDuplicateBind(valueInputToken);
                        return result;
                    }
                } else {
                    // no duplicate check necessary: directly return the first match
                    return in;
                }
            }
        }
        if (result == null) {
            throw new ProcessingException("Cannot find input for '{}' in bind bases.", valueInputToken);
        }
        return result;
    } else if (bindToken instanceof FunctionInputToken) {
        return new FunctionInput(m_callerService, m_bindBases, (FunctionInputToken) bindToken);
    }
    throw new ProcessingException("Cannot find input for {}", bindToken.getClass());
}
Also used : FunctionInputToken(org.eclipse.scout.rt.server.jdbc.parsers.token.FunctionInputToken) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) ValueInputToken(org.eclipse.scout.rt.server.jdbc.parsers.token.ValueInputToken) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 12 with NVPair

use of org.eclipse.scout.rt.platform.holders.NVPair in project scout.rt by eclipse.

the class CsvSqlAdapter method exportData.

/**
 * Export sql data into a file
 *
 * @param params
 */
public void exportData(CsvSettings params) {
    final CsvHelper h = new CsvHelper(params.getContentLocale(), params.getColSeparator(), params.getTextDelimiter(), "\n");
    if (params.getCsvColumnTypes() != null) {
        h.setColumnTypes(params.getCsvColumnTypes());
    }
    if (params.getCsvColumnNames() != null) {
        h.setColumnNames(params.getCsvColumnNames());
    }
    Collection<String> cols = new ArrayList<String>();
    cols.addAll(params.getCsvColumnNames());
    // prepare select statement
    String sqlText;
    Object[] base = null;
    if (params.getSqlSelect() != null) {
        sqlText = params.getSqlSelect();
        base = params.getBindBase();
    } else {
        StringBuilder buf = new StringBuilder();
        buf.append("SELECT ");
        for (Iterator<String> it = cols.iterator(); it.hasNext(); ) {
            String colName = it.next();
            buf.append(colName);
            if (it.hasNext()) {
                buf.append(",");
            }
        }
        buf.append(" FROM ");
        buf.append(params.getTableName());
        if (params.getGroupKeyValue() != null) {
            buf.append(" WHERE ");
            buf.append(params.getGroupKeyColumnName());
            buf.append("=:groupKeyColumnValue");
        }
        if (params.getLineNumberColumnName() != null) {
            buf.append(" ORDER BY ");
            buf.append(params.getLineNumberColumnName());
        }
        sqlText = buf.toString();
        if (params.getGroupKeyValue() != null) {
            base = new Object[1];
            base[0] = new NVPair("groupKeyColumnValue", params.getGroupKeyValue());
        }
    }
    try (FileOutputStream out = new FileOutputStream(params.getFile());
        Writer w = new OutputStreamWriter(out, params.getEncoding())) {
        h.exportHeaderRows(w, params.getWriteColumnNames(), params.getWriteColumnTypes());
        ISelectStreamHandler handler = new ISelectStreamHandler() {

            @Override
            public void handleRow(Connection con, PreparedStatement stm, ResultSet rs, int rowIndex, List<SqlBind> values) {
                Object[] row = new Object[values.size()];
                for (int i = 0; i < row.length; i++) {
                    row[i] = values.get(i).getValue();
                }
                h.exportDataRow(row, w, false);
            }

            @Override
            public void finished(Connection con, PreparedStatement stm, ResultSet rs, int rowCount) {
            // do nothing
            }
        };
        m_sqlService.selectStreaming(sqlText, handler, base);
    } catch (IOException e) {
        throw new ProcessingException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) CsvHelper(org.eclipse.scout.rt.shared.csv.CsvHelper) OutputStreamWriter(java.io.OutputStreamWriter) ISelectStreamHandler(org.eclipse.scout.rt.server.jdbc.ISelectStreamHandler) ArrayList(java.util.ArrayList) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 13 with NVPair

use of org.eclipse.scout.rt.platform.holders.NVPair in project scout.rt by eclipse.

the class BindValueTest method testNullBindWithLongHolder.

@Test
public void testNullBindWithLongHolder() throws Exception {
    m_sqlService.clearProtocol();
    // actual behaviour
    m_sqlService.select("SELECT A FROM T WHERE A = :a", new NVPair("a", new LongHolder()));
    String actual = m_sqlService.getProtocol().toString();
    // expected behaviour
    VerboseMock m = new VerboseMock(new StringBuffer());
    m.log(Connection.class, "prepareStatement", "SELECT A FROM T WHERE A = ?");
    m.log(PreparedStatement.class, "setObject", 1, null, Types.BIGINT);
    m.log(PreparedStatement.class, "executeQuery");
    m.log(ResultSet.class, "getFetchSize");
    m.log(ResultSet.class, "next");
    m.log(ResultSet.class, "close");
    String expected = m.getProtocol().toString();
    // check
    assertEquals(expected, actual);
}
Also used : LongHolder(org.eclipse.scout.rt.platform.holders.LongHolder) VerboseMock(org.eclipse.scout.rt.server.jdbc.fixture.VerboseMock) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) Test(org.junit.Test)

Example 14 with NVPair

use of org.eclipse.scout.rt.platform.holders.NVPair in project scout.rt by eclipse.

the class SelectInputBindTest method testBatchUpdateFromArray.

/**
 * Batch update from an array.
 */
@Test
public void testBatchUpdateFromArray() throws Exception {
    SqlServiceMock sql = createSqlServiceMock();
    Long person = 9L;
    Long[] roles = new Long[] { 5L, 6L };
    sql.update("UDPATE this_table SET v = :value where r = :{roles} and p = :personNr", new NVPair("personNr", person), new NVPair("roles", roles), new NVPair("value", "lorem"));
    assertExpectedProtocol2(sql);
}
Also used : SqlServiceMock(org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) Test(org.junit.Test)

Example 15 with NVPair

use of org.eclipse.scout.rt.platform.holders.NVPair in project scout.rt by eclipse.

the class SelectInputBindTest method testBatchUpdateFromTableFieldBeanDataInNVPair.

/**
 * {@link TableFieldBeanData} is from type {@link ITableBeanHolder} (introduced with Luna). TableData for batch update
 * is in NVPair bind.
 */
@Test
public void testBatchUpdateFromTableFieldBeanDataInNVPair() throws Exception {
    SqlServiceMock sql = createSqlServiceMock();
    TableFieldBeanData tableData = createTableFieldBeanData(false);
    sql.update("UDPATE my_table SET a=:{table.active}, s=:{table.state} where n=:{table.name} ", new NVPair("table", tableData));
    assertExpectedProtocol(sql);
}
Also used : SqlServiceMock(org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) TableFieldBeanData(org.eclipse.scout.rt.server.jdbc.fixture.TableFieldBeanData) Test(org.junit.Test)

Aggregations

NVPair (org.eclipse.scout.rt.platform.holders.NVPair)25 Test (org.junit.Test)19 SqlServiceMock (org.eclipse.scout.rt.server.jdbc.fixture.SqlServiceMock)12 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)5 ITableBeanHolder (org.eclipse.scout.rt.platform.holders.ITableBeanHolder)4 List (java.util.List)3 BeanArrayHolder (org.eclipse.scout.rt.platform.holders.BeanArrayHolder)3 IntegerHolder (org.eclipse.scout.rt.platform.holders.IntegerHolder)3 AbstractSqlService (org.eclipse.scout.rt.server.jdbc.AbstractSqlService)3 TableFieldBeanData (org.eclipse.scout.rt.server.jdbc.fixture.TableFieldBeanData)3 VerboseMock (org.eclipse.scout.rt.server.jdbc.fixture.VerboseMock)3 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Holder (org.eclipse.scout.rt.platform.holders.Holder)2 IBeanArrayHolder (org.eclipse.scout.rt.platform.holders.IBeanArrayHolder)2 IHolder (org.eclipse.scout.rt.platform.holders.IHolder)2 LongHolder (org.eclipse.scout.rt.platform.holders.LongHolder)2 TableBeanHolderFilter (org.eclipse.scout.rt.platform.holders.TableBeanHolderFilter)2 FastPropertyDescriptor (org.eclipse.scout.rt.platform.reflect.FastPropertyDescriptor)2