/* * Copyright 2005,2006 WSO2, Inc. http://wso2.com * * 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_once "wso2/DataServices/DataService.php"; require_once "constants.php"; // database configuration $config = array("db" => "mysql", "username" => DB_USERNAME, "password" => DB_PASSWORD, "dbname" => "ds", "dbhost" => "localhost"); // the input format array(param_name => SQL_type) $inputFormat = array("lastName" => "STRING", "firstName" => "STRING"); // the output format (visit the API from http://wso2.org/wiki/display/wsfphp/API+for+Data+Services+Revised) $outputFormat = array("resultElement" => "customer-addresse", "rowElement" => "customer-address", "elements" => array("contactlastname" => "CONTACTLASTNAME", "contactfirstname" => "CONTACTFIRSTNAME", "addressline1" => "ADDRESSLINE1", "addressline2" => "ADDRESSLINE2", "city" => "CITY", "state" => "STATE", "postalcode" => "POSTALCODE", "country" => "COUNTRY"), "attributes" => array("customernumber" => "CUSTOMERNUMBER"), "elementsOrder" => array("customernumber", "contactfirstname", "contactlastname", "addressline1", "addressline2", "city", "state", "postalcode", "country")); // the sql to execute $sql = "select CUSTOMERNUMBER, CONTACTLASTNAME, CONTACTFIRSTNAME, ADDRESSLINE1, ADDRESSLINE2, CITY, STATE, POSTALCODE, COUNTRY from Customers where CONTACTLASTNAME = ? and CONTACTFIRSTNAME = ?"; // operations are consists of inputFormat (optional), outputFormat(required), sql(sql), input_mapping(optional) $operations = array("customerAddress" => array("inputFormat" => $inputFormat, "outputFormat" => $outputFormat, "sql" => $sql)); $my_data_service = new DataService(array("config" => $config, "operations" => $operations, "serviceName" => "CustomerAddress")); $my_data_service->reply();
// building the output elements order for the customerOrdersSQL $customerOrdersSQL_elements_order = array("Order_number", "Order_date", "Status", "OrderId", "query1"); // building the output format for the customerOrdersSQL $customerOrdersSQL_output_format = array("resultElement" => "Orders", "rowElement" => "Order", "elements" => $customerOrdersSQL_elements, "attributes" => array("OrderId" => "ORDERNUMBER"), "queries" => $customerOrdersSQL_queries, "elementsOrder" => $customerOrdersSQL_elements_order); // building the sql for the customerOrdersSQL $customerOrdersSQL_sql = "select o.ORDERNUMBER,o.ORDERDATE, o.STATUS,o.CUSTOMERNUMBER from ds.Orders o"; // building the input mapping for the customerOrdersSQL $customerOrdersSQL_input_mapping = NULL; $customerOrders = array("inputFormat" => $customerOrdersSQL_input_format, "outputFormat" => $customerOrdersSQL_output_format, "sql" => $customerOrdersSQL_sql, "inputMapping" => $customerOrdersSQL_input_mapping); // ------------finished queries for customerOrders-------------- // ----------building the queries for customerName ------------ // building the input format for the customerNameSQL $customerNameSQL_input_format = array("customerNumber" => "INTEGER"); // building the output elements for the customerNameSQL $customerNameSQL_elements = array("Name" => "CUSTOMERNAME"); // building the output elements order for the customerNameSQL $customerNameSQL_elements_order = array("Name"); // building the output format for the customerNameSQL $customerNameSQL_output_format = array("resultElement" => "Customers", "rowElement" => "Customer", "elements" => $customerNameSQL_elements, "elementsOrder" => $customerNameSQL_elements_order); // building the sql for the customerNameSQL $customerNameSQL_sql = "select c.CUSTOMERNAME from ds.Customers c where c.CUSTOMERNUMBER = ?"; // building the input mapping for the customerNameSQL $customerNameSQL_input_mapping = array("customerNumber" => "CUSTOMERNUMBER"); $customerName = array("inputFormat" => $customerNameSQL_input_format, "outputFormat" => $customerNameSQL_output_format, "sql" => $customerNameSQL_sql, "inputMapping" => $customerNameSQL_input_mapping); // ------------finished queries for customerName-------------- // building the operations array $operations = array("customerOrders" => $customerOrders, "customerName" => $customerName); // create the DataService object and reply $ds = new DataService(array("operations" => $operations, "config" => $config, "serviceName" => "NextedQuerySample")); $ds->reply();
// ============= Then get students by name operation ======================== // ------------ Here we first prepare the inner query ------------- $inner_query_input_format = array("studentID" => "STRING"); $inner_query_output_format = array("resultElement" => "subjects", "rowElement" => "subject", "elements" => array("name" => "subjectName", "marks" => "marks")); $inner_query_sql = "SELECT subjectName, marks FROM Marks m, Subjects s where m.studentId = ? and m.subjectID = s.subjectId"; $inner_query = array("inputFormat" => $inner_query_input_format, "outputFormat" => $inner_query_output_format, "sql" => $inner_query_sql); // ------------- Secondly we prepare the outer query and set the inner query in output format ----- $out_query_input_format = array("name" => "STRING"); $output_format = array("resultElement" => "students", "rowElement" => "student", "elements" => array("name" => "studentName", "age" => "studentAge", "address" => "studentAddress"), "queries" => array($inner_query)); $sql = "SELECT * FROM Students where StudentName = ?"; $get_students_with_name_op = array("inputFormat" => $input_format, "outputFormat" => $output_format, "sql" => $sql); $get_students_with_name_url = array("HTTPMethod" => "GET", "RESTLocation" => "students/{name}"); // ================================================================== // ============= Then get marks per student, per subject operation ======================== $input_format = array("student" => "STRING", "subject" => "STRING"); $output_format = array("resultElement" => "Marks", "rowElement" => "mark", "texts" => array("marks")); $sql = "SELECT marks FROM Marks, Subjects, Students where StudentName = ? and SubjectName = ? " . "and Marks.subjectId = Subjects.subjectId and Marks.studentID = Students.StudentId;"; $marks_per_student_per_subject_op = array("inputFormat" => $input_format, "outputFormat" => $output_format, "sql" => $sql); //$marks_per_student_per_subject_url = array("HTTPMethod" => "GET", "RESTLocation" => "marks/{student}/{subject}"); $marks_per_student_per_subject_url = array("HTTPMethod" => "GET", "RESTLocation" => "students/{student}/marks/{subject}"); // ================================================================== // list of operations $operations = array("getSubjects" => $get_subjects_op, "getSubjectsWithName" => $get_subjects_with_name_op, "marksPerStudentPerSubject" => $marks_per_student_per_subject_op, "getStudentsWithName" => $get_students_with_name_op, "getStudents" => $get_students_op); // list of rest url mappping (operation => url) $restmap = array("getSubjects" => $get_subjects_url, "getSubjectsWithName" => $get_subjects_with_name_url, "getStudents" => $get_students_url, "marksPerStudentPerSubject" => $marks_per_student_per_subject_url, "getStudentsWithName" => $get_students_with_name_url); // creating DSService and reply $service = new DataService(array("config" => $config, "operations" => $operations, "RESTMapping" => $restmap)); $service->reply(); ?>