Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b272a2df
提交
b272a2df
authored
5月 07, 2013
作者:
noelgrandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Issue 442: groovy patch for SourceCompiler (function ALIAS)
上级
1140d4a9
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
74 行增加
和
2 行删除
+74
-2
help.csv
h2/src/docsrc/help/help.csv
+7
-0
changelog.html
h2/src/docsrc/html/changelog.html
+1
-0
SourceCompiler.java
h2/src/main/org/h2/util/SourceCompiler.java
+66
-2
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
b272a2df
...
...
@@ -441,6 +441,8 @@ parameter count, all methods are mapped.
Admin rights are required to execute this command.
This command commits an open transaction.
If you have the Groovy jar in your classpath, it is also possible to write methods using Groovy.
","
CREATE ALIAS MY_SQRT FOR ""java.lang.Math.sqrt"";
CREATE ALIAS GET_SYSTEM_PROPERTY FOR ""java.lang.System.getProperty"";
...
...
@@ -448,6 +450,11 @@ CALL GET_SYSTEM_PROPERTY('java.class.path');
CALL GET_SYSTEM_PROPERTY('com.acme.test', 'true');
CREATE ALIAS REVERSE AS $$ String reverse(String s) { return new StringBuilder(s).reverse().toString(); } $$;
CALL REVERSE('Test');
CREATE ALIAS tr AS $$@groovy.transform.CompileStatic
static String tr(String str, String sourceSet, String replacementSet){
return str.tr(sourceSet, replacementSet);
}
$$
"
"Commands (DDL)","CREATE CONSTANT","
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
b272a2df
...
...
@@ -38,6 +38,7 @@ Change Log
</li><li>
Issue 274: Sybase/MSSQLServer compatibility - swap parameters of CONVERT function.
</li><li>
Issue 274: Sybase/MSSQLServer compatibility - support index clause e.g. "select * from test (index table1_index)"
</li><li>
Fix bug in optimising SELECT * FROM A WHERE X=1 OR X=2 OR X=3 into SELECT * FROM A WHERE X IN (1,2,3)
</li><li>
Issue 442: groovy patch for SourceCompiler (function ALIAS)
</li></ul>
<h2>
Version 1.3.171 (2013-03-17)
</h2>
...
...
h2/src/main/org/h2/util/SourceCompiler.java
浏览文件 @
b272a2df
...
...
@@ -77,6 +77,13 @@ public class SourceCompiler {
return
compiledClass
;
}
String
source
=
sources
.
get
(
packageAndClassName
);
if
(
isGroovySource
(
source
))
{
Class
<?>
clazz
=
GroovyCompiler
.
parseClass
(
source
,
packageAndClassName
);
compiled
.
put
(
packageAndClassName
,
clazz
);
return
clazz
;
}
ClassLoader
classLoader
=
new
ClassLoader
(
getClass
().
getClassLoader
())
{
public
Class
<?>
findClass
(
String
name
)
throws
ClassNotFoundException
{
Class
<?>
classInstance
=
compiled
.
get
(
name
);
...
...
@@ -105,6 +112,10 @@ public class SourceCompiler {
return
classLoader
.
loadClass
(
packageAndClassName
);
}
private
static
boolean
isGroovySource
(
String
source
)
{
return
source
.
startsWith
(
"//groovy"
)
||
source
.
startsWith
(
"@groovy"
);
}
/**
* Get the first public static method of the given class.
*
...
...
@@ -116,8 +127,9 @@ public class SourceCompiler {
Method
[]
methods
=
clazz
.
getDeclaredMethods
();
for
(
Method
m
:
methods
)
{
int
modifiers
=
m
.
getModifiers
();
if
(
Modifier
.
isPublic
(
modifiers
))
{
if
(
Modifier
.
isStatic
(
modifiers
))
{
if
(
Modifier
.
isPublic
(
modifiers
)
&&
Modifier
.
isStatic
(
modifiers
))
{
String
mname
=
m
.
getName
();
if
(!
mname
.
startsWith
(
"_"
)
&&
!
m
.
getName
().
equals
(
"main"
))
{
return
m
;
}
}
...
...
@@ -248,5 +260,57 @@ public class SourceCompiler {
throw
DbException
.
get
(
ErrorCode
.
SYNTAX_ERROR_1
,
err
);
}
}
/**
* Access the groovy compiler using reflection, so that we do not gain a compile-time dependency
* unnecessarily.
*/
private
static
final
class
GroovyCompiler
{
private
static
final
Object
loader
;
private
static
final
Throwable
initfailException
;
static
{
Object
tmpLoader
=
null
;
Throwable
tmpInitfailException
=
null
;
try
{
// create an instance of ImportCustomiser
Class
<?>
importCustomizerClass
=
Class
.
forName
(
"org.codehaus.groovy.control.customizers.ImportCustomizer"
);
Object
importCustomizer
=
Utils
.
newInstance
(
"org.codehaus.groovy.control.customizers.ImportCustomizer"
);
// Call the method ImportCustomizer#addImports(String[])
String
[]
importsArray
=
new
String
[]
{
"java.sql.Connection"
,
"java.sql.Types"
,
"java.sql.ResultSet"
,
"groovy.sql.Sql"
,
"org.h2.tools.SimpleResultSet"
};
Utils
.
callMethod
(
importCustomizer
,
"addImports"
,
new
Object
[]
{
importsArray
});
// Call the method CompilerConfiguration#addCompilationCustomizers(ImportCustomizer...)
Object
importCustomizerArray
=
java
.
lang
.
reflect
.
Array
.
newInstance
(
importCustomizerClass
,
1
);
java
.
lang
.
reflect
.
Array
.
set
(
importCustomizerArray
,
0
,
importCustomizer
);
Object
configuration
=
Utils
.
newInstance
(
"org.codehaus.groovy.control.CompilerConfiguration"
);
Utils
.
callMethod
(
configuration
,
"addCompilationCustomizers"
,
new
Object
[]
{
importCustomizerArray
});
ClassLoader
parent
=
GroovyCompiler
.
class
.
getClassLoader
();
tmpLoader
=
Utils
.
newInstance
(
"groovy.lang.GroovyClassLoader"
,
parent
,
configuration
);
}
catch
(
Exception
ex
)
{
tmpInitfailException
=
ex
;
}
loader
=
tmpLoader
;
initfailException
=
tmpInitfailException
;
}
public
static
Class
<?>
parseClass
(
String
source
,
String
packageAndClassName
)
{
if
(
loader
==
null
)
{
throw
new
RuntimeException
(
"compile fail: there is no groovy jar on the classpath?"
,
initfailException
);
}
try
{
Object
codeSource
=
Utils
.
newInstance
(
"groovy.lang.GroovyCodeSource"
,
source
,
packageAndClassName
+
".groovy"
,
"UTF-8"
);
Utils
.
callMethod
(
codeSource
,
"setCachable"
,
false
);
Class
<?>
clazz
=
(
Class
<?>)
Utils
.
callMethod
(
loader
,
"parseClass"
,
codeSource
);
return
clazz
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论