The predefined query will be saved into the database document.
/* get the service manager */
xContext = UNO.connect()
XMcf = xContext~getServiceManager
/* retrieve the DatabaseContext and get its XNameAccess interface */
xNameAccess = xMcf~createInstanceWithContext(-
"{%see com.sun.star.sdb.DatabaseContext}", xContext)~{%see com.sun.star.container.XNameAccess%XNameAccess}
/* let's use the datasource mysql-test */
dataSource = xNameAccess~getByName("mysql-test")
/*create an empty QueryDefinition and request it's XPropertySet interface*/
xQueryDefinitionsSupplier = dataSource~{%see com.sun.star.sdb.XQueryDefinitionsSupplier%XQueryDefinitionsSupplier}
xQDefs = xQueryDefinitionsSupplier~getQueryDefinitions
xSingleServiceFactory = xQDefs~{%see com.sun.star.lang.XSingleServiceFactory%XSingleServiceFactory}
xPropertySet = xSingleServiceFactory~createInstance~{%see com.sun.star.beans.XPropertySet%XPropertySet}
/* define the query */
xPropertySet~setPropertyValue("Command",-
"SELECT firstname, lastname, product.name "-
"FROM customer, sales, product "-
"WHERE customer.cid = sales.cid AND sales.name = product.name")
xPropertySet~setPropertyValue(-
"EscapeProcessing",box("BOOL",.true))
/* insert it into the query definition container */
xNameContainer = xQDefs~{%see com.sun.star.container.XNameContainer%XNameContainer}
queryName = "Query_names+products"
IF xNameContainer~hasByName(queryName) THEN
DO
say "removing existing query definition...."
xNameContainer~removeByName(queryName)
END
say "insert Query to the container...."
xNameContainer~insertByName(queryName,xPropertySet)
/* store the database document */
/* retrieve the XDocumentDataSource interface of the data source */
xDocumentDataSource = dataSource~{%see com.sun.star.sdb.XDocumentDataSource%XDocumentDataSource}
/* get the attribute 'DatabaseDocument' */
xOfficeDatabaseDocument = xDocumentDataSource~getDatabaseDocument
/* store the database document and with it the query definition */
xStorable = xOfficeDatabaseDocument~{%see com.sun.star.frame.XStorable%XStorable}~store
say "storing database document...."
::requires UNO.cls -- get UNO support