FS::Record::qsearch - more portable, doesn't depend on $sth->execute returning
[freeside.git] / FS / FS / dbdef.pm
1 package FS::dbdef;
2
3 use strict;
4 use vars qw(@ISA);
5 use Exporter;
6 use Carp;
7 use FreezeThaw qw(freeze thaw cmpStr);
8 use FS::dbdef_table;
9 use FS::dbdef_unique;
10 use FS::dbdef_index;
11 use FS::dbdef_column;
12
13 @ISA = qw(Exporter);
14
15 =head1 NAME
16
17 FS::dbdef - Database objects
18
19 =head1 SYNOPSIS
20
21   use FS::dbdef;
22
23   $dbdef = new FS::dbdef (@dbdef_table_objects);
24   $dbdef = load FS::dbdef "filename";
25
26   $dbdef->save("filename");
27
28   $dbdef->addtable($dbdef_table_object);
29
30   @table_names = $dbdef->tables;
31
32   $FS_dbdef_table_object = $dbdef->table;
33
34 =head1 DESCRIPTION
35
36 FS::dbdef objects are collections of FS::dbdef_table objects and represnt
37 a database (a collection of tables).
38
39 =head1 METHODS
40
41 =over 4
42
43 =item new TABLE, TABLE, ...
44
45 Creates a new FS::dbdef object
46
47 =cut
48
49 sub new {
50   my($proto,@tables)=@_;
51   my(%tables)=map  { $_->name, $_ } @tables; #check for duplicates?
52
53   my($class) = ref($proto) || $proto;
54   my($self) = {
55     'tables' => \%tables,
56   };
57
58   bless ($self, $class);
59
60 }
61
62 =item load FILENAME
63
64 Loads an FS::dbdef object from a file.
65
66 =cut
67
68 sub load {
69   my($proto,$file)=@_; #use $proto ?
70   open(FILE,"<$file") or die "Can't open $file: $!";
71   my($string)=join('',<FILE>); #can $string have newlines?  pry not?
72   close FILE or die "Can't close $file: $!";
73   my($self)=thaw $string;
74   #no bless needed?
75   $self;
76 }
77
78 =item save FILENAME
79
80 Saves an FS::dbdef object to a file.
81
82 =cut
83
84 sub save {
85   my($self,$file)=@_;
86   my($string)=freeze $self;
87   open(FILE,">$file") or die "Can't open $file: $!";
88   print FILE $string;
89   close FILE or die "Can't close file: $!";
90   my($check_self)=thaw $string;
91   die "Verify error: Can't freeze and thaw dbdef $self"
92     if (cmpStr($self,$check_self));
93 }
94
95 =item addtable TABLE
96
97 Adds this FS::dbdef_table object.
98
99 =cut
100
101 sub addtable {
102   my($self,$table)=@_;
103   ${$self->{'tables'}}{$table->name}=$table; #check for dupliates?
104 }
105
106 =item tables 
107
108 Returns the names of all tables.
109
110 =cut
111
112 sub tables {
113   my($self)=@_;
114   keys %{$self->{'tables'}};
115 }
116
117 =item table TABLENAME
118
119 Returns the named FS::dbdef_table object.
120
121 =cut
122
123 sub table {
124   my($self,$table)=@_;
125   $self->{'tables'}->{$table};
126 }
127
128 =head1 BUGS
129
130 Each FS::dbdef object should have a name which corresponds to its name within
131 the SQL database engine.
132
133 =head1 SEE ALSO
134
135 L<FS::dbdef_table>, L<FS::Record>,
136
137 =cut
138
139 1;
140