Home

PHP Integration with object relational DBMS

image

Contents

1. Figure 17 Database definition for the addressbook To generate the overview of all persons the query as follows is used select p name p from person p With this query the OID string and the name of that person is retrieved out of the database If there is a person object without a name it won t be displayed It is still left to retrieve the email addresses the telefone numbers and the addresses from that person To test different methods all email addresses of one person are fetched by using the fast path interface fct_emails amos getfunction SESSION con PERSON EMAILS gt VECTOR CHARSTRING emails scan amos_callfunction _SESSION con fct_emails person tpl emails tpl amos_createtuple _SESSION con amos getrow emails scan emails_tpl emails amos getsegelem emails tpl 0 for i 0 i lt amos_getarity emails i email amos getstring emails i print email amos_closetuple emails amos_closetuple emails tpl amos_closescan emails_scan The telefone numbers are fetched by sending a query to Amos II tels scan amos_query _SESSION con select tels p from person p where p pid tels tpl amos createtuple SESSION con amos getrow tels scan tels tpl tels amos getsegelem tels tpl 0 for i 0 i lt amos_getarity tels i tel amos getstring tels i print tel 46 amos_closetuple tels a
2. result_int amos getint tpl 1 5 4 5 amos setint Assigns an integer value to an element of a tuple Syntax void amos setint long amos tuple long pos long value Description Function amos setint stores a value of type long to the element in tuple amos tuple at position pos This function is not intended to set an integer value to a result tuple e g result of 37 amos_getrow The reaction would be a fatal error But it is possible to set the element value of a tuple more than once even with different types 5 4 6 amos getdouble Returns a double precision floating point number Syntax double amos getdouble long amos_tuple long pos Description Use function amos getdouble to fetch a double value It takes the element value of tuple amos_tuple at position pos and returns the double value The specified element must hold a double otherwise an error message is printed and script execution is stopped The code line as follows gives an example on how to gain a double value at position 1 from tuple tp1 result_double amos_getdouble tpl 1 5 4 7 amos_setdouble Assigns a double value to an element of a tuple Syntax void amos_setdouble long amos_tuple long position double value Description Function amos_setdouble stores a value of type double to the element in tuple amos_tuple at position pos This function is not intended to set a double value to a result tuple e g result of a
3. 6 Example Program The Addressbook 6 1 The database on on nn 6 2 MN E 6 3 Program test results 2 2222 2 Coon nn 7 Amos Il tuples and PHP arrays 7 1 Tuple to array conversion CC rn nn 7 2 Array to tuple conversion oaoa 8 Conclusion and future work References 45 45 45 50 50 5l 52 55 56 1 Introduction The definition and properties of an API between Amos Il and PHP are the main topics of this report Amos II Active Mediator Object System is an extensible functional multi database system 1 Functional queries expressed in AmosQL can be executed by one database or over a federation of distributed databases AmosQL is a query language similar to the OO parts of SQL 99 Furthermore Amos II can be set up as a stand alone main memory object relational DBMS Two kinds of interfaces are offered between Amos II and the programming languages C 5 or Java 6 the callin interface and the callout interface To call Amos II from an application the callin interface can be used Establishing connections sending queries and dealing with result sets are tasks of this interface Contrary the callout interface is able to call external functions from C or Java Once known to Amos II these external functions can be used in AmosQL queries The callin and callout interfaces permit the development of wrappers Wrappers are small program modules that can access external data for example XML documents relational da
4. Zend Engine Web Browser Server Interface Figure 6 The structure of PHP 3 2 1 Internal PHP structure First of all the structure of PHP must be discussed As already mentioned PHP is a web script interpreter To implement such an interpreter three parts are necessary e The interpreter that analyses the input code compiles it and executes it e The functionality part offers function calls to the user e An interface to the webserver Therefore PHP is split up into two parts the PHP language and the real core of PHP named Zend Zend contains the interpreter and offers in addition some basic functionality to the user via the Zend API PHP takes the rest of the functionality and the interface part Figure 6 illustrates the structure of PHP PHP can be extended at three points e External Modules e Built in Modules e Zend Engine External modules are loaded at script runtime by using the d1 modulename command A shared object is loaded from the disk and all functionality of the module is made available to the current script At the end of the script the module is unloaded and discarded from memory An advantage of this procedure is when changing the module only that module must be recompiled PHP itself need not be recompiled and thus the size of PHP remains small and uses less memory But as the module has to be loaded every time a script is bei
5. Retrieves the OID string of a function Syntax string amos_getfunction long amos_con string function_name Description Before calling amos_getfunction the OID of the called function is needed Normally the user has only knowledge of the function name e g FUNCTION ARGUMENTS gt VECTOR Thus a function is necessary to retrieve the OID from the function name Function amos_getfunction takes a connection amos_con and an Amos II function name function name as parameters The return value is an OID string that represents the function name 5 5 2 amos_gettype Returns the OID string of a type 41 Syntax string amos_gettype long amos_con string typename Description Function amos_gettype is the analogous to amos_getfunction for types It computes in de pendence of the connection amos_con the OID string of a specified type typename The OID string of a type is for example necessary for creating and deleting new objects see amos createobject and amos_deleteobject at chapter 5 6 5 5 3 amos_callfunction Calls a specified function Syntax long amos_callfunction long amos_con string function_oid long fct_argl Description With Amos II connection amos con an Amos II function specified by OID string func tion_oid is called by using this PHP function Parameters of the Amos II function must be stored in a tuple and passed through parameter fct_argl The return value is an index pointig to the resu
6. 35 Mod TAMOS NX o sewed Sake La Sea ea ee ed aa ee has 35 Tuple Interface Asiri ma tele re ee eek a etd el le 36 5 41 amos getarity asus sete a e ee a 36 9 4 2 amos_createtuple 5 0 0 0 00000 36 9 4 3 camos closetuples im ba Ste nen en Be 37 DAA amos getinto ursos a ee ee 37 9 4 9 AMOS S EbANE tee os he e er ae ee ee ee es 37 5 4 6 amos getdouble ii ee doe Oe ee ia ed oe a a 38 9 4 7 amos setdouble 2 002 ee eee eee eee 38 5 4 8 amos_getstring 2 0 0000000 ee eee 38 5 4 9 amos setstring 0 0000 eee eee 39 5 4 10 amos getobjectelem 0 20 02 00 000 39 5 4 11 amos_setobjectelem 0 0 0000000 39 5 4 12 amos g ts gelem ass i 0 0000000000000 40 5 4 13 amos_setseqelem 0 000000 00000004 40 9 4 14 amos ielemSize visa tuo A A 40 Fast path function calling 000 eee 40 8 9 1 amos getfunction e 41 9 9 2 AMOS PETTY PO gt ene an sn A e ee te a 41 5 9 3 amos callfunction o eaae e 42 5 5 4 amos addfunction 2 2 2 Coon 42 5 5 5 amos setfunction 2 2 Corn 43 5 9 6 amos_remfunction 0 0 00 2 eee ee 43 Object creation and deletion 2 ee 43 8 6 1 amos createobject 0 000000000000 e 43 5 6 2 amos deleteobject 0 0 00 00000000008 44 Transaction control 2 2 2 Coon ee 44 OF fel amos Commit nen en ae ae LH Bose 44 5 1 2 amos rollb ck 2 hata an er ee Dans 44
7. 4 3 1 JSP structure JavaServer Pages short JSP are based on Java Servlets Servlets are Java classes that are used to extend the capability of servers Through request response architecture a servlet communicates with clients e g browsers A JSP page is a simple ASCII textfile that contains two kinds of data First static template data that is responsible for creating static content of a webpage Secondly JSP elements are provided to add a dynamic content to the 27 Enterprise Servers Data Sources Application Server Figure 12 The JSP Model 2 structure proposed in 12 text file JSP is able to project the full dynamic functionality of Java Servlets Technology but provides a more natural approach to create static content 11 The combination of all described technologies results in the JSP Model 2 structure illustrated by figure 12 Let s follow a request initiated by a browser The request is received by the Servlet that works as a controller The data transfered through the request has to be stored Therefore a JavaBean is instantiated that contains the sent data Additionally data from exernal data sources may be loaded into this JavaBean instance As a third step the controller starts the view generation in other words an HTML page is generated out of the JSP page JSP is used for the so called presentation layer For binding the dynamic data into the generated HTML page data from the JavaBean instance is used As soon as
8. Additionally a PHP session variable action is created that indicates the action at the beginning of script index php For adding a new person action is set to new Back to the overview the posted data is read and added to the database by using the fast path interface 47 First a new person object has to be created Then PHP function amos_addfunction 5 5 4 is used to add the new data A code extract is given that will add all email addresses create a new person object p type amos _gettype _SESSION con person new person amos createobject SESSION con p type new person tpl amos_createtuple _SESSION con 1 amos setobjectelem new person tpl O new person check email entries if isset _POST email data if isset _POST email2 two given email entries gt tuple arity 2 data amos_createtuple _SESSION con 2 amos setstring data 0 _POST email amos_setstring data 1 _POST email2 unset _POST email unset _POST email2 else one given email address gt tuple arity 1 data amos_createtuple _SESSION con 1 amos_setstring data 0 _POST email unset _POST email add function data data_seq amos_createtuple _SESSION con 1 amos setsegelem data seg 0 data amos addfunction SESSION con fct emails new person tpl data seg a
9. It is a distributed mediator system consisting of one or several mediator peers These peers can communicate via the Internet using TCP IP Each peer offers its own virtual functional database layer consisting of e data abstractions that provide transparent functional views for clients and other me diator peers to access data sources Application Mediator 3 Name Server Mediator Mediator Legend Amos Il peer Server access REAGIR Web Service External system Name server access Y Figure 2 Example of an Amos II system with three mediators 1 e a functional query language AmosQL e a storage manager e a recovery manager and e a transaction manager The core of Amos Il is a open light weight and extensible database management system DBMS In Amos II it is possible to build up layers of peers with a dynamic communication topology A distributed mediator query optimizer optimizes this communication topology To compute an optimized execution plan for a given query data and schema information are exchanged between the peers In figure 2 the high level mediator defines mediating functional view integrating data from them The views include facilities for semantic reconciliation of data retrieved from the two lower mediators The two lower mediators translate data from a wrapped relational database and a web server respectively They have knowledge of how to translate Amos
10. PHP functions is strongly reduced as no set and get functions are necessary to fill and read tuples e long amos_connect string dbname e void amos_close long amos_con e long amos_query long amos_con string query e void amos_closescan long amos_scan e bool amos_eos long amos_scan e void amos_getrow long amos_scan array empty_array e void amos_next long amos_scan e string amos getfunction long amos con string function name e string amos gettype long amos con string typename e long amos_callfunction long amos_con string function_oid array fct_argl e void amos_addfunction long amos_con string function_oid array argl array resl e void amos_setfunction long amos_con string function_oid array argl array resl e void amos_remfunction long amos_con string function_oid array argl array resl e amos_createobject long amos_con string type e amos deleteobject long amos con string type_oid e amos commit long amos con e amos rollback long amos con This implementation would be a great advantage for the PHP user He would not have to create a tuple and fill each entry with a seperate PHP function Instead a PHP array could be created and passed to functions of the Amos II extension Again for handling result rows this would be a great advantage The user retrieves an already filled array containing all the data of one resulting row instead of dealing with tuples that have to be read by the get methods o
11. amos_getrow scan tuple row PHP method for printing arrays print_r row 5 3 5 amos next Advances the scan curser position one step forward Syntax void amos next long amos scan Description 35 i 0 scan amos_query con Select name f from function f tpl amos_createtuple con while amos_eos scan amos getrow scan tpl val amos_getstring tpl 0 print row i val amos next scan i i 1 Figure 14 Example PHP code for using the tuple and scan interface The current tuple is set to the next tuple in scan amos_scan Notice that the scan handle will automatically be closed by Amos II when all tuples have been read or another query result is assigned to the scan An example code is shown in figure 13 5 4 Tuple interface The tuple interface offers PHP functions to set and get data to and from a tuple Figure 14 shows an example how to use the tuple interface in combination with the scan interface 5 4 1 amos getarity Returns the arity of a tuple Syntax long amos getarity long amos_tuple Description Tuples are representing a complete row of a query result This functions returns the number of elements in tuple amos tuple All elements in a tuple are enumerated beginning with index O for the first element 5 4 2 amos createtuple Creates an empty tuple Syntax long amos_createtuple long amos_con long arity Descrip
12. an HTML has been generated it is returned as a response to the request to the browser which displays the dynamic generated web page 4 3 2 General comparison PHP and JSP are not entirely different but they exist for a different purpose While PHP does everything concerning processing logic and layout in the same page JSP should only be used on the presentation layer The following list gives a comparison translated from 13 1 Common features of PHP and JSP e server side scripting languages for HTML integreation e requires an HTTP servermodule with JSP PHP runtime environment e pages must be read and interpreted e offer session handling independent of operating system independent of server 28 programming is made in Java no new programming language access to the complete Java API and Java classes supports separation of layout and program logic unique access on almost all relational database systems via JDBC large use of resources for Java runtime environment 3 PHP typeless variables and dynamic array structures simplify web programming automatic processing of formulars high error tolerance especially for programming with variables contains a great amount of modules e g for data compression pay services XML processing offers a lot of macros and functions to write external modules supported DB functionality is dependent on the database specific functions for
13. are objects types and functions 2 3 1 Types For each entity type in an Entity Relationship diagram an Amos II type is created Types unite objects with similar properties Furthermore multiple inheritance is offered what means a supertype subtype hierarchy can be built An instance of a type is always an instance of allsupertypes If an object inherits from more than one type it gains all properties from all supertypes Two kinds of types are existing stored and derived types Derived types are mainly used for reconciliation while the stored types are defined and stored in an Amos II database The following command creates a person and an assistant type in Amos II create type Person create type Assistant under Person Every Amos II peer offers a basic type hierarchy that can be seen in figure 4 The root element is named Object All system and user defined type names are stored in a type named Type Again all functions described in chapter 2 3 2 are instances of a type named Function When a user defines a type it is always a subtype of type Userobject 10 name SearchEngine E ER Bag GE ed a User defined s types Figure 4 System type hierarchy 2 3 2 Functions Functions model the relationship between objects model properties of objects and computa tions over objects In functional queries and views they are representing the basic primitives As already mentioned functions are instances of the t
14. array and fill it with Zend macros and functions A PHP array is stored in a zval container new_array in the example below which has to be initialized zval new_array MAKE STD ZVAL new array array init new array Now this array is ready to fill Another possibility is to pass an empty array to a PHP function as parameter and initialize it with this function This second method is applied in PHP function amos getrow which has been extended to amos getrow long scan long tpl array tofill Now data from the current row represented as a tuple handle in C can be copied into the PHP array by using Zend macros The copy process is dependent on the data type stored in ol the tuple A string must inserted to the PHP array as a string The same for integers and doubles An object handle must be transformed to an OID string and added as a string to the array A tuple can also contain a sequence element The sequence is represented again by a tuple Consequently the tuple must be read recursively and PHP arrays must be stored into a PHP array position Concerning arrays PHP is very flexible One array can contain data of any type including arrays Figure 18 shows the algorithm that copies all kind of tuple data to a PHP array In dependence of the Amos II type a PHP function add index xxx is used to copy the data The filled array can be returned by using the code fragment as follows 8 Make this our array 0 I do not use add in
15. data with higher levels of mediator layers or with applications The task of a mediator is to answer queries that are sent from applications to the mediator s common data model The query will be split depending on the data and capabilities of the target data sources As a mediator can regard other mediators as data sources a network of mediators consisting of several layers can be created In such a network only primitive mediators should have access to data sources Then higher layers consisting of an advanced data abstraction can be accessed by applications One mediator can access different autonomous data sources like databases XML files or object stores but never directly Each data source can be accessed through software inter faces called wrapper Queries sent from the mediator are translated by the wrapper to a data source specific format and thus hides the heterogenity of that data source After that the wrapper retrieves the query result which has to be translated again into the common data model of the corresponding mediator The translated data is passed to the mediator There all results from all data sources are integrated and returned to the application Figure 1 illustrates the way of a query beginning at the application through the mediator server and the wrappers to the data sources Then the result set takes the same way up 2 2 The Amos II architecture The Amos II system is based on the mediator wrapper architecture
16. how can result sets be handled in C code There are two answers for the first question e On the one hand a tight connection to an Amos II system can be established Here Amos II is directly linked with an application program in C and thus runs as an embedded database inside the application As the same address space is used this is the fastest possibility between an application and Amos II However only a single application can be linked to Amos II Another disadvantage with the tight connection is that errors appearing in the application may also cause the crash of Amos II e Amos II can as already described act as an server An application can establish a client server connection to the Amos II peer With this connection several applications can access the same Amos II concurrently If one application crashes it will have no influence on Amos II Speed is a disadvantage for this kind of connection A connection handle to an Amos II database is stored in a C variable of type a_connection The connection is established by calling int a_connect a_connection c char dbname int catcherror with dbname being the name of the database peer to connect to If dbname is an empty string then a tight connection is established Otherwise this string must be a name of an Amos II mediator peer known to the nameserver In the PHP extension the connection handle is stored in an array of type amos_con See figure 5 for more details The element status ind
17. is printed Timestamps are taken before and after query execution and after printing all tuple data of the scan Thus query execution time tquery result set printing time taisplay and total time trotal can be determined and calculated Time is measured in the current Unix timestamp with microseconds by calling PHP function microtime for description see 8 A test database with 10000 person objects having a name has been created for the test To test a selection query one of the persons has an address Therefore an AmosQL script has been generated which fills the database at Amos Il startup with person objects having 24 function sess open save path session name global sess save path sess session name sess save path save path sess session name session name return true function sess_close return true function sess_read id global sess save path sess session name sess_file sess_save_path sess_ id if fp Ofopen sess file r sess data fread fp filesize sess file return sess data else 4 return Must return here function sess write id sess data global sess save path sess session name sess file sess save pathisess Sid if fp Ofopen sess file w return fwrite fp sess data else return false function sess_destroy id global sess save path sess session name amos gc session id sess file se
18. number of PHP arguments int param count ZEND NUM ARGS parsing arguments switch param count parse parameters here case x default WRONG PARAM COUNT check parameters do function action return possible values Figure 8 Basic structure for an PHP function task to pass module information to Zend in order to inform the engine about the module contents This function can be implemented by using the macro ZEND GET MODULE Since this feature is only required if the module is built as a dynamic extension the implementation must be surrounded by a conditional compilation statement that can again be seen in figure 7 Thus if this module would be compiled as a built in extension the implementation of get module is simply left out Now the basic module structure is described but there still remains the layout of an PHP function The next section considers this point 3 2 3 PHP function structure The general structure of an PHP function and some useful macros are described in this section First of all the new module function must be initiated with macro PHP FUNCTION The macro takes the function name as parameter as it can be seen in figure 8 The interior structure of the function starts with initializing all necessary variables for this function Especially the number of function parameters can be retrieved by the macro ZEND NUM ARGS O The next block deals with parsing the parameters
19. page The first function parameter defines the error type which can have influence on script execution There exist different error types in PHP but for this project only two of them are of interest e E_ERROR Signals an error and the script execution terminates immideately e E WARNING Signals a generic warning but the script execution continues Integrating this with the Amos II management system results in two define statements one representing a warning and the other an error define AMOS ERROR zend_error E_ERROR Error d s a_errno a_errstr define AMOS WARNING zend error E ERROR Error d s a_errno a_errstr 3 6 Garbage Collection Every time a connection is established or queries are sent to Amos II connection scan and tuple handles are initialized and they are occupying memory After termination of a script or a session this storage has again to be freed That means two different ways for dealing with garbage collection have to be considered First on script termination a garbage collector function should be called that frees all used memory Except the memory that is needed for a session for which the connetion should stay up The Amos II extension offers a completely automatic method to deal with this kind of garbage collection The most important thing is to retrieve the connection handles that can be freed To put in concrete terms as soon as a new connection is going to be established a function fo
20. path function call is again a scan handle It can be treated the same way like the result sets from the embedded query call During this chapter PHP functions are introduced to call functions and to add set and 40 create a new tuple for storing function arguments argl amos createtuple con 1 amos_setobjectelem argl 0 person_oid get function id fct_id amos_getfunction con person name gt charstring execute function scan amos_callfunction con argl fct_id handle scan result by using tuple interface Figure 15 Example PHP code for using the fast path interface remove data of a function But how to pass parameters to the called functions It can be done by using argument lists that contains all arguments A tuple represents this list and must be allocated with the correct arity number of parameters by using tpl amos_createtuple con arity Now this tuple can be filled with argument values by using tuple update functions The first parameter should be stored at position 0 the first tuple element etc Finally this list can be passed as parameter to the corresponding PHP function amos_callfunction Figure 15 shows an example on how to retrieve the name of a person by using the fast path interface Concerning the speed this interface is much faster than the embedded query call as no query parsing and optimization has to be done 5 5 1 amos_getfunction
21. the connection itself In case of a session the session identifier is stored in combination with the connection handle If a correct working session save handler is implemented for the script which has to be done by the user everything corresponding to the connection will be deallocated as soon as the session ends See chapter 3 6 for more details If no error occured the index that holds the new handle is returned to the user otherwise 1 is returned 5 2 2 amos_close Closes a connection and initiates garbage collection Syntax void amos_close long amos_con Description Function amos_close is offered to the user to control garbage collection in addition to the other methods that are described so far If the passed index amos_con contains a valid connection handle the garbage collection routine is started All tuples and scans belonging to that connection are deallocated At last the connection handle is freed The array index is signaled to be free by setting a flag 5 3 Scan interface The scan interface offers PHP functions to send queries to Amos II peers and to run through returned result sets Figure 13 show how the scan interface can be used 33 sending query current_scan amos query con query handling result set while amos_eos current_scan amos getrow current scan tpl code to retrieve elements of tuple tpl amos next current scan Figur
22. the current session data 23 e write id sess_data writes the current session data e destroy id destroys a session e gc maxlifetime starts garbage collection after a given time To make these functions available set them by using session set save handler open close read write destroy gc An example implementation for a session save handler can be seen in figure 11 It is an extended version to the one offered by 8 The most important part for collecting garbage are the functions destroy and gc There must be a call to the Amos II extension to start clearing memory amos_gc session id This functions checks each stored connection whether it belongs to the current session If the session identifiers are the same deallocation process starts Now all connection handles can be freed when script process is complete or a sessions ends Scan and tuple handles are stored in seperate arrays As the connection is stored for each scan and tuple handle a simple loop runs through these arrays and frees all with the connection associated scans and tuples A Performance observations 4 1 Performance The performance is tested by using e Amos II Release 6 v7 e Apache HTTP Server 2 0 e PHP 4 3 4 The test machine is a Pentium with 800MHz In case of a client server database access the Amos II server runs on the same machine During the test a query is sent to the database and the result set
23. three values are set to NULL as they are not used in this project The programmer has the possibility to register at this place startup and shutdown functions for page requests and an info function if a footnote should appear at the output of the PHP function phpinfo The ninth entry identifies the version of this module and the last is a macro setting some remaining internal values Another special function for dynamic loadable modules must be considered the creation of the function get_module It is called by Zend at load time of the module and has the 16 include standard PHP and Amos II headers include php h include callin h include php amos h compiled function list so Zend knows what functions are distributed by the Amos II module function entry amos functions PHP FE amos functionname NULL NULL NULL NULL compiled information about Amos II module zend module entry amos module entry STANDARD MODULE HEADER amos amos functions ZEND MINIT amos init ZEND MSHUTDOWN amos free NULL NULL NULL NO VERSION YET STANDARD MODULE PROPERTIES implement standard stub routine to introduce Amos II module to Zend ifdef COMPILE_DL_AMOS ZEND_GET_MODULE amos endif Macro for implenting a PHP function PHP_FUNCTION amos_functionname Figure 7 Code structure for the Amos II extension 17 PHP FUNCTION function name retrieving the
24. tuple by using amos_getXXX amos next scan Let s assume furthermore that scan handle scan holds 10000 tuples Function amos createtuple would then create a new tuple for each loop so to say really 10000 tuples And the variable tpl is only able to reference one of these tuples To work around this the user should place the tuple creation in front of the while loop thus only one tuple will be created But still it is possible to write such a program The best solution for this problem are arrays while amos eos scan data array create new array amos getrow scan data read data from the tuple by using e g datal2 amos next scan Function amos getrow will internally allocate a tuple representing the current row and copy its complete content into the PHP array data Finally the internal used tuple can immediately be deallocated again and no memory leaks would stay as PHP has its own storage management that will deallocate the array if its is not used any more by the script This would be a great advantage especially for server use To realise the connection between Amos II tuples and PHP arrays two ways need to be considered e conversion of an Amos II tuple handle to a PHP array e conversion of a PHP array to an Amos II tuple handle 7 1 Tuple to array conversion The first point concerns the PHP function that might return an array like amos_getrow It is quite easy to create a new
25. with differing between the amount of parameters Parsing with a fixed parameter count can be done by using a PHP function as follows int zend parse parameters int num args TSRMLS CC char type_spec Where the first argument refers to the number of PHP function arguments The second defines the parameter types by a string When parsing a long and a string value the 18 PHP type Receive Send long a getintelem a setintelem double a getdoubleelem a setdoubleelem string a getstringelem a setstringelem array a getsegelem a setsegelem Table 1 Data exchange between PHP and Amos II string type spec looks like ls where l denotes the first parameter to be a long value and s represents a string value Other possibilities can be found in the PHP manual 8 Finally there must be a list of initialized variable addresses where Zend should store the parameter values If there is an illegal number of arguments in the PHP function the macro WRONG PARAM COUNT prints an error message Now we can deal with a fixed number of function arguments of a special type But what to do if a function must deal with one argument having a runtime dependent type Zend offers a structure type named zval for handling this Every kind of data can be stored in a zval variable and additional information about that data e g the type Type checking can thus easily be done with the help of constan
26. 2 1 amos connect A connection to Amos II is established and an index to a connection handle is returned to the user Syntax long amos connect string dbname Description 32 Before accessing an Amos II database the user must open a connection by using function amos_connect The database can be specified by argument dbname In dependance of the parameter value two different kinds of connections can be established 1 The first possibility is a tight connection to an Amos II database It can be established by passing an empty string mytightcon amos_connect Now Amos Il runs as an embedded system inside the PHP application 2 The second possibility is to open a connection to an existing Amos II database by passing a non empty string to amos_connect mycon amos_connect mydatabasename This will work like a client server connection with Amos II running as server Amos II peer Then a free amount of scripts clients can access the database As soon as a connection handle is created it will be stored in an internal global array The user can access this connection by a long value that points to the array position holding the connection handle Now garbage collection has to be prepared Therefore function amos_gc is registered to Zend with the connection as parameter At the end of the current PHP script it will be executed and thus automatically free all to the connection correlated tuples and scans and finally
27. 4 http www php net manual en George Schlossnagle Advanced PHP Programming Macmillan Computer Pub ISBN 0672325616 2004 Stephanie Bodoff Java Servlet Technology Sun Microsystems Technical Report http java sun com j2ee tutorial 1_3 fcs doc Servlets html Stephanie Bodoff JavaServer Pages Technology Sun Microsystems Technical Re port http java sun com j2ee tutorial 1_3 fcs doc JSPIntro html Govind Seshadri Understanding JavaServer Pages Model 2 architecture JavaWorld Technical Report 2004 http www javaworld com javaworld jw 12 1999 jw 12 ssj jspmvc html Erhard Rahm Datenbanksysteme 2 Online Skript Kapitel 2 Universitat Leipzig Database Group 2003 http dbs uni leipzig de en skripte DBS2 inhalt2 html 56 14 15 16 Gio Wiederhold Mediators in the Architecture of Future Information Systems IEEE Computer 25 3 38 49 1992 George Shi Data Integration using Agent based Mediator Wrapper Architecture Tutorial Report Dept of Electrical and Computer Engineering The University of Calgary 2002 Felix Naumann Qualit tsgesteuerte Anfragebearbeitung f r Integrierte Informa tionssysteme it Information Technology 45 1 55 58 Lehrstuhl Datenbanken und Informationssysteme Humboldt Universit t zu Berlin 2001 Guido Gerding Werner Kuhn A Functional Approach to a Wrapper Mediator Architecture Paper Universit t M nster 2003 Michael Stonebraker Paul Brown Dorothy Moore Objec
28. 46 No object numbered in index php on line 272 When refreshing the page the error mainly disappears 7 Amos II tuples and PHP arrays As studies have come so far a basic interface has been implemented All PHP functions chapter 5 are based on the same idea and structure like the external interface to C This chapter describes an interesting result that is not implemented or only partly implemented Similarities cannot only be seen in the functionality of the Amos II extension and the external interface to C but also in structures A connection to Amos II in C is represented as a connection handle a_con As soon as a connection is established it is stored in an array The PHP user can access this connection handle by using an index to the storage position It is valid for all PHP functions that if an index to a connection handle is passed then the corresponding connection handle will be used for the C function Scans and tuples are treated the same way The conclusion for this implementation is that it is quite an inelegant way especially for tuples All tuples contain nothing more than an array with data As the language PHP also offers arrays to its users it would be better to use arrays in PHP where C uses tuples Another argument for PHP arrays is storage management Let s consider the following loop to read a result set 50 while amos_eos scan tpl amos_createtuple con amos_getrow scan tpl read data from the
29. 94878673553467 0 00044012069702148 0 0099279880523682 19 2 e 0 009490966796875 0 00058412551879883 0 010075092315674 20 2 e 0 0096340179443359 0 0004417896270752 0 010075807571411 Table 2 Performance measurement results a name and one of them an address Two queries a scan and a selection query have been tested 1 select name p from person p 2 select livesat p from person p where name p Name9975 The first query scans the database for all names which will cause the database and PHP to handle a large result set 10000 names The display time taisplay can be expected to be quite high The second query searches the address of a person with the name Name9975 Here the display time can be expected to be small Both queries are sent via two different connection types a tight connection symbolized through an e in the DB column and a client server connection symbolized through a c s The resulting times are displayed in table 2 To summarize table 2 and to draw conclusions the average for each type is calculated e Result for Query 1 c s test cases 1 5 tquery avg display avg ttotal avg 1 401 0 404 1 805 e Result for Query 1 e test cases 6 10 26 mala TEO tdisplay avg 0 443 trotal avg 1 547 e Result for Query 2 c s test cases 11 15 tquery avg 0 015 tdisplay avg 0 00046 ttotal avg 0 015 e Result for Query 2 e test cases 16 20 toueryavg 0 010 tdi
30. It is also possible to tell PHP to load the module automatically by adding the Amos extension to file php ini The last possibility is used in this project and no real performance problems have been observed The next thing to discuss is how to write a PHP module 3 2 2 PHP module structure PHP offers a lot of macros to implement a module The basic structure for the Amos II extension to PHP can be seen in figure 7 First of all the header files for PHP and Amos II have to be included The next important entry is a list of all implemented PHP functions The list function entry amos functions can be created by using the macro PHP_FE for each entry Now Zend knows which functions this module offers to the user Next all necessary information about the module contents must be collected This infor mation is stored in a structure named zend module entry The first entry is a macro and sets the size of the whole zend_module_entry the number of the Zend module API ZEND_MODULE_API_NO whether it is a debug build or normal build and if ZTS Zend Thread Safety is enabled the fourth value is set to USING_ZTS The second entry contains the name of the module amos Furthermore the name of the array with all function names amos_functions must be available to Zend The fourth and fifth entry are defining func tions that are called when initializing amos_init section 5 1 1 and closing amos_free section 5 1 2 this module The next
31. QL queries to SQL through JDBC and for the web server to web service requests The description of figure 2 can also be found in 1 To summarize this figure 2 illustrates the distribution of mediator peers Two distributed data sources offer through three distributed mediator peers their data to an application Communication between peers is illustrated by thick lines where the arrow indicates the peer running as server One special mediator peer is listed in the figure the name server Every mediator peer must belong to a group and every group of mediator peers must have a name server The name server stores meta information like e names of peers e locations of peers and e additional data about all peers in that group It must be mentioned that the mediator peers forming a group are still autonomous and there exists no control schema in the name server And it is left to each peer to describe its own local data view and data sources In what way is the name server involved in peer communication The kind of communication is done through messages that can request or deliver data To avoid a bottleneck the name server is only involved when a new mediator peer wants to register to the group As soon as mediators are known to each other they can communicate directly without any information from the name server In figure 2 the communication with the nameserver is illustrated by dotted lines The name server always acts with regard to the other
32. Uppsala Master s Theses in Computing Science 274 Examensarbete MN3 2004 07 01 ISSN 1100 1836 PHP Integration with object relational DBMS Christian Werner Information Technology Computing Science Department Uppsala University Box 337 S 751 05 Uppsala Sweden Abstract This report describes how to extend a functional mediator system Amos II for allowing access from web servers through PHP Several possibilities are analysed to combine the Amos Il external interface with PHP Based on this discussions new functionality has been added to the PHP language by implementing a PHP external module A basic API between PHP and Amos Il is proposed in this workout The interface was illustrated by implementing a web interface to a simple database Further studies and experiences from this illustration resulted in a simplified and more dynamic interface definition based on PHP arrays Supervisor Tore Risch Examiner Tore Risch Passed Contents 1 Introduction 2 The Amos Il system 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 3 9 3 6 4 1 4 2 4 3 5 1 5 2 The mediator wrapper architecture 2 ee The Amos Il architecture eee ee Data model ar a as ot dia Ri ioe AE A ca ae Boe Dh EYES de mel pia lye Sy ae Peek ks Go eee SU eee ig nig Gee Dab Roe ek ES 223 25 r FUNCIONS ices or aS dee fay Aste RAS ee A ek he E an te Bs 23 3 Objects a bal Ws ae ea eee An tad le le Extern lkintertacez stage z
33. arse aa 8 i hy AL BRD ion oe PHP integration Introduction to PHP 0 02 00 00 20 eee ee Extendi ePHB 1 2 5404 ra 2er es nd Ane ties 3 2 1 Internal PHP structure 0200000004 3 2 2 PHP module structure 0 0 0000 eee eee 3 2 3 PHP function structure 2 2 2 0 020000 eee eee Connecting PHP and Amos II types o nn Passing ODS zu ach eet be Ak a is ic Error Management 2 2 ee Garbage Collection 2 22 CH A Performance observations Performance iii otras Bg aOR Et Sh En Be a ee a Debugability gt 24 at rete a E el Cae gh i ak Ge eee Pe a Comparision PHP versus JSP raabe a miina a a E e a 0000000004 4 34 LISP struchure 2 A AA A Aa 4 3 2 General comparison nn 4 3 3 Amos II specific comparison 0 000002 2 ee Function reference Module functions 2 2 2 2 on nn 9 1 1 amos nit 2 o le no a LD amos fres sms re en en ae O a Olid AMOSEBO os tate ea A as te EY ee EN a AA Se Connection interface 2 m m nn 10 10 11 11 12 14 14 14 15 16 18 19 19 21 22 24 24 27 27 27 28 29 5 3 5 4 9 9 5 6 8 7 EAN amos conhe tin anae ne RE rn na bee a Ea 32 DZ vamos close m nn ee en ee ee 33 Scan Interface a A seta ei eal a a Bes 33 Oil AMOS QUERY a a Da ddl e he Be 34 9 3 2 amMoseclosescan as ek ld en 34 Doo AMOS COB cs Am ee ie en ae a Pee ee wi en Oe a 35 5 34 AMOS BETTON ines oe eM A A A
34. ashTable by using the set methods from the external tuple interface like a setintelem to fill the 52 int fill array zval array int tpl int i for i 0 i lt a getarity amos tuple list tpl tuple FALSE i int type a getelemtype amos tuple list tpl tuple i TRUE if a errorflag 0 AMOS WARNING return 1 switch type case INTEGERTYPE add_index_long array i retrieve_int tpl i break case REALTYPE add_index_double array i retrieve_double tpl i break case STRINGTYPE add index string array i retrieve string tpl i 1 break case ARRAYTYPE 4 int sequence tuple zval sequence MAKE STD ZVAL sequence array init sequence retrieve amos vector sequence tuple create new tuple amos tuple list tpl con index if retrieve sequence tpl i sequence tuple 0 AMOS WARNING return 1 recursive call to fill next array if fill array sequence sequence tuple 0 return 1 add_index_zval array i sequence break case OIDTYPE add index string array i retrieve_oid tpl i 1 break return O Figure 18 Algorithm for copying tuple data to a PHP array 53 tuple Finally this tuple can be used by some Amos II functions as parameter such as a_callfunction c fid tuple An overview as follows lists all necessary PHP functions to Amos II with using PHP arrays instead of Amos II tuples The amount of
35. ast path interface all data can be removed Below the code extract is displayed that deletes a given person object It is not necessary to delete all function entries on its own it is enough to delete only the person object if lisset SESSION action 1 amp amp isset _POST person amos deleteobject _SESSION con _POST L person 49 unset _POST person 6 3 Program test results While testing the program some errors occurred which could not be resolved before this report was finished They are listed in this section 1 The adding of two email addresses results in an error Normally one or several emails are stored in an Amos II vector displayed on the command line as follows emaili email2 The result of adding new mails via the PHP script is NIL When trying to read the data with PHP function amos_getstring this error message is displayed Fatal error Error 72 Illegal kind of object in index php on line 278 The same problem exists for the telefone numbers that are also stored in an Amos II vector 2 A similar error appears when changing a person s email address All email addresses for this person are set to NIL The error message is the same like above Fatal error Error 72 Illegal kind of object in index php on line 278 The problem again exists for telefone numbers 3 A different error occurrs sometimes when trying to display data for a person Fatal error Error
36. can amos_scan points to a valid tuple If so TRUE is returned otherwise FALSE This function is well used inside a while loop as shown in figure 13 In that way all tuples of a scan are taken into account 5 3 4 amos_getrow Copies the current row into a tuple handle Syntax long amos getrow long amos scan long amos tuplel array empty array Description Before elements of data can be retrieved it must be copied from the current curser position of scan amos scan into tuple amos tuple The tuple can be created by using PHP function amos createtuple section 5 4 2 The index of the tuple handle with new data is returned But it is the same index like the parameter amos_tuple An example code is shown in figure 13 As an additional feature amos getrow can be called by passing an empty array This function initializes this array and fills it recursively with the data of the current row Why recursively An Amos II tuple can contain arrays thus the array is stored inside the main array And this has to be done recursively The PHP code gets very simple by using this method No functions calls of the tuple interface have to be done The array is filled with all the data of the row and the user can run through the array to read the data This function still returns the used tuple that contains the same data and can be accessed by functions from the tuple interface The following PHP code gives an example row array tuple
37. ch to Data Management Modeling Analyzing and Integrating Heterogeneous Data Springer ISBN 3 540 00375 4 2003 Gustav Fahl Tore Risch Amos II Introduction Tutorial Dept of Information Science Uppsala University Sweden 1999 Staffan Flodin Martin Hansson Vanja Josifovski Timour Katchaounov Tore Risch and Martin Sk ld Amos II Release 6 User s Manual UDBL Technical Report Dept of Information Science Uppsala University Sweden March 27 2004 http user it uu se udbl amos doc amos_users_guide html Tore Risch AMOS II Functional Mediators for Information Modelling Query ing and Integration UDBL whitepaper Dept of Information Science Uppsala University Sweden http user it uu se udbl amos amoswhite html Tore Risch Amos II External Interfaces UDBL Technical Report Dept of Information Science Uppsala University Sweden 2001 http user it uu se torer publ external pdf Daniel Elin Tore Risch Amos II Java Interfaces UDBL Technical Re port Dept of Information Science Uppsala University Sweden 2000 http user it uu se torer publ javaapi pdf Kristofer Cassel Tore Risch An Object Oriented Multi Mediator Browser 2nd International Workshop on User Interfaces to Data Intensive Systems Zurich Switzerland 2001 Stig S ther Bakken Alexander Aulbach Egon Schmid Jim Winstead Lars Tor ben Wilson Rasmus Lerdorf Andrei Zmievski Jouni Ahto PHP Manual PHP Documentation Group 12 04 200
38. d returns the object identifier string The returned OID string has a structure as follows 0ID 1101 with 1101 being the actual identifier number This function only retrieves the OID of surrogate objects Literals have no OID Use the other get methods to get literal objects e g to get a string use amos_getstring The code line as follows gives an example on how to gain an OID string at position 1 from tuple tp1 result_oid amos _getobjectelem tpl 1 The result string can simply be used in queries written in AmosQL select name p from person p where address p address_oid This query delivers the name of the person that lives at address_oid 9 4 11 amos setobjectelem Assigns an OID to an element of a tuple Syntax void amos setobjectelem long amos tuple long pos string oid Description Function amos setoblectelem stores an object with the corresponding OID string oid to an element in tuple amos tuple at position pos This function is not intended to set an OID string to a result tuple e g result of amos getrow The reaction would be a fatal error But it is possible to set the element value of a tuple more than once even with different types 39 5 4 12 amos getsegelem Returns an index to a tuple that represents the sequence Syntax long amos getsegelem long amos tuple long pos When tuple amos_tuple holds a sequence element at position pos the index to a new tuple which represents t
39. d to initialize Amos II for the first time Normally file amos2 dmp should be declared The other three entries are defining the arraysize for storing connection scan and tuple handles at startup If the average number of accesses database connections is known these values can be adjusted It will have no influence on the programs behaviour and little on the performance as the arrays will automatically increased when running out of memory Notice that there should be more scans than connections as every connection can hold several scans The same is valid for tuples This chapter is organized as follows First functions concerning the module are explained Next primitive functions starting with connecting to Amos II scan handling and ending with 30 the tuple interface Leaving the basic functions the fast path interface object creation and finally transaction control is explained Before studying the functions some conventions as follows should be considered 1 A connection is an index to the array of Amos II connection handles 2 A scan is an index to the array of Amos Il scan handles 3 A tuple is an index to the array of Amos II tuple handles A PHP programmer uses all indices via PHP variables of type long So each time a connection scan or tuple is passed to or returned by a function it is only the index that is passed or returned It has no influence on the meaning of the described function but this reference is much eas
40. dex long to to show a by hand assignation zend_hash_index_update HASH_OF return_value O void amp my_long sizeof zval NULL Returns nothing to satisfy the void prototype return 7 2 Array to tuple conversion The second point concerns the PHP functions with an array as parameter like amos_callfunction long connection string function_oid array argl instead of amos_callfunction long connection string function_oid long argl_tuple This approach to access data from a PHP array and store it into an Amos Il tuple has not been implemented yet Studies have come so far e First of alla header file has to be included include zend hash h e An array is retrieved as a zval container with zval type IS ARRAY e The array itself is stored in zval value as a HashTable and can be obtained as follows HashTable ht zval gt value ht e The header file offers some methods for traversing a HashTable zend_hash_move_forward ht zend hash move backwards ht zend hash get current key ht str index num index duplicate zend hash get current key type ht zend hash get current data ht pData zend hash internal pointer reset ht zend hash internal pointer end ht First the internal pointer has to be reset Then by using a method that forwards the pointer the complete hashtable can be traversed and data copied out of the hashtable Now an Amos II tuple can be created internally and filled with data from the H
41. e 13 Example code for using the scan interface 5 3 1 amos query Sends queries to Amos II databases and returns scans Syntax long amos query long amos con string query Description Any kind of queries written in AmosQL can be passed to this function via parameter query In dependance of connection amos con the query is forwarded to an Amos II database The Amos II system generates a result set and fills it with tuples Then this query result is delivered to the Amos II module and is stored in a global array In combination with the current scan the index to connection amos con is stored Thus the garbage collection routine is able to determine all scans that belong to a connection If no error occurs the index that holds the new handle is returned to the user otherwise 1 is returned The following example returns all function names currently stored in an Amos II system query select name f from function f scan index amos query current con query Notice that all AmosQL statements must end with a semicolon 5 3 2 amos closescan Closes a scan Syntax void amos closescan long amos scan Description Function amos closescan frees the scan handle stored at index amos_scan The array index is signaled to be free by setting a flag 34 5 3 3 amos_eos Checks for more rows tuples in a scan Syntax bool amos eos long amos scan Description It is checked if the current curser of s
42. e AMOS REALTYPE marks an object of type double e AMOS_STRINGTYPE marks an object of type string 31 e AMOS_ARRAYTYPE marks an object of type array e AMOS_OIDTYPE marks an object of type object The scope of these constants is dependent on the module s lifetime 5 1 2 amos free During module shutdown all three arrays that are storing connection scan and tuple handles are freed 9 1 3 amos ge Frees memory used by a connection or a session Syntax void amos gc long amos con void amos gc string session id Description Not only on module shutdown but also at the end of scripts or sessions garbage must be collected and used memory freed It is possible for the user to invoke this function whenever he wants but it must be invoked at the points chapter 3 6 lists This method can receive two different parameter types e The first possibility is calling amos gc with a connection amos_con All tuples and scans are searched and deallocated that are belonging to that connection The connection is closed and the handle is deallocated e A string representing a session identifier session id is the second possibility There is a loop that searches all connections that are correlated to the current session If a connection is found it will be handled the same way as described above 5 2 Connection interface This interface offers PHP functions for connecting and disconnecting to Amos II databases 5
43. e real value or he would have to call an additional function to transform the object handle into a fitting string Therefore the object handle must reference a surrogate object otherwise this transformation would lead into an error An object handle cannot directly be used with a query The same string as mentioned in the second item has to be used Thus a function call for transforming the object handle to this string is a must The other possibility is to convert a surrogate object handle straight into a PHP string with syntax OID lt oidnr gt and return it to the PHP script Integer value lt oidnr gt is the system maintained object identifier for surrogate objects The value is unique for each object It must be mentioned that only references to surrogate objects can be transformed as literal objects have no object identifiers And thus literal objects such as string and numbers must be converted to the corresponding PHP literal objects The advantage of this method is that the PHP user need not take care of the internal representation of objects as object handles But how to transform a surrogate object handle into this OID string and vice versa Two steps are necessary for converting an oidtype to the object string as it can be seen in the code extract of figure 9 Before starting with transformation an object handle must be initialized and a new value must be assigned to it First step is to get the OID number from the Amos II
44. each database system are existing a large part of program logic has to be implemented as an external module in C C 4 3 3 Amos II specific comparison In this chapter an Amos II specific comparison between PHP and JSP is done As JSP is built on Java the PHP extension is compared with the external Java interface 6 1 Garbage Collection e The Amos II extension to PHP offers a partly automatic garbage collection to the user For scripts executed like CGI scripts the user need not care about garbage collection Using Amos II funtions in combination with sessions a session save handler has to implemented by the user See chapter 3 6 for more details e Garbage collection in Java is secured through the already built in garbage collec tor 2 Error Management e All Amos Il errors are forwarded to the Error Management of PHP These errors or warnings are then printed to the current output stream e There exists an AmosException in Java that can be caught by using a try catch clause 3 Connections scans and tuples are treated in dependence of the programming language almost the same way 4 As Java supports overloading on method arguments there exists only one function setElem to set values to tuples There exists no overloading for return values in Java therefore different get functions exist for each type In PHP it is possible to have only one function for setting and one for getting values As explained in chapter 7 t
45. eral object has an OID 11 It will be discussed later in chapter 3 4 how object handles are passed from C to PHP and the other way 2 4 External interface Applications that are developed in programming languages and want to access the Amos II system require special interfaces to the mediator layer Interfaces to several programming languages are already existing 1 Lisp Amos II can be called from an extended Lisp language named ALisp The AmosQL parser is based on Lisp macros that translate queries and statements written in AmosQL to Lisp code The interface from ALisp to Amos Il is an embedded query interface As explained in 5 this method for accessing Amos II can be very efficient 2 Java The most convenient way to write Amos II applications is using the Java in terface that is described in 6 The Java interface is divided into a callin and callout interface The use of these two interfaces is similar to the ones from C Thus the use is only described for C but the technic is explained shortly A driver program called JavaA MOS is necessary to call Amos II from Java code and is provided through javaamos jar For using the callout interface the multi directional foreign function interface can be used 3 C The external interface to C is used for this project and is thus explained in detail below It is PHP that needs to be extended in this project As the core of PHP Zend is written in C the study of the C interface
46. ers O Ds between Amos II systems and PHP Thus Amos II objects can easily be accessed from PHP The error management of Amos Il is integrated with the error management of PHP The defined API is illustrated by the implementation of a web interface to a simple database An addressbook containing person and address objects is stored in this database The web interface tests the implemented functions Based on experiences of using the simple interface and further studies of PHP a simplified and dynamic interface based on PHP arrays is proposed This report is constructed as follows First of all the Amos II system is detailly described in chapter 2 The architecture behind Amos II the data model based on Amos II and the interfaces to this system are explained Section 3 explains the basic structure of PHP and the possibility to extend PHP s core Zend It is furthermore investigated how to connect PHP types with the ones from Amos II how to pass OIDs between the two systems and the integration of the two error managements very important point for server systems garbage collection is also investigated in this chapter After this performance observations are presented in chapter 4 Comparisons to a similar language JSP have additionally been made in this chapter A complete function reference is given in chapter 5 To test the system an example PHP program has been implemented and is described in chapter 6 With progressive studies a very im
47. escribed 3 1 Introduction to PHP PHP is above all a scripting language that can be embedded into HTML and is therefore mainly used as a server side scripting language The shortcut PHP is a recursive acronym for PHP Hypertext Preprocessor There are three possibilities for using PHP It can be used e as a server side scripting language e for command line scripting or e for client side GUI applications The first point is the most important one As soon as a client e g web browser requests a PHP site from the server PHP is called by the server to translate the PHP page into pure HTML code The resulting HTML page is then returned to the client The advantage of this architecture is that the PHP code from the PHP page cannot be seen by the user The page is translated on the server and the user only sees the resulting HTML page For this project the Apache Web Server is used as Apache already has a PHP integration PHP also offers the possibility to execute scripts via the command line This feature has been used in this project to test the implemented Amos II functions described in section 5 The third possibility to use PHP is developing GUI applications with the help of the PHP GTK extension For this work that feature is not of interest 3 2 Extending PHP For calling Amos II from PHP code the functionality of PHP must be extended But how to extend this language 14 Web Server Software h PHP Modules
48. ex to the connection handle amos_con and the OID string that identifies this object is necessary 5 7 Transaction control These transaction control primitives have only effect on tight connections Using an embed ded database the user can send a lot of operations to the Amos II system Only when the commit function is called changes will be fixed In case of a rollback all changes disappear There exists an autocommit for client server connections After the execution of every oper ation data changes are fixed and stored in the database It is thus useless to call transaction control functions explicitly 5 7 1 amos commit Commits a transaction Syntax void amos_commit long amos_con Description All operations for the specified connection amos_con are fixed with calling this function It will have effect only on embedded databases 5 7 2 amos_rollback Aborts a transaction Syntax void amos_rollback long amos_con Description All operations to connection amos_con that are executed after the last commit are cancelled with this operation It will have effect only on embedded databases 44 Address Figure 16 Entity Relationship diagram for the Addressbook database 6 Example Program The Addressbook This chapter describes an example database in Amos II and explains PHP sites using this database 6 1 The database This chapter describes the database for the addressbook The ER diagram is shown in fi
49. f isset _POST telefone2 two given telefone numbers gt tuple arity 2 data amos_createtuple _SESSION con 2 amos setstring data 0 _POST telefone amos setstring data 1 _POST telefone2 1 unset _POST telefone unset handled data unset _POST telefone2 else one given telefone number gt tuple arity 1 data amos_createtuple _SESSION con 1 amos setstring data 0 _POST telefone unset _POST telefone set function data amos setfunction SESSION con fct tels person_tpl data amos closetuple data else if isset _POST telefone2 one given telefone number gt tuple arity 1 data amos createtuple SESSION con 1 amos setstring data 0 _POST telefone2 set function data amos setfunction SESSION con fct tels person_tpl data amos closetuple data unset _POST telefone2 else no given telefone number gt remove old values old_data amos remfunction SESSION con fct_tels person tpl fold data The last possible user action is removing an existing person No new script is necessary file index php is reloaded The person to delete is specified by an OID string The remove action is defined by an unset action variable and a set post variable _POST person With function amos remfunction 5 5 6 from the f
50. f the extension The last point has been implemented and is doing fine chapter 5 3 4 The return of scan handles as arrays has also been considered but rejected again Filling an array with the complete query result can result in large memory usage Result sets normally contain a few columns what is good for using arrays instead of tuples but a lot of rows 54 8 Conclusion and future work With this project a complete PHP extension API to access Amos II has been realized It is possible to establish connections either to one embedded database via a tight connection or to one or several databases where the Amos II system runs as a server It is a very interesting feature that one client script can open more than one connection to one database This might be necessary when a PHP based web page is requested by several users e g n user requests will open n connections to the same database Each connection handle has its own storage and can be treated seperately PHP functions have been implemented to send queries written im AmosQL to Amos II peers and to handle returned result scans At the moment only the tuple interface can be used for this As outlined in chapter 7 an interesting improvement would be to realise the connection of PHP arrays and Amos II tuples In addition to this investigations about PHP resources 8 should be made to represent Amos II interface objects such as scans connections and objects through PHP resources These fea
51. gether the route of a query is observed Figure 3 gives a picture of such a route An application sends a query to any mediator peer using AmosQL In dependence of the data to access the query will be split and forwarded to the wrappers There the query is translated into a format that is analyzable by the data source The right side of the picture shows the way of the result set The data sources deliver their query result back to the wrapper that translates the data to the CDM of the mediator There all result sets are unified and returned to the application The first time an Amos II server is initialized it runs initially as a stand alone database in single user mode For this project it is also necessary to run Amos Il as a server Therefore the nameserver must first be started by using nameserver Charstring name gt Charstring with name being the mediator name that is registered with starting the name server If name is an empty string only the name server is started All other peers that want to belong to the same group must register to the name server of the current system by using Application query result set Mediator split combine D D Figure 3 Example for a query execution register Charstring name gt Charstring For preparing the mediator to receive queries start the listening loop listen 2 3 Data model The basic components of the data model of Amos II
52. gure 16 Figure 17 shows the corresponding AmosQL script that defines the database 6 2 The program The program should have several properties e It should be able to display all persons with all information correlated to each person e It should be possible to add a new person to the database e It should be possible to edit data of an existing person e It should be possible to delete an existing person e The connection should stay up for a complete session These features are realized in three PHP scripts First of all there is a file index php which displays all information of all persons in a table Script new php offers text fields for entering data for a new person And finally file edit php offers filled text fields for editing data for a person The connection is stored in the global PHP session array with the keyword con _SESSION con Thus the connection stays up and is accessible in other PHP scripts 45 create type person create function name person gt charstring as stored create function emails person gt vector of charstring as stored create function tels person gt vector of charstring as stored create type address create function street address gt charstring as stored create function plz address gt integer as stored create function city address gt charstring as stored create function country address gt charstring as stored create function livesat person gt address as stored
53. has been the basics for implementing the API of chapter 5 The rest of this chapter deals with this interface The external interface to C is divided into the callin and the callout interface e For calling Amos II from a C program the callin interface can be used As it appears between the Amos IT kernel peer and the PHP extension callin makes the mediator wrapper available to PHP e With the callout interface external procedures can be called from AmosQL There is a similarity to data blades 18 in object relational databases and foreign functions can be used as a form of stored procedures Having a more detailed look on the internal interface there appear two ways to call Amos H from C e The evaluation of passed strings containing AmosQL statements is done by the embed ded query interface Some basic functions are provided to the programmer to handle results of AmosQL statements e Since the embedded query interface always has to parse and compile AmosQL statements it is rather slow To handle this disadvantage the fast path interface is able to call predefined Amos II functions from C 12 typedef struct int status a_connection con char session_id amos_con typedef struct int status a_scan scan int con_index amos_scan typedef struct int status a_tuple tuple int con_index amos_tuple Figure 5 Data structures used in the PHP extension In what way can Amos II be connected or
54. he 29 interface can be further simplified by using PHP arrays In this way all set and get methods can be eliminated which makes the PHP interface significantly simpler than the Java interface 5 Function calls e Every time an Amos Il function is called from Java the function name has to be passed as argument e In C the function to call is specified by an OID representing the Amos II function to be called Right now parameters have to be passed as a tuple handle Based on experiences from the basic interface an improved one is proposed using PHP arrays instead of tuples The proposed improvements are explained in chapter 7 The same characteristic is valid for adding setting and removing stored function data 5 Function reference This chapter gives a complete overview of all implemented functions to access Amos II databases from PHP scripts For full functionality some entries in file php ini must be added 1 Make PHP know the Amos II extension extension php amos dll This entry makes the Amos II extension available to PHP Be sure not to use the semicolon in front of the line Thus the extension will automatically be loaded at PHP startup 2 Define some module initialization parameters amos file for Amos II initialization amos init_file AMOS_ROOT bin amos2 dmp array sizes at module initialization amos max_cons 10 amos max_scans 20 amos max_tuples 30 The first entry is an image file that is use
55. he sequence is returned by function amos_getsegelem To ensure garbage collection the connection index stored with tuple amos_tuple is used to create the new tuple Thus in case of a disconnect enough information exists to free this tuple 9 4 13 amos setsegelem Assigns a sequence to a tuple Syntax void amos setsegelem long amos tuple long position long sequence_tuple Description Function amos setsegelem stores a sequence sequence tuple to the element in tuple amos tuple at position pos This function is not intended to set a sequence to a result tuple e g result of amos getrow The reaction would be a fatal error But it is possible to set the element value of a tuple more than once even with different types 5 4 14 amos elemsize Returns the size of an element in a tuple Syntax long amos elemsize long amos tuple long pos Description In dependence of the element type at position pos of tuple amos tuple the size of that element is returned In case of a string the string size is returned If the element holds a sequence the number of elements in that sequence is returned 5 5 Fast path function calling All functions described so far deal with the embedded query call Queries can be sent to Amos II databases and a result set is returned This result can be explored by the tuple interface The fast path interface permits to call Amos II functions from PHP without sending a query The result of a fast
56. icates whether there is a valid connection handle con stored or not Additionaly the string session id is used for garbage collection see section 3 6 and stores either the value 0 if connection con is established outside of a session otherwise the session identifier is stored in this variable 13 Now the connection is established and queries can be sent to an Amos II system that can return result sets Scan handles are holding the result set of queries and are returning them to the application as scans Scans are specified by the type a scan in Amos Il and by amos scan in the PHP extension Again status indicates a valid scan A scan handle is always created in correlation with a connection handle This information is stored in con_index A scan is a stream of tuples that hold the actual values Tuples are stored by using type a tuple in Amos II and amos_tuple in PHP The variables in the PHP structure are the same as in amos_scan 3 PHP integration This report deals with integrating Amos II into PHP First of all a short introduction of the PHP environment in section 3 1 is done followed by a description for extending PHP in chapter 3 2 The next topic to be studied is how to pass OIDs of Amos II objects to the user of PHP In case of an error thrown in Amos II the error managements of Amos Il and PHP must be combined Section 3 5 deals with this Finally but very important techniques for garbage collection to cleaning used storage are d
57. ier to read 5 1 Module functions As soon as PHP starts up the Amos II extension is automatically loaded With it amos_init is called On module shutdown amos_free is called Notice here that these two functions cannot be accessed by PHP users Furthermore it is possible to call a garbage collection routine amos_gc out of a PHP script 5 1 1 amos init Three things are done in this function First the Amos II system is started using a initialize char image int catcherror with image being the file for initializing Amos II In addition a default database that store for example information about existing types and functions is loaded The mage value can be defined in php ini by setting amos imit_file Zend function ini_get amos init_file retrieves this value The second step consists of allocating memory to store connection scan and tuple handles Therefore three arrays of structure amos_con amos_scan and amos_tuple are allocated For every entry the status is set to O with the meaning that no handle is stored at this position Furthermore con_index is set to 1 for all scans and tuples As soon as a scan or a tuple is stored the index to the connection handle under that the scan or tuple has been created is assigned to con index If one of the arrays runs out of storage space a realloc operation resizes the array Finally some constants are registered to Zend e AMOS_INTEGERTYPE marks an object of type integer
58. les have the C datatype oidtype that is represented by an unsigned integer They can be declared by using dcl_oid o 19 and after usage they must be freed by calling free oid o And there exists a macro for assigning a new value to a handle a setf lt location gt lt new value gt where lt location gt is an oidtype that receives a new value lt new value gt This macro invokes an incremental garbage collector based on reference counting Thus the old value stored in lt location gt is deallocated if no other location references the old value It must be men tioned that free oid deallocates the referenced object only if no other location refers to it Forgetting to free a handle can result in memory leaks as the internal counter might not be reduced The question now for this section is how to pass an object to the user It might simply be done by passing the object handle as an unsigned integer to the PHP script But this method has some significant disadvantages listed below e The PHP user has no idea what is referenced by this integer value It might be a string an integer or what we really want an OID e There is a difference compared to the Amos II command line There an OID for a surrogate object is printed in a scheme as follows 0ID lt oidnr gt lt oidnr gt is a system maintained object identifier for surrogate objects When the PHP user wants to print the object he either prints only the references to th
59. lt scan Figure 15 shows an example that calls an Amos II function to retrieve names of all persons 5 5 4 amos_addfunction Adds new data to a function Syntax void amos_addfunction long amos_con string function_oid long argl long resl Description Function amos_addfunction adds new data to an Amos II function In dependence of connec tion amos_con new data is added to function function_oid The data of a function is definite specified by its arguments and by its result set All arguments are stored in an argument list a tuple The result values are again stored in a tuple For both an index to the prevailing tuple argl and resl is passed to this function The following code gives an example that sets a name Marc to an empty person object person_oid add new data to function person name gt charstring amos_setobjectelem argl 0 person_oid name Marc amos_setstring resl 0 name amos addfunction con fct_oid argl resl 42 5 5 5 amos_setfunction Sets the value of a function Syntax void amos setfunction long amos con string function oid long argl long resl Description Assigns a new value to an Amos II function Concerning parameters an index to a connection handle amos_con the function handle function_oid a tuple argl containing the argument list and a tuple res that stores the result value are needed PHP function amos_setfunction assigns resl as the result of appl
60. mediator peers as a server To access external data sources an Amos II mediator peer can have one or several wrappers A wrapper is a small program module that translates queries received by the mediator and forwards them to the data source It is also responsible to handle the result set returned from that data source The data must be adapted to the mediator s data schema Therefore the wrapper must contain information about the meta data and the data itself of the data source It furthermore must contain rewrite rules to translate the queries As follows a number of tasks for a wrapper is listed 1 e Schema importation translates schema information from the sources into a set of Amos II types and functions e Query translation translates internal calculus representations of AmosQL queries into equivalent API calls or query language expressions executable by the source e Source statistics computation estimates costs and selectivities for API calls or query expressions to a data source Proxy OID generation executes in the source query expressions or API calls to construct proxy OIDs describing source data e OID verification executes in the source query expressions or API calls to verify the validity of involved proxy OIDs in case they have become invalid between different query requests As soon as a wrapper is defined for a special data source every query written in AmosQL can be rewritten To summarize and to put everything to
61. mos closetuple data amos closetuple data seg else if isset _POST email2 one given email address gt tuple arity 1 data amos_createtuple _SESSION con 1 amos setstring data 0 _POST email2 add function data data_seq amos _createtuple _SESSION con 1 amos setsegelem data seg 0 data amos addfunction SESSION con fct emails new person tpl data seg amos closetuple data amos closetuple data seg unset _POST email2 For changing the data of an existing person a similar page edit php is displayed To specify the person to change the OID string must be passed to this page There all text fields contain the old values that can be edited The action variable is set to edit Via the post method the edited data is sent back to the overview script where it is read and the changes are fixed 48 in the database Therefore function amos_setfunction 5 5 5 from the fast path interface is used An example code that sets a new value for a telefone number is shown below It is assumed that there already exists some telefone numbers for this person so no new function entry has to be created Otherwise a function call would have to be done to check for an existing entry Variable person_tpl is a tuple that contains the existing person object which data is going to be changed check for telefone entries if isset _POST telefone i
62. mos_closetuple tels_tpl amos_closescan tels_scan The addresses are fetched by using both methods the fast path interface to retrieve the address objects and the query interface to retrieve the street the postal code and the city name of the specified address object The following code fragment is responsible for these actions retrieve address objects fct livesat amos_getfunction _SESSION con PERSON LIVESAT gt ADDRESS ad scan amos callfunction SESSION con fct livesat person_tpl ad_tpl amos createtuple SESSION con ad oid amos getobjectelem amos getrow ad scan ad_tpl 0 amos closetuple ad tp1 amos closescan ad scan retrieve street postal code and city ad scan amos query SESSION con select street a plz a city a from address a where a ad_oid ad tpl amos createtuple SESSION con 1 amos getrow ad scan ad_tpl street amos getstring ad tpl 0 plz amos getint ad tpl 1 city amos getstring ad tpl 2 print street plz city amos closetuple ad tp1 amos closescan ad scan For adding a new person the user is able to switch to new php where text fields as follows can be filled E Mails and telefone numbers will be stored in an Amos II vector For sending the data back to index php the post method from PHP is used e Name e E Mail e E Mail 2 e Telefone e Telefone 2 e Street e Postal code e City
63. mos_getrow The reaction would be a fatal error But it is possible to set the element value of a tuple more than once even with different types 5 4 8 amos getstring Returns a string Syntax string amos getstring long amos tuple long pos Description Use function amos getstring to gain a string value It takes the element value of tuple amos tuple at position pos and returns the string value The specified element must hold a string otherwise an error message is printed and script execution is stopped The code line as follows gives an example on how to gain an string value at position 1 from tuple tpl result_str amos_getstring tpl 1 38 5 4 9 amos setstring Assigns a string value to an element of a tuple Syntax void amos setstring long amos tuple long position string value Description Function amos_setstring stores a value of type string to the element in tuple amos_tuple at position pos This function is not intended to set a string value to a result tuple e g result of amos_getrow The reaction would be a fatal error But it is possible to set the element value of a tuple more than once even with different types 5 4 10 amos getobjectelem Returns the OID of an Amos II surrogate object as a string Syntax string amos_getobjectelem long amos_tuple long position Use function amos_getobjectelem to gain an OID string It takes the element value of tuple amos_tuple at position pos an
64. ng executed PHP is getting quite slow Furthermore external additional files clutter up the disk and every script that wants to call functions from an external module must include a call to d1 O 15 Thus external modules are only recommendable for small programs or modules that are rarely used The second possibility built in modules avoids the disadvantages of an external module by compiling it into PHP Thus each time a PHP process is started the functionality is automatically available to every running script without calling the function d1 The disadvantages are if changes to built in modules are made recompiling of PHP is required and the PHP binary grows and consumes more memory Thus built in modules can be used when it has a solid library of functions that remain relatively unchanged The disadvantage recompiling PHP is quickly compensated by the speedup of the built in module The last possibilty to extend PHP is to change the Zend Engine itself This will result in a change of the PHP language behaviour But as mentioned in 8 this precedure is not recommended as changes result in incompatibilities with the rest of the world All changes to the PHP core will disappear with the next version of PHP or have to rewritten This project uses an external module In that way a recompilation of PHP can be avoided As explained above the module must be loaded dynamically by the di O command which will probably reduce the performance
65. ore this function s parameters the Amos II extension function to initiate the garbage collection amos close and its parameter the array position of the used connection must be stored in a zval array params Finally a return value must be allocated again as a zval variable and then amos close can be registered The second step in garbage collection concerns sessions A session consists of one or more scripts while old data is still needed For example the Amos II extension to PHP should offer the possibility to keep connections alive although script execution has ended PHP already comprises the handling of sessions which makes it easier to use this behavior in the Amos II extension The difficult part is deallocating all connections scans and tuples that are associated with this session As it can be seen in figure 5 it is possible to link a connection handle to a session session has a unique identifier and it can be obtained by invoking the user function session id Similar code to the one for registering shutdown functions is necessary to call this function from C code Thus each time a new connection is going to be established inside a session this connection is stored together with the current session identifier Now it is up to the user to implement a session save handler A session save handler controls six session functions e open save path session name opens a session e close closes a session e read id reads
66. portant improvement is proposed in section 7 the combination of Amos II tuples and PHP arrays Finally chapter 8 gives a conclusion and some ideas for possible future works 2 The Amos II system Amos Il is a distributed mediator system with an object oriented and functional data model The name Amos stands for Active Mediator Object System Queries to that data model are written in AmosQL a relationally complete functional query language The system can consist of several autonomous and distributed Amos II peers These peers can interoperate through its distributed multi database facilities Bach mediator peer offers three possibilities to access data e Access to data stored in an Amos II database e Access to wrapped datasources e Access to data that is reconciled from other mediator peers Especially the second point makes Amos II extensible New application oriented data types or operators can easily be wrapped And it is possible to add them to the query language AmosQL Thus a powerful query and data integration is offered by the Amos II system First of all this chapter deals with the mediator wrapper approach on which the Amos II system is based To put this into concrete terms the Amos II architecture is described after it As the main purpose of Amos II is using it as a database the data model of Amos II is described Finally the callin interface basically used to extend PHP is explained 2 1 The mediator wrapper architectu
67. r closing this connection on script termination has to be registered to Zend The Amos II extension offers amos_close section 5 2 2 for resetting a connection and for freeing all memory that is correlated to that connection The user can register functions that are executed when script process is complete It can be done by using void register shutdown function callback function As register shutdown function is a PHP scripting function it cannot be called directly from the C code Here Zend offers a possibility to call and execute user functions by us ing Zend function call user function ex Figure 10 explains how to to this First the 22 zval fct_shutdown fct_aclose fctparam zval retval zval params MAKE_STD_ZVAL fct_shutdown MAKE STD ZVAL fct aclose MAKE STD ZVAL fctparam ZVAL STRING fct shutdown register shutdown function 1 ZVAL STRING fct aclose amos close 1 ZVAL LONG fctparam new con params zval emalloc 2 sizeof zval params 0 amp fct_aclose params 1 amp fctparam register amos close new con if call user function ex CG function table NULL fct shutdown amp retval 2 params O NULL TSRMLS CC SUCCESS 1 zend error E WARNING Function call failed Figure 10 Example code for registering a function for garbage collection function name to be called register shutdown function is stored in a zval container fct shutdown Furtherm
68. re Today a lot of applications especially web applications are developed that need to access databases Therefore an increasing number of distributed databases is in use Online travel agencies for example must be able to access databases of flight companies to gain information of empty seats of airports to see arrival and departure timetables and of hotels to search for free rooms Of course these information won t be stored in one database but each flight company airport and hotel will have their own database Thus access to distributed databases is necessary Furthermore there exists a large amount of possibilities to store data One company having this database and that schema the next company might have a completely different view on the stored data Thus an application needs the possibility to access herterogeneous data SOUTCES Application Mediator Server vv gt a Wrapper 1 Ra Wrapper n DB File System Figure 1 Mediator Wrapper Architecture Therefore the mediator wrapper approach first proposed by Wiederhold 14 gives a good support for applications to access distributed and heterogeneous data sources But what is this approach A mediator system consists of a mediator server and one or several wrappers The mediator server is a central software module that comprises a common data model CDM This CDM shares its domain specific knowledge about
69. splay avg 0 00047 trotal avg 0 010 What is the gain in running Amos II in the PHP address spaces compared to running it client server To focus on the first query there is a speedup of 21 when using an embedded connection instead of a client server connection The second query delivers a speedup of 33 The display time is just as expected It has a larger influence on the first query it takes 0 4s to print the result set Definitely no influence on total time has the display time of the second query 4 2 Debugability The Amos II extension to PHP is program module that lies between two large projects PHP and Amos II This makes the possibility to debug the program quite difficult It is possible to execute PHP scripts on the command line With the extern variable extern int trace_interface TRUE message logging for Amos II is switched on Thus it is possible to see messages that are received and sent by an Amos II server Another point that shows the difficulty for debugging is that some errors occured only when PHP is used with a web server The execution of the PHP script resulted in a crash As Amos II also run as a server it stayed up When testing the same script on the command line to debug the script no error occurred 4 3 Comparision PHP versus JSP JSP JavaServer Pages is similar to PHP a server side scripting language First the struc ture for JSP is explained and after this a comparision between both is done
70. ss save pathisess Sid return Qunlink sess file function sess_gc maxlifetime amos gc session id return true session set save handler sess open sess close sess read sess write sess destroy sess_gc session start O Figure 11 An example implementation for a session save handler 25 Test Query DB query display total 1 1 c s 1 4849369525909 0 48234820365906 1 96728515625 2 1 c s 1 0670609474182 0 39609503746033 1 4631559848785 3 1 c s 1 5469870567322 0 41786289215088 1 9648499488831 4 1 c s 1 2793309688568 0 38292217254639 1 6622531414032 5 1 c s 1 627240896225 0 3448691368103 1 9721100330353 6 1 e 1 3398990631104 0 50183582305908 1 8417348861694 7 1 e 0 95206499099731 0 36082601547241 1 3128910064697 8 1 e 1 031594991684 0 40175485610962 1 4333498477936 9 1 e 1 1383380889893 0 45624804496765 1 5945861339569 10 1 e 1 0584740638733 0 49389100074768 1 552365064621 11 2 c s 0 016000032424927 0 00046181678771973 0 016461849212646 12 2 c s 0 015414953231812 0 00045299530029297 0 015867948532104 13 2 c s 0 015239000320435 0 00049495697021484 0 015733957290649 14 2 c s 0 016252994537354 0 00043201446533203 0 016685009002686 15 2 c s 0 015311002731323 0 00045895576477051 0 015769958496094 16 2 e 0 011516809463501 0 00043606758117676 0 011952877044678 17 2 e 0 011462926864624 0 00046205520629883 0 011924982070923 18 2 e 0 00
71. system by calling the system C function a getid A try to convert a literal object would result in an error After this a string is allocated and the OID number is printed into the object string Finally this string can be returned to the PHP script The other direction passing an object string from PHP to Amos II can occur when calling e g the function amos setobjectelem section 5 4 11 Again two steps are necessary to 20 char make 0ID string oidtype oid char toreturn char buffer 30 int id a getid oid TRUE if a_errorflag AMOS_ERROR sprintf buffer 0ID d id toreturn char emalloc strlen buffer 1 strcpy toreturn buffer return toreturn Figure 9 Transforming an object handle to an object string gain an object handle out of an object string The first thing to do is to retrieve the number the real OID out of the given string For example 1101 is the OID from the object string 0ID 1101 Next the location handle of the OID must be obtained given its OID number It can be done by calling the Amos II function a_getobjectno a_connection c int oid int catcherror which returns the expected location handle 3 5 Error Management Another important topic for this project is error management integration of Amos II with the one of PHP First the Amos Il error management is explained then the PHP error management and finally the integration Almost all functions of the external C in
72. t Relational DBMSs Tracking the Next Great Wave Morgan Kaufmann Publishers Inc ISBN 1558604529 1999 87
73. tabases web forms MIDI files and web browsers PHP PHP Hypertext Preprocessor is a server side scripting language 8 On the one hand PHP scripts can be executed on the command line This is very helpful feature for debugging On the other hand PHP is able to run inside a PHP enabled web server In this context PHP can be used to create interactive web pages The core of PHP Zend is extensible which offers the possibility to access external systems like Amos II in this project There are already existing several PHP extensions to various relational DBMS such as Oracle MySQL DB2 and ODBC The goal of this project is the implemention of new PHP functions that call Amos II systems This documentation deals with designing and implementing an API between Amos II and PHP First of all interfaces to Amos II and PHP in a web server are analyzed in order to combine these two programs Based on this analysis and previous Amos Il interfaces for C and Java the basic API was developed Several features have been considered while implementing the extension Now it is possible to establish either a tight connection or a client server connection With a tight connection the Amos II database runs inside the PHP engine as a subsystem The other possibility is to run Amos II as a seperate server PHP scripts can send their requests to this server where they are worked on and answered Another very interesting feature is the passing of Amos II object identifi
74. terface have a flag as parameter that indicates the kind of error handling This parameter is an integer value and can accept the values TRUE or FALSE As well most of the functions return an error flag that indicates the occurance of an error n example for such a function is int a_nextrow a_scan s int catcherror with catcherror defining the kind of error handling If this parameter is FALSE then Amos II deals with the error and possibly shuts down the system The other possibilty TRUE means that the user has to deal with the error on his own what is done in this project What happens if an error occurs First of all there exists a global variable int a_errorflag which is O if the current Amos II functions ends normally or not equal to zero for the case of an error Some functions as the one mentioned above return this flag and for other functions this global flag must be investigated For example like this if a errorflag 0 do some error handling here To receive information about the error the following three global variables are of use 21 e For storing the error number int a errno e A string explanation of the error char a_errstr e A reference to an Amos II object oidtype a_errform For the case of an Amos II error this error must be forwarded to the calling PHP script Zend offers function zend_error int type char message for printing a message to the current output e g an HTML
75. tion Before using a tuple it must be created by this function Either a tuple with an undefined length or a tuple with a given arity can be created If the tuple is used with function 36 amos_getrow it is not necessary to pass an arity to create a tuple It will automatically be filled with all necessary elements For garbage collection tuples must belong to a connection defined by parameter amos_con This connection is stored in combination with the tuple handle If no error occurs the index that holds the new handle is returned to the user otherwise 1 is returned The following example creates a new tuple with the arity of another one new_tpl amos createtuple con amos _getarity 01d_tp1 5 4 3 amos closetuple Closes a tuple Syntax void amos closetuple long amos tuple Description Function amos closetuple frees the tuple handle stored at index amos_tuple The array index is signaled to be free by setting a flag 9 4 4 amos getint Returns an integer value Syntax long amos getint long amos tuple long pos Description Use function amos getint to gain an integer value It takes the element value of tuple amos tuple at position pos and returns the integer value The specified element must hold an integer otherwise an error message is printed and script execution is stopped The code line as follows gives an example on how to gain an integer value at position 1 from tuple tp1
76. ts e g IS_LONG offered by Zend The next step consists of checking the parsed parameters Normally Amos II connections scans and tuples need to be checked whether they are still valid After this the parameters can be edited and or passed to Amos II functions For the case of a returning function PHP again offers some macros to return spezial types e g to return a long value the macro RETURN LONG long value should be used 3 3 Connecting PHP and Amos II types Till this chapter all explanations concern the structure of the PHP module named amos Now a closer eye must be kept on some internal aspects Data exchange between PHP applications and Amos II databases can be done via the get and set methods of the callin interface from Amos II Table 1 lists an overview of all basic PHP types and how to receive and send this data to Amos II The corresponding PHP functions this project offers to the PHP user are described in chapter 5 4 The next section describes how Amos II object identifiers are handled 3 4 Passing OIDs The object oriented character of Amos Il is already explained in chapter 2 3 3 It is mentioned that a programmer using C must deal with object handles But how to use these object identifiers Basically object handles are logical pointers to an Amos II data structure Any kind of data in Amos II numbers strings arrays Amos II OIDs and other internal data structures can be referenced by an object handle Object hand
77. tures would make the handling of this extension much easier to PHP users that want to access Amos II databases Furthermore it is possible to use the fast path interface which offers a fast way to call Amos IT functions add set and remove data from a function Data of a function is defined through its parameters and its result elements Both items are currently stored in tuples As soon as these tuples are replaced by PHP arrays it offers an easy to handle way to use the fast path interface Another idea for a future work is to extend the Amos II extension with the callout interface This interface offers the possibility to call external subroutines by using AmosQL statements To put in concrete terms a PHP user could implement PHP functions make them known to Amos II and could to call them in queries written in AmosQL The implemented function should have a signature as follows similar to the implementation of external C functions void fn long ctx array a With ctx being a reference to an internal Amos II data structure a_callcontext for man aging calls and a representing an PHP array that contains the actual arguments and results First the function parameters are stored and after this the result values 55 References 1 T Risch V Josifovski and T Katchaounov Functional Data Integration in a Dis 10 11 12 tributed Mediator System in P Gray L Kerschberg P King and A Poulovassilis eds Functional Approa
78. ying function_oid on the argument tuple argl The difference to function amos_addfunction is the replacement of an old existing value while the add function adds completely new data 5 5 6 amos_remfunction Removes data from a function Syntax void amos_remfunction long amos_con string function_oid long argl long resl Description Removes data from an Amos II function In dependence of connection amos_con data is removed from function function_oid The data to remove is described by the argument list represented by tuple argl and the result tuple resl 5 6 Object creation and deletion The Amos II extension offers methods for creating and deleting objects 5 6 1 amos createobject Creates a new object Syntax string amos createobject long amos con string type Description Function amos createobject creates a new object of a specified type The index to a connec tion handle amos con and the type as a string type are taken as parameters With this information a new surrogate object is created and the OID string of that created object is returned to the user For creating a new object of type person call newobj createobject con person 43 Variable newobj holds the returned OID string that identifies the new object 5 6 2 amos deleteobject Deletes a surrogate object Syntax void amos deleteobject long amos con string type oid Description For deleting an existing object the ind
79. ype Function The function s signature contains information about all arguments such as the types and optional names and about the result of the function The next example shows the signature of a function modeling the attribute street of type Address street Address gt Charstring 2 3 3 Objects Objects in Amos II are corresponding to Entities in an Entity Relationship diagram and are instances of Amos II types Everything in Amos II is represented as an object independent whether the object is user defined or system defined Literals and surrogates are are the main kinds for representing objects Literal objects are primitive objects like integers strings or even collections that represent arrays of other objects In addition to this are surrogates that are created by the user or the system and are describing real world entities An address object in Amos II can be created with a command as follows create address instances hereiswhereilive When a query requests this object the returned result will be displayed similar to this scheme H OID 1101 The system assigns unique object identifiers OIDs to all surrogate objects In combination with the external interface for the programming language C these OIDs are stored as object handles An object handle is a reference to any kind of data stored in Amos II databases such as numbers strings Amos II OIDs and other internal structures Reversed no object handle that references a lit

Download Pdf Manuals

image

Related Search

Related Contents

LG AS-W1838*H0 User's Manual  manuale di istruzioni  CCWorldWeatherGen manual - Sustainable Energy Research Group  L`économie sociaLe et soLidaire face à La crise : simpLe  競 技 規 則 - 日本トライアスロン連合  Bedienungsanleitung  Users Manual - Custom Advisor Solutions, Inc  H AVLINK-3  Jabra Pro 920 - User Manual  Neff S65M63N1GB dishwasher  

Copyright © All rights reserved.
Failed to retrieve file