Loading TOC...

MarkLogic 12 EA 1 Product Documentation
op:generate-view

op:generate-view(
   $schemaName as xs:string,
   $viewName as xs:string,
   [$columnDeclarations as map:map*]
) as element()

Summary

This method generates a view that encapsulates a query. Insert the generated XML into the schemas database and then use the op.fromView op:from-view accessor to use the generated view.

Parameters
$schemaName The schema name that can be passed to op:from-view() when accessing the generated view.
$viewName The view name that must be passed to op:from-view() when accessing the generated view.
$columnDeclarations Optional type and nullability declarations for some or all of the columns of the view.
  • name

    The name of the column in the QBV (required)

  • type

    The type to define the column as. Any TDE column type is valid for this field. (required)

  • nullable

    True or false. If you expect null results in this column, set this field to true. (optional - default is true)

  • collation

    This is specifically for string types. Provide the collation you expect the result to be (optional - default is no collation)

  • invalidValues

    Provide skip or reject to this field. If skip is provided, the engine skips rows that contain values whose type, nullability, or collation don't match your specification. If reject is provided, an error is thrown if a value that does not match the column criteria is found (optional - default is skip)

Example

xquery version "1.0-ml";

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

op:from-view("main", "employees")
   => op:where(cts:collection-query("/division/manufacturing"))
   => op:select(("EmployeeID", "FirstName", "LastName"))
   => op:order-by("EmployeeID")
   => op:generate-view("main", "manufacturingEmployees")
  

Example

xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";
let $plan1 := op:from-literals((
            map:entry("rowId", 1) => map:with("colorIdShape", 1) => map:with("desc", "ball"),
            map:entry("rowId", 2) => map:with("colorIdShape", 2) => map:with("desc", "square"),
            map:entry("rowId", 3) => map:with("colorIdShape", 1) => map:with("desc", "box"),
            map:entry("rowId", 4) => map:with("colorIdShape", 1) => map:with("desc", "oval"),
            map:entry("rowId", 5) => map:with("colorIdShape", 5) => map:with("desc", "circle")
            ))
let $plan2 := op:from-literals((
            map:entry("colorId", 1) => map:with("colorDesc", "red"),
            map:entry("colorId", 2) => map:with("colorDesc", "blue"),
            map:entry("colorId", 3) => map:with("colorDesc", "black"),
            map:entry("colorId", 4) => map:with("colorDesc", "yellow")
            ))
let $output := $plan1
    => op:join-inner($plan2, op:on(op:col("colorIdShape"), op:col("colorId")))
    => op:select(("rowId", op:as("description", op:col("desc")), "colorId", "colorDesc"))
    => op:order-by("rowId")
return $output => op:generate-view('ThreeParams', 'ThreeColsSelected',(
      map:entry("name", "rowId") => map:with("type", "integer") => map:with("nullable", fn:true()) => map:with("invalidValues", 'reject'),
      map:entry("name", "description") => map:with("type", "string") => map:with("invalidValues", 'reject'),
      map:entry("name", "colorId") => map:with("type", "integer") => map:with("nullable", fn:false()) => map:with("invalidValues", 'skip')
      ))
  

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.