ruby_test.rb
This is a simple example that demonstrate how to use the Zorba XQuery Engine to create, compile, and execute queries.
# Copyright 2006-2008 The FLWOR Foundation.
#
# 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.
require '/home/jenkins/.jenkins/ubuntu-remote-queue/jenkins-BuildZorbaUbuntu-462/source/zorba/build/swig/ruby/zorba_api'
def example1(zorba)
xquery = zorba.compileQuery("1+2")
print xquery.printPlanAsDOT()
print xquery.execute()
xquery.destroy()
end
def example2(zorba)
xquery = zorba.compileQuery("(1,2,3,4,5)")
iter = xquery.iterator()
iter.open()
item = Zorba_api::Item::createEmptyItem()
while iter.next(item) do
print item.getStringValue()
print "\n"
end
iter.close()
iter.destroy()
xquery.destroy()
end
def example3(zorba)
begin
xquery = zorba.compileQuery("1 div 0")
print xquery.execute()
rescue RuntimeError => e
print e
ensure
xquery.destroy()
end
end
def example4(zorba)
begin
xquery = zorba.compileQuery("for $i in (1,2,")
print xquery.execute()
rescue RuntimeError => e
print e
ensure
if not xquery.nil? then
xquery.destroy()
end
end
end
def example5(zorba)
dataManager = zorba.getXmlDataManager()
docMgr = dataManager.getDocumentManager()
docIter = dataManager.parseXML("<books><book>Book 1</book><book>Book 2</book></books>")
docIter.open()
doc = Zorba_api::Item::createEmptyItem()
docIter.next(doc)
docIter.destroy()
docMgr.put("books.xml", doc);
xquery = zorba.compileQuery("doc('books.xml')
print xquery.execute()
xquery.destroy()
docMgr.remove("books.xml")
end
store = Zorba_api::InMemoryStore.getInstance()
zorba = Zorba_api::Zorba.getInstance(store)
print "Example1:\n"
example1(zorba)
print "\n\n"
print "Example2:\n"
example2(zorba)
print "\n"
print "Example3:\n"
example3(zorba)
print "\n\n"
print "Example4:\n"
example4(zorba)
print "\n\n"
print "Example5:\n"
example5(zorba)
print "\n\n"
zorba.shutdown()
Zorba_api::InMemoryStore.shutdown(store)