Patch for mysqldump

Marco Gergele <[email protected]> Sun, 5 May 2013 22:06:42 +0200
Newsgroups gmane.comp.db.mysql.devel
Message-ID <CAG_tJ0HJwUgov8d9HQHnozzty_m=rFkNFq5S6aR96GmczsbSUA@mail.gmail.com>
Hi,

mysqldump can sort by primary key, but only ascending.

mysqldump can not limit the number of rows.

I have added two options:

--order-by-primary-desc
                      Sorts descending each table's rows by primary key, or
                      first unique key, if such a key exists.  Useful when
                      dumping a MyISAM table to be loaded into an InnoDB
table,
                      but will make the dump itself take considerably
longer.

--limit[=name]      (numeric!) Number of rows of each table to dump, 0=all
                     rows

I do need the number for "limit" as a string, so I try to avoid converting
it into numbers and back into string.

Greets - Marco Gergele


-- 
MySQL Internals Mailing List
For list archives: http://lists.mysql.com/internals
To unsubscribe:    http://lists.mysql.com/internals
mydump.diff.txt (text/plain, 4.9 KB)
=== modified file 'client/client_priv.h'
--- client/client_priv.h	2013-02-26 05:35:17 +0000
+++ client/client_priv.h	2013-05-05 17:10:48 +0000
@@ -13,6 +13,9 @@
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+
+   Patched for mysqldump-options limit and OPT_ORDER_BY_PRIMARY_DESC.
+
 */
 
 /* Common defines for all clients */
@@ -88,7 +91,9 @@
   OPT_DEFAULT_AUTH,
   OPT_DEFAULT_PLUGIN,
   OPT_ENABLE_CLEARTEXT_PLUGIN,
-  OPT_MAX_CLIENT_OPTION
+  OPT_MAX_CLIENT_OPTION,
+  OPT_ORDER_BY_PRIMARY_DESC, 
+  OPT_DUMP_LIMIT
 };
 
 /**

=== modified file 'client/mysqldump.c'
--- client/mysqldump.c	2013-02-26 05:35:17 +0000
+++ client/mysqldump.c	2013-05-05 19:10:06 +0000
@@ -36,6 +36,9 @@
 ** and adapted to mysqldump 05/11/01 by Jani Tolonen
 ** Added --single-transaction option 06/06/2002 by Peter Zaitsev
 ** 10 Jun 2003: SET NAMES and --no-set-names by Alexander Barkov
+
+
+** 05 May 2013: Patched for limit and OPT_ORDER_BY_PRIMARY_DESC by Marco Gergele
 */
 
 #define DUMP_VERSION "10.13"
@@ -109,7 +112,7 @@
                 opt_autocommit=0,opt_disable_keys=1,opt_xml=0,
                 opt_delete_master_logs=0, tty_password=0,
                 opt_single_transaction=0, opt_comments= 0, opt_compact= 0,
-                opt_hex_blob=0, opt_order_by_primary=0, opt_ignore=0,
+                opt_hex_blob=0, opt_order_by_primary=0, opt_order_by_primary_desc=0, opt_ignore=0,
                 opt_complete_insert= 0, opt_drop_database= 0,
                 opt_replace_into= 0,
                 opt_dump_triggers= 0, opt_routines=0, opt_tz_utc=1,
@@ -127,7 +130,8 @@
              *where=0, *order_by=0,
              *opt_compatible_mode_str= 0,
              *err_ptr= 0,
-             *log_error_file= NULL;
+             *log_error_file= NULL,
+             *opt_dump_limit=0;
 static char **defaults_argv= 0;
 static char compatible_mode_normal_str[255];
 /* Server supports character_set_results session variable? */
@@ -423,6 +427,9 @@
   {"order-by-primary", OPT_ORDER_BY_PRIMARY,
    "Sorts each table's rows by primary key, or first unique key, if such a key exists.  Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer.",
    &opt_order_by_primary, &opt_order_by_primary, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
+  {"order-by-primary-desc", OPT_ORDER_BY_PRIMARY_DESC,
+   "Sorts descending each table's rows by primary key, or first unique key, if such a key exists.  Useful when dumping a MyISAM table to be loaded into an InnoDB table, but will make the dump itself take considerably longer.",
+   &opt_order_by_primary_desc, &opt_order_by_primary_desc, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
   {"password", 'p',
    "Password to use when connecting to server. If password is not given it's solicited on the tty.",
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
@@ -521,6 +528,11 @@
    "Default authentication client-side plugin to use.",
    &opt_default_auth, &opt_default_auth, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
+  {"limit", OPT_DUMP_LIMIT, 
+   "(numeric!) Number of rows of each table to dump, 0=all rows",
+    &opt_dump_limit, &opt_dump_limit, 0,
+    GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
+
   {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 };
 
@@ -2489,7 +2501,7 @@
   result_table=     quote_name(table, table_buff, 1);
   opt_quoted_table= quote_name(table, table_buff2, 0);
 
-  if (opt_order_by_primary)
+  if (opt_order_by_primary || opt_order_by_primary_desc)
     order_by= primary_key_fields(result_table);
 
   if (!opt_xml && !mysql_query_with_error_report(mysql, 0, query_buff))
@@ -3457,6 +3469,14 @@
     {
       dynstr_append_checked(&query_string, " ORDER BY ");
       dynstr_append_checked(&query_string, order_by);
+      if(opt_order_by_primary_desc){
+          dynstr_append_checked(&query_string, " DESC ");
+      }
+    }
+
+    if(opt_dump_limit){
+        dynstr_append_checked(&query_string, " LIMIT ");
+        dynstr_append_checked(&query_string, opt_dump_limit);
     }
 
     if (mysql_real_query(mysql, query_string.str, query_string.length))
@@ -3484,10 +3504,22 @@
     }
     if (order_by)
     {
-      print_comment(md_result_file, 0, "-- ORDER BY:  %s\n", order_by);
 
       dynstr_append_checked(&query_string, " ORDER BY ");
       dynstr_append_checked(&query_string, order_by);
+
+      if(opt_order_by_primary_desc){
+          print_comment(md_result_file, 0, "-- ORDER BY:  %s DESC \n", order_by);
+          dynstr_append_checked(&query_string, " DESC ");
+      }
+      else{
+          print_comment(md_result_file, 0, "-- ORDER BY:  %s\n", order_by);
+      }
+    }
+
+    if(opt_dump_limit){
+        dynstr_append_checked(&query_string, " LIMIT ");
+        dynstr_append_checked(&query_string, opt_dump_limit);
     }
 
     if (!opt_xml && !opt_compact)