~ubuntu-branches/ubuntu/feisty/libdbd-csv-perl/feisty

« back to all changes in this revision

Viewing changes to t/CSV.dbtest

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Zander
  • Date: 2002-04-12 16:10:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412161047-7aiszzv9vwkgz8rk
Tags: upstream-0.2002
ImportĀ upstreamĀ versionĀ 0.2002

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Hej, Emacs, give us -*- perl -*- mode here!
 
2
#
 
3
#   $Id: CSV.dbtest,v 1.1.1.1 1999/06/13 12:59:35 joe Exp $
 
4
#
 
5
# database specific definitions for a 'CSV' database
 
6
 
 
7
 
 
8
#   This function generates a mapping of ANSI type names to
 
9
#   database specific type names; it is called by TableDefinition().
 
10
#
 
11
sub AnsiTypeToDb ($;$) {
 
12
    my ($type, $size) = @_;
 
13
    my ($ret);
 
14
 
 
15
    if ((lc $type) eq 'char'  ||  (lc $type) eq 'varchar') {
 
16
        $size ||= 1;
 
17
        return (uc $type) . " ($size)";
 
18
    } elsif ((lc $type) eq 'blob'  ||  (lc $type) eq 'real'  ||
 
19
               (lc $type) eq 'integer') {
 
20
        return uc $type;
 
21
    } elsif ((lc $type) eq 'int') {
 
22
        return 'INTEGER';
 
23
    } else {
 
24
        warn "Unknown type $type\n";
 
25
        $ret = $type;
 
26
    }
 
27
    $ret;
 
28
}
 
29
 
 
30
 
 
31
#
 
32
#   This function generates a table definition based on an
 
33
#   input list. The input list consists of references, each
 
34
#   reference referring to a single column. The column
 
35
#   reference consists of column name, type, size and a bitmask of
 
36
#   certain flags, namely
 
37
#
 
38
#       $COL_NULLABLE - true, if this column may contain NULL's
 
39
#       $COL_KEY - true, if this column is part of the table's
 
40
#           primary key
 
41
#
 
42
#   Hopefully there's no big need for you to modify this function,
 
43
#   if your database conforms to ANSI specifications.
 
44
#
 
45
 
 
46
sub TableDefinition ($@) {
 
47
    my($tablename, @cols) = @_;
 
48
    my($def);
 
49
 
 
50
    #
 
51
    #   Should be acceptable for most ANSI conformant databases;
 
52
    #
 
53
    #   msql 1 uses a non-ANSI definition of the primary key: A
 
54
    #   column definition has the attribute "PRIMARY KEY". On
 
55
    #   the other hand, msql 2 uses the ANSI fashion ...
 
56
    #
 
57
    my($col, @keys, @colDefs, $keyDef);
 
58
 
 
59
    #
 
60
    #   Count number of keys
 
61
    #
 
62
    @keys = ();
 
63
    foreach $col (@cols) {
 
64
        if ($$col[2] & $::COL_KEY) {
 
65
            push(@keys, $$col[0]);
 
66
        }
 
67
    }
 
68
 
 
69
    foreach $col (@cols) {
 
70
        my $colDef = $$col[0] . " " . AnsiTypeToDb($$col[1], $$col[2]);
 
71
        if (!($$col[3] & $::COL_NULLABLE)) {
 
72
            $colDef .= " NOT NULL";
 
73
        }
 
74
        push(@colDefs, $colDef);
 
75
    }
 
76
    if (@keys) {
 
77
        $keyDef = ", PRIMARY KEY (" . join(", ", @keys) . ")";
 
78
    } else {
 
79
        $keyDef = "";
 
80
    }
 
81
    $def = sprintf("CREATE TABLE %s (%s%s)", $tablename,
 
82
                   join(", ", @colDefs), $keyDef);
 
83
}
 
84
 
 
85
 
 
86
#
 
87
#   This function generates a list of tables associated to a
 
88
#   given DSN.
 
89
#
 
90
sub ListTables(@) {
 
91
    my($dbh) = shift;
 
92
    my(@tables);
 
93
 
 
94
    @tables = $dbh->func('list_tables');
 
95
    if ($dbh->errstr) {
 
96
        die "Cannot create table list: " . $dbh->errstr;
 
97
    }
 
98
    @tables;
 
99
}
 
100
 
 
101
 
 
102
#
 
103
#   This function is called by DBD::pNET; given a hostname and a
 
104
#   dsn without hostname, return a dsn for connecting to dsn at
 
105
#   host.
 
106
sub HostDsn ($$) {
 
107
    my($hostname, $dsn) = @_;
 
108
    "$dsn:$hostname";
 
109
}
 
110
 
 
111
 
 
112
#
 
113
#   Return a string for checking, whether a given column is NULL.
 
114
#
 
115
sub IsNull($) {
 
116
    my($var) = @_;
 
117
 
 
118
    "$var IS NULL";
 
119
}
 
120
 
 
121
 
 
122
#
 
123
#   Return TRUE, if database supports transactions
 
124
#
 
125
sub HaveTransactions () {
 
126
    0;
 
127
}
 
128
 
 
129
 
 
130
if (! -d "output") {
 
131
    mkdir "output", 0755;
 
132
}
 
133
 
 
134
1;