summaryrefslogtreecommitdiffstats
path: root/examples/declarative/tutorials/contacts/shared/contactGenSQL.pl
blob: 2d328da1f7a4b16b1c0e5c9b73b6a65a8ef43608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl

use warnings;
use strict;

my $count = shift;

open(MFIRST, "<english-m.txt") or die "Could not open male names";
open(FFIRST, "<english-f.txt") or die "Could not open female names";
open(SURNAME, "<english-s.txt") or die "Could not open surnames names";
open(ISP, "<email.txt") or die "Could not open isp names";

my @mnames = <MFIRST>;
my @fnames = <FFIRST>;
my @surnames = <SURNAME>;
my @isps = <ISP>;

print "BEGIN;\n";
print "CREATE TABLE contacts (recid INTEGER PRIMARY KEY, label TEXT, phone TEXT, email TEXT);\n";
print "CREATE INDEX contactSortOrder ON contacts(label, recid);\n";

my $i = 0;
while ($i < $count) {
    $i++;
    my $fn = randomFirstName();
    my $sn = randomLastName();
    my $em = randomEmail($fn, $sn);
    my $ph = randomPhoneNumber();

    $fn =~ s/'/''/g;
    $sn =~ s/'/''/g;
    $em =~ s/'/''/g;
    print "INSERT INTO contacts (label, email, phone) VALUES('$fn $sn', '$em', '$ph');\n"
}

print "COMMIT;\n";


sub randomFirstName
{
    my $name;
    if (int(rand 2) == 1) {
        $name = $mnames[int(rand @mnames)];
    } else {
        $name = $fnames[int(rand @fnames)];
    }
    chomp $name;
    $name;
}

sub randomLastName
{
    my $name = $surnames[int(rand @surnames)];
    chomp $name;
    $name;
}

sub randomEmail
{
    my ($fn, $ln) = @_;
    my $isp = $isps[int(rand @isps)];
    chomp $isp;
    my $path = int(rand 3);
    my $email;
    if ($path == 0) {
        $email = "$fn.$ln\@$isp";
    } elsif ($path == 1) {
        $email = "$fn\@$isp";
    } elsif ($path == 2) {
        $email = "$ln\@$isp";
    }
}

sub randomPhoneNumber
{
    my @digits = qw(1 2 3 4 5 6 7 8 9 0);
    my $phonenumber;
    for (1 .. 8) {
        $phonenumber .= $digits[int(rand @digits)];
    }
    $phonenumber;
}