summaryrefslogtreecommitdiffstats
path: root/backend/php-pdo/index.php
blob: e0ed70478ab22f9a6cdda8cdd45b3ce9db2f7801 (plain)
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php

error_reporting(E_ALL);

class BackendPhpPdo
{

    public static function getConfig($id)
    {
        switch($id)
        {
            case 'saveloadlist':
                $type = 'pgsql';
                $dsn = 'pgsql:dbname=wwwsqldesigner';
                $user = 'wwwsqldesigner';
                $pass = 'xxx';
                $table = 'wwwsqldesigner';
                return array($type,$dsn,$user,$pass,$table);
            case 'import':
                $type = 'pgsql';
                $dsn = 'pgsql:dbname=wwwsqldesigner;host=localhost';
                $type = 'mysql';
                $dsn = 'mysql:dbname=wwwsqldesigner;host=localhost';
                $user = 'wwwsqldesigner';
                $pass = 'xxx';
                return array($type,$dsn,$user,$pass);
        }
    }

    public $what = '';
    # PDO
    public $type = NULL;
    public $pdo = NULL;
    # PDO STATEMENT
    public $pdo_statement = NULL;
    public $req = '';
    public $pos = 0;
    # EXCEPTION
    public $message = NULL;
    public $trace = array();
    # BENCH
    public $in = 0;
    public $out = 0;
    public static $benchs = array();

    # Constructors
    public static function getPdo($type,$dsn,$user,$password)
    {
        $obj = new BackendPhpPdo();
        $obj->what = 'PDO';
        $obj->type = $type;
        try {
            $obj->pdo = new PDO($dsn,$user,$password);
        } catch( Exception $e ) {
            self::getException($e);
        }
        if(empty($obj->pdo))
            self::getException($obj);
        return $obj;
    }
    public static function getPdoStatement($pdo=NULL,$pdo_statement=NULL,$req='',$in=NULL)
    {
        $obj = new BackendPhpPdo();
        $obj->what = 'PDO_STATEMENT';
        $obj->pdo = $pdo;
        $obj->pdo_statement = $pdo_statement;
        $obj->req = $req;
        $obj->in = $in;
        if(empty($obj->pdo_statement))
            self::getException($obj);
        return $obj;
    }
    public static function getException($message)
    {
        $trace = debug_backtrace();
        if(is_object($message))
        {
            if($message instanceOf Exception)
                $trace = $message->getTrace();
            $message = $message->getMessage();
        }
        $obj = new BackendPhpPdo();
        $obj->what = 'EXCEPTION';
        $obj->message = $message;
        $obj->trace = $trace;
        header("HTTP/1.0 500 Internal Server Error");
        echo $obj->getMessage();
        die();
    }
    public static function getBench($in=NULL,$message='')
    {
        $t = microtime(true);
        if(is_null($in))
            return $t;
        $obj = new BackendPhpPdo();
        $obj->what = 'BENCH';
        $obj->in = $in;
        $obj->out = $t;
        $obj->message = $message;
        self::$benchs[] = $obj;
        return $t;
    }

    # PDO
    public function prepare($req)
    {
        $in = self::getBench();
        $args = func_get_args();
        try {
            $pdo_statement = call_user_func_array(array($this->pdo,'prepare'),$args);
        } catch (Exception $e) {
            self::getException($e);
        }
        return self::getPDOStatement($this->pdo,$pdo_statement,$req,$in);
    }
    public function query($req)
    {
        $in = self::getBench();
        $args = func_get_args();
        try {
            $pdo_statement = call_user_func_array(array($this->pdo,'query'),$args);
        } catch (Exception $e) {
            self::getException($e);
        }
        return self::getPDOStatement($this->pdo,$pdo_statement,$req,$in);
    }
    public static $dones = array();
    public function getClass()
    {
        if(empty(self::$dones))
            require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'AbstractLayer.php');
        AbstractLayer::$pdo = $this;

        $class = 'Layer'.strtoupper($this->type);
        if(isset(self::$dones[$class]))
            return $class;

        $file = dirname(__FILE__).DIRECTORY_SEPARATOR.$class.'.php';
        if(!file_exists($file))
            self::getException('Layer not implemented : '.$this->type);
        require_once($file);

        self::$dones[$class] = 1;
        return $class;
    }
    public function getLayer()
    {
        $class = $this->getClass();
        $args = func_get_args();
        $method = str_replace(' ','',ucwords(str_replace('-',' ',array_shift($args))));
        if(!method_exists($class,$method))
            self::getException($class.'::'.$method.' not implemented');
        return call_user_func_array(array($class,$method),$args);
    }
    
    # PDO STATEMENT
    public function execute()
    {
        $count = count(explode('?',$this->req))-1;
        if($count != $this->pos)
            self::getException('Not enough or too much bindValue in regards to the number of "?" : '.$this->req.' ('.$this->pos.';'.$count.')');
        $this->pos = 0;
        $args = func_get_args();
        $status = call_user_func_array(array($this->pdo_statement,'execute'),$args);
        self::getBench($this->in,'execute');
        if(!$status)
            self::getException($this);
        return $this;
    }
    public function fetchName()
    {
        $args = func_get_args();
        $args[] = PDO::FETCH_ASSOC;
        $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
        return $results;
    }
    public function fetchNum()
    {
        $args = func_get_args();
        $args[] = PDO::FETCH_NUM;
        $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
        return $results;
    }
    public function fetchOne()
    {
        $args = func_get_args();
        $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
        foreach($results as $k=>$result)
            if(count($result) == 2)
                $results[$k] = $result[0];
        return $results;
    }
    public function fetchAll()
    {
        $args = func_get_args();
        $results = call_user_func_array(array($this->pdo_statement,'fetchAll'),$args);
        self::getBench($this->in,'fetch');
        return $results;
    }
    public function bindValue($value,$type)
    {
        switch($type)
        {
            case 'int': $type = PDO::PARAM_INT; break;
            case 'string': $type = PDO::PARAM_STR; break;
            case 'resource': $type = PDO::PARAM_LOB; break;
            default: self::getException('Unknown type : '.$type); break;
        }
        $this->pdo_statement->bindValue(++$this->pos,$value,$type);
        return $this;
    }

    # General
    public function getMessage()
    {
        switch($this->what)
        {
            case 'EXCEPTION':
                $trace = array();
                foreach($this->trace as $e)
                {
                    if(!isset($e['file'])) $e['file'] = '<i>user_func</i>';
                    if(!isset($e['line'])) $e['line'] = '0';
                    if(!isset($e['class'])) $e['class'] = '';
                    if(!isset($e['type'])) $e['type'] = '';
                    $trace[] = '<small>'.$e['file'].':'.$e['line'].'</small> '.$e['class'].$e['type'].$e['function'];
                }
                return '<strong>'.$this->message.'</strong>'."\n".'<hr />'."\n".implode('<br />'."\n",$trace);
            case 'PDO':
                if(empty($this->pdo))
                    return 'Fail to connect.';
                $error = $this->pdo->errorInfo();
                return $error[0].':'.$error[1].' <pre>'.$error[2].'</pre><pre>'.$this->req.'</pre>';
            case 'PDO_STATEMENT':
                if(empty($this->pdo_statement))
                    $error = $this->pdo->errorInfo();
                else
                    $error = $this->pdo_statement->errorInfo();
                return $error[0].':'.$error[1].' <pre>'.$error[2].'</pre><pre>'.$this->req.'</pre>';
            case 'BENCH':
                return (round(($this->out-$this->in)*1000)/1000).'s '.$this->message;
        }
    }
    public function getTrace()
    {
        switch($this->what)
        {
            case 'EXCEPTION':
                return $this->trace;
            default:
                return array();
        }
    }

    public static function fetchDatatypes($file)
    {
        if(!file_exists($file))
        {
            $header = '<'.'?xml version="1.0" ?'.'>';
            $datatypes = '';
        }
        else
        {
            $datatypes = file($file);
            $header = array_shift($datatypes);
            $datatypes = implode('',$datatypes);
        }
        return array($header,$datatypes);
    }
    public static function buildXML($pdo)
    {
        list($header,$datatypes) = self::fetchDatatypes($pdo->getLayer('datatypes-file'));

        $xml = array();
        $xml[] = $header;
        $xml[] = '<sql db="'.$pdo->type.'">';
        $xml[] = $datatypes;

        $tables = $pdo->getLayer('tables');
        foreach($tables as $table)
        {
            $xmltable = '<table name="'.$table['name'].'">';
            if(!empty($table['comment']))
                $xmltable .= '<comment>'.$table['comment'].'</comment>';

            $columns = $pdo->getLayer('columns',$table);
            foreach($columns as $column)
            {
                $xmlcolumn = '<row name="'.$column['name'].'" null="'.$column['null'].'" autoincrement="'.$column['autoincrement'].'">';
                $xmlcolumn .= '<datatype>'.strtoupper($column['type']).'</datatype>';
                $xmlcolumn .= '<default>'.$column['default'].'</default>';
                if(!empty($column['comment']))
                    $xmlcolumn .= '<comment>'.$column['comment'].'</comment>';

                $relations = $pdo->getLayer('relations',$table,$column);
                foreach($relations as $relation)
                    $xmlcolumn .= '<relation table="'.$relation['table'].'" row="'.$relation['column'].'" />';

                $xmlcolumn .= '</row>';
                $xmltable .= $xmlcolumn;
            }

            $keys = $pdo->getLayer('keys',$table);
            foreach($keys as $name=>$key)
            {
                $xmlkey = '<key name="'.$name.'" type="'.$key['type'].'">';

                foreach($key['columns'] as $column)
                    if(!empty($column))
                        $xmlkey .= '<part>'.$column.'</part>';

                $xmlkey .= '</key>';
                $xmltable .= $xmlkey;
            }

            $xmltable .= '</table>';
            $xml[] = $xmltable;
        }

        $xml[] = '</sql>';

        return implode("\n",$xml);
    }

    public static function actionList()
    {
        list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
        $pdo = self::getPdo($type,$dsn,$user,$pass);
        $r = $pdo->getLayer('keywords',$table)->fetchOne();
        foreach($r as $keyword)
            echo $keyword."\n";
    }
    public static function actionSave($key='keyword')
    {
        $keyword = self::req($key);
        $data = file_get_contents("php://input");
        if (get_magic_quotes_gpc() || get_magic_quotes_runtime())
           $data = stripslashes($data);

        list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
        $pdo = self::getPdo($type,$dsn,$user,$pass);

        $r = $pdo->getLayer('keywords',$table,$keyword)->fetchOne();
        if(count($r) > 0)
            $pdo->getLayer('keywords-update',$table,$keyword,$data);
        else
            $pdo->getLayer('keywords-insert',$table,$keyword,$data);

        header("HTTP/1.0 201 Created");
    }
    public static function actionLoad($key='keyword')
    {
        $keyword = self::req($key);

        list($type,$dsn,$user,$pass,$table) = self::getConfig('saveloadlist');
        $pdo = self::getPdo($type,$dsn,$user,$pass);

        $r = $pdo->getLayer('keywords',$table,$keyword)->fetchOne();
        if(count($r) > 0)
        {
            header("Content-type: text/xml");
            echo $r[0];
        }
        else
            header("HTTP/1.0 404 Not Found");
        die();
    }
    public static function actionImport()
    {
        ob_start();

        list($type,$dsn,$user,$pass) = self::getConfig('import');
        $pdo = self::getPdo($type,$dsn,$user,$pass);

        $xml = self::buildXML($pdo);

        if(!ob_get_contents())
            header("Content-type: text/xml");
        echo $xml;
        die();
    }

    public static function req($key)
    {
        return isset($_REQUEST[$key])?$_REQUEST[$key]:NULL;
    }
    public static function controler($key='action')
    {
        $action = self::req($key);
        switch(strtolower($action))
        {
            case 'list':
                return self::actionList();
            case 'save':
                return self::actionSave();
            case 'load':
                return self::actionLoad();
            case 'import':
                return self::actionImport();
            default:
                header("HTTP/1.0 501 Not Implemented");
                echo 'Action not implemented';
                die();
        }
    }
}

function bench()
{
    foreach(BackendPhpPdo::$benchs as $bench)
        echo $bench->getMessage().'<br />';
}
//register_shutdown_function('bench');

BackendPhpPdo::controler();

?>