会员登录 | 会员注册 | 意见建议 | 网站地图

站长资源综合门户

当前位置:首页 > 站长学院 > 编程程序 > 同时支持三个MySQL+SQLite+PDO的PHP数据库类

同时支持三个MySQL+SQLite+PDO的PHP数据库类

时间:2012-08-16 20:39:14   作者:   来源:   点击:

} else if ($this->adapter == "sqlite") {

return "";

}

}

}

function listCollation() {

if ($this->conn) {

if ($this->adapter == "mysql") {

return $this->query("SHOW COLLATION");

} else if ($this->adapter == "sqlite") {

return "";

}

}

}

function insertId() {

if ($this->conn) {

if ($this->method == "pdo") {

return $this->conn->lastInsertId();

} else if ($this->method == "mysql") {

return @mysql_insert_id($this->conn);

} else if ($this->method == "sqlite") {

return sqlite_last_insert_rowid($this-conn);

}

}

}

function escapeString($toEscape) {

if ($this->conn) {

if ($this->method == "pdo") {

$toEscape = $this->conn->quote($toEscape);

$toEscape = substr($toEscape, 1, -1);

return $toEscape;

} else if ($this->adapter == "mysql") {

return mysql_real_escape_string($toEscape);

} else if ($this->adapter == "sqlite") {

return sqlite_escape_string($toEscape);

}

}

}

function getVersion() {

if ($this->conn) {

// cache

if ($this->version) {

return $this->version;

}

if ($this->adapter == "mysql") {

$verSql = mysql_get_server_info();

$version = explode("-", $verSql);

$this->version = $version[0];

return $this->version;

} else if ($this->adapter == "sqlite") {

$this->version = sqlite_libversion();

return $this->version;

}

}

}

// returns the number of rows in a table

function tableRowCount($table) {

if ($this->conn) {

if ($this->adapter == "mysql") {

$countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table . "`");

$count = (int)($this->result($countSql, 0, "RowCount"));

return $count;

} else if ($this->adapter == "sqlite") {

$countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $table . "'");

$count = (int)($this->result($countSql, 0, "RowCount"));

return $count;

}

}

}

// gets column info for a table

function describeTable($table) {

if ($this->conn) {

if ($this->adapter == "mysql") {

return $this->query("DESCRIBE `" . $table . "`");

} else if ($this->adapter == "sqlite") {

$columnSql = $this->query("SELECT sql FROM sqlite_master where tbl_name = '" . $table . "'");

$columnInfo = $this->result($columnSql, 0, "sql");

$columnStart = strpos($columnInfo, '(');

$columns = substr($columnInfo, $columnStart+1, -1);

$columns = split(',[^0-9]', $columns);

$columnList = array();

foreach ($columns as $column) {

$column = trim($column);

$columnSplit = explode(" ", $column, 2);

$columnName = $columnSplit[0];

$columnType = (sizeof($columnSplit) > 1) ? $columnSplit[1] : "";

$columnList[] = array($columnName, $columnType);

}

return $columnList;

}

}

}

/*

Return names, row counts etc for every database, table and view in a JSON string

*/

function getMetadata() {

$output = '';

if ($this->conn) {

if ($this->adapter == "mysql" && version_compare($this->getVersion(), "5.0.0", ">=")) {

$this->selectDB("information_schema");

$schemaSql = $this->query("SELECT `SCHEMA_NAME` FROM `SCHEMATA` ORDER BY `SCHEMA_NAME`");

if ($this->rowCount($schemaSql)) {

while ($schema = $this->fetchAssoc($schemaSql)) {

$output .= '{"name": "' . $schema['SCHEMA_NAME'] . '"';

// other interesting columns: TABLE_TYPE, ENGINE, TABLE_COLUMN and many more

$tableSql = $this->query("SELECT `TABLE_NAME`, `TABLE_ROWS` FROM `TABLES` WHERE `TABLE_SCHEMA`='" . $schema['SCHEMA_NAME'] . "' ORDER BY `TABLE_NAME`");

if ($this->rowCount($tableSql)) {

$output .= ',"items": [';

while ($table = $this->fetchAssoc($tableSql)) {

if ($schema['SCHEMA_NAME'] == "information_schema") {

$countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table['TABLE_NAME'] . "`");

$rowCount = (int)($this->result($countSql, 0, "RowCount"));

} else {

$rowCount = (int)($table['TABLE_ROWS']);

分享到:

网友评论

热门编程程序