1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26: include_once XOOPS_ROOT_PATH.'/class/logger.php';
27: include_once XOOPS_ROOT_PATH.'/class/database/databasefactory.php';
28: include_once XOOPS_ROOT_PATH.'/class/database/'.XOOPS_DB_TYPE.'database.php';
29: include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php';
30:
31: 32: 33: 34: 35: 36: 37:
38: class db_manager {
39:
40: var $s_tables = array();
41: var $f_tables = array();
42: var $db;
43:
44: function db_manager(){
45: $this->db = XoopsDatabaseFactory::getDatabase();
46: $this->db->setPrefix(XOOPS_DB_PREFIX);
47: $this->db->setLogger(XoopsLogger::instance());
48: }
49:
50: function connectDB($selectdb = true) {
51: $ret = $this->db->connect($selectdb);
52: if ($ret != false) {
53: $fname = dirname(dirname(__FILE__)).'/language/'.$GLOBALS['language'].'/charset_mysql.php';
54: if (file_exists($fname)) {
55: require_once($fname);
56: }
57: }
58: return $ret;
59: }
60:
61: function isConnectable(){
62: return ($this->connectDB(false) != false) ? true : false;
63: }
64:
65: function dbExists(){
66: return ($this->connectDB() != false) ? true : false;
67: }
68:
69: function createDB()
70: {
71: $this->connectDB(false);
72:
73: $result = $this->db->query("CREATE DATABASE ".XOOPS_DB_NAME);
74:
75: return ($result != false) ? true : false;
76: }
77:
78: function queryFromFile($sql_file_path){
79: $tables = array();
80:
81: if (!file_exists($sql_file_path)) {
82: return false;
83: }
84: $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
85: SqlUtility::splitMySqlFile($pieces, $sql_query);
86: $this->connectDB();
87: foreach ($pieces as $piece) {
88: $piece = trim($piece);
89:
90:
91: $prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix());
92: if ($prefixed_query != false ) {
93: $table = $this->db->prefix($prefixed_query[4]);
94: if($prefixed_query[1] == 'CREATE TABLE'){
95: if ($this->db->query($prefixed_query[0]) != false) {
96: if(! isset($this->s_tables['create'][$table])){
97: $this->s_tables['create'][$table] = 1;
98: }
99: }else{
100: if(! isset($this->f_tables['create'][$table])){
101: $this->f_tables['create'][$table] = 1;
102: }
103: }
104: }
105: elseif($prefixed_query[1] == 'INSERT INTO'){
106: if ($this->db->query($prefixed_query[0]) != false) {
107: if(! isset($this->s_tables['insert'][$table])){
108: $this->s_tables['insert'][$table] = 1;
109: }else{
110: $this->s_tables['insert'][$table]++;
111: }
112: }else{
113: if(! isset($this->f_tables['insert'][$table])){
114: $this->f_tables['insert'][$table] = 1;
115: }else{
116: $this->f_tables['insert'][$table]++;
117: }
118: }
119: }elseif($prefixed_query[1] == 'ALTER TABLE'){
120: if ($this->db->query($prefixed_query[0]) != false) {
121: if(! isset($this->s_tables['alter'][$table])){
122: $this->s_tables['alter'][$table] = 1;
123: }
124: }else{
125: if(! isset($this->s_tables['alter'][$table])){
126: $this->f_tables['alter'][$table] = 1;
127: }
128: }
129: }elseif($prefixed_query[1] == 'DROP TABLE'){
130: if ($this->db->query('DROP TABLE '.$table) != false) {
131: if(! isset($this->s_tables['drop'][$table])){
132: $this->s_tables['drop'][$table] = 1;
133: }
134: }else{
135: if(! isset($this->s_tables['drop'][$table])){
136: $this->f_tables['drop'][$table] = 1;
137: }
138: }
139: }
140: }
141: }
142: return true;
143: }
144:
145: function report(){
146: $reports = array();
147: if (isset($this->s_tables['create'])) {
148: foreach($this->s_tables['create'] as $key => $val){
149: $reports[] = _OKIMG.sprintf(_INSTALL_L45, "<b>$key</b>");
150: }
151: }
152: if (isset($this->s_tables['insert'])) {
153: foreach($this->s_tables['insert'] as $key => $val){
154: $reports[] = _OKIMG.sprintf(_INSTALL_L119, $val, "<b>$key</b>");
155: }
156: }
157: if (isset($this->s_tables['alter'])) {
158: foreach($this->s_tables['alter'] as $key => $val){
159: $reports[] = _OKIMG.sprintf(_INSTALL_L133, "<b>$key</b>");
160: }
161: }
162: if (isset($this->s_tables['drop'])) {
163: foreach($this->s_tables['drop'] as $key => $val){
164: $reports[] = _OKIMG.sprintf(_INSTALL_L163, "<b>$key</b>");
165: }
166: }
167:
168: if (isset($this->f_tables['create'])) {
169: foreach($this->f_tables['create'] as $key => $val){
170: $reports[] = _NGIMG.sprintf(_INSTALL_L118, "<b>$key</b>");
171: }
172: }
173: if (isset($this->f_tables['insert'])) {
174: foreach($this->f_tables['insert'] as $key => $val){
175: $reports[] = _NGIMG.sprintf(_INSTALL_L120, $val, "<b>$key</b>");
176: }
177: }
178: if (isset($this->f_tables['alter'])) {
179: foreach($this->f_tables['alter'] as $key => $val){
180: $reports[] = _NGIMG.sprintf(_INSTALL_L134, "<b>$key</b>");
181: }
182: }
183: if (isset($this->f_tables['drop'])) {
184: foreach($this->f_tables['drop'] as $key => $val){
185: $reports[] = _NGIMG.sprintf(_INSTALL_L164, "<b>$key</b>");
186: }
187: }
188: return $reports;
189: }
190:
191: function query($sql){
192: $this->connectDB();
193: return $this->db->query($sql);
194: }
195:
196: function prefix($table){
197: $this->connectDB();
198: return $this->db->prefix($table);
199: }
200:
201: function fetchArray($ret){
202: $this->connectDB();
203: return $this->db->fetchArray($ret);
204: }
205:
206: function insert($table, $query){
207: $this->connectDB();
208: $table = $this->db->prefix($table);
209: $query = 'INSERT INTO '.$table.' '.$query;
210: if(!$this->db->queryF($query)){
211: if(!isset($this->f_tables['insert'][$table])){
212: $this->f_tables['insert'][$table] = 1;
213: }else{
214: $this->f_tables['insert'][$table]++;
215: }
216: return false;
217: }else{
218: if(!isset($this->s_tables['insert'][$table])){
219: $this->s_tables['insert'][$table] = 1;
220: }else{
221: $this->s_tables['insert'][$table]++;
222: }
223: return $this->db->getInsertId();
224: }
225: }
226:
227: function isError(){
228: return (isset($this->f_tables)) ? true : false;
229: }
230:
231: function deleteTables($tables){
232: $deleted = array();
233: $this->connectDB();
234: foreach ($tables as $key => $val) {
235: if(! $this->db->query("DROP TABLE ".$this->db->prefix($key))){
236: $deleted[] = $ct;
237: }
238: }
239: return $deleted;
240: }
241:
242: function tableExists($table){
243: $table = trim($table);
244: $ret = false;
245: if ($table != '') {
246: $this->connectDB();
247: $sql = 'SELECT * FROM '.$this->db->prefix($table);
248: $ret = (false != $this->db->query($sql)) ? true : false;
249: }
250: return $ret;
251: }
252: }
253:
254: ?>
255: