/** * Initialization */ public static function ini() { # Get info about all tables foreach (DB::fetch(DB::SQL()::SHOW_TABLES()) as $k) { $table = new SchemaTable($k[0]); # Get columns foreach (DB::fetch(DB::SQL()::SHOW_TABLE($table->getName())) as $k) { preg_match('/\\((.*)\\)/', $k['Type'], $length); $type = preg_replace('/\\((.*)\\)/', '', $k['Type']); $column = new SchemaColumn(['table' => $table->getName(), 'name' => $k['Field'], 'type' => $type, 'length' => isset($length[1]) ? $length[1] : null, 'null' => $k['Null'] == 'YES', 'default' => $k['Default'], 'primary' => $k['Key'] == 'PRI', 'unique' => $k['Key'] == 'UNI', 'auto_increment' => $k['Extra'] == 'auto_increment']); $table->addColumn($column); } # Get index foreach (DB::fetch(DB::SQL()::SHOW_INDEX($table->getName())) as $k) { if (!$table->getColumn($k['Column_name'])->getPrimary()) { $table->getColumn($k['Column_name'])->setIndex($k['Key_name']); } } foreach (DB::fetch(DB::SQL()::SHOW_CONSTRAINT(DB::getName(), $table->getName())) as $k) { $c = $table->getColumn($k['COLUMN_NAME']); $c->setConstraint($k['CONSTRAINT_NAME']); $c->setForeign($k['REFERENCED_TABLE_NAME'], $k['REFERENCED_COLUMN_NAME']); } self::$tables[$table->getName()] = $table; } }
/** * 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; }
public static function getDTQuery($trash, $timestamps = true) { $conn = DB::getName(); if (!empty(static::$connection)) { $conn = static::$connection; } $query = null; if ($timestamps) { if (isset($trash['delete']) && (int) $trash['delete'] == 1) { if (isset($trash['active']) && (int) $trash['active'] == 1) { $query = DB::connection($conn)->table(static::getTableName()); } else { $query = DB::connection($conn)->table(static::getTableName())->whereNotNull(static::getTableName() . '.deleted_at'); } } else { $query = DB::connection($conn)->table(static::getTableName())->whereNull(static::getTableName() . '.deleted_at'); } } else { $query = DB::connection($conn)->table(static::getTableName()); } return $query; }
public function testMysqlDatabase() { $db_name = \DB::getName(); $this->assertEquals('mysql', $db_name); }