Advertisement
sorvani

choosing an instance id

Feb 23rd, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 1.96 KB | None | 0 0
  1.  common/database.cpp | 42 +++++++++++++++++++++++++++++-------------
  2.  1 file changed, 29 insertions(+), 13 deletions(-)
  3.  
  4. diff --git a/common/database.cpp b/common/database.cpp
  5. index d0e2f3c..04ea755 100644
  6. --- a/common/database.cpp
  7. +++ b/common/database.cpp
  8. @@ -2518,25 +2518,41 @@ bool Database::GetUnusedInstanceID(uint16 &instance_id)
  9.  
  10.     uint32 count = RuleI(Zone, ReservedInstances) + 1;
  11.     uint32 max = 65535;
  12. -
  13. -   if (RunQuery(query, MakeAnyLenString(&query, "SELECT id FROM instance_list where id >= %i ORDER BY id", count), errbuf, &result)) {
  14. +   if (RunQuery(query, MakeAnyLenString(&query, "SELECT IFNULL(MAX(id),%i)+1 FROM instance_list  WHERE id > %i", count,count), errbuf, &result)) {
  15.         safe_delete_array(query);
  16. -       if (mysql_num_rows(result) != 0) {
  17. -           while((row = mysql_fetch_row(result))) {
  18. -               if(count < atoi(row[0])) {
  19. -                   instance_id = count;
  20. -                   mysql_free_result(result);
  21. -                   return true;
  22. -               } else if(count > max) {
  23. -                   instance_id = 0;
  24. -                   mysql_free_result(result);
  25. -                   return false;
  26. +       if (mysql_num_rows(result) != 1) {
  27. +           row = mysql_fetch_row(result);
  28. +           mysql_free_result(result);
  29. +           if(atoi(row[0]) <= max) {
  30. +               count = atoi(row[0]);
  31. +           } else {
  32. +               if (RunQuery(query, MakeAnyLenString(&query, "SELECT id FROM instance_list where id >= %i ORDER BY id", count), errbuf, &result)) {
  33. +                   safe_delete_array(query);
  34. +                   if (mysql_num_rows(result) != 0) {
  35. +                       while((row = mysql_fetch_row(result))) {
  36. +                           if(count < atoi(row[0])) {
  37. +                               instance_id = count;
  38. +                               mysql_free_result(result);
  39. +                               return true;
  40. +                           } else if(count > max) {
  41. +                               instance_id = 0;
  42. +                               mysql_free_result(result);
  43. +                               return false;
  44. +                           } else {
  45. +                               count++;
  46. +                           }
  47. +                       }
  48. +                   } else {
  49. +                       mysql_free_result(result);
  50. +                   }
  51.                 } else {
  52. -                   count++;
  53. +                   safe_delete_array(query);
  54.                 }
  55.             }
  56.         } else {
  57. +           instance_id = 0;
  58.             mysql_free_result(result);
  59. +           return false;
  60.         }
  61.     } else {
  62.         safe_delete_array(query);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement