File: mSQL1.dbtest

package info (click to toggle)
msql-mysql-modules 1.2005-1
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 744 kB
  • ctags: 412
  • sloc: perl: 4,402; ansic: 1,753; makefile: 73
file content (124 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download | duplicates (2)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Hej, Emacs, give us -*- perl -*- mode here!
#
#   $Id: mSQL.dbtest 1.1 Tue, 30 Sep 1997 01:28:08 +0200 joe $
#
# database specific definitions for an 'mSQL1' database


#   This function generates a mapping of ANSI type names to
#   database specific type names; it is called by TableDefinition().
#
sub AnsiTypeToDb ($;$) {
    my ($type, $size) = @_;
    my ($ret);

    if ((lc $type) eq 'int'  ||  (lc $type) eq 'integer') {
	$ret = $type;
    } elsif ((lc $type) eq 'char') {
	$ret = "CHAR($size)";
    } else {
	warn "Unknown type $type\n";
	$ret = $type;
    }
    $ret;
}


#
#   This function generates a table definition based on an
#   input list. The input list consists of references, each
#   reference referring to a single column. The column
#   reference consists of column name, type, size and a bitmask of
#   certain flags, namely
#
#       $COL_NULLABLE - true, if this column may contain NULL's
#       $COL_KEY - true, if this column is part of the table's
#           primary key
#
#   Hopefully there's no big need for you to modify this function,
#   if your database conforms to ANSI specifications.
#

sub TableDefinition ($@) {
    my($tablename, @cols) = @_;
    my($def);

    #
    #   Should be acceptable for most ANSI conformant databases;
    #
    #   msql 1 uses a non-ANSI definition of the primary key: A
    #   column definition has the attribute "PRIMARY KEY". On
    #   the other hand, msql 2 uses the ANSI fashion ...
    #
    my($col, @keys, @colDefs, $keyDef);

    #
    #   Count number of keys
    #
    @keys = ();
    foreach $col (@cols) {
	if ($$col[2] & $::COL_KEY) {
	    push(@keys, $$col[0]);
	}
    }
    if (@keys > 1) {
	warn "Warning: Your test won't run with msql 1\n";
    }

    foreach $col (@cols) {
	my $colDef = $$col[0] . " " . AnsiTypeToDb($$col[1], $$col[2]);
	if (($$col[3] & $::COL_KEY)  &&  @keys == 1) {
	    $colDef .= " PRIMARY KEY";
	} elsif (!($$col[3] & $::COL_NULLABLE)) {
	    $colDef .= " NOT NULL";
	}
	push(@colDefs, $colDef);
    }
    if (@keys > 1  ||  defined(&DBD::mSQL1::IDX_TYPE)) {
	$keyDef = ", PRIMARY KEY (" . join(", ", @keys) . ")";
    } else {
	$keyDef = "";
    }
    $def = sprintf("CREATE TABLE %s (%s%s)", $tablename,
		   join(", ", @colDefs), $keyDef);
    if ($::verbose) {
	print "Table definition: $def\n";
    }
    $def;
}


#
#   This function generates a list of tables associated to a
#   given DSN. Highly DBMS specific, EDIT THIS!
#
sub ListTables($) {
    my($dbh) = @_;
    my(@tables);

    if (!defined(@tables = $dbh->func('_ListTables'))  ||  $dbh->errstr) {
	return undef;
    }
    @tables;
}


#
#   Return a string for checking, whether a given column is NULL.
#
sub IsNull($) {
    my($var) = @_;

    "$var = NULL";
}


#
#   Return TRUE, if database supports transactions
#
sub HaveTransactions () {
    0;
}


1;