Exakat\Datastore::addRow PHP Метод

addRow() публичный Метод

public addRow ( $table, $data )
    public function addRow($table, $data)
    {
        if (empty($data)) {
            return true;
        }
        $this->checkTable($table);
        $first = current($data);
        if (is_array($first)) {
            $cols = array_keys($first);
        } else {
            $query = "PRAGMA table_info({$table})";
            $res = self::$sqliteRead->query($query);
            $cols = array();
            while ($row = $res->fetchArray()) {
                if ($row['name'] == 'id') {
                    continue;
                }
                $cols[] = $row['name'];
            }
            if (count($cols) != 2) {
                throw new Exceptions\WrongNumberOfColsForAHash();
            }
        }
        foreach ($data as $key => $row) {
            if (is_array($row)) {
                $d = array_values($row);
                foreach ($d as &$e) {
                    $e = \Sqlite3::escapeString($e);
                }
                unset($e);
            } else {
                $d = array($key, \Sqlite3::escapeString($row));
            }
            $query = 'REPLACE INTO ' . $table . ' (' . implode(', ', $cols) . ") VALUES ('" . implode("', '", $d) . "')";
            self::$sqliteWrite->querySingle($query);
        }
        return true;
    }

Usage Example

Пример #1
0
 public function run()
 {
     $loc = array('files' => 0, 'total' => 0, 'tokens' => 0, 'comments' => 0, 'code' => 0);
     $project = $this->config->project;
     if ($project != 'default') {
         $projectPath = $this->config->projects_root . '/projects/' . $project;
         if (!file_exists($projectPath)) {
             throw new NoSuchProject($this->config->project);
         }
         if (!file_exists($projectPath . '/datastore.sqlite')) {
             throw new NoDatastore($this->config->project);
         }
         $datastore = new Datastore($this->config);
         $files = $datastore->getCol('files', 'file');
         foreach ($files as $file) {
             $counts = $this->countLocInFile($this->config->projects_root . '/projects/' . $project . '/code' . $file);
             array_add($loc, $counts);
             if ($counts['error'] != self::OK) {
                 $datastore->deleteRow('files', array('file' => $file));
                 $datastore->addRow('ignoredFiles', array(array('file' => $file, 'reason' => $counts['error'])));
                 display("Finally ignoring {$file}\n");
             }
         }
         $this->datastore->addRow('hash', array(array('key' => 'loc', 'value' => $loc['code']), array('key' => 'locTotal', 'value' => $loc['total']), array('key' => 'files', 'value' => $loc['files']), array('key' => 'tokens', 'value' => $loc['tokens'])));
     } elseif (!empty($this->config->dirname)) {
         $dirPath = $this->config->dirname;
         $ignoreDirs = array();
         $ignoreName = array();
         foreach ($this->config->ignore_dirs as $ignore) {
             if ($ignore[0] == '/') {
                 $d = $dirPath . $ignore;
                 if (file_exists($d)) {
                     $ignoreDirs[] = $d;
                 }
             } else {
                 $ignoreName[] = $ignore;
             }
         }
         $files = $this->readRecursiveDir($dirPath, $ignoreName, $ignoreDirs);
         foreach ($files as $file) {
             array_add($loc, $this->countLocInFile($file));
         }
     } elseif (!empty($this->config->filename)) {
         $loc = $this->countLocInFile($this->config->filename);
     } else {
         print "Usage : php exakat phploc <-p project> <-d dirname> <-f filename>\n";
         return;
     }
     if ($this->config->json) {
         print json_encode($loc);
     } elseif ($this->config->verbose) {
         foreach ($loc as $k => $v) {
             print substr("{$k}        ", 0, 8) . " : {$v}\n";
         }
     }
 }
All Usage Examples Of Exakat\Datastore::addRow