<?php
class DB
{
	public static function CurrentConnection()
	{
		global $currentConnection;
		return $currentConnection ? $currentConnection : DB::DefaultConnection();
	}

	public static function CurrentConnectionId()
	{
		$conn = DB::CurrentConnection();
		return $conn->connId;
	}

	public static function DefaultConnection()
	{
		global $cman;
		return $cman->getDefault();
	}

	public static function ConnectionByTable( $table )
	{
		global $cman;
		return $cman->byTable($table);
	}

	public static function ConnectionByName( $name )
	{
		global $cman;
		return $cman->byName( $name );
	}

	public static function SetConnection( $connection )
	{
		global $currentConnection;
		if ( is_string( $connection ) )
		{
			$currentConnection = DB::ConnectionByName( $connection );
		}
		else if ( is_a($connection, 'Connection') ) {
		 	$currentConnection = $connection;
		}
	}

	public static function LastId()
	{
		return DB::CurrentConnection()->getInsertedId();
	}

	public static function Query( $sql )
	{
		return DB::CurrentConnection()->querySilent( $sql );
	}

	public static function Exec( $sql )
	{
		return DB::CurrentConnection()->execSilent( $sql ) != NULL;
	}

	public static function LastError()
	{
		return DB::CurrentConnection()->lastError();
	}

	public static function Select($table, $userConditions = array() )
	{
		$tableInfo = DB::_getTableInfo($table);

		if ( !$tableInfo )
			return false;

		$whereSql = DB::_getWhereSql($userConditions, $tableInfo["fields"]);

		$sql = "SELECT * FROM ".DB::CurrentConnection()->addTableWrappers( $tableInfo['fullName'] ) . $whereSql;
		$queryResult = DB::CurrentConnection()->querySilent( $sql );

		return $queryResult;
	}

	public static function Delete($table, $userConditions = array() )
	{
		$tableInfo = DB::_getTableInfo($table);

		if ( !$tableInfo )
			return false;

		$whereSql = DB::_getWhereSql($userConditions, $tableInfo["fields"]);

		if( $whereSql == "" )
			return false;

		$sql = "DELETE FROM ".DB::CurrentConnection()->addTableWrappers( $tableInfo['fullName'] ) . $whereSql;
		$ret = DB::CurrentConnection()->execSilent( $sql );

		return $ret;
	}

	public static function Insert($table, $data)
	{
		$result = false;
		$tableInfo = DB::_getTableInfo($table);

		if ( !$tableInfo )
			return false;

		$iFields = "";
		$iValues = "";
		$blobs = array();
		foreach($data as $fieldname => $value)
		{
			$field = getArrayElementNC($tableInfo["fields"], $fieldname);

			// user field not found in table
			if ( is_null($field) )
				continue;

			$iFields.= DB::CurrentConnection()->addFieldWrappers( $field["name"] ).",";
			$iValues.= DB::_prepareValue($value, $field["type"]) . ",";

			if( DB::CurrentConnection()->dbType == nDATABASE_Oracle || DB::CurrentConnection()->dbType == nDATABASE_DB2 || DB::CurrentConnection()->dbType == nDATABASE_Informix )
			{
				if( IsBinaryType( $field["type"] ) )
					$blobs[ $field["name"] ] = $value;

				if( DB::CurrentConnection()->dbType == nDATABASE_Informix && IsTextType( $field["type"] ) )
					$blobs[ $field["name"] ] = $value;
			}
		}

		if( $iFields != "" && $iValues != "" )
		{
			$iFields = substr($iFields, 0, -1);
			$iValues = substr($iValues, 0, -1);
			$sql = "INSERT INTO ".DB::CurrentConnection()->addTableWrappers( $tableInfo['fullName'] )." (".$iFields.") values (".$iValues.")";

			if ( count($blobs) > 0 )
				$result = DB::_execSilentWithBlobProcessing($blobs, $sql, $tableInfo['fields']);
			else
				$result = DB::CurrentConnection()->execSilent( $sql );
		}

		return $result;
	}

	public static function Update($table, $data, $userConditions)
	{
		$result = false;
		$tableInfo = DB::_getTableInfo($table);

		if ( !$tableInfo )
			return false;

		$whereSql = DB::_getWhereSql($userConditions, $tableInfo["fields"]);

		if( $whereSql == "" )
			return false;

		$updateValues = array();
		$blobs = array();
		foreach( $data as $fieldname => $value )
		{
			$field = getArrayElementNC($tableInfo["fields"], $fieldname);

			// user field not found in table
			if ( is_null($field) )
				continue;

			$prepareFieldName = DB::CurrentConnection()->addFieldWrappers( $field["name"] );
			$prepareValue = DB::_prepareValue($value, $field["type"]);
			$updateValues[] = $prepareFieldName."=".$prepareValue;

			if ( DB::CurrentConnection()->dbType == nDATABASE_Oracle || DB::CurrentConnection()->dbType == nDATABASE_DB2 || DB::CurrentConnection()->dbType == nDATABASE_Informix )
			{
				if ( IsBinaryType( $field["type"] ) )
					$blobs[ $field["name"] ] = $value;

				if ( DB::CurrentConnection()->dbType == nDATABASE_Informix && IsTextType( $field["type"] ) )
					$blobs[ $field["name"] ] = $value;
			}
		}

		if ( count($updateValues) > 0 )
		{
			$updateSQL = implode(",", $updateValues);
			$sql = "UPDATE ".DB::CurrentConnection()->addTableWrappers( $tableInfo['fullName'] )." SET ". $updateSQL . $whereSql;

			if ( count($blobs) > 0 )
				$result = DB::_execSilentWithBlobProcessing($blobs, $sql, $tableInfo['fields']);
			else
				$result = DB::CurrentConnection()->execSilent( $sql );
		}

		return $result;
	}

	protected static function _getWhereSql($userConditions, $founedfields)
	{
		if( !is_array( $userConditions ) )
		{
			$whereSql = trim( $userConditions );
			if( $whereSql != "")
				$whereSql = " WHERE " . $whereSql;
			return $whereSql;
		}

		$conditions = array();
		foreach($userConditions as $fieldname => $value)
		{
			$field = getArrayElementNC($founedfields, $fieldname);
			// user field not found in table
			if ( is_null($field) )
				continue;

			$wrappedField = DB::CurrentConnection()->addFieldWrappers( $field["name"] );
			if ( is_null($value) )
			{
				$conditions[] = $wrappedField . " IS NULL";
			}
			else
			{
				$conditions[] = $wrappedField . "=" . DB::_prepareValue($value, $field["type"]);
			}
		}

		$whereSql = "";
		if( count($conditions) > 0 )
		{
			$whereSql .= " WHERE " . implode(" AND ", $conditions);
		}

		return $whereSql;
	}

	/**
	 * @param Array blobs
	 * @param String dalSQL
	 * @param Array tableinfo
	 */
	protected static function _execSilentWithBlobProcessing($blobs, $dalSQL, $tableinfo)
	{
		$blobTypes = array();
		if( DB::CurrentConnection()->dbType == nDATABASE_Informix )
		{
			foreach( $blobs as $fname => $fvalue )
			{
				$blobTypes[ $fname ] = $tableinfo[ $fname ]["type"];
			}
		}

		DB::CurrentConnection()->execSilentWithBlobProcessing( $dalSQL, $blobs, $blobTypes );
	}

	protected static function _prepareValue($value, $type)
	{
		if ( is_null($value) )
			return "NULL";

		if( DB::CurrentConnection()->dbType == nDATABASE_Oracle || DB::CurrentConnection()->dbType == nDATABASE_DB2 || DB::CurrentConnection()->dbType == nDATABASE_Informix )
		{
			if( IsBinaryType($type) )
			{
				if( DB::CurrentConnection()->dbType == nDATABASE_Oracle )
					return "EMPTY_BLOB()";

				return "?";
			}

			if( DB::CurrentConnection()->dbType == nDATABASE_Informix  && IsTextType($type) )
				return "?";
		}

		if( IsNumberType($type) && !is_numeric($value) )
		{
			$value = trim($value);
			$value = str_replace(",", ".", $value);
			if ( !is_numeric($value) )
				return "NULL";
		}

		if( IsDateFieldType($type) || IsTimeType($type) )
		{
			if( !$value )
				return "NULL";

			// timestamp
			if ( is_int($value) )
			{
				if ( IsDateFieldType($type) )
				{
					$value = getYMDdate($value) . " " . getHISdate($value);
				}
				else if ( IsTimeType($type) )
				{
					$value = getHISdate($value);
				}
			}

			return DB::CurrentConnection()->addDateQuotes( $value );
		}

		if( NeedQuotes($type) )
			return DB::CurrentConnection()->prepareString( $value );

		return $value;
	}

	/**
	 * 	Find table info stored in the project file
	 *
	 */
	public static function _findDalTable( $table, $conn = null )
	{
		global $dalTables;
		if( !$conn )
			$conn = DB::CurrentConnection();
		$tableName = $conn->getTableNameComponents( $table );

		DB::_fillTablesList();

		//	exact match
		foreach( $dalTables[$conn->connId] as $t )
		{
			if( $t["schema"] == $tableName["schema"] && $t["name"] == $tableName["table"] )
				return $t;
		}

		//	case-insensitive
		$tableName["schema"] = strtoupper( $tableName["schema"] );
		$tableName["table"] = strtoupper( $tableName["table"] );

		foreach( $dalTables[$conn->connId] as $t )
		{
			if( strtoupper( $t["schema"] ) == $tableName["schema"] && strtoupper( $t["name"] ) == $tableName["table"] )
				return $t;
		}
		return null;
	}

	/**
	 * 	Get list of table field names and types
	 *	Check tables stored in the project first, then fetch it from the database.
	 *
	 */
	public static function _getTableInfo($table, $connId = null )
	{
		global $dal_info, $tableinfo_cache;
		if( !$connId )
			$connId = DB::CurrentConnectionId();

		//	prepare cache
		if( !isset($tableinfo_cache[ $connId ] ) )
			$tableinfo_cache[ $connId ] = array();

		$tableInfo = array();


		$tableDescriptor = DB::_findDalTable( $table );

		if ( $tableDescriptor )
		{
			importTableInfo( $tableDescriptor["varname"] );

			$tableInfo["fields"] = $dal_info[ $tableDescriptor["varname"] ];

			if( $tableDescriptor["schema"] )
				$tableInfo["fullName"] = $tableDescriptor["schema"] . "." . $tableDescriptor["name"];
			else
				$tableInfo["fullName"] = $tableDescriptor["name"];
		}
		else
		{
			//	check cache first
			if( isset($tableinfo_cache[ $connId ][ $table ] ) )
				return $tableinfo_cache[ $connId ][ $table ];

			//	fetch table info from the database
			$helpSql = "select * from " . DB::CurrentConnection()->addTableWrappers( $table ) . " where 1=0";

			$tableInfo["fullName"] = $table;
			$tableInfo["fields"] = array();

			// in case getFieldsList throws error
			$tableinfo_cache[ $connId ][ $table ] = false;

			$fieldList = DB::CurrentConnection()->getFieldsList($helpSql);
			foreach ($fieldList as $f )
			{
				$tableInfo["fields"][ $f["fieldname"] ] = array( "type" => $f["type"], "name" => $f["fieldname"] );
			}
			$tableinfo_cache[ $connId ][ $table ] = $tableInfo;
		}

		return $tableInfo;
	}


	protected static function _fillTablesList()
	{
		global $dalTables;
		$conn = DB::CurrentConnection();
		if( $dalTables[ $conn->connId ] )
			return;
		$dalTables[ $conn->connId ] = array();
		if( "test2_at_localhost" == $conn->connId )
		{
			$dalTables[$conn->connId][] = array("name" => "advances_to_supplier", "varname" => "test2_at_localhost__advances_to_supplier", "altvarname" => "advances_to_supplier", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "attendance", "varname" => "test2_at_localhost__attendance", "altvarname" => "attendance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "attendance_master", "varname" => "test2_at_localhost__attendance_master", "altvarname" => "attendance_master", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "bank_accounts", "varname" => "test2_at_localhost__bank_accounts", "altvarname" => "bank_accounts", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "bank_activity", "varname" => "test2_at_localhost__bank_activity", "altvarname" => "bank_activity", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "brand", "varname" => "test2_at_localhost__brand", "altvarname" => "brand", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "buyers", "varname" => "test2_at_localhost__buyers", "altvarname" => "buyers", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "buyers_advance", "varname" => "test2_at_localhost__buyers_advance", "altvarname" => "buyers_advance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "category", "varname" => "test2_at_localhost__category", "altvarname" => "category", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "cheque_register", "varname" => "test2_at_localhost__cheque_register", "altvarname" => "cheque_register", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "cheque_register_m", "varname" => "test2_at_localhost__cheque_register_m", "altvarname" => "cheque_register_m", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "company_profile", "varname" => "test2_at_localhost__company_profile", "altvarname" => "company_profile", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "customer", "varname" => "test2_at_localhost__customer", "altvarname" => "customer", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "daily_report", "varname" => "test2_at_localhost__daily_report", "altvarname" => "daily_report", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "expen_detail", "varname" => "test2_at_localhost__expen_detail", "altvarname" => "expen_detail", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "expen_source", "varname" => "test2_at_localhost__expen_source", "altvarname" => "expen_source", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "expenditure_data", "varname" => "test2_at_localhost__expenditure_data", "altvarname" => "expenditure_data", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "expens", "varname" => "test2_at_localhost__expens", "altvarname" => "expens", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "expense_category", "varname" => "test2_at_localhost__expense_category", "altvarname" => "expense_category", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "loa_types", "varname" => "test2_at_localhost__loa_types", "altvarname" => "loa_types", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "loans", "varname" => "test2_at_localhost__loans", "altvarname" => "loans", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "new_start6_audit", "varname" => "test2_at_localhost__new_start6_audit", "altvarname" => "new_start6_audit", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "new_start6_locking", "varname" => "test2_at_localhost__new_start6_locking", "altvarname" => "new_start6_locking", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "new_start8_settings", "varname" => "test2_at_localhost__new_start8_settings", "altvarname" => "new_start8_settings", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "opening_balance", "varname" => "test2_at_localhost__opening_balance", "altvarname" => "opening_balance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "payment_for_pur", "varname" => "test2_at_localhost__payment_for_pur", "altvarname" => "payment_for_pur", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "payment_of_loan_installments", "varname" => "test2_at_localhost__payment_of_loan_installments", "altvarname" => "payment_of_loan_installments", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "payments", "varname" => "test2_at_localhost__payments", "altvarname" => "payments", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "product", "varname" => "test2_at_localhost__product", "altvarname" => "product", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "purchasing", "varname" => "test2_at_localhost__purchasing", "altvarname" => "purchasing", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "purchasing_details", "varname" => "test2_at_localhost__purchasing_details", "altvarname" => "purchasing_details", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "salaries_and_allowances", "varname" => "test2_at_localhost__salaries_and_allowances", "altvarname" => "salaries_and_allowances", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "salary_advance", "varname" => "test2_at_localhost__salary_advance", "altvarname" => "salary_advance", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "salary_list", "varname" => "test2_at_localhost__salary_list", "altvarname" => "salary_list", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "salary_summary", "varname" => "test2_at_localhost__salary_summary", "altvarname" => "salary_summary", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "salary_voucher", "varname" => "test2_at_localhost__salary_voucher", "altvarname" => "salary_voucher", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "sales", "varname" => "test2_at_localhost__sales", "altvarname" => "sales", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "sales_details", "varname" => "test2_at_localhost__sales_details", "altvarname" => "sales_details", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "seasonal discounts", "varname" => "test2_at_localhost__seasonal_discounts", "altvarname" => "seasonal_discounts", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "special_discount", "varname" => "test2_at_localhost__special_discount", "altvarname" => "special_discount", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "stock_transfer", "varname" => "test2_at_localhost__stock_transfer", "altvarname" => "stock_transfer", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "supplier_advance_cut", "varname" => "test2_at_localhost__supplier_advance_cut", "altvarname" => "supplier_advance_cut", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "sys_manage", "varname" => "test2_at_localhost__sys_manage", "altvarname" => "sys_manage", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "theme_list", "varname" => "test2_at_localhost__theme_list", "altvarname" => "theme_list", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
			$dalTables[$conn->connId][] = array("name" => "units", "varname" => "test2_at_localhost__units", "altvarname" => "units", "connId" => "test2_at_localhost", "schema" => "", "connName" => "tharanga at localhost");
		}
	}


	public static function PrepareSQL($sql)
	{
		$args = func_get_args();

		$conn = DB::CurrentConnection();

		$tokens = DB::scanTokenString($sql);

		$replacements = array();
		// build array of replacements in this format:
		//	"offset" => position in the string where replacement should be done
		//  "len" => length of original substring to cut out
		//  "insert" => string to insert in place of cut out

		foreach ($tokens["matches"] as $i => $match) {
			$offset = $tokens["offsets"][$i];
			$token = $tokens["tokens"][$i];

			$repl = array(
				"offset" => $offset,
				"len" => strlen($match)
			);

			$val = "";
			if (is_numeric($token) && count( $args ) > $token) {
				$val = $args[(int)$token];
			} else {
				$val = RunnerContext::getValue($token);
			}

			/**
			 * Don't ever dare to alter this code!
			 * Everything outside quotes must be converted to number to avoid SQL injection
			 */
			if ($conn->positionQuoted($sql, $offset))
				$repl["insert"] = $conn->addSlashes($val);
			else
				$repl["insert"] = DB::prepareNumberValue($val);

			$replacements[] = $repl;
		}

		//	do replacements
		return RunnerContext::doReplacements( $sql, $replacements );
	}

	/**
	 *	@return Array
	 */
	public static function readSQLTokens( $sql )
	{
		$arr = DB::scanTokenString( $sql );
		return $arr["tokens"];
	}

	/**
	 *	@return Array
	 */
	public static function readMasterTokens( $sql )
	{
		$masterTokens = array();

		$allTokens = DB::readSQLTokens( $sql );
		foreach ( $allTokens as $key => $token )
		{
			$dotPos = strpos(  $token, "." );
			if( $dotPos !== FALSE && strtolower( substr( $token, 0, $dotPos ) ) == "master")
			{
				$masterTokens[] = $token;
			}
		}

		return $masterTokens;
	}

	/**
	 *	Scans SQL string, finds all tokens. Returns three arrays - 'tokens', 'matches' and 'offsets'
	 *  Offsets are positions of corresponding 'matches' items in the string
	 *  Example:
	 *  insert into table values (':aaa', :old.bbb, ':{master.order date}')
	 *  tokens: ["aaa", "old.bbb", "master.order date"]
	 *  matches: [":aaa", ":old.bbb", ":{master.order date}"]
	 *  offsets: [28, 35, 46]
	 *
	 *	Exceptions for tokens without {}
	 *	1. shouldn't start with number
	*		:62aaa
	 *	2. shouldn't follow letter
	 *		x:aaa
	 *	3. shouldn't follow :
	 *		::aaa
	 *
 	 *	@return Array [ "tokens" => Array, "matches" => Array, "offsets" => Array ]
	 */
	public static function scanTokenString($sql)
	{
		$tokens = array();
		$offsets = array();
		$matches = array();

		//	match aaa, old.bbb, master.order date from:
		//	insert into table values (':aaa', :old.bbb, ':{master.order date}')

		$pattern = '/(?:[^\w\:]|^)(\:([a-zA-Z_]{1}[\w\.]*))|\:\{(.*?)\}|(?:[^\w\:]|^)(\:([1-9]+[0-9]*))/';


		$result = findMatches($pattern, $sql);
		foreach ($result as $m) {
			if ($m["submatches"][0] != "") {
				// first variant, no {}
				$matches[] = $m["submatches"][0];
				$tokens[] = $m["submatches"][1];
				$offsets[] = $m["offset"] + strpos($m["match"], $m["submatches"][0]);
			} else if ($m["submatches"][2] != "") {
				// second variant, in {}
				$matches[] = $m["match"];
				$tokens[] = $m["submatches"][2];
				$offsets[] = $m["offset"];
			} else if ($m["submatches"][3] != "") {
				// third variant, numeric like (:1, ':2')
				$matches[] = $m["submatches"][3];
				$tokens[] = $m["submatches"][4];
				$offsets[] = $m["offset"] + strpos($m["match"], $m["submatches"][3]);
			}
		}

		return array("tokens" => $tokens, "matches" => $matches, "offsets" => $offsets);
	}

	public static function scanNewTokenString($sql)
	{
		$tokens = array();
		$offsets = array();
		$matches = array();

		//	match aaa, old.bbb, master.order date from:
		//	insert into table values (':aaa', :old.bbb, ':{master.order date}')

		$pattern = "/\\\${[^\\s\{\\}]+}/";


		$result = findMatches($pattern, $sql);
		foreach ($result as $m) {
			$match = $m["match"];
			if ( $match != "" ) {
				$matches[] = $match;
				$tokens[] = substr( $match, 2, strlen( $match ) - 3 );
				$offsets[] = $m["offset"];
			}
		}

		return array("tokens" => $tokens, "matches" => $matches, "offsets" => $offsets);
	}


	public static function prepareNumberValue( $value )
	{
		$strvalue = (string)$value;
		if(is_numeric($strvalue))
			return str_replace(",",".",$strvalue);
		return 0;
	}

}

?>