Search in sources :

Example 11 with Function

use of org.h2.expression.Function in project h2database by h2database.

the class DateTimeFunctions method getIntDatePart.

/**
 * Get the specified field of a date, however with years normalized to positive
 * or negative, and month starting with 1.
 *
 * @param date
 *            the date value
 * @param field
 *            the field type, see {@link Function} for constants
 * @return the value
 */
public static int getIntDatePart(Value date, int field) {
    long[] a = DateTimeUtils.dateAndTimeFromValue(date);
    long dateValue = a[0];
    long timeNanos = a[1];
    switch(field) {
        case YEAR:
            return DateTimeUtils.yearFromDateValue(dateValue);
        case MONTH:
            return DateTimeUtils.monthFromDateValue(dateValue);
        case DAY_OF_MONTH:
            return DateTimeUtils.dayFromDateValue(dateValue);
        case HOUR:
            return (int) (timeNanos / 3_600_000_000_000L % 24);
        case MINUTE:
            return (int) (timeNanos / 60_000_000_000L % 60);
        case SECOND:
            return (int) (timeNanos / 1_000_000_000 % 60);
        case MILLISECOND:
            return (int) (timeNanos / 1_000_000 % 1_000);
        case MICROSECOND:
            return (int) (timeNanos / 1_000 % 1_000_000);
        case NANOSECOND:
            return (int) (timeNanos % 1_000_000_000);
        case DAY_OF_YEAR:
            return DateTimeUtils.getDayOfYear(dateValue);
        case DAY_OF_WEEK:
            return DateTimeUtils.getSundayDayOfWeek(dateValue);
        case WEEK:
            GregorianCalendar gc = DateTimeUtils.getCalendar();
            return DateTimeUtils.getWeekOfYear(dateValue, gc.getFirstDayOfWeek() - 1, gc.getMinimalDaysInFirstWeek());
        case QUARTER:
            return (DateTimeUtils.monthFromDateValue(dateValue) - 1) / 3 + 1;
        case ISO_YEAR:
            return DateTimeUtils.getIsoWeekYear(dateValue);
        case ISO_WEEK:
            return DateTimeUtils.getIsoWeekOfYear(dateValue);
        case ISO_DAY_OF_WEEK:
            return DateTimeUtils.getIsoDayOfWeek(dateValue);
        case TIMEZONE_HOUR:
        case TIMEZONE_MINUTE:
            {
                int offsetMinutes;
                if (date instanceof ValueTimestampTimeZone) {
                    offsetMinutes = ((ValueTimestampTimeZone) date).getTimeZoneOffsetMins();
                } else {
                    offsetMinutes = DateTimeUtils.getTimeZoneOffsetMillis(null, dateValue, timeNanos);
                }
                if (field == TIMEZONE_HOUR) {
                    return offsetMinutes / 60;
                }
                return offsetMinutes % 60;
            }
    }
    throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
}
Also used : GregorianCalendar(java.util.GregorianCalendar) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone)

Example 12 with Function

use of org.h2.expression.Function in project h2database by h2database.

the class BnfSyntax method getLink.

/**
 * Get the HTML link to the given token.
 *
 * @param bnf the BNF
 * @param token the token
 * @return the HTML link
 */
String getLink(Bnf bnf, String token) {
    RuleHead found = null;
    String key = Bnf.getRuleMapKey(token);
    for (int i = 0; i < token.length(); i++) {
        String test = StringUtils.toLowerEnglish(key.substring(i));
        RuleHead r = bnf.getRuleHead(test);
        if (r != null) {
            found = r;
            break;
        }
    }
    if (found == null) {
        return token;
    }
    String page = "grammar.html";
    if (found.getSection().startsWith("Data Types")) {
        page = "datatypes.html";
    } else if (found.getSection().startsWith("Functions")) {
        page = "functions.html";
    } else if (token.equals("@func@")) {
        return "<a href=\"functions.html\">Function</a>";
    } else if (found.getRule() instanceof RuleFixed) {
        found.getRule().accept(this);
        return html;
    }
    String link = found.getTopic().toLowerCase().replace(' ', '_');
    link = page + "#" + StringUtils.urlEncode(link);
    return "<a href=\"" + link + "\">" + token + "</a>";
}
Also used : RuleFixed(org.h2.bnf.RuleFixed) RuleHead(org.h2.bnf.RuleHead)

Example 13 with Function

use of org.h2.expression.Function in project h2database by h2database.

the class JdbcDatabaseMetaData method getFunctions.

private String getFunctions(String section) throws SQLException {
    try {
        checkClosed();
        PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT TOPIC " + "FROM INFORMATION_SCHEMA.HELP WHERE SECTION = ?");
        prep.setString(1, section);
        ResultSet rs = prep.executeQuery();
        StatementBuilder buff = new StatementBuilder();
        while (rs.next()) {
            String s = rs.getString(1).trim();
            String[] array = StringUtils.arraySplit(s, ',', true);
            for (String a : array) {
                buff.appendExceptFirst(",");
                String f = a.trim();
                if (f.indexOf(' ') >= 0) {
                    // remove 'Function' from 'INSERT Function'
                    f = f.substring(0, f.indexOf(' ')).trim();
                }
                buff.append(f);
            }
        }
        rs.close();
        prep.close();
        return buff.toString();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 14 with Function

use of org.h2.expression.Function in project h2database by h2database.

the class TestSpatial method testTableFunctionGeometry.

/**
 * Check that geometry column type is kept with a table function
 */
private void testTableFunctionGeometry() throws SQLException {
    deleteDb("spatial");
    try (Connection conn = getConnection(URL)) {
        Statement stat = conn.createStatement();
        stat.execute("CREATE ALIAS POINT_TABLE FOR \"" + TestSpatial.class.getName() + ".pointTable\"");
        stat.execute("create table test as select * from point_table(1, 1)");
        // Read column type
        ResultSet columnMeta = conn.getMetaData().getColumns(null, null, "TEST", "THE_GEOM");
        assertTrue(columnMeta.next());
        assertEquals("geometry", columnMeta.getString("TYPE_NAME").toLowerCase());
        assertFalse(columnMeta.next());
    }
    deleteDb("spatial");
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 15 with Function

use of org.h2.expression.Function in project ignite by apache.

the class GridSqlQueryParser method parseGroupConcatOrder.

/**
 * @param f Aggregate function.
 * @param orders Orders.
 * @param calcTypes Calculate types for all the expressions.
 */
private void parseGroupConcatOrder(GridSqlAggregateFunction f, ArrayList<SelectOrderBy> orders, boolean calcTypes) {
    GridSqlElement[] grpConcatOrderExpression = new GridSqlElement[orders.size()];
    boolean[] grpConcatOrderDesc = new boolean[orders.size()];
    for (int i = 0; i < orders.size(); ++i) {
        SelectOrderBy o = orders.get(i);
        grpConcatOrderExpression[i] = parseExpression(o.expression, calcTypes);
        grpConcatOrderDesc[i] = o.descending;
    }
    f.setGroupConcatOrder(grpConcatOrderExpression, grpConcatOrderDesc);
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint)

Aggregations

SimpleResultSet (org.h2.tools.SimpleResultSet)10 ResultSet (java.sql.ResultSet)9 Function (org.h2.expression.Function)8 JavaFunction (org.h2.expression.JavaFunction)8 TableFunction (org.h2.expression.TableFunction)8 Expression (org.h2.expression.Expression)7 ValueExpression (org.h2.expression.ValueExpression)7 Column (org.h2.table.Column)7 Statement (java.sql.Statement)6 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)6 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 IndexColumn (org.h2.table.IndexColumn)6 SQLException (java.sql.SQLException)5 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)5 PreparedStatement (java.sql.PreparedStatement)4 ValueString (org.h2.value.ValueString)4 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)3