Exemplo n.º 1
0
 public static function resetAutoIncrement($tableName, $startIndex = 1)
 {
     $startIndex = intval($startIndex);
     if ($startIndex <= 0 || !strlen($tableName)) {
         return false;
     }
     $dbConnection = Main\HttpApplication::getConnection();
     $dbHelper = $dbConnection->getSqlHelper();
     $tableName = $dbHelper->forSql(trim($tableName));
     if (strlen($tableName) > 27) {
         // too long
         return false;
     }
     if ($sqName = Helper::checkSequenceExistsForTable($tableName)) {
         $dbConnection->query('drop sequence ' . $sqName);
     }
     $dbConnection->query('create sequence ' . $sqName . ' start with ' . $startIndex . ' increment by 1 NOMAXVALUE NOCYCLE NOCACHE NOORDER');
     $dbConnection->query("\n\t\t\tCREATE OR REPLACE TRIGGER " . $tableName . "_I\n\t\t\tBEFORE INSERT\n\t\t\tON " . $tableName . "\n\t\t\tFOR EACH ROW\n\t\t\tBEGIN\n\t\t\t\tIF :NEW.ID IS NULL THEN\n\t\t\t\t\tSELECT " . $sqName . ".NEXTVAL INTO :NEW.ID FROM dual;\n\t\t\t\tEND IF;\n\t\t\tEND;\n\t\t");
     return true;
 }