From 1f24daefeeaccdf0b75357cdf5bdb16d24f907ee Mon Sep 17 00:00:00 2001 From: Guillaume Delhumeau Date: Thu, 16 Aug 2018 15:12:16 +0200 Subject: [PATCH] * Handle the list of tables for HSQLDB (need to be done for PostGreSQL as well) * Handle transaction commits --- sql-tools/pom.xml | 24 +- .../main/resources/SQLTools/ListTables.xml | 70 ++++-- .../resources/SQLTools/ListTablesOfXWiki.xml | 73 ++++-- .../src/main/resources/SQLTools/Query.xml | 68 +++-- .../main/resources/SQLTools/QueryOnXWiki.xml | 72 ++++-- .../resources/SQLTools/SQLToolsGroovy.xml | 236 ++++++++++-------- .../src/main/resources/SQLTools/WebHome.xml | 68 +++-- 7 files changed, 379 insertions(+), 232 deletions(-) diff --git a/sql-tools/pom.xml b/sql-tools/pom.xml index 430b210..318b455 100644 --- a/sql-tools/pom.xml +++ b/sql-tools/pom.xml @@ -22,9 +22,9 @@ 4.0.0 - org.xwiki.platform.applications - xwiki-applications - 35 + org.xwiki.commons + xwiki-commons-pom + 6.4.1 org.xwiki.contrib xwiki-sql-tools @@ -32,4 +32,22 @@ xar SQL Tools for XWiki + + + + + org.xwiki.commons + xwiki-commons-tool-xar-handlers + ${commons.version} + + + + + + org.xwiki.commons + xwiki-commons-tool-xar-plugin + ${commons.version} + + + diff --git a/sql-tools/src/main/resources/SQLTools/ListTables.xml b/sql-tools/src/main/resources/SQLTools/ListTables.xml index c2fb64c..5f89171 100644 --- a/sql-tools/src/main/resources/SQLTools/ListTables.xml +++ b/sql-tools/src/main/resources/SQLTools/ListTables.xml @@ -1,35 +1,52 @@ - -SQLTools -ListTables - -en -0 - -XWiki.Admin -XWiki.Admin - -XWiki.Admin -1352109523000 -1352109523000 -1352109523000 -1.1 - - - - - -false -xwiki/2.1 -false -{{velocity}} + + + + + SQLTools + ListTables + + + 0 + SQLTools.WebHome + xwiki:XWiki.Admin + 1352109523000 + xwiki:XWiki.Admin + xwiki:XWiki.Admin + 1534424225000 + 1534424225000 + 1.1 + + <comment/> + <minorEdit>false</minorEdit> + <syntaxId>xwiki/2.1</syntaxId> + <hidden>true</hidden> + <content>{{velocity}} #if($hasProgramming) #set($sqlTools = $xwiki.parseGroovyFromPage('SQLTools.SQLToolsGroovy')) = Form = $sqlTools.getForm($request, $doc, true, false) #if($request.url) #set($connection = $sqlTools.getConnectionFromRequest($request)) - #set($results = $sqlTools.getTables($connection)) + #set($results = $sqlTools.getTables($connection, $xwiki, $xcontext)) = Table of content = {{toc /}} @@ -40,4 +57,5 @@ {{error}}Sorry, you need the programming rights to use this tool{{/error}} #end {{/velocity}} -</content></xwikidoc> \ No newline at end of file +</content> +</xwikidoc> diff --git a/sql-tools/src/main/resources/SQLTools/ListTablesOfXWiki.xml b/sql-tools/src/main/resources/SQLTools/ListTablesOfXWiki.xml index c7deb17..f389f72 100644 --- a/sql-tools/src/main/resources/SQLTools/ListTablesOfXWiki.xml +++ b/sql-tools/src/main/resources/SQLTools/ListTablesOfXWiki.xml @@ -1,28 +1,45 @@ <?xml version="1.0" encoding="UTF-8"?> -<xwikidoc> -<web>SQLTools</web> -<name>ListTablesOfXWiki</name> -<language></language> -<defaultLanguage>en</defaultLanguage> -<translation>0</translation> -<parent></parent> -<creator>XWiki.Admin</creator> -<author>XWiki.Admin</author> -<customClass></customClass> -<contentAuthor>XWiki.Admin</contentAuthor> -<creationDate>1352113380000</creationDate> -<date>1352113380000</date> -<contentUpdateDate>1352113380000</contentUpdateDate> -<version>1.1</version> -<title> - - - - -false -xwiki/2.1 -false -{{velocity}} + + + + + SQLTools + ListTablesOfXWiki + + + 0 + SQLTools.WebHome + xwiki:XWiki.Admin + 1352113380000 + xwiki:XWiki.Admin + xwiki:XWiki.Admin + 1534423829000 + 1534423829000 + 1.1 + + <comment/> + <minorEdit>false</minorEdit> + <syntaxId>xwiki/2.1</syntaxId> + <hidden>true</hidden> + <content>{{velocity}} #if($hasProgramming) #set($sqlTools = $xwiki.parseGroovyFromPage('SQLTools.SQLToolsGroovy')) #set($system = $sqlTools.getXWikiDatabaseSystem($xwiki, $xcontext)) @@ -30,8 +47,9 @@ #if($system == "HSQL Database Engine") {{warning}}$system is not supported for this action{{/warning}} #end - #set($connection = $sqlTools.getXWikiConnection($xwiki, $xcontext)) - #set($results = $sqlTools.getTables($connection)) + #set($store = $sqlTools.getXWikiStore($xwiki, $xcontext)) + #set($connection = $sqlTools.getXWikiConnection($store, $xcontext)) + #set($results = $sqlTools.getTables($connection, $xwiki, $xcontext)) = Table of content = {{toc /}} @@ -41,4 +59,5 @@ {{error}}Sorry, you need the programming rights to use this tool{{/error}} #end {{/velocity}} -</content></xwikidoc> \ No newline at end of file +</content> +</xwikidoc> diff --git a/sql-tools/src/main/resources/SQLTools/Query.xml b/sql-tools/src/main/resources/SQLTools/Query.xml index b30c6d3..84c88e4 100644 --- a/sql-tools/src/main/resources/SQLTools/Query.xml +++ b/sql-tools/src/main/resources/SQLTools/Query.xml @@ -1,28 +1,45 @@ <?xml version="1.0" encoding="UTF-8"?> -<xwikidoc> -<web>SQLTools</web> -<name>Query</name> -<language></language> -<defaultLanguage>en</defaultLanguage> -<translation>0</translation> -<parent></parent> -<creator>XWiki.Admin</creator> -<author>XWiki.Admin</author> -<customClass></customClass> -<contentAuthor>XWiki.Admin</contentAuthor> -<creationDate>1352109523000</creationDate> -<date>1352109523000</date> -<contentUpdateDate>1352109523000</contentUpdateDate> -<version>1.1</version> -<title> - - - - -false -xwiki/2.1 -false -{{velocity}} + + + + + SQLTools + Query + + + 0 + SQLTools.WebHome + xwiki:XWiki.Admin + 1352109523000 + xwiki:XWiki.Admin + xwiki:XWiki.Admin + 1534414594000 + 1534414594000 + 1.1 + + <comment/> + <minorEdit>false</minorEdit> + <syntaxId>xwiki/2.1</syntaxId> + <hidden>true</hidden> + <content>{{velocity}} #if($hasProgramming) #set($sqlTools = $xwiki.parseGroovyFromPage('SQLTools.SQLToolsGroovy')) = Query = @@ -37,4 +54,5 @@ {{error}}Sorry, you need the programming rights to use this tool{{/error}} #end {{/velocity}} -</content></xwikidoc> \ No newline at end of file +</content> +</xwikidoc> diff --git a/sql-tools/src/main/resources/SQLTools/QueryOnXWiki.xml b/sql-tools/src/main/resources/SQLTools/QueryOnXWiki.xml index d7ebb8e..b84a280 100644 --- a/sql-tools/src/main/resources/SQLTools/QueryOnXWiki.xml +++ b/sql-tools/src/main/resources/SQLTools/QueryOnXWiki.xml @@ -1,28 +1,45 @@ <?xml version="1.0" encoding="UTF-8"?> -<xwikidoc> -<web>SQLTools</web> -<name>QueryOnXWiki</name> -<language></language> -<defaultLanguage>en</defaultLanguage> -<translation>0</translation> -<parent></parent> -<creator>XWiki.Admin</creator> -<author>XWiki.Admin</author> -<customClass></customClass> -<contentAuthor>XWiki.Admin</contentAuthor> -<creationDate>1352113376000</creationDate> -<date>1352113376000</date> -<contentUpdateDate>1352113376000</contentUpdateDate> -<version>1.1</version> -<title> - - - - -false -xwiki/2.1 -false -{{velocity}} + + + + + SQLTools + QueryOnXWiki + + + 0 + SQLTools.WebHome + xwiki:XWiki.Admin + 1352113376000 + xwiki:XWiki.Admin + xwiki:XWiki.Admin + 1534423539000 + 1534423539000 + 1.1 + + <comment/> + <minorEdit>false</minorEdit> + <syntaxId>xwiki/2.1</syntaxId> + <hidden>true</hidden> + <content>{{velocity}} #if($hasProgramming) #set($sqlTools = $xwiki.parseGroovyFromPage('SQLTools.SQLToolsGroovy')) #set($system = $sqlTools.getXWikiDatabaseSystem($xwiki, $xcontext)) @@ -30,8 +47,10 @@ Your database is: **$system** $sqlTools.getForm($request, $doc, false, true) #if($request.query) - #set($connection = $sqlTools.getXWikiConnection($xwiki, $xcontext)) + #set($store = $sqlTools.getXWikiStore($xwiki, $xcontext)) + #set($connection = $sqlTools.getXWikiConnection($store, $xcontext)) #set($results = $sqlTools.getResults($connection, $request.query, true)) + $store.endTransaction($xcontext.context, true) = Results = $results #end @@ -39,4 +58,5 @@ {{error}}Sorry, you need the programming rights to use this tool{{/error}} #end {{/velocity}} -</content></xwikidoc> \ No newline at end of file +</content> +</xwikidoc> diff --git a/sql-tools/src/main/resources/SQLTools/SQLToolsGroovy.xml b/sql-tools/src/main/resources/SQLTools/SQLToolsGroovy.xml index 96e3e7d..593b147 100644 --- a/sql-tools/src/main/resources/SQLTools/SQLToolsGroovy.xml +++ b/sql-tools/src/main/resources/SQLTools/SQLToolsGroovy.xml @@ -1,84 +1,62 @@ <?xml version="1.0" encoding="UTF-8"?> -<xwikidoc> -<web>SQLTools</web> -<name>SQLToolsGroovy</name> -<language></language> -<defaultLanguage>en</defaultLanguage> -<translation>0</translation> -<parent></parent> -<creator>XWiki.Admin</creator> -<author>xwiki:XWiki.GuillaumeDelhumeau</author> -<customClass></customClass> -<contentAuthor>xwiki:XWiki.GuillaumeDelhumeau</contentAuthor> -<creationDate>1352114288000</creationDate> -<date>1365090128000</date> -<contentUpdateDate>1365090128000</contentUpdateDate> -<version>1.2</version> -<title>SQLToolsGroovy - - - - -true -xwiki/2.1 -false - - -XWiki.RequiredRightClass - - - - - - - - -0 -0 -select -0 -level -1 -0 -level -0 - - ,| -1 -none -0 - - -edit|programming -com.xpn.xwiki.objects.classes.StaticListClass - - -SQLTools.SQLToolsGroovy -0 -XWiki.RequiredRightClass -4bf0810f-053d-4cc4-8dc5-05148950b699 - -programming - - -/** + + + + + SQLTools + SQLToolsGroovy + + + 0 + SQLTools.WebHome + xwiki:XWiki.Admin + 1352114288000 + xwiki:XWiki.Admin + xwiki:XWiki.Admin + 1534424087000 + 1534424014000 + 1.1 + SQLToolsGroovy + + false + plain/1.0 + true + /** * Import - */ + */ import org.apache.velocity.tools.generic.EscapeTool; import java.sql.* /** - * Class + * Class */ class SQLTools{ /** * Get Form */ - String getForm(request, doc, withJDBCForm, withQuery){ + String getForm(request, doc, withJDBCForm, withQuery){ def escapeTool = new EscapeTool(); - def spageUrl = escapeTool.xml(doc.getURL()); - + def spageUrl = escapeTool.xml(doc.getURL()); + // <form> def toReturn = """ {{html clean=="false"}} @@ -99,13 +77,13 @@ class SQLTools{ def driver = request.driver; if (driver==null) driver = "com.mysql.jdbc.Driver"; - + // Escape datas def surl = escapeTool.xml(url); def suser = escapeTool.xml(user); def spassword = escapeTool.xml(password); - def sdriver = escapeTool.xml(driver); - + def sdriver = escapeTool.xml(driver); + // Print form toReturn += """ <dl> @@ -114,7 +92,7 @@ class SQLTools{ </dt> <dd> <input type="text" name="user" id="user" value="${suser}" size="80" /> - </dd> + </dd> </dl> <dl> <dt> @@ -122,7 +100,7 @@ class SQLTools{ </dt> <dd> <input type="text" name="password" id="password" value="${spassword}" size="80" /> - </dd> + </dd> </dl> <dl> <dt> @@ -130,7 +108,7 @@ class SQLTools{ </dt> <dd> <input type="text" name="url" id="url" value="${surl}" size="80" /> - </dd> + </dd> </dl> <dl> <dt> @@ -138,7 +116,7 @@ class SQLTools{ </dt> <dd> <input type="text" name="driver" id="driver" value="${sdriver}" size="80" /> - </dd> + </dd> </dl> """; } @@ -149,7 +127,7 @@ class SQLTools{ if (query==null) query = "SHOW TABLES"; def squery = escapeTool.xml(query); - + // Print the form toReturn += """ <dl> @@ -158,21 +136,21 @@ class SQLTools{ </dt> <dd> <textarea name="query" id="query" cols="100" rows="10"/>${squery}</textarea> - </dd> - </dl> + </dd> + </dl> """; } - + // </form> toReturn += """ <p><span class="buttonwrapper"><input type="submit" value="Go" class="button"/></span></p> </form> {{/html}} """; - + return toReturn; } - + /** * Get connection from request */ @@ -190,7 +168,7 @@ class SQLTools{ def conn = DriverManager.getConnection (url, userName, password); return conn; } - + /** * Get XWiki Database System */ @@ -199,22 +177,29 @@ class SQLTools{ def db = store.getDatabaseProductName(xcontext.getContext()); return db.getProductName(); } - + /** * Get XWiki Connection */ - def getXWikiConnection(xwiki, xcontext){ + def getXWikiStore(xwiki, xcontext){ def store = xwiki.getXWiki().getHibernateStore(); store.beginTransaction(xcontext.getContext()); + return store; + } + + /** + * Get XWiki Connection + */ + def getXWikiConnection(store, xcontext){ def session = store.getSession(xcontext.getContext()); - return session.connection(); + return session.connection(); } - - + + /** * Get results */ - String getResults(connection, sql, closeConnectionAtTheEnd){ + String getResults(connection, sql, closeConnectionAtTheEnd){ def toReturn = ""; try{ def stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); @@ -229,10 +214,10 @@ class SQLTools{ def nbcols = mdata.getColumnCount(); for (def i=1;i<=nbcols;i++) { toReturn += "|="; - toReturn += mdata.getColumnName(i); + toReturn += mdata.getColumnName(i); } toReturn += "\n"; - while (resultset.next()) { + while (resultset.next()) { for (def i=1;i<=nbcols;i++) { def value = resultset.getString(i); if ((value==null)||(value=="")) @@ -248,7 +233,7 @@ class SQLTools{ toReturn += "\n{{error}} **Error running sql: {{{${sql}}}}**\n" + e2.getMessage() + "{{/error}}\n"; } finally { stmt.close(); - if(closeConnectionAtTheEnd) + if(closeConnectionAtTheEnd) connection.close(); } } @@ -256,33 +241,84 @@ class SQLTools{ toReturn += "\n{{error}} Error: " + e.getMessage() + "{{/error}}\n"; } return toReturn; - } - + } + /** - * Get Tables + * Get the query for listing the list of tables */ - String getTables(connection){ + String getShowTableCommand(xwiki, xcontext) { def sql = "SHOW TABLES"; + if (this.getXWikiDatabaseSystem(xwiki, xcontext) == 'HSQL Database Engine') { + sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA NOT IN ('INFORMATION_SCHEMA', 'SYSTEM_LOBS')"; + } + return sql; + } + + /** + * Get Tables + */ + String getTables(connection, xwiki, xcontext){ + def sql = getShowTableCommand(xwiki, xcontext); def stmt1 = connection.createStatement(); def toReturn = ""; try { // Get Tables list def rs = stmt1.executeQuery(sql); while(rs.next()){ - def table = rs.getString(1); + def table = rs.getString(1); toReturn += "== ${table} ==\n"; - toReturn += getResults(connection, "DESCRIBE ${table}", false); + toReturn += getResults(connection, "DESCRIBE ${table}", false); } } catch (Exception e2) { toReturn += "{{error}} **Error running sql: {{{${sql}}}}**\n" + e2.getMessage() + "{{/error}}\n"; } finally { - stmt1.close(); + stmt1.close(); connection.close(); } - + return toReturn; } }; - \ No newline at end of file + + + SQLTools.SQLToolsGroovy + 0 + XWiki.RequiredRightClass + 4bf0810f-053d-4cc4-8dc5-05148950b699 + + XWiki.RequiredRightClass + + + + + + + + + 0 + 0 + select + 0 + level + 1 + 0 + level + 0 + + ,| + 1 + none + 0 + + + edit|programming + com.xpn.xwiki.objects.classes.StaticListClass + + + + programming + + + diff --git a/sql-tools/src/main/resources/SQLTools/WebHome.xml b/sql-tools/src/main/resources/SQLTools/WebHome.xml index 28db699..3288326 100644 --- a/sql-tools/src/main/resources/SQLTools/WebHome.xml +++ b/sql-tools/src/main/resources/SQLTools/WebHome.xml @@ -1,30 +1,48 @@ - -SQLTools -WebHome - -en -0 -Dashboard.WebHome -XWiki.Admin -XWiki.Admin - -XWiki.Admin -1352109523000 -1352109523000 -1352109523000 -1.1 -SQL Tools - - - - -false -xwiki/2.1 -false -= SQL Tools = + + + + + SQLTools + WebHome + + + 0 + xwiki:XWiki.Admin + 1352109523000 + Main.WebHome + xwiki:XWiki.Admin + xwiki:XWiki.Admin + 1534414594000 + 1534414594000 + 1.1 + SQL Tools + + false + xwiki/2.1 + false + = SQL Tools = {{warning}}You need the programming rights to use these scripts. They can be very dangerous, since you can change everything in the wiki database. So, take care!{{/warning}} * [[SQL Query on XWiki Database>>QueryOnXWiki]] * [[SQL Query on JDBC Database>>Query]] * [[List tables of XWiki Database>>ListTablesOfXWiki]] -* [[List tables of JDBC Database>>ListTables]] \ No newline at end of file +* [[List tables of JDBC Database>>ListTables]] +