Lasso Soft Inc. > Home

[inline_transaction]

Linkinline_transaction
AuthorJason Huck
CategoryUtility
Version8.x
Licensehttp://opensource.org/licenses/artistic-license.php
Posted26 Oct 2005
Updated16 Jan 2006
More by this author...

Description

Executes the supplied SQL queries as a single transaction. Accepts an array of SQL queries to execute and a pair array containing the connection parameters for the inlines.  If any single SQL query fails, the entire transaction will be rolled back.

Requires MySQL 4.x or higher and tables that support transactions, i.e., InnoDB.

Sample Usage

var('dbConn') = array(
    -username='foo',
    -password='bar',
    -database='test',
    -maxrecords='all'
);

var('sql' = array);

$sql->insert('INSERT INTO t1 (col1, col2) VALUES (1,2);');
$sql->insert('SET @newID = LAST_INSERT_ID();');
$sql->insert('INSERT INTO t2 (col3, col4) VALUES (@newID, 3);');

inline_transaction(
    -connection=$dbConn,
    -sql=$sql
);

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag(
	'transaction',
	-namespace='inline_',
	-required='connection',
	-type='array',
	-required='sql',
	-type='array',
	-priority='replace',
	-description='
		Executes the supplied SQL queries as a single transaction.
		Requires MySQL 4.x. and tables that support transactions, i.e., InnoDB.
	'
);
	local('error' = false);

	inline(
		#connection,
		-sql='START TRANSACTION;'
	);			
		iterate(#sql, local('qry'));
			inline( -sql=#qry);
				if(error_code != 0);
					inline( -sql='ROLLBACK;');/inline;
					#error = true;
					loop_abort;
				/if;
			/inline;
		/iterate;
		
		if(!#error);
			inline( -sql='COMMIT;');/inline;
		else;
			fail(-1, 'Transaction failed.');
		/if;
	/inline;
/define_tag;

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft