~drizzle-trunk/drizzle/development

0.67.404 by Bernt M. Johnsen
Moved server handling to lib/DBServer
1
# Copyright (C) 2008-2010 Sun Microsystems, Inc. All rights reserved.
2
# Use is subject to license terms.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
7
#
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
16
# USA
17
18
package DBServer::DBServer;
19
use base 'Exporter';
20
0.108.2 by John H. Embretsen
DBServer: Add basic support for Mac OS X by having utility functions to check for darwin, and looking for libmysqlclient.dylib instead of .so.
21
@EXPORT = ('say', 'sayFile', 'tmpdir', 'safe_exit', 
22
           'osWindows', 'osLinux', 'osSolaris', 'osMac',
23
           'isoTimestamp', 'isoUTCTimestamp',
0.67.404 by Bernt M. Johnsen
Moved server handling to lib/DBServer
24
           'DBSTATUS_OK','DBSTATUS_FAILURE');
25
26
use strict;
27
28
use Cwd;
29
use POSIX;
30
use Carp;
31
32
use constant DBSTATUS_OK => 0;
33
use constant DBSTATUS_FAILURE => 1;
34
35
my $tmpdir;
36
37
1;
38
39
sub BEGIN {
40
	foreach my $tmp ($ENV{TMP}, $ENV{TEMP}, $ENV{TMPDIR}, '/tmp', '/var/tmp', cwd()."/tmp" ) {
41
		if (
42
			(defined $tmp) &&
43
			(-e $tmp)
44
		) {
45
			$tmpdir = $tmp;
46
			last;
47
		}
48
	}
49
50
	if (defined $tmpdir) {
51
		if (
52
			($^O eq 'MSWin32') ||
53
			($^O eq 'MSWin64')
54
		) {
55
			$tmpdir = $tmpdir.'\\';
56
		} else {
57
			$tmpdir = $tmpdir.'/';
58
		}
59
	}
60
61
	croak("Unable to locate suitable temporary directory.") if not defined $tmpdir;
62
	
63
	return 1;
64
}
65
66
sub new {
67
	my $class = shift;
68
	my $args = shift;
69
70
	my $obj = bless ([], $class);
71
72
        my $max_arg = (scalar(@_) / 2) - 1;
73
74
        foreach my $i (0..$max_arg) {
75
                if (exists $args->{$_[$i * 2]}) {
76
			if (defined $obj->[$args->{$_[$i * 2]}]) {
77
				carp("Argument '$_[$i * 2]' passed twice to ".$class.'->new()');
78
			} else {
79
	                        $obj->[$args->{$_[$i * 2]}] = $_[$i * 2 + 1];
80
			}
81
                } else {
82
                        carp("Unkown argument '$_[$i * 2]' to ".$class.'->new()');
83
                }
84
        }
85
86
        return $obj;
87
}
88
89
sub say {
90
	my $text = shift;
91
92
	if ($text =~ m{[\r\n]}sio) {
93
	        foreach my $line (split (m{[\r\n]}, $text)) {
94
			print "# ".isoTimestamp()." $line\n";
95
		}
96
	} else {
97
		print "# ".isoTimestamp()." $text\n";
98
	}
99
}
100
101
sub sayFile {
102
    my ($file) = @_;
103
104
    say("--------- Contents of $file -------------");
105
    open FILE,$file;
106
    while (<FILE>) {
107
	say("| ".$_);
108
    }
109
    close FILE;
110
    say("----------------------------------");
111
}
112
113
sub tmpdir {
114
	return $tmpdir;
115
}
116
117
sub safe_exit {
118
	my $exit_status = shift;
119
	POSIX::_exit($exit_status);
120
}
121
122
sub osWindows {
123
	if (
124
		($^O eq 'MSWin32') ||
125
	        ($^O eq 'MSWin64')
126
	) {
127
		return 1;
128
	} else {
129
		return 0;
130
	}	
131
}
132
133
sub osLinux {
134
	if ($^O eq 'linux') {
135
		return 1;
136
	} else {
137
		return 0;
138
	}
139
}
140
141
sub osSolaris {
142
	if ($^O eq 'solaris') {
143
		return 1;
144
	} else {
145
		return 0;
146
	}	
147
}
148
0.108.2 by John H. Embretsen
DBServer: Add basic support for Mac OS X by having utility functions to check for darwin, and looking for libmysqlclient.dylib instead of .so.
149
sub osMac {
150
    if ($^O eq 'darwin') {
151
        return 1;
152
    } else {
153
        return 0;
154
   }
155
}
156
0.67.404 by Bernt M. Johnsen
Moved server handling to lib/DBServer
157
sub isoTimestamp {
158
	my $datetime = shift;
159
160
	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? localtime($datetime) : localtime();
161
	return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
162
163
}
164
165
sub isoUTCTimestamp {
166
	my $datetime = shift;
167
168
	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
169
	return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
170
	
171
}
172
173
1;