Permalink
Browse files

Endpoint: schemas.

  • Loading branch information...
1 parent 99c82fd commit f08dd276df7f370224f93fbbf02cb675b8af172e @ryangrimm committed Jan 28, 2012
Showing with 304 additions and 2 deletions.
  1. +9 −0 config/endpoints.xqy
  2. +87 −0 corona/lib/manage.xqy
  3. +69 −0 corona/manage/schemas.xqy
  4. +4 −2 corona/manage/summary.xqy
  5. +1 −0 test/index.html
  6. +116 −0 test/js/schema-tests.js
  7. +18 −0 test/schema.html
@@ -276,6 +276,15 @@ declare variable $endpoints:ENDPOINTS as element(rest:options) :=
<http method="DELETE"/>
</request>
+ <!-- Schema management -->
+ <request uri="^/manage/(schemas|schema)/?$" endpoint="/corona/manage/schemas.xqy" user-params="allow">
+ <param name="uri" required="false"/>
+ <http method="GET"/>
+ <http method="PUT"/>
+ <http method="DELETE"/>
+ </request>
+
+ <!-- Environment variables -->
<request uri="^/manage/(env|env/([^/]+))/?$" endpoint="/corona/manage/env.xqy" user-params="allow">
<uri-param name="name" as="string">$2</uri-param>
<http method="GET"/>
View
@@ -1321,6 +1321,93 @@ declare function manage:deleteAllTransformers(
};
+(: Schemas :)
+declare function manage:setSchema(
+ $uri as xs:string,
+ $schema as element(xs:schema)
+) as empty-sequence()
+{
+ xdmp:eval("
+ declare variable $uri as xs:string external;
+ declare variable $schema as element(xs:schema) external;
+
+ xdmp:document-insert($uri, $schema)
+ ",
+ (
+ xs:QName("uri"), $uri,
+ xs:QName("schema"), $schema
+ ),
+ <options xmlns="xdmp:eval">
+ <database>{ xdmp:schema-database() }</database>
+ </options>
+ )
+};
+
+declare function manage:deleteSchema(
+ $uri as xs:string
+) as empty-sequence()
+{
+ xdmp:eval("
+ declare variable $uri as xs:string external;
+
+ xdmp:document-delete($uri)
+ ",
+ (
+ xs:QName("uri"), $uri
+ ),
+ <options xmlns="xdmp:eval">
+ <database>{ xdmp:schema-database() }</database>
+ </options>
+ )
+};
+
+declare function manage:getSchema(
+ $uri as xs:string
+) as element(xs:schema)?
+{
+ xdmp:eval("
+ declare variable $uri as xs:string external;
+
+ doc($uri)/xs:schema
+ ",
+ (
+ xs:QName("uri"), $uri
+ ),
+ <options xmlns="xdmp:eval">
+ <database>{ xdmp:schema-database() }</database>
+ </options>
+ )
+};
+
+declare function manage:getAllSchemaURIs(
+) as xs:string*
+{
+ xdmp:eval("
+ for $schema in /xs:schema
+ return base-uri($schema)
+ ",
+ (),
+ <options xmlns="xdmp:eval">
+ <database>{ xdmp:schema-database() }</database>
+ </options>
+ )
+};
+
+declare function manage:deleteAllSchemas(
+) as empty-sequence()
+{
+ xdmp:eval("
+ for $schema in /xs:schema
+ return xdmp:document-delete(base-uri($schema))
+ ",
+ (),
+ <options xmlns="xdmp:eval">
+ <database>{ xdmp:schema-database() }</database>
+ </options>
+ )
+};
+
+
(: Private functions :)
declare private function manage:createJSONRangeIndex(
@@ -0,0 +1,69 @@
+(:
+Copyright 2011 MarkLogic Corporation
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+:)
+
+xquery version "1.0-ml";
+
+import module namespace manage="http://marklogic.com/corona/manage" at "../lib/manage.xqy";
+import module namespace common="http://marklogic.com/corona/common" at "../lib/common.xqy";
+import module namespace json="http://marklogic.com/json" at "../lib/json.xqy";
+
+import module namespace rest="http://marklogic.com/appservices/rest" at "../lib/rest/rest.xqy";
+import module namespace endpoints="http://marklogic.com/corona/endpoints" at "/config/endpoints.xqy";
+
+declare option xdmp:mapping "false";
+
+
+let $params := rest:process-request(endpoints:request("/corona/manage/schemas.xqy"))
+let $requestMethod := xdmp:get-request-method()
+let $uri := map:get($params, "uri")
+let $schemaContent := xdmp:get-request-body("xml")/xs:schema
+
+return common:output(
+ if($requestMethod = "GET")
+ then
+ if(string-length($uri))
+ then
+ let $existing := manage:getSchema($uri)
+ return
+ if(exists($existing))
+ then $existing
+ else common:error("corona:SCHEMA-NOT-FOUND", "Schema not found", "json")
+ else json:array(manage:getAllSchemaURIs())
+
+ else if($requestMethod = "PUT")
+ then
+ if(string-length($uri))
+ then
+ try {
+ manage:setSchema($uri, $schemaContent)
+ }
+ catch ($e) {
+ common:errorFromException($e, "xml")
+ }
+ else common:error("corona:INVALID-PARAMETER", "Must specify a URI for the schema", "json")
+
+ else if($requestMethod = "DELETE")
+ then
+ if(string-length($uri))
+ then
+ let $existing := manage:getSchema($uri)
+ return
+ if(exists($existing))
+ then manage:deleteSchema($uri)
+ else common:error("corona:SCHEMA-NOT-FOUND", "Schema not found", "json")
+ else manage:deleteAllSchemas()
+ else common:error("corona:UNSUPPORTED-METHOD", concat("Unsupported method: ", $requestMethod), "json")
+)
@@ -86,7 +86,8 @@ return common:output(
"TextDocumentCount", $numTextDocs,
"BinaryDocumentCount", $numBinaryDocs
)),
- "xmlNamespaces", json:array(manage:getAllNamespaces())
+ "xmlNamespaces", json:array(manage:getAllNamespaces()),
+ "xmlSchemas", json:array(manage:getAllSchemaURIs())
))
)
else if($requestMethod = "DELETE")
@@ -96,7 +97,8 @@ return common:output(
manage:deleteAllPlaces(),
manage:deleteAllGeos(),
manage:deleteAllTransformers(),
- manage:deleteAllNamespaces()
+ manage:deleteAllNamespaces(),
+ manage:deleteAllSchemas()
)
else ()
)
View
@@ -7,6 +7,7 @@
<li><a href="structured-query-parser.html">Structured Query Parser</a></li>
<li><a href="dateparser.html">Date Parser</a></li>
<li><a href="indexmgmt.html">Index Management</a></li>
+ <li><a href="schema.html">Schema Management</a></li>
<li><a href="store.html">Document Store</a></li>
<li><a href="bulk-store.html">Bulk Document Store</a></li>
<li><a href="named-query.html">Named Query Endpoint</a></li>
@@ -0,0 +1,116 @@
+if(typeof corona == "undefined" || !corona) {
+ corona = {};
+ corona.stash = {};
+}
+
+corona.removeSchemas = function(info, callback) {
+ var i = 0;
+ var schemas = [];
+ for(i = 0; i < info.xmlSchemas.length; i += 1) {
+ schemas.push(info.xmlSchemas[i]);
+ }
+
+ var processingPosition = 0;
+ var removeNextItem = function() {
+ removeItem(schemas[processingPosition]);
+ processingPosition++;
+ };
+
+ var removeItem = function(schema) {
+ if(schema === undefined) {
+ callback.call();
+ return;
+ }
+
+ asyncTest("Remove the schema: " + schema, function() {
+ var url = "/manage/schema?uri=" + schema;
+ $.ajax({
+ url: url,
+ type: 'DELETE',
+ success: function() {
+ ok(true, "Removed schema: " + schema);
+ asyncTest("Check to make sure the schema is gone", function() {
+ $.ajax({
+ url: url,
+ success: function(data) {
+ ok(false, "Schema still exists: " + schema);
+ },
+ error: function() {
+ ok(true, "Schema is gone");
+ },
+ complete: function() { start(); }
+ });
+ });
+ removeNextItem();
+ },
+ error: function(j, t, error) {
+ ok(false, "Could not delete schema: " + schema + ": " + error);
+ },
+ complete: function() { start(); }
+ });
+ });
+ }
+
+ removeNextItem();
+};
+
+corona.addSchemas = function(callback) {
+ var schemas = [
+ {
+ "uri": "/schema/foo.xs",
+ "schema": '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="foo"><xs:complexType><xs:sequence><xs:element name="bar" type="xs:string"/><xs:element name="baz" type="xs:string"/></xs:sequence></xs:complexType></xs:element></xs:schema>',
+ "shouldSucceed": true
+ }
+ ];
+
+ var processingPosition = 0;
+ var addNextItem = function() {
+ addItem(schemas[processingPosition]);
+ processingPosition++;
+ };
+
+ var addItem = function(schema) {
+ if(schema === undefined) {
+ callback.call();
+ return;
+ }
+
+ asyncTest("PUT: " + schema.uri, function() {
+ $.ajax({
+ url: "/manage/schema?uri=" + schema.uri,
+ data: schema.schema,
+ type: "PUT",
+ success: function() {
+ addNextItem();
+ ok(schema.shouldSucceed, "Schema added: " + schema.uri);
+ },
+ error: function(j, t, error) {
+ ok(!schema.shouldSucceed, "Could not add schema: " + error);
+ addNextItem();
+ },
+ complete: function() { start(); }
+ });
+ });
+ };
+
+ addNextItem();
+};
+
+$(document).ready(function() {
+ module("Schemas");
+ asyncTest("Schema tests", function() {
+ $.ajax({
+ url: '/manage',
+ success: function(info) {
+ corona.removeSchemas(info, function() {
+ corona.addSchemas(function() {
+ });
+ });
+ },
+ error: function() {
+ ok(false, "Could not fetch server info");
+ },
+ complete: function() { start(); }
+ });
+ });
+});
View
@@ -0,0 +1,18 @@
+<html>
+ <head>
+ <title>Schema qunit tests</title>
+ <script type="text/javascript" src="js/qunit.js"></script>
+ <script type="text/javascript" src="js/jquery.js"></script>
+ <link rel="stylesheet" href="css/qunit.css" type="text/css" media="screen" />
+ </head>
+ <body>
+ <script type="text/javascript" src="js/common.js"></script>
+ <script type="text/javascript" src="js/schema-tests.js"></script>
+ <h1 id="qunit-header">Corona schema management unit tests</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture">test markup, will be hidden</div>
+ </body>
+</html>

0 comments on commit f08dd27

Please sign in to comment.