1 : <?php
2 :
3 : /**
4 : * Dictionaries of Old French and Latin
5 : *
6 : * PHP 5
7 : *
8 : * @category DicFro
9 : * @package Model
10 : * @subpackage Query
11 : * @author Michel Corne <mcorne@yahoo.com>
12 : * @copyright 2008-2010 Michel Corne
13 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
14 : * @link http://www.micmap.org/dicfro
15 : */
16 :
17 : require_once 'Base/String.php';
18 :
19 : /**
20 : * Queries a dictionary database
21 : *
22 : * @category DicFro
23 : * @package Model
24 : * @subpackage Query
25 : * @author Michel Corne <mcorne@yahoo.com>
26 : * @copyright 2008-2010 Michel Corne
27 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
28 : */
29 :
30 : abstract class Model_Query
31 : {
32 : /**
33 : * Template of the dictionary database name
34 : */
35 : const DSN_TPL = 'sqlite:%s/dictionary.sqlite';
36 :
37 : /**
38 : * Name of the dictionary database
39 : * @var string
40 : */
41 : public $dsn;
42 :
43 : /**
44 : * String object
45 : * @var object
46 : */
47 : public $string;
48 :
49 : /**
50 : * Constructor
51 : *
52 : * @param string $directory the dictionaries directory
53 : * @return void
54 : */
55 : public function __construct($directory)
56 : {
57 : // sets the dictionary database name
58 38 : $this->dsn = $this->createDsn($directory);
59 :
60 38 : $this->string = new Base_String;
61 38 : }
62 :
63 : /**
64 : * Creates the name of the dictionary database
65 : *
66 : * @param string $directory the dictionaries directory
67 : * @return string the name of the dictionary database
68 : */
69 : public function createDsn($directory)
70 : {
71 38 : return sprintf(self::DSN_TPL, $directory);
72 : }
73 :
74 : /**
75 : * Prepares and executes a query and fetches the result
76 : *
77 : * @param string $query the SQL query
78 : * @param array $parameter the list of parameters, format: array(<key> => <value>,...)
79 : * @param string $fetchStyle the fetch style
80 : * @return mixed the result of the query
81 : * @throws Exception if no result is returned
82 : */
83 : public function execute($query, $parameter = array(), $fetchStyle = PDO::FETCH_ASSOC)
84 : {
85 31 : $pdo = new PDO($this->dsn) and
86 31 : $statement = $pdo->prepare($query) and
87 31 : $statement->execute($parameter) and
88 31 : $result = $statement->fetchAll($fetchStyle);
89 :
90 31 : if (!isset($result)) {
91 1 : throw new Exception('query-error');
92 : }
93 :
94 31 : return $result;
95 : }
|