21 public $s_tables = [];
22 public $f_tables = [];
25 public function __construct() {
27 $this->db->setPrefix( XOOPS_DB_PREFIX );
31 public function connectDB( $selectdb =
true ) {
32 $ret = $this->db->connect( $selectdb );
33 if (
false !== $ret ) {
34 $fname = dirname( __DIR__ ) .
'/language/' . $GLOBALS[
'language'] .
'/charset_mysql.php';
35 if ( file_exists( $fname ) ) {
43 public function isConnectable() {
44 return $this->connectDB(
false) !=
false;
47 public function dbExists() {
48 return $this->connectDB() !=
false;
51 public function createDB() {
52 $this->connectDB(
false);
54 $result = $this->db->query(
"CREATE DATABASE ".XOOPS_DB_NAME);
56 return $result !=
false;
59 public function queryFromFile( $sql_file_path ) {
63 if ( ! file_exists( $sql_file_path ) ) {
66 $sql_query = trim( fread( fopen( $sql_file_path,
'r' ), filesize( $sql_file_path ) ) );
67 SqlUtility::splitMySqlFile( $pieces, $sql_query );
69 foreach ( $pieces as $piece ) {
70 $piece = trim( $piece );
73 $prefixed_query = SqlUtility::prefixQuery( $piece, $this->db->prefix() );
74 if (
false !== $prefixed_query ) {
75 $table = $this->db->prefix( $prefixed_query[4] );
76 if (
'CREATE TABLE' === $prefixed_query[1] ) {
77 if (
false !== $this->db->query( $prefixed_query[0] ) ) {
78 if ( ! isset( $this->s_tables[
'create'][ $table ] ) ) {
79 $this->s_tables[
'create'][ $table ] = 1;
82 if ( ! isset( $this->f_tables[
'create'][ $table ] ) ) {
83 $this->f_tables[
'create'][ $table ] = 1;
86 } elseif (
'INSERT INTO' === $prefixed_query[1] ) {
87 if (
false !== $this->db->query( $prefixed_query[0] ) ) {
88 if ( ! isset( $this->s_tables[
'insert'][ $table ] ) ) {
89 $this->s_tables[
'insert'][ $table ] = 1;
91 $this->s_tables[
'insert'][ $table ] ++;
93 }
else if ( ! isset( $this->f_tables[
'insert'][ $table ] ) ) {
94 $this->f_tables[
'insert'][ $table ] = 1;
96 $this->f_tables[
'insert'][ $table ] ++;
98 } elseif (
'ALTER TABLE' === $prefixed_query[1] ) {
99 if (
false !== $this->db->query( $prefixed_query[0] ) ) {
100 if ( ! isset( $this->s_tables[
'alter'][ $table ] ) ) {
101 $this->s_tables[
'alter'][ $table ] = 1;
103 }
else if ( ! isset( $this->s_tables[
'alter'][ $table ] ) ) {
104 $this->f_tables[
'alter'][ $table ] = 1;
106 } elseif (
'DROP TABLE' === $prefixed_query[1] ) {
107 if (
false !== $this->db->query(
'DROP TABLE ' . $table ) ) {
108 if ( ! isset( $this->s_tables[
'drop'][ $table ] ) ) {
109 $this->s_tables[
'drop'][ $table ] = 1;
111 }
else if ( ! isset( $this->s_tables[
'drop'][ $table ] ) ) {
112 $this->f_tables[
'drop'][ $table ] = 1;
121 public function report() {
123 if ( isset( $this->s_tables[
'create'] ) ) {
124 foreach ( $this->s_tables[
'create'] as $key => $val ) {
125 $reports[] = _OKIMG . sprintf( _INSTALL_L45,
"<b>$key</b>" );
128 if ( isset( $this->s_tables[
'insert'] ) ) {
129 foreach ( $this->s_tables[
'insert'] as $key => $val ) {
130 $reports[] = _OKIMG . sprintf( _INSTALL_L119, $val,
"<b>$key</b>" );
133 if ( isset( $this->s_tables[
'alter'] ) ) {
134 foreach ( $this->s_tables[
'alter'] as $key => $val ) {
135 $reports[] = _OKIMG . sprintf( _INSTALL_L133,
"<b>$key</b>" );
138 if ( isset( $this->s_tables[
'drop'] ) ) {
139 foreach ( $this->s_tables[
'drop'] as $key => $val ) {
140 $reports[] = _OKIMG . sprintf( _INSTALL_L163,
"<b>$key</b>" );
144 if ( isset( $this->f_tables[
'create'] ) ) {
145 foreach ( $this->f_tables[
'create'] as $key => $val ) {
146 $reports[] = _NGIMG . sprintf( _INSTALL_L118,
"<b>$key</b>" );
149 if ( isset( $this->f_tables[
'insert'] ) ) {
150 foreach ( $this->f_tables[
'insert'] as $key => $val ) {
151 $reports[] = _NGIMG . sprintf( _INSTALL_L120, $val,
"<b>$key</b>" );
154 if ( isset( $this->f_tables[
'alter'] ) ) {
155 foreach ( $this->f_tables[
'alter'] as $key => $val ) {
156 $reports[] = _NGIMG . sprintf( _INSTALL_L134,
"<b>$key</b>" );
159 if ( isset( $this->f_tables[
'drop'] ) ) {
160 foreach ( $this->f_tables[
'drop'] as $key => $val ) {
161 $reports[] = _NGIMG . sprintf( _INSTALL_L164,
"<b>$key</b>" );
168 public function query( $sql ) {
171 return $this->db->query( $sql );
174 public function prefix( $table ) {
177 return $this->db->prefix( $table );
180 public function fetchArray( $ret ) {
183 return $this->db->fetchArray( $ret );
186 public function insert( $table, $query ) {
188 $table = $this->db->prefix( $table );
189 $query =
'INSERT INTO ' . $table .
' ' . $query;
190 if ( ! $this->db->queryF( $query ) ) {
191 if ( ! isset( $this->f_tables[
'insert'][ $table ] ) ) {
192 $this->f_tables[
'insert'][ $table ] = 1;
194 $this->f_tables[
'insert'][ $table ] ++;
200 if ( ! isset( $this->s_tables[
'insert'][ $table ] ) ) {
201 $this->s_tables[
'insert'][ $table ] = 1;
203 $this->s_tables[
'insert'][ $table ] ++;
206 return $this->db->getInsertId();
209 public function isError() {
211 return isset( $this->f_tables );
214 public function deleteTables( $tables ) {
217 foreach ( $tables as $key => $val ) {
218 if ( ! $this->db->query(
'DROP TABLE ' . $this->db->prefix( $key ) ) ) {
226 public function tableExists( $table ) {
227 $table = trim( $table );
229 if ( $table !==
'' ) {
231 $sql =
'SELECT * FROM ' . $this->db->prefix( $table );
232 $ret =
false !== $this->db->query( $sql );