93 lines
2.2 KiB
Perl
93 lines
2.2 KiB
Perl
# Selima Website Content Management System
|
|
# NewSN.pm: The new S/N generator.
|
|
|
|
# Copyright (c) 2003-2018 imacat.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
# Author: imacat <imacat@mail.imacat.idv.tw>
|
|
# First written: 2003-04-03
|
|
|
|
package Selima::NewSN;
|
|
use 5.008;
|
|
use strict;
|
|
use warnings;
|
|
use base qw(Exporter);
|
|
use vars qw(@EXPORT @EXPORT_OK);
|
|
BEGIN {
|
|
@EXPORT = qw(newsn newsn_hash newsn_xmltab);
|
|
@EXPORT_OK = @EXPORT;
|
|
# Prototype declaration
|
|
sub newsn($);
|
|
sub newsn_hash(%);
|
|
sub newsn_xmltab($);
|
|
}
|
|
|
|
use Selima::DataVars qw($DBH);
|
|
|
|
use constant SN_LEN => 9;
|
|
|
|
# The relevent variables
|
|
use vars qw($MIN $MAX $INT);
|
|
$MIN = 10 ** (SN_LEN - 1); # 100000000
|
|
$MAX = (10 ** SN_LEN) - 1; # 999999999
|
|
$INT = $MAX - $MIN + 1;
|
|
|
|
# newsn: Generate a new random serial number for a database table
|
|
sub newsn($) {
|
|
local ($_, %_);
|
|
my ($table, $sth, $sql);
|
|
$table = $_[0];
|
|
|
|
do {
|
|
# Generate a random serial number
|
|
$_ = $MIN + int rand $INT;
|
|
# Check if this serial number exists
|
|
$sql = "SELECT sn FROM $table WHERE sn=$_;\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
} until $sth->rows == 0;
|
|
|
|
return $_;
|
|
}
|
|
|
|
# newsn_hash: Generate a new random serial number
|
|
sub newsn_hash(%) {
|
|
local ($_, %_);
|
|
my %SN = @_;
|
|
|
|
do {
|
|
# Generate a random serial number
|
|
$_ = $MIN + int rand $INT;
|
|
} until !exists $SN{$_};
|
|
|
|
return $_;
|
|
}
|
|
|
|
# newsn_xmltab: Generate a new random serial number
|
|
sub newsn_xmltab($) {
|
|
local ($_, %_);
|
|
my $SN = $_[0];
|
|
|
|
do {
|
|
# Generate a random serial number
|
|
$_ = $MIN + int rand $INT;
|
|
} until !exists ${$SN}{$_};
|
|
|
|
${$SN}{$_} = (keys %{$SN});
|
|
|
|
return $_;
|
|
}
|
|
|
|
return 1;
|