@@ -81,7 +81,19 @@ class MysqliDb
8181 * @var array
8282 */
8383 protected $ _groupBy = array ();
84-
84+
85+ /**
86+ * Dynamic type list for tempromary locking tables.
87+ * @var array
88+ */
89+ protected $ _tableLocks = array ();
90+
91+ /**
92+ * Variable which holds the current table lock method.
93+ * @var string
94+ */
95+ protected $ _tableLockMethod = "READ " ;
96+
8597 /**
8698 * Dynamic array that holds a combination of where condition/table data value types and parameter references
8799 * @var array
@@ -1092,7 +1104,124 @@ public function groupBy($groupByField)
10921104 $ this ->_groupBy [] = $ groupByField ;
10931105 return $ this ;
10941106 }
1107+
1108+
1109+ /**
1110+ * This method sets the current table lock method.
1111+ *
1112+ * @author Jonas Barascu
1113+ * @param string $method The table lock method. Can be READ or WRITE.
1114+ *
1115+ * @throws Exception
1116+ * @return MysqliDb
1117+ */
1118+ public function setLockMethod ($ method )
1119+ {
1120+ // Switch the uppercase string
1121+ switch (strtoupper ($ method )) {
1122+ // Is it READ or WRITE?
1123+ case "READ " || "WRITE " :
1124+ // Succeed
1125+ $ this ->_tableLockMethod = $ method ;
1126+ break ;
1127+ default :
1128+ // Else throw an exception
1129+ throw new Exception ("Bad lock type: Can be either READ or WRITE " );
1130+ break ;
1131+ }
1132+ return $ this ;
1133+ }
1134+
1135+ /**
1136+ * Locks a table for R/W action.
1137+ *
1138+ * @author Jonas Barascu
1139+ * @param string $table The table to be locked. Can be a table or a view.
1140+ *
1141+ * @throws Exception
1142+ * @return MysqliDb if succeeeded;
1143+ */
1144+ public function lock ($ table )
1145+ {
1146+ // Main Query
1147+ $ this ->_query = "LOCK TABLES " ;
1148+
1149+ // Is the table an array?
1150+ if (gettype ($ table ) == "array " ) {
1151+ // Loop trough it and attach it to the query
1152+ foreach ($ table as $ key => $ value ) {
1153+ if (gettype ($ value ) == "string " ) {
1154+ if ($ key > 0 ) {
1155+ $ this ->_query .= ", " ;
1156+ }
1157+ $ this ->_query .= " " .self ::$ prefix .$ value ." " .$ this ->_tableLockMethod ;
1158+ }
1159+ }
1160+ }
1161+ else
1162+ {
1163+ // Build the table prefix
1164+ $ table = self ::$ prefix . $ table ;
1165+
1166+ // Build the query
1167+ $ this ->_query = "LOCK TABLES " .$ table ." " .$ this ->_tableLockMethod ;
1168+ }
1169+
1170+ // Exceute the query unprepared because LOCK only works with unprepared statements.
1171+ $ result = $ this ->queryUnprepared ($ this ->_query );
1172+
1173+ // Reset the query
1174+ $ this ->reset ();
1175+
1176+ // Are there rows modified?
1177+ if ($ result ) {
1178+ // Return true
1179+ // We can't return ourself because if one table gets locked, all other ones get unlocked!
1180+ return true ;
1181+ }
1182+ // Something went wrong
1183+ else {
1184+ throw new Exception ("Locking of table " .$ table ." failed " );
1185+ }
1186+
1187+ // Return the success value
1188+ return false ;
1189+ }
1190+
1191+ /**
1192+ * Unlocks all tables in a database.
1193+ * Also commits transactions.
1194+ *
1195+ * @author Jonas Barascu
1196+ * @return MysqliDb
1197+ */
1198+ public function unlock ()
1199+ {
1200+ // Build the query
1201+ $ this ->_query = "UNLOCK TABLES " ;
1202+
1203+ // Exceute the query unprepared because UNLOCK and LOCK only works with unprepared statements.
1204+ $ result = $ this ->queryUnprepared ($ this ->_query );
10951205
1206+ // Reset the query
1207+ $ this ->reset ();
1208+
1209+ // Are there rows modified?
1210+ if ($ result ) {
1211+ // return self
1212+ return $ this ;
1213+ }
1214+ // Something went wrong
1215+ else {
1216+ throw new Exception ("Unlocking of tables failed " );
1217+ }
1218+
1219+
1220+ // Return self
1221+ return $ this ;
1222+ }
1223+
1224+
10961225 /**
10971226 * This methods returns the ID of the last inserted item
10981227 *
@@ -2087,4 +2216,4 @@ public function paginate ($table, $page, $fields = null) {
20872216 }
20882217}
20892218
2090- // END class
2219+ // END class
0 commit comments