Mercurial > hg > LGToc
comparison edit_section_db.php @ 2:bd46d26a7b18
adding transaction when updating sections
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Fri, 20 Mar 2015 11:32:06 +0100 |
| parents | 1f9d2bfe1d13 |
| children | 5d0bfd909857 |
comparison
equal
deleted
inserted
replaced
| 1:1f9d2bfe1d13 | 2:bd46d26a7b18 |
|---|---|
| 75 // $table="sections"; | 75 // $table="sections"; |
| 76 $table="sections_index"; | 76 $table="sections_index"; |
| 77 $condition=""; | 77 $condition=""; |
| 78 } | 78 } |
| 79 | 79 |
| 80 // echo "select from ".$table."\n"; | |
| 81 | |
| 82 | |
| 83 $query="SELECT * | 80 $query="SELECT * |
| 84 FROM ".$table." | 81 FROM ".$table." |
| 85 WHERE books_id=".$bookId." ".$condition." | 82 WHERE books_id=".$bookId." ".$condition." |
| 86 GROUP BY books_id, start_page, end_page | 83 GROUP BY books_id, start_page, end_page |
| 87 HAVING COUNT( * ) >=".$count." | 84 HAVING COUNT( * ) >=".$count." |
| 97 $i++; | 94 $i++; |
| 98 } | 95 } |
| 99 if($versionInfo['version']!=0){ | 96 if($versionInfo['version']!=0){ |
| 100 $query="SELECT id AS revisions_id, name,books_id,section_after,start_page,end_page,level,split_from,sections_id AS id, deleted FROM sections_revisions WHERE versions_id=".$versionInfo['id']." ORDER BY revisions_id ASC"; | 97 $query="SELECT id AS revisions_id, name,books_id,section_after,start_page,end_page,level,split_from,sections_id AS id, deleted FROM sections_revisions WHERE versions_id=".$versionInfo['id']." ORDER BY revisions_id ASC"; |
| 101 }else{ | 98 }else{ |
| 102 //$query="SELECT * FROM sections WHERE books_id=".$bookId." ORDER BY id ASC"; | |
| 103 $query="SELECT * FROM sections_index WHERE books_id=".$bookId." ORDER BY id ASC"; | 99 $query="SELECT * FROM sections_index WHERE books_id=".$bookId." ORDER BY id ASC"; |
| 104 } | 100 } |
| 105 $result=mysql_query($query); | 101 $result=mysql_query($query); |
| 106 | 102 |
| 107 $i=0; | 103 $i=0; |
| 129 } | 125 } |
| 130 return $sectionArray; | 126 return $sectionArray; |
| 131 } | 127 } |
| 132 function updateSectionArray($bookId,$sectionArray,$version,$editor){ | 128 function updateSectionArray($bookId,$sectionArray,$version,$editor){ |
| 133 | 129 |
| 134 // TODO: ---transaction --- | 130 // -- Start Transaction -- |
| 135 | 131 mysql_query("BEGIN"); |
| 136 | 132 |
| 137 $query="INSERT INTO sections_versions (version,editor,date,books_id) VALUES (".($version+1).",'".$editor."',NOW(),'".$bookId."')"; | 133 $query="INSERT INTO sections_versions (version,editor,date,books_id) VALUES (".($version+1).",'".$editor."',NOW(),'".$bookId."')"; |
| 138 $result=mysql_query($query); | 134 $result_versions=mysql_query($query); |
| 139 if(!$result){ | 135 if(!$result_versions){ |
| 140 echo json_encode("Failed during inserting sections_version records."); // .mysql_error(); | 136 echo json_encode("Failed during inserting sections_version records."); // .mysql_error(); |
| 141 //return; | |
| 142 } | 137 } |
| 143 $versionId=mysql_insert_id(); | 138 $versionId=mysql_insert_id(); |
| 139 | |
| 140 | |
| 141 | |
| 144 $query="INSERT INTO sections_revisions (name,books_id,section_after,start_page,end_page,level,split_from,sections_id,versions_id,deleted) VALUES "; | 142 $query="INSERT INTO sections_revisions (name,books_id,section_after,start_page,end_page,level,split_from,sections_id,versions_id,deleted) VALUES "; |
| 145 foreach($sectionArray as $idx=>$row){ | 143 foreach($sectionArray as $idx=>$row){ |
| 146 // get the section_id | 144 // get the section_id |
| 147 $section_id = $row['id']; | 145 $section_id = $row['id']; |
| 148 $name = $row['name']; | 146 $name = $row['name']; |
| 152 $end_page = $row['endPage']; | 150 $end_page = $row['endPage']; |
| 153 $level = $row['level']; | 151 $level = $row['level']; |
| 154 $split_from = $row['splitFrom']; | 152 $split_from = $row['splitFrom']; |
| 155 $deleted = $row['deleted']; | 153 $deleted = $row['deleted']; |
| 156 | 154 |
| 157 // update to section_index table, which is the up-to-date section table | 155 // --- update to section_index table, which is the up-to-date section table --- |
| 156 $qry = ""; | |
| 158 if ($section_id == 0) { // section_id = 0 means it is a new record created by user | 157 if ($section_id == 0) { // section_id = 0 means it is a new record created by user |
| 159 // insert new row to section_index table | 158 // insert new row to section_index table |
| 160 $qry = "INSERT INTO sections_index (name,books_id,section_after,start_page,end_page,level,split_from) VALUES | 159 $qry = "INSERT INTO sections_index (name,books_id,section_after,start_page,end_page,level,split_from) VALUES |
| 161 ('".$name."','".$books_id."','".$section_after."',".$start_page. | 160 ('".$name."','".$books_id."','".$section_after."',".$start_page. |
| 162 ",".$end_page.",".$level.",".$split_from.")"; | 161 ",".$end_page.",".$level.",".$split_from.")"; |
| 163 $rst = mysql_query($qry); | |
| 164 if (!$rst) { | |
| 165 echo json_encode(mysql_error()); | |
| 166 } | |
| 167 $section_id = mysql_insert_id(); | |
| 168 | 162 |
| 169 } else { | 163 } else { |
| 170 // update | 164 // update |
| 171 $qry = "UPDATE sections_index SET name='".$name."',books_id='".$books_id."',section_after='".$section_after. | 165 $qry = "UPDATE sections_index SET name='".$name."',books_id='".$books_id."',section_after='".$section_after. |
| 172 "',start_page=".$start_page.",end_page=".$end_page.",level=".$level.",split_from=".$split_from." WHERE id=".$section_id; | 166 "',start_page=".$start_page.",end_page=".$end_page.",level=".$level.",split_from=".$split_from." WHERE id=".$section_id; |
| 173 $rst = mysql_query($qry); | 167 } |
| 174 if (!$rst){ | 168 |
| 175 echo json_encode(mysql_error()); | 169 $rst = mysql_query($qry); |
| 176 } | 170 if (!$rst) { |
| 171 break; // handle to ROLLBACK | |
| 172 } | |
| 173 | |
| 174 if ($section_id == 0) { | |
| 175 // set section_id from section_index if this is a new section | |
| 176 $section_id = mysql_insert_id(); | |
| 177 } | 177 } |
| 178 | 178 |
| 179 $str="("; | 179 $str="("; |
| 180 $str.="'".$name."',"; | 180 $str.="'".$name."',"; |
| 181 $str.="'".$books_id."',"; | 181 $str.="'".$books_id."',"; |
| 190 $str.=")"; | 190 $str.=")"; |
| 191 if($idx!=sizeof($sectionArray)-1){ | 191 if($idx!=sizeof($sectionArray)-1){ |
| 192 $str.=", "; | 192 $str.=", "; |
| 193 } | 193 } |
| 194 | 194 |
| 195 | |
| 196 /* | |
| 197 $str="("; | |
| 198 $str.="'".$row['name']."',"; | |
| 199 $str.="'".sprintf("%05d",$row['booksId'])."',"; | |
| 200 $str.="'".$row['sectionAfter']."',"; | |
| 201 $str.=$row['startPage'].","; | |
| 202 $str.=$row['endPage'].","; | |
| 203 $str.=$row['level'].","; | |
| 204 $str.=$row['splitFrom'].","; | |
| 205 $str.=$section_id.","; // sections_id should get from section_index table | |
| 206 $str.=$versionId.","; | |
| 207 $str.=$row['deleted']; | |
| 208 $str.=")"; | |
| 209 if($idx!=sizeof($sectionArray)-1){ | |
| 210 $str.=", "; | |
| 211 } | |
| 212 */ | |
| 213 $query.=$str; | 195 $query.=$str; |
| 214 } | 196 |
| 215 $result=mysql_query($query); | 197 } |
| 198 $result=mysql_query($query); | |
| 199 | |
| 200 // --- End Transaction --- | |
| 201 if ($result and $rst) { | |
| 202 mysql_query("COMMIT"); | |
| 203 echo json_encode("Succeeded."); // important returning info to js | |
| 204 | |
| 205 } else { | |
| 206 mysql_query("ROLLBACK"); | |
| 207 echo json_encode("Failed during updating sections."); | |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 /* | |
| 216 if(!$result){ | 212 if(!$result){ |
| 217 echo json_encode("Failed during inserting sections_revisions records."); // .mysql_error(); | 213 echo json_encode("Failed during inserting sections_revisions records."); // .mysql_error(); |
| 218 | 214 |
| 219 // Delete the sections_versions for this user | 215 // Delete the sections_versions for this user |
| 220 // delete $versionId in table sections_versions | 216 // delete $versionId in table sections_versions |
| 225 } | 221 } |
| 226 return; | 222 return; |
| 227 }else{ | 223 }else{ |
| 228 echo json_encode("Succeeded."); | 224 echo json_encode("Succeeded."); |
| 229 } | 225 } |
| 226 */ | |
| 227 | |
| 230 | 228 |
| 231 | 229 |
| 232 } | 230 } |
| 233 | 231 |
| 234 | 232 |
| 252 echo '\n'; | 250 echo '\n'; |
| 253 */ | 251 */ |
| 254 | 252 |
| 255 | 253 |
| 256 //$query="SELECT id FROM books WHERE id NOT IN (SELECT DISTINCT (books_id) FROM sections)"; | 254 //$query="SELECT id FROM books WHERE id NOT IN (SELECT DISTINCT (books_id) FROM sections)"; |
| 257 // $query="SELECT books.id, books.name FROM books LEFT JOIN sections ON books.id=sections.books_id WHERE sections.books_id IS NULL"; | |
| 258 $query="SELECT books.id, books.name FROM books LEFT JOIN sections_index ON books.id=sections_index.books_id WHERE sections_index.books_id IS NULL"; | 255 $query="SELECT books.id, books.name FROM books LEFT JOIN sections_index ON books.id=sections_index.books_id WHERE sections_index.books_id IS NULL"; |
| 259 | 256 |
| 260 $result=mysql_query($query); | 257 $result=mysql_query($query); |
| 261 if(!$result){ | 258 if(!$result){ |
| 262 echo json_encode("Failed during querying records."); | 259 echo json_encode("Failed during querying records."); |
| 293 } | 290 } |
| 294 | 291 |
| 295 | 292 |
| 296 function _add_book_to_sections($name, $bookId){ | 293 function _add_book_to_sections($name, $bookId){ |
| 297 | 294 |
| 298 /* | |
| 299 // get Auto_increment | |
| 300 $query = mysql_query("SHOW TABLE STATUS WHERE name='sections'"); | |
| 301 if (mysql_num_rows($query)) { | |
| 302 $result = mysql_fetch_assoc($query); | |
| 303 echo $result['Auto_increment']; | |
| 304 } else {//error | |
| 305 //error control here | |
| 306 echo "error in getting Auto_increment"; | |
| 307 } | |
| 308 */ | |
| 309 | |
| 310 // id should be autoincremated | |
| 311 // $query="INSERT INTO sections (name,books_id,section_after) VALUES "; | |
| 312 $query="INSERT INTO sections_index (name,books_id,section_after) VALUES "; | 295 $query="INSERT INTO sections_index (name,books_id,section_after) VALUES "; |
| 313 | 296 |
| 314 $str="("; | 297 $str="("; |
| 315 $str.="'".$name."','"; | 298 $str.="'".$name."','"; |
| 316 $str.=$bookId."',"; | 299 $str.=$bookId."',"; |
