Difference between revisions of "Standard Mode"

From BaseX Documentation
Jump to navigation Jump to search
m (Text replacement - "syntaxhighlight" to "pre")
 
(21 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<p>In the standard mode, a database command can be
+
<p>In the standard mode of the [[Clients]], a database command can be
 
sent to the server using the <code>execute()</code> function
 
sent to the server using the <code>execute()</code> function
 
of the <code>Session</code>. This functions returns the whole
 
of the <code>Session</code>. This functions returns the whole
Line 10: Line 10:
 
<p>The standard execution works as follows:</p>  
 
<p>The standard execution works as follows:</p>  
  
#Create a new session instance with hostname, port, username and password.  
+
# Create a new session instance with hostname, port, username and password.  
#Call the <code>execute()</code> function of the session with the [[Commands|database commands]] as argument.  
+
# Call the <code>execute()</code> function of the session with the [[Commands|database commands]] as argument.  
#Receive the result of a successfully executed command. As an error occurs an exception is thrown.  
+
# Receive the result of a successfully executed command. If an error occurs, an exception is thrown.  
#Continue using the client (back to 2.), or close the session.
+
# Optionally, call <code>info()</code> to get some process information
 +
# Continue using the client (back to 2.), or close the session.
 
   
 
   
==Example==  
+
==Example in PHP==
 
   
 
   
#Create a session object:<br/><pre class="brush:java">Session session = new Session("localhost", 1984, "admin", "admin");</pre>
+
Taken from our [https://github.com/BaseXdb/basex/blob/master/basex-api/src/main/php/Example.php repository]:
#Run the command in question and print the textual result:<br/><pre class="brush:java">print session.execute("info database");</pre>  
+
 
#Close the session:<br/><pre class="brush:java">session.close();</pre>
+
<pre lang="php">
 +
<?php
 +
/*
 +
* This example shows how database commands can be executed.
 +
* Documentation: http://basex.org/api
 +
*
 +
* (C) BaseX Team 2005-15, BSD License
 +
*/
 +
include("BaseXClient.php");
 +
 
 +
try {
 +
  // initialize timer
 +
  $start = microtime(true);
 +
 
 +
  // create session
 +
  $session = new Session("localhost", 1984, "admin", "...");
 +
 
 +
  // perform command and print returned string
 +
  print $session->execute("xquery 1 to 10");
 +
 
 +
  // close session
 +
  $session->close();
 +
 
 +
  // print time needed
 +
  $time = (microtime(true) - $start) * 1000;
 +
  print "\n$time ms\n";
 +
 
 +
} catch (Exception $e) {
 +
  // print exception
 +
  print $e->getMessage();
 +
}
 +
?>
 +
</pre>

Latest revision as of 17:39, 1 December 2023

In the standard mode of the Clients, a database command can be sent to the server using the execute() function of the Session. This functions returns the whole result. With the info() function, you can request some information on your executed process. If an error occurs, an exception with the error message will be thrown.

Usage[edit]

The standard execution works as follows:

  1. Create a new session instance with hostname, port, username and password.
  2. Call the execute() function of the session with the database commands as argument.
  3. Receive the result of a successfully executed command. If an error occurs, an exception is thrown.
  4. Optionally, call info() to get some process information
  5. Continue using the client (back to 2.), or close the session.

Example in PHP[edit]

Taken from our repository:

<?php
/*
 * This example shows how database commands can be executed.
 * Documentation: http://basex.org/api
 *
 * (C) BaseX Team 2005-15, BSD License
 */
include("BaseXClient.php");

try {
  // initialize timer
  $start = microtime(true);

  // create session
  $session = new Session("localhost", 1984, "admin", "...");
  
  // perform command and print returned string
  print $session->execute("xquery 1 to 10");

  // close session
  $session->close();

  // print time needed
  $time = (microtime(true) - $start) * 1000;
  print "\n$time ms\n";

} catch (Exception $e) {
  // print exception
  print $e->getMessage();
}
?>