Chapter 3 DynaScript Predefined Objects
The connection
object
has these methods:
connection.Commit ( )
Commits the transaction.
Boolean.
This example moves 100 dollars from a checking account to a savings account. If the money is moved successfully, a commit is performed:
<!--SCRIPT
success = false;
query = connection.CreateQuery( "select amount from savings where id = '99999999'" );
if( query != null ) {
balance = query.GetValue(1) - 100;
query.Close();
query = connection.CreateQuery( "update savings set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
query = connection.CreateQuery( "select amount from chequing where id = '99999999'" );
if( query != null ) {
balance = query.GetValue(1) + 100;
query.Close();
query = connection.CreateQuery( "update chequing set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
success = true;
}
}
}
}
if( success ) {
connection.Commit();
} else {
connection.Rollback();
}
-->
connection.Connect ( )
Connects to a connection object.
Boolean. Indicates whether the connection was successful or not.
This example connects to the myConn connection:
<!--SCRIPT
myConn = site.CreateConnection( "myConn","My new ODBC connection", "Dynamo demo","dba","sql","ODBC" );
myConn.Connect();
-->
connection.CreateComponent (packageName, componentName )
This method is for calling Jaguar components. connection.CreateComponent
returns
a component object representing the object referenced within the
package.
The component object or null.
This example calls a Jaguar component and uses its setMessage method:
<!--SCRIPT
myJagComponent = myJaguarconnection.CreateComponent( "Demo_Components", "SharedMsg" );
myQueryObject = myJagcomponent.setMessage( message );
-->
connection.CreateQuery ( [ SQLStatement, [doExecute ]] )
Executes a query containing a sqlStatement
and returns a query object (equivalent to performing myQuery.SetSQL
and myQuery.Execute
).
You can use CreateQuery method in these ways:
connection.CreateQuery()
creates
an "empty" query object. You can then call the SetSQL
and Execute
methods at
a later time.
connection.CreateQuery("select * from
product")
creates and executes a query.
connection.CreateQuery("select * from
product", false)
creates a query object and
sets the SQL statement, but does not execute it. You can then call
the Execute
method at a
later time.
Returns a query object.
This example queries the sample database (using the connection "sample") for a list of all the employees and then displays the list in alphabetical order:
<!--SCRIPT
// Create a query to display first and last name
// of employees in alphabetical order
myConn=site.GetConnection( "sample" );
if ( myConn != null ) {
myQuery = myConn.CreateQuery( "select emp_lname, emp_fname from employee order by emp_lname, emp_fname" );
if ( myQuery.GetErrorCode != 0 )
while( myQuery.MoveNext() ) {
lastName = myQuery.GetValue(1);
firstName = myQuery.GetValue(2);
document.WriteLn( lastName + ", " + firstName );
}
}
-->
This example has the following output:
Ahmed, Alex
Barker, Joseph
Barletta, Irene
Bertrand, Jeannette
Bigelow, Janet
Blaikie, Barbara
Braun, Jane
Breault, Robert
Bucceri, Matthew
Butterfield, Joyce
Chao, Shih Lin
Charlton, Doug
Chin, Philip
Clark, Alison
Cobb, Matthew
Coe, Kristen
Evans, Scott
...
Disconnects a connection object from a database.
connection.Disconnect ( )
This example disconnects from a connection called myConn:
<!--SCRIPT
myConn = site.CreateConnection( "myConn","My new ODBC connection", "Dynamo demo","dba","sql","ODBC" );
myConn.Disconnect();
-->
connection.GetErrorCode( )
Returns the current error code.
Integer. Returns zero if a SQL instruction is carried out correctly, and information to which the script can respond if it fails.
This example displays a 0 if the query executes without error:
<HTML>
<TITLE>sample.stm</TITLE>
<BODY>
<H1></H1>
<!--SQL
SELECT customer.fname, customer.lname, customer.phone, customer.id
FROM DBA.customer customer
-->
<!--SCRIPT
queryError = connection.GetErrorCode();
document.WriteLn( queryError );
-->
</BODY>
</HTML>
The "query properties".
The "GetState method".
connection.GetErrorInfo( )
A description of the error.
String.
This example displays information explaining why the query did not work correctly:
<HTML>
<TITLE>sample.stm</TITLE>
<BODY>
<H1></H1>
<!--SQL
SELECT customer.fname, customer.lname, customer.phone, customer.id
FROM DBA.customer customeer
-->
<!--SCRIPT
queryState = connection.GetState();
document.WriteLn( queryState );
queryInfo = connection.GetErrorInfo();
document.WriteLn( queryInfo );
-->
</BODY>
</HTML>
The "query properties".
connection.GetState( )
Returns the current SQL state of the query.
The values returned by GetState
depend
on your database driver. For more information, see your database's
documentation.
This example displays the SQL state for the connection:
<HTML>
<TITLE>sample.stm</TITLE>
<BODY>
<H1></H1>
<!--SQL
SELECT customer.fname, customer.lname, customer.phone, customer.id
FROM DBA.customer customeer
-->
<!--SCRIPT
queryState = connection.GetState();
document.WriteLn( queryState );
queryInfo = connection.GetErrorInfo();
document.WriteLn( queryInfo );
-->
</BODY>
</HTML>
The "query properties".
connection.GetSupportedMoves ( )
Provides a list of Move
methods
that can be called for queries using that particular connection.
This example displays the available Move methods
for the connection
connection:
<!--SCRIPT
moves = connection.GetSupportedMoves();
i = 0;
while( exists( moves[i] ) ) {
document.writeln( moves[i] );
i++;
}
-->
If this was a connection to Adaptive Server Anywhere, the output would be:
MoveFirst
MoveNext
MovePrevious
MoveRelative
Move
MoveLast
connection.Rollback ( )
Performs a rollback on the transaction.
Boolean.
This example moves 100 dollars from a checking account to a savings account. If the money is not moved successfully, a rollback is done:
<!--SCRIPT
success = false;
query = connection.CreateQuery( "select amount from savings where id = '99999999'" );
if( query != null ) {
balance = query.GetValue(1) - 100;
query.Close();
query = connection.CreateQuery( "update savings set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
query = connection.CreateQuery( "select amount from chequing where id = '99999999' );
if( query != null ) {
balance = query.GetValue(1) + 100;
query.Close();
query = connection.CreateQuery( "update chequing set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
success = true;
}
}
}
if( success ) {
connection.Commit();
} else {
connection.Rollback();
}
-->
Copyright © 1999 Sybase, Inc. All rights reserved. |
![]() |