]> git.nbdom.net Git - nb.git/commitdiff
tot optimisation
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Feb 2016 04:13:36 +0000 (04:13 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Feb 2016 04:13:36 +0000 (04:13 +0000)
lib/php/db/table.php
lib/php/nb.php

index 7a0b041086fa5ff4d332ab72b9d0efed64d9d8dc..d116e3bd70040373700af7f0b9ca0d6d1065fc08 100644 (file)
@@ -685,42 +685,48 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
     }
 
-    //
-    // Tot
-    //
-    if ($select_count) {
-      $query = $this->db->conn->query('SELECT FOUND_ROWS()');
-
-    } elseif ($where) {
-      $sql_count = $sql;
-      $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
-      $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
-      $query = $this->db->conn->query($sql_count);
+    if ($opt['is_html']) {
+      //
+      // Tot
+      //
+      if (true and !$where and !$limit) {
+        debug("Not using count(*)");
+        $query = $this->db->conn->query("SELECT $count");
+
+      } elseif ($select_count) {
+        $query = $this->db->conn->query('SELECT FOUND_ROWS()');
+
+      } elseif ($where) {
+        $sql_count = $sql;
+        $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
+        $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
+        $query = $this->db->conn->query($sql_count);
 
-    } else {
-      $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name());
+      } else {
+        $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name());
 
-    }
+      }
 
-    if (!$query) {
-      $err = $this->db->conn->errorInfo();
-      $err[] = $sql;
-      err(join(' | ',$err));
-      return $err[0];
-    }
+      if (!$query) {
+        $err = $this->db->conn->errorInfo();
+        $err[] = $sql;
+        err(join(' | ',$err));
+        return $err[0];
+      }
 
-    $tot = $query->fetch(PDO::FETCH_COLUMN);
-    $opt['tot'] = $tot;
-    #if (!$tot) return;
+      $tot = $query->fetch(PDO::FETCH_COLUMN);
+      $opt['tot'] = $tot;
+      #if (!$tot) return;
 
-    $opt['count'] = $count;
-    $opt['limit'] = $limit;
-    echo $this->{"rows_end_$format"}($opt);
+      $opt['count'] = $count;
+      $opt['limit'] = $limit;
+      echo $this->{"rows_end_$format"}();
 
-    if ($opt['is_html']) echo '<div class="nav bottom">'
-      .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
-     .'</div>'
-     .PHP_EOL;
+      echo '<div class="nav bottom">'
+        .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
+       .'</div>'.PHP_EOL
+      ;
+     }
 
     $st->closeCursor();
 
@@ -780,21 +786,9 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   /*-----------------------------------------------------------------
     Yaml
   -----------------------------------------------------------------*/
-  function rows_begin_yaml() {
-    return "---\n";
-  }
-
-  function rows_rec_yaml(&$row) {
-    $yaml = yaml_emit($row);
-    $yaml = preg_replace('/^---\n/','',$yaml);
-    $yaml = preg_replace('/\n\.\.\.$/','',$yaml);
-    $yaml = preg_replace('/^/m','  ',$yaml);
-    return '- '.trim($yaml)."\n";
-  }
-
-  function rows_end_yaml() {
-    return '';
-  }
+  function rows_begin_yaml() { return "---\n"; }
+  function rows_rec_yaml(&$row) { return $this->yaml_encode($row); }
+  function rows_end_yaml() { return ''; }
 
   /*-----------------------------------------------------------------
     Xml
index ca02fba135ec85eac93470bcdaf3e6a14262fa33..c063a4dc221e5d383e1bd31896784dfdb57467f7 100644 (file)
@@ -192,10 +192,10 @@ class nb {
     $yaml = yaml_emit($row);
     $yaml = preg_replace('/^---\n/','',$yaml);
     $yaml = preg_replace('/\n\.\.\.$/','',$yaml);
-    return "--- \n".trim($yaml)."\n";
-    return trim($yaml)."\n";
-    $yaml = preg_replace('/^/m','  ',$yaml);
-    return '- '.trim($yaml)."\n";
+    $yaml = trim($yaml);
+    if ($yaml === "---") return '' ;
+    if (preg_match('/^---\s*$/m',$yaml)) return '' ;
+    return "--- \n$yaml\n";
   }
 
 }