reasonableframework/helper/database.mysql.cmd.php

79 lines
2.2 KiB
PHP
Raw Normal View History

2019-04-06 12:04:49 +00:00
<?php
/**
* @file database.mysql.cmd.php
* @date 2019-04-06
* @auther Go Namhyeon <gnh1201@gmail.com>
* @brief MySQL(MariaDB) command line driver
*/
if(!is_fn("get_db_mysql_cmd_connect")) {
2019-09-16 04:40:00 +00:00
function get_db_mysql_cmd_connect() {
$result = false;
$config = get_config();
if(loadHelper("exectool")) {
$args = array("mysql");
$args[] = sprintf("-u'%s'", $config['db_username']);
$args[] = sprintf("-p'%s'", $config['db_password']);
$args[] = sprintf("-h'%s'", $config['db_host']);
$args[] = "-s"; // --slient
$args[] = "-N"; // --skip-column-names
$args[] = "-e'select 1'";
$cmd = implode(" ", $args);
$result = exec_command($cmd);
}
return $result;
}
}
if(!is_fn("exec_db_mysql_cmd_query")) {
2019-05-20 08:19:05 +00:00
function exec_db_mysql_cmd_query($sql, $bind) {
$result = false;
$config = get_config();
$sql = get_db_binded_sql($sql, $bind);
2019-04-06 12:04:49 +00:00
2019-05-20 08:19:05 +00:00
if(loadHelper("exectool")) {
2019-09-16 04:40:00 +00:00
$args = array("mysql");
2019-09-16 04:28:11 +00:00
$args[] = sprintf("-u'%s'", $config['db_username']);
$args[] = sprintf("-p'%s'", $config['db_password']);
$args[] = sprintf("-h'%s'", $config['db_host']);
2019-09-16 02:40:48 +00:00
$args[] = "-s"; // --slient
2019-09-16 04:28:11 +00:00
$args[] = sprintf("-D'%s'", $config['db_name']);
$args[] = sprintf("-e'%s'", make_safe_argument($sql));
2019-04-24 11:33:43 +00:00
2019-09-16 02:40:48 +00:00
$cmd = implode(" ", $args);
2019-05-20 08:19:05 +00:00
$result = exec_command($cmd);
}
2019-04-24 11:33:43 +00:00
2019-05-20 08:19:05 +00:00
return $result;
}
2019-04-06 12:04:49 +00:00
}
2019-09-16 01:51:09 +00:00
if(!is_fn("exec_db_mysql_cmd_fetch_all")) {
2019-09-16 02:40:48 +00:00
function exec_db_mysql_cmd_fetch_all($sql, $bind) {
$result = false;
2019-09-16 04:17:12 +00:00
2019-09-16 02:40:48 +00:00
$tsvData = exec_db_mysql_cmd_query($sql, $bind);
$lines = explode(DOC_EOL, $tsvData);
$rows = array();
2019-09-16 04:31:44 +00:00
if(is_fn("str_getcsv")) {
2019-09-16 04:31:44 +00:00
foreach($lines as $line) {
$rows[] = str_getcsv($line, "\t");
}
} else {
foreach($lines as $line) {
$rows[] = explode("\t", $line);
}
2019-09-16 02:40:48 +00:00
}
2019-09-16 04:28:11 +00:00
2019-09-16 02:40:48 +00:00
if(count($rows) > 0) {
$result = $rows;
}
2019-09-16 04:17:12 +00:00
2019-09-16 02:40:48 +00:00
return $result;
2019-09-16 01:51:09 +00:00
}
}