Lines 99-126
Link Here
|
99 |
return $self; |
99 |
return $self; |
100 |
} |
100 |
} |
101 |
|
101 |
|
102 |
=head2 tie_db_readonly |
102 |
# |
103 |
|
103 |
# it's not need to be visible to package users |
104 |
public instance (Boolean) tie_db_readonly (); |
104 |
# |
105 |
|
105 |
sub _tie_db_connector { |
106 |
Description: |
106 |
my ($self, $db_writable) = @_; |
107 |
This method ensures that the database connection is properly setup |
107 |
|
108 |
and working. If necessary it will initialize a user's bayes variables |
|
|
109 |
so that they can begin using the database immediately. |
110 |
|
111 |
=cut |
112 |
|
113 |
sub tie_db_readonly { |
114 |
my ($self) = @_; |
115 |
|
116 |
return 0 unless (HAS_DBI); |
117 |
|
118 |
if ($self->{_dbh}) { |
119 |
# already connected, but connection has now become readonly |
120 |
$self->{db_writable_p} = 0; |
121 |
return 1; |
122 |
} |
123 |
|
124 |
my $main = $self->{bayes}->{main}; |
108 |
my $main = $self->{bayes}->{main}; |
125 |
|
109 |
|
126 |
$self->read_db_configs(); |
110 |
$self->read_db_configs(); |
Lines 137-199
Link Here
|
137 |
return 0; |
121 |
return 0; |
138 |
} |
122 |
} |
139 |
|
123 |
|
140 |
unless ($self->_initialize_db(0)) { |
124 |
unless ($self->_initialize_db()) { |
141 |
dbg("bayes: unable to initialize database for ".$self->{_username}." user, aborting!"); |
125 |
dbg("bayes: unable to initialize database for ".$self->{_username}." user, aborting!"); |
142 |
$self->untie_db(); |
126 |
$self->untie_db(); |
143 |
return 0; |
127 |
return 0; |
144 |
} |
128 |
} |
|
|
129 |
|
130 |
$self->{db_writable_p} = $db_writable; |
145 |
|
131 |
|
146 |
return 1; |
132 |
return 1; |
147 |
} |
133 |
} |
148 |
|
134 |
|
149 |
=head2 tie_db_writable |
135 |
=head2 tie_db_readonly |
150 |
|
136 |
|
151 |
public instance (Boolean) tie_db_writable () |
137 |
public instance (Boolean) tie_db_readonly (); |
152 |
|
138 |
|
153 |
Description: |
139 |
Description: |
154 |
This method ensures that the database connetion is properly setup |
140 |
This method ensures that the database connection is properly setup |
155 |
and working. If necessary it will initialize a users bayes variables |
141 |
and working. If necessary it will initialize a user's bayes variables |
156 |
so that they can begin using the database immediately. |
142 |
so that they can begin using the database immediately. |
157 |
|
143 |
|
158 |
=cut |
144 |
=cut |
159 |
|
145 |
|
160 |
sub tie_db_writable { |
146 |
sub tie_db_readonly { |
161 |
my ($self) = @_; |
147 |
my ($self) = @_; |
162 |
|
148 |
|
163 |
return 0 unless (HAS_DBI); |
149 |
return 0 unless (HAS_DBI); |
164 |
|
150 |
|
165 |
if ($self->{_dbh}) { |
151 |
if ($self->{_dbh}) { |
166 |
# already connected, but now it will be writable |
152 |
# already connected, but connection has now become readonly |
167 |
$self->{db_writable_p} = 1; |
153 |
$self->{db_writable_p} = 0; |
168 |
return 1; |
154 |
return 1; |
169 |
} |
155 |
} |
170 |
|
156 |
|
171 |
my $main = $self->{bayes}->{main}; |
157 |
return $self->_tie_db_connector(0); |
|
|
158 |
} |
172 |
|
159 |
|
173 |
$self->read_db_configs(); |
160 |
=head2 tie_db_writable |
174 |
|
161 |
|
175 |
return 0 unless ($self->_connect_db()); |
162 |
public instance (Boolean) tie_db_writable () |
176 |
|
163 |
|
177 |
my $db_ver = $self->_get_db_version(); |
164 |
Description: |
178 |
$self->{db_version} = $db_ver; |
165 |
This method ensures that the database connetion is properly setup |
179 |
dbg("bayes: found bayes db version ".$self->{db_version}); |
166 |
and working. If necessary it will initialize a users bayes variables |
|
|
167 |
so that they can begin using the database immediately. |
180 |
|
168 |
|
181 |
if ( $db_ver != $self->DB_VERSION ) { |
169 |
=cut |
182 |
warn("bayes: database version $db_ver is different than we understand (".$self->DB_VERSION."), aborting!"); |
|
|
183 |
$self->untie_db(); |
184 |
return 0; |
185 |
} |
186 |
|
170 |
|
187 |
unless ($self->_initialize_db(1)) { |
171 |
sub tie_db_writable { |
188 |
dbg("bayes: unable to initialize database for ".$self->{_username}." user, aborting!"); |
172 |
my ($self) = @_; |
189 |
|
173 |
|
190 |
$self->untie_db(); |
174 |
return 0 unless (HAS_DBI); |
191 |
return 0; |
|
|
192 |
} |
193 |
|
175 |
|
194 |
$self->{db_writable_p} = 1; |
176 |
if ($self->{_dbh}) { |
|
|
177 |
# already connected, but now it will be writable |
178 |
$self->{db_writable_p} = 1; |
179 |
return 1; |
180 |
} |
195 |
|
181 |
|
196 |
return 1; |
182 |
return $self->_tie_db_connector(1) |
197 |
} |
183 |
} |
198 |
|
184 |
|
199 |
|
185 |
|
Lines 1721-1727
Link Here
|
1721 |
=cut |
1707 |
=cut |
1722 |
|
1708 |
|
1723 |
sub _initialize_db { |
1709 |
sub _initialize_db { |
1724 |
my ($self, $create_entry_p) = @_; |
1710 |
my ($self) = @_; |
1725 |
|
1711 |
|
1726 |
return 0 unless (defined($self->{_dbh})); |
1712 |
return 0 unless (defined($self->{_dbh})); |
1727 |
|
1713 |
|
Lines 1762-1810
Link Here
|
1762 |
|
1748 |
|
1763 |
my ($id) = $sthselect->fetchrow_array(); |
1749 |
my ($id) = $sthselect->fetchrow_array(); |
1764 |
|
1750 |
|
1765 |
if ($id) { |
1751 |
unless ($id) { |
1766 |
$self->{_userid} = $id; |
1752 |
# For now let the database setup the other variables as defaults |
1767 |
dbg("bayes: Using userid: ".$self->{_userid}); |
1753 |
my $sqlinsert = "INSERT INTO bayes_vars (username) VALUES (?)"; |
1768 |
$sthselect->finish(); |
1754 |
|
1769 |
return 1; |
1755 |
my $rows = $self->{_dbh}->do($sqlinsert, |
1770 |
} |
1756 |
undef, |
1771 |
|
1757 |
$self->{_username}); |
1772 |
# Do not create an entry for this user unless we were specifically asked to |
1758 |
unless (defined($rows)) { |
1773 |
return 0 unless ($create_entry_p); |
1759 |
dbg("bayes: _initialize_db: SQL error: ".$self->{_dbh}->errstr()); |
|
|
1760 |
return 0; |
1761 |
} |
1774 |
|
1762 |
|
1775 |
# For now let the database setup the other variables as defaults |
1763 |
$self->{_dbh}->commit(); |
1776 |
my $sqlinsert = "INSERT INTO bayes_vars (username) VALUES (?)"; |
|
|
1777 |
|
1764 |
|
1778 |
my $rows = $self->{_dbh}->do($sqlinsert, |
1765 |
# Now we need to figure out what id we inserted them at, in a perfect |
1779 |
undef, |
1766 |
# world the database driver would handle this for us (ie mysql_insert_id) |
1780 |
$self->{_username}); |
1767 |
# but this is far from a perfect world, however since in theory we only |
1781 |
unless (defined($rows)) { |
1768 |
# ever do this once it's ok to take the hit |
1782 |
dbg("bayes: _initialize_db: SQL error: ".$self->{_dbh}->errstr()); |
1769 |
$rc = $sthselect->execute($self->{_username}); |
1783 |
return 0; |
|
|
1784 |
} |
1785 |
|
1770 |
|
1786 |
# Now we need to figure out what id we inserted them at, in a perfect |
1771 |
unless ($rc) { |
1787 |
# world the database driver would handle this for us (ie mysql_insert_id) |
1772 |
dbg("bayes: _initialize_db: SQL error: ".$self->{_dbh}->errstr()); |
1788 |
# but this is far from a perfect world, however since in theory we only |
1773 |
return 0; |
1789 |
# ever do this once it's ok to take the hit |
1774 |
} |
1790 |
$rc = $sthselect->execute($self->{_username}); |
|
|
1791 |
|
1775 |
|
1792 |
unless ($rc) { |
1776 |
($id) = $sthselect->fetchrow_array(); |
1793 |
dbg("bayes: _initialize_db: SQL error: ".$self->{_dbh}->errstr()); |
|
|
1794 |
return 0; |
1795 |
} |
1777 |
} |
1796 |
|
1778 |
|
1797 |
($id) = $sthselect->fetchrow_array(); |
|
|
1798 |
|
1799 |
$sthselect->finish(); |
1779 |
$sthselect->finish(); |
1800 |
|
1780 |
|
1801 |
if ($id) { |
1781 |
if ($id) { |
1802 |
$self->{_userid} = $id; |
1782 |
$self->{_userid} = $id; |
1803 |
dbg("bayes: using userid: ".$self->{_userid}); |
1783 |
dbg("bayes: using userid: ".$self->{_userid}); |
1804 |
return 1; |
1784 |
return 1; |
1805 |
} |
1785 |
} |
1806 |
|
1786 |
else { |
1807 |
return 1; |
1787 |
dbg("bayes: _initialize_db: error: can't fetch userid"); |
|
|
1788 |
return 0; |
1789 |
} |
1808 |
} |
1790 |
} |
1809 |
|
1791 |
|
1810 |
=head2 _put_token |
1792 |
=head2 _put_token |