/** * Returns list of primary key columns as array. * * @param string $table * @return array * @throws Exception */ public function getPK($table) { // @TODO add cache $result = array(); $type = $this->db->getType(); $database = $this->db->getName(); switch ($type) { case "mysql": $sql = "SHOW INDEX FROM `{$table}` WHERE key_name = 'PRIMARY'"; $filed = 'Column_name'; $table_check_sql = "SELECT count(*) FROM information_schema.tables WHERE table_schema = '{$database}' AND table_name = '{$table}'"; break; case "pgsql": $sql = "\tSELECT\n\t\t\t\tc.column_name, c.data_type\n\t\t\t\tFROM\n\t\t\t\tinformation_schema.table_constraints tc\n\t\t\t\tJOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)\n\t\t\t\tJOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name\n\t\t\t\twhere constraint_type = 'PRIMARY KEY' and tc.table_name = '{$table}';"; $filed = 'column_name'; $table_check_sql = "SELECT count(*) FROM information_schema.tables WHERE table_catalog = '{$database}' AND table_name = '{$table}'"; break; default: throw new Exception("Database type '{$type}' is not yet supported."); } if (!$this->db->value($table_check_sql)) { throw new Exception("Table '{$table}' doesn't exist."); } foreach ($this->db->query($sql)->fetchAll() as $row) { $result[] = $row[$filed]; } return $result; }
/** * Releases an application level lock * @param string $name The lock name to be released * @return boolean Signals if the lock could be released */ public static final function releaseLock($name) { switch (DB::value('SELECT RELEASE_LOCK(#s)', $name)) { case 0: return false; default: return true; } }
public function replace() { $this->serialize(); $sql = sprintf('REPLACE INTO `%s` SET ', $this->name); $sqlFields = array(); $primKey = $this->getPrimKey(); foreach ($this->data as $colName => $value) { if ($colName == $primKey && $this->primIsAI) { continue; } $sqlFields[] = sprintf('`%s` = %s', ucfirst($colName), DB::value($value)); } $sql .= ' ' . implode(',', $sqlFields); $result = DB::q($sql); if ($result && $primKey && $this->primIsAI) { $this->{$primKey}(DB::lastId()); } }
<?php error_log(E_ALL); define('ROOT', dirname(__FILE__) . '/'); include_once ROOT . 'DB.class.php'; include_once ROOT . '../helper/Debug.class.php'; include_once ROOT . '../Generator/Table.class.php'; $table = new Table(['class' => 'table']); $dbh = new DB(DB::DB_NefaDB); $query = 'SELECT * FROM tags;'; $map = $dbh->map($query, function (&$rows, $row) { $rows[] = $row; }); $value = $dbh->value($query); $item = $dbh->item($query); $listing = $dbh->listing($query); $database = $dbh->database(); $tables = $dbh->tables(); ?> <!DOCTYPE HTML> <html> <head> <title>Database Class Tests</title> <meta charset="UTF-8"/> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> <style> .wrapper{ width: 768px; margin: 50px auto; }