generate SQL from table and pk
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 Jan 2010 16:32:35 +0000 (17:32 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 Jan 2010 16:32:35 +0000 (17:32 +0100)
dbi2mongo.pl

index 378728e..653d74d 100755 (executable)
@@ -13,19 +13,9 @@ $|++;
 
 my $debug = @ARGV ? 1 : 0;
 
 
 my $debug = @ARGV ? 1 : 0;
 
-our $dbi = "DBI:mysql:database=test";
-our ( $dbi, $user, $password );
+our ( $dbi, $user, $password ) = ( "DBI:mysql:database=test" );
 our ( $database, $collection ) = ( 'test', 'test' );
 our ( $database, $collection ) = ( 'test', 'test' );
-
-our $sql = qq{
-       select
-               id as _id,
-               table.*
-       from table
-       where id > ?
-       order by id asc
-       limit 100000
-};
+our ( $table, $pk ) = ( 'items', 'id' );
 
 require 'config.pl';
 
 
 require 'config.pl';
 
@@ -34,18 +24,30 @@ warn "# $dbi $user -> $database $collection\n";
 my $conn = MongoDB::Connection->new;
 my $db   = $conn->get_database( $database );
 my $coll = $db->get_collection( $collection );
 my $conn = MongoDB::Connection->new;
 my $db   = $conn->get_database( $database );
 my $coll = $db->get_collection( $collection );
-my $dbh  = DBI->connect($dbi,$user,$password,{ RaiseError => 1 });
+my $dbh  = DBI->connect($dbi,$user,$password, {
+       RaiseError => 1,
+#      mysql_enable_utf8 => 1,
+});
 
 $db->drop if $debug;
 
 
 $db->drop if $debug;
 
-# > db.items.find().sort({item_id:-1}).limit(1);
+# db.items.find().sort({_id:-1}).limit(1);
 my $last = $coll->query()->sort({ '_id' => -1 })->limit(1)->next;
 warn dump( $last );
 my $last_id = $last->{_id} || 0;
 
 print "Fetching items from $dbi _id > $last_id\n";
 
 my $last = $coll->query()->sort({ '_id' => -1 })->limit(1)->next;
 warn dump( $last );
 my $last_id = $last->{_id} || 0;
 
 print "Fetching items from $dbi _id > $last_id\n";
 
-my $sth = $dbh->prepare($sql);
+my $sth = $dbh->prepare(qq{
+       select
+               $pk as _id,
+               $table.*
+       from $table
+       where $pk > ?
+       order by $pk asc
+       limit 100000
+});
+
 $sth->execute( $last_id );
 
 warn dump( $sth->{NAME} );
 $sth->execute( $last_id );
 
 warn dump( $sth->{NAME} );