{"id":19747629,"url":"https://github.com/codingmiao/h2","last_synced_at":"2026-05-10T12:07:06.962Z","repository":{"id":65406648,"uuid":"77189849","full_name":"codingmiao/h2","owner":"codingmiao","description":"魔改版h2,主要针对sql的自定义重写，原h2项目地址https://github.com/h2database/h2database","archived":false,"fork":false,"pushed_at":"2017-08-14T06:49:07.000Z","size":1515,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-10T21:09:51.414Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codingmiao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-23T02:03:53.000Z","updated_at":"2019-04-08T16:45:49.000Z","dependencies_parsed_at":"2023-01-23T10:54:57.123Z","dependency_job_id":null,"html_url":"https://github.com/codingmiao/h2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingmiao%2Fh2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingmiao%2Fh2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingmiao%2Fh2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codingmiao%2Fh2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codingmiao","download_url":"https://codeload.github.com/codingmiao/h2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241078486,"owners_count":19905867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T02:18:22.998Z","updated_at":"2026-05-10T12:07:01.894Z","avatar_url":"https://github.com/codingmiao.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wowtools h2\n魔改版h2,主要针对sql的自定义重写。\n原h2项目地址[https://github.com/h2database/h2database][1]\n\n扩展功能：\n=====\n - sql重写\n\n有时候，我们需要利用某些软件查询h2中的数据，但直接去修改软件源码工作量较大(例如在geoserver中的一个图层，我们需要按照自定义的数据来出图)。\n此时，可利用sql重写功能，重写sql后查询我们想要的数据，例如：\n\n有数据表如下：\n\n 学生表 STUDENT:\ncreate table student(sid int,name varchar(32),cid int)\n\n 班级表 CLAZZ:\ncreate table clazz(cid int,name varchar(32))\n\n我们可以把sql拦截一下，替换成连接查询而直接获得学生所属班级：\n\n        SqlRewriter r = new SqlRewriter() {\n\n\t\t\t@Override\n\t\t\tpublic String rewrite(String sql) {\n\t\t\t\treturn \"select s.name,c.name from student s,clazz c where s.cid = c.cid\";\n\t\t\t}\n\n\t\t\t@Override\n\t\t\tpublic boolean isConform(String sql) {\n\t\t\t\treturn sql.indexOf(\"CNAMEANDSNAME\") \u003e 0;\n\t\t\t}\n\t\t};\n\t\tSqlRewriterManager.add(r);\n\n然后，当我们输入sql比如select ooxx from CNAMEANDSNAME时，就会被替换成代码中的连接查询语句啦~~\n\n这个例子看起来好像没什么卵用？我们再来看h2自带的一个功能：\n\n\u003e **Using a Function as a Table**\nA function that returns a result set can be used like a table. However, in this case the function is called at least twice: first while parsing the statement to collect the column names (with parameters set to null where not known at compile time). And then, while executing the statement to get the data (maybe multiple times if this is a join). If the function is called just to get the column list, the URL of the connection passed to the function is jdbc:columnlist:connection. Otherwise, the URL of the connection is jdbc:default:connection. \n\n    public static ResultSet getMatrix(Connection conn, Integer size) throws SQLException {\n\t\tSimpleResultSet rs = new SimpleResultSet();\n\t\trs.addColumn(\"X\", Types.INTEGER, 10, 0);\n\t\trs.addColumn(\"Y\", Types.INTEGER, 10, 0);\n\t\tString url = conn.getMetaData().getURL();\n\t\tif (url.equals(\"jdbc:columnlist:connection\")) {\n\t\t\treturn rs;\n\t\t}\n\t\tfor (int s = size.intValue(), x = 0; x \u003c s; x++) {\n\t\t\tfor (int y = 0; y \u003c s; y++) {\n\t\t\t\trs.addRow(x, y);\n\t\t\t}\n\t\t}\n\t\treturn rs;\n\t}\n\t\tCREATE ALIAS MATRIX FOR\"org.h2.samples.Function.getMatrix\";\n\t\tSELECT * FROM MATRIX(4) ORDER BY X, Y;\n\n直接吧java代码写在自定义函数里，然后通过sql重写来调用自定义函数，就能返回任意格式、任意来源的数据啦~~这样一看是不是很管用了呢^_^\n \n\n  [1]: https://github.com/h2database/h2database","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodingmiao%2Fh2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodingmiao%2Fh2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodingmiao%2Fh2/lists"}