php 序列生成工具

<?php
/**
 * 
 * 现在简单实现,可以处理成,根据对应的表,生成对应的序列进行生成,主要同步自增id
 *
 */
class OracleTools {
	/**
	 * 通过序列获取最新的id
	 * 如果制定表,如果表不存在,则创建新表。并以原表的id+1000的数据添加.
	 * 可以单独指定需要序列的表名和id
	 */
	public static function getNewId($seqname = "common", $id = "id", $tablename = "", $maxvalue = 0, $startbyzero = false) {
		$sseqname = $seqname;
		if (empty ( $tablename ))
			$tablename = $seqname;
		$seqname = "O2O_SEQ_{$seqname}";

		$exists = sql_fetch ( "SELECT
	count(1)  existsrow
FROM
	all_sequences
WHERE
	sequence_name = '$seqname'
AND sequence_owner = '" . Yii::app ()->db->username . "'", 'existsrow' );
		if (! $exists) {
			$lastid = 1;
			if (! $startbyzero) {
				$lastid = sql_fetch ( "SELECT {$id} FROM {$tablename} WHERE  ROWNUM =1 ORDER BY {$id} DESC ", $id );
				$lastid += 1000;
			}
			if (! $maxvalue) {
				$maxvalue = '9999999999999999';
			}
			sql_execute ( "CREATE SEQUENCE {$seqname} start WITH {$lastid} increment BY 1 MAXVALUE {$maxvalue} minvalue 1 cycle" );
		}
		$id = sql_fetch ( "SELECT {$seqname}.nextVal newid FROM dual", 'newid' );
		return $id;
	}
	public static function dropSeq($seqname = "common") {
		$seqname = "O2O_SEQ_{$seqname}";
		sql_execute ( "DROP SEQUENCE {$seqname} " );
	}
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注