Changeset 90 for trunk/DNSDB.pm
- Timestamp:
- 04/12/11 14:46:22 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r87 r90 690 690 # last name - defaults to blank 691 691 # phone - defaults to blank (could put other data within column def) 692 # Returns (OK, OK) on success, (FAIL,<message>) on failure692 # Returns (OK,<uid>) on success, (FAIL,<message>) on failure 693 693 sub addUser { 694 694 $errstr = ''; … … 699 699 my $state = shift; 700 700 701 return ('FAIL',"Missing one or more required entries") if !defined($state); 701 return ('FAIL', "Missing one or more required entries") if !defined($state); 702 return ('FAIL', "Username must not be blank") if !$username; 702 703 703 704 my $type = shift || 'u'; # create limited users by default - fwiw, not sure yet how this will interact with ACLs … … 816 817 817 818 ## DNSDB:: updateUser() 818 # 819 # Update general data about user 819 820 sub updateUser { 820 821 my $dbh = shift; … … 1020 1021 my $id = shift; 1021 1022 1022 my $sql = "SELECT record_id,host,type,val,distance,weight,port,ttl". 1023 (($def eq 'def' or $def eq 'y') ? ',group_id FROM default_' : ',domain_id FROM '). 1024 "records WHERE record_id=?"; 1025 my $sth = $dbh->prepare($sql); 1026 $sth->execute($id); 1027 1028 my ($recid,$host,$rtype,$val,$distance,$weight,$port,$ttl,$parid) = $sth->fetchrow_array(); 1029 1030 if ($sth->err) { 1023 my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata". 1024 (($def eq 'def' or $def eq 'y') ? ',r.group_id FROM default_' : ',r.domain_id FROM '). 1025 "records r LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id WHERE record_id=?"; 1026 my $ret = $dbh->selectrow_hashref($sql, undef, ($id) ) or warn $dbh->errstr; 1027 1028 if ($dbh->err) { 1031 1029 $errstr = $DBI::errstr; 1032 1030 return undef; 1033 1031 } 1034 my %ret; 1035 $ret{recid} = $recid; 1036 $ret{host} = $host; 1037 $ret{type} = $rtype; 1038 $ret{val} = $val; 1039 $ret{distance}= $distance; 1040 $ret{weight} = $weight; 1041 $ret{port} = $port; 1042 $ret{ttl} = $ttl; 1043 $ret{parid} = $parid; 1044 1045 return %ret; 1032 1033 $ret->{val} = $ret->{recdata} if $ret->{longrec_id}; # put the long data in the real value space 1034 delete $ret->{longrec_id}; # remove these since they shouldn't be exposed - the caller 1035 delete $ret->{recdata}; # should not care about "long records" vs normal ones. 1036 1037 return $ret; 1046 1038 } 1047 1039 … … 1065 1057 my $direction = shift || 'ASC'; 1066 1058 1067 my $sql = "SELECT record_id,host,type,val,distance,weight,port,ttl FROM "; 1068 if ($type eq 'def' or $type eq 'y') { 1069 $sql .= " default_records where group_id=$id"; 1059 $type = 'y' if $type eq 'def'; 1060 1061 my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata FROM "; 1062 $sql .= "default_" if $type eq 'y'; 1063 $sql .= "records r "; 1064 $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id "; 1065 if ($type eq 'y') { 1066 $sql .= "WHERE r.group_id=?"; 1070 1067 } else { 1071 $sql .= " records where domain_id=$id";1072 } 1073 $sql .= " and not type=$reverse_typemap{SOA} order by$order $direction";1068 $sql .= "WHERE r.domain_id=?"; 1069 } 1070 $sql .= " AND NOT r.type=$reverse_typemap{SOA} ORDER BY r.$order $direction"; 1074 1071 ##fixme: need to set nstart properly (offset is not internally multiplied with limit) 1075 $sql .= " limit $nrecs offset".($nstart*$nrecs) if $nstart ne 'all';1076 1077 my $sth = $dbh->prepare($sql) ;1078 $sth->execute ;1072 $sql .= " LIMIT $nrecs OFFSET ".($nstart*$nrecs) if $nstart ne 'all'; 1073 1074 my $sth = $dbh->prepare($sql) or warn $dbh->errstr; 1075 $sth->execute($id) or warn "$sql: ".$sth->errstr; 1079 1076 1080 1077 my @retbase; 1081 1078 while (my $ref = $sth->fetchrow_hashref()) { 1079 $ref->{val} = $ref->{recdata} if $ref->{longrec_id}; # put the long data in the real value space 1080 delete $ref->{longrec_id}; # remove these since they shouldn't be exposed - the caller 1081 delete $ref->{recdata}; # should not care about "long records" vs normal ones. 1082 1082 push @retbase, $ref; 1083 1083 } … … 1133 1133 } 1134 1134 1135 my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallen)"; 1136 ##fixme: use array for values, replace "vallist" with series of ?,?,? etc 1137 # something is bugging me about this... 1138 #warn "DEBUG: $sql"; 1139 my $sth = $dbh->prepare($sql); 1140 $sth->execute(@vallist); 1141 1142 return ('FAIL',$sth->errstr) if $sth->err; 1135 # Allow transactions, and raise an exception on errors so we can catch it later. 1136 # Use local to make sure these get "reset" properly on exiting this block 1137 local $dbh->{AutoCommit} = 0; 1138 local $dbh->{RaiseError} = 1; 1139 1140 eval { 1141 if (length($val) > 100 ) { 1142 # extralong records get an entry in a separate table. 1143 $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) ); 1144 my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($val) ); 1145 $fields .= ",longrec_id"; 1146 $vallen .= ",?"; 1147 push @vallist, $longid; 1148 $vallist[3] = ''; # so we don't barf when we insert the main record 1149 } 1150 $dbh->do("INSERT INTO ".($defrec eq 'y' ? 'default_' : '')."records ($fields) VALUES ($vallen)", 1151 undef, @vallist); 1152 $dbh->commit; 1153 }; 1154 if ($@) { 1155 my $msg = $@; 1156 eval { $dbh->rollback; }; 1157 return ('FAIL',$msg); 1158 } 1143 1159 1144 1160 return ('OK','OK'); 1161 1145 1162 } # end addRec() 1146 1163 … … 1179 1196 } 1180 1197 1181 my $sth = $dbh->prepare("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ". 1182 "SET host=?,type=?,val=?,ttl=?,distance=?,weight=?,port=? ". 1183 "WHERE record_id=?"); 1184 $sth->execute($host,$type,$val,$ttl,$dist,$weight,$port,$id); 1185 1186 return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err; 1198 # my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata FROM "; 1199 # $sql .= "default_" if $type eq 'y'; 1200 # $sql .= "records r "; 1201 # $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id "; 1202 1203 # get the long record ID, if any 1204 my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM ".($defrec eq 'y' ? 'default_' : '')."records ". 1205 "WHERE record_id=?", undef, ($id) ); 1206 1207 local $dbh->{AutoCommit} = 0; 1208 local $dbh->{RaiseError} = 1; 1209 1210 eval { 1211 # there's really no tidy way to squash this down. :/ 1212 if (length($val) > 100) { 1213 if ($longid) { 1214 $dbh->do("UPDATE longrecs SET recdata=? WHERE longrec_id=?", undef, ($val, $longid) ); 1215 } else { 1216 ##fixme: has to be a better way to be sure we get the right recid back once inserted... 1217 $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) ); 1218 my ($newlongid) = $dbh->selectrow_array("SELECT currval('longrecs_longrec_id_seq')"); 1219 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=? ". 1220 "WHERE record_id=?", undef, ('', $newlongid, $id) ); 1221 } 1222 } else { 1223 if ($longid) { 1224 $dbh->do("DELETE FROM longrecs WHERE longrec_id=?", undef, ($longid) ); 1225 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=NULL ". 1226 "WHERE record_id=?", undef, ($val, $id) ); 1227 } else { 1228 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=? ". 1229 "WHERE record_id=?", undef, ($val, $id) ); 1230 } 1231 } 1232 1233 $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ". 1234 "SET host=?,type=?,ttl=?,distance=?,weight=?,port=? ". 1235 "WHERE record_id=?", undef, ($host, $type, $ttl, $dist, $weight, $port, $id) ); 1236 1237 }; 1238 if ($@) { 1239 my $msg = $@; 1240 $dbh->rollback; 1241 return ('FAIL', $msg); 1242 } 1243 # return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err; 1187 1244 1188 1245 return ('OK','OK');
Note:
See TracChangeset
for help on using the changeset viewer.