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