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 + ')');
}
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>";
}
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);
}
}
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");
}
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);
}
Aggregations