| 1 |
lars |
1 |
<?php
|
|
|
2 |
// $Id: MDB_test.php,v 1.40 2003/04/26 12:54:23 lsmith Exp $
|
|
|
3 |
//
|
|
|
4 |
// MDB test script.
|
|
|
5 |
//
|
|
|
6 |
|
|
|
7 |
// BC hack to define PATH_SEPARATOR for version of PHP prior 4.3
|
|
|
8 |
if(!defined('PATH_SEPARATOR')) {
|
|
|
9 |
if(defined('DIRECTORY_SEPARATOR') && DIRECTORY_SEPARATOR == "\\") {
|
|
|
10 |
define('PATH_SEPARATOR', ';');
|
|
|
11 |
} else {
|
|
|
12 |
define('PATH_SEPARATOR', ':');
|
|
|
13 |
}
|
|
|
14 |
}
|
|
|
15 |
ini_set('include_path', '..'.PATH_SEPARATOR.ini_get('include_path'));
|
|
|
16 |
|
|
|
17 |
// MDB.php doesnt have to be included since manager.php does that
|
|
|
18 |
// manager.php is only necessary for handling xml schema files
|
|
|
19 |
require_once('MDB.php');
|
|
|
20 |
MDB::loadFile('Manager');
|
|
|
21 |
// only including this to output result data
|
|
|
22 |
require_once('Var_Dump.php');
|
|
|
23 |
|
|
|
24 |
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handle_pear_error');
|
|
|
25 |
function handle_pear_error ($error_obj)
|
|
|
26 |
{
|
|
|
27 |
print '<pre><b>PEAR-Error</b><br />';
|
|
|
28 |
echo $error_obj->getMessage().': '.$error_obj->getUserinfo();
|
|
|
29 |
print '</pre>';
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
// just for kicks you can mess up this part to see some pear error handling
|
|
|
33 |
$user = 'metapear';
|
|
|
34 |
$pass = 'funky';
|
|
|
35 |
//$pass = '';
|
|
|
36 |
$host = 'localhost';
|
|
|
37 |
$db_name = 'metapear_test_db';
|
|
|
38 |
if(isset($_GET['db_type'])) {
|
|
|
39 |
$db_type = $_GET['db_type'];
|
|
|
40 |
} else {
|
|
|
41 |
$db_type = 'mysql';
|
|
|
42 |
}
|
|
|
43 |
echo($db_type.'<br>');
|
|
|
44 |
|
|
|
45 |
// Data Source Name: This is the universal connection string
|
|
|
46 |
$dsn['username'] = $user;
|
|
|
47 |
$dsn['password'] = $pass;
|
|
|
48 |
$dsn['hostspec'] = $host;
|
|
|
49 |
$dsn['phptype'] = $db_type;
|
|
|
50 |
// MDB::connect will return a Pear DB object on success
|
|
|
51 |
// or a Pear MDB error object on error
|
|
|
52 |
// You can also set to TRUE the second param
|
|
|
53 |
// if you want a persistent connection:
|
|
|
54 |
// $db = MDB::connect($dsn, TRUE);
|
|
|
55 |
// you can alternatively build a dsn here
|
|
|
56 |
//$dsn = "$db_type://$user:$pass@$host/$db_name";
|
|
|
57 |
Var_Dump::display($dsn);
|
|
|
58 |
$db =& MDB::connect($dsn);
|
|
|
59 |
// With MDB::isError you can differentiate between an error or
|
|
|
60 |
// a valid connection.
|
|
|
61 |
if (MDB::isError($db)) {
|
|
|
62 |
die (__LINE__.$db->getMessage());
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$manager =& new MDB_Manager;
|
|
|
66 |
$input_file = 'metapear_test_db.schema';
|
|
|
67 |
// you can either pass a dsn string, a dsn array or an exisiting db connection
|
|
|
68 |
$manager->connect($db);
|
|
|
69 |
// lets create the database using 'metapear_test_db.schema'
|
|
|
70 |
// if you have allready run this script you should have 'metapear_test_db.schema.before'
|
|
|
71 |
// in that case MDB will just compare the two schemas and make any necessary modifications to the existing DB
|
|
|
72 |
echo(Var_Dump::display($manager->updateDatabase($input_file, $input_file.'.before')).'<br>');
|
|
|
73 |
echo('updating database from xml schema file<br>');
|
|
|
74 |
|
|
|
75 |
echo('switching to database: '.$db_name.'<br>');
|
|
|
76 |
$db->setDatabase($db_name);
|
|
|
77 |
// happy query
|
|
|
78 |
$query ='SELECT * FROM test';
|
|
|
79 |
echo('query for the following examples:'.$query.'<br>');
|
|
|
80 |
// run the query and get a result handler
|
|
|
81 |
$result = $db->query($query);
|
|
|
82 |
// lets just get row:0 column:0 and free the result
|
|
|
83 |
$field = $db->fetchOne($result);
|
|
|
84 |
echo('<br>field:<br>'.$field.'<br>');
|
|
|
85 |
// run the query and get a result handler
|
|
|
86 |
$result = $db->query($query);
|
|
|
87 |
// lets just get row:0 and free the result
|
|
|
88 |
$array = $db->fetchRow($result);
|
|
|
89 |
echo('<br>row:<br>');
|
|
|
90 |
echo(Var_Dump::display($array).'<br>');
|
|
|
91 |
// run the query and get a result handler
|
|
|
92 |
$result = $db->query($query);
|
|
|
93 |
// lets just get column:0 and free the result
|
|
|
94 |
$array = $db->fetchCol($result);
|
|
|
95 |
echo('<br>column:<br>');
|
|
|
96 |
echo(Var_Dump::display($array).'<br>');
|
|
|
97 |
// run the query and get a result handler
|
|
|
98 |
$result = $db->query($query);
|
|
|
99 |
// lets just get column:0 and free the result
|
|
|
100 |
$array = $db->fetchCol($result, MDB_FETCHMODE_DEFAULT, 2);
|
|
|
101 |
echo('<br>get column #2 (counting from 0):<br>');
|
|
|
102 |
echo(Var_Dump::display($array).'<br>');
|
|
|
103 |
// run the query and get a result handler
|
|
|
104 |
$result = $db->query($query);
|
|
|
105 |
echo('tableInfo:<br>');
|
|
|
106 |
echo(Var_Dump::display($db->tableInfo($result)).'<br>');
|
|
|
107 |
// lets just get everything and free the result
|
|
|
108 |
$result = $db->query($query);
|
|
|
109 |
$types = array('integer', 'text', 'timestamp');
|
|
|
110 |
$db->setResultTypes($result, $types);
|
|
|
111 |
$array = $db->fetchAll($result, MDB_FETCHMODE_FLIPPED);
|
|
|
112 |
echo('<br>all with result set flipped:<br>');
|
|
|
113 |
echo(Var_Dump::display($array).'<br>');
|
|
|
114 |
// save some time with this function
|
|
|
115 |
// lets just get all and free the result
|
|
|
116 |
$array = $db->queryAll($query);
|
|
|
117 |
echo('<br>all with just one call:<br>');
|
|
|
118 |
echo(Var_Dump::display($array).'<br>');
|
|
|
119 |
// run the query with the offset 1 and count 1 and get a result handler
|
|
|
120 |
$result = $db->limitQuery($query, NULL, 1, 1);
|
|
|
121 |
// lets just get everything but with an associative array and free the result
|
|
|
122 |
$array = $db->fetchAll($result, MDB_FETCHMODE_ASSOC);
|
|
|
123 |
echo('<br>associative array with offset 1 and count 1:<br>');
|
|
|
124 |
echo(Var_Dump::display($array).'<br>');
|
|
|
125 |
// lets create a sequence
|
|
|
126 |
echo('<br>create a new seq with start 3 name real_funky_id<br>');
|
|
|
127 |
$err = $db->createSequence('real_funky_id', 3);
|
|
|
128 |
if (MDB::isError($err)) {
|
|
|
129 |
echo('<br>could not create sequence again<br>');
|
|
|
130 |
}
|
|
|
131 |
echo('<br>get the next id:<br>');
|
|
|
132 |
$value = $db->nextId('real_funky_id');
|
|
|
133 |
echo($value.'<br>');
|
|
|
134 |
// lets try an prepare execute combo
|
|
|
135 |
$alldata = array(
|
|
|
136 |
array(1, 'one', 'un'),
|
|
|
137 |
array(2, 'two', 'deux'),
|
|
|
138 |
array(3, 'three', 'trois'),
|
|
|
139 |
array(4, 'four', 'quatre')
|
|
|
140 |
);
|
|
|
141 |
$prepared_query = $db->prepareQuery('INSERT INTO numbers VALUES(?,?,?)');
|
|
|
142 |
foreach ($alldata as $row) {
|
|
|
143 |
echo('running execute<br>');
|
|
|
144 |
$db->execute($prepared_query, NULL, $row);
|
|
|
145 |
}
|
|
|
146 |
// lets try an prepare execute combo
|
|
|
147 |
$alldata = array(
|
|
|
148 |
array(5, 'five', 'cinq'),
|
|
|
149 |
array(6, 'six', 'six'),
|
|
|
150 |
array(7, 'seven', 'sept'),
|
|
|
151 |
array(8, 'eight', 'huit')
|
|
|
152 |
);
|
|
|
153 |
$prepared_query = $db->prepareQuery('INSERT INTO numbers VALUES(?,?,?)');
|
|
|
154 |
echo('running executeMultiple<br>');
|
|
|
155 |
echo(Var_Dump::display($db->executeMultiple($prepared_query, NULL, $alldata)).'<br>');
|
|
|
156 |
$array = array(4);
|
|
|
157 |
echo('<br>see getOne in action:<br>');
|
|
|
158 |
echo(Var_Dump::display($db->getOne('SELECT trans_en FROM numbers WHERE number = ?','text',$array)).'<br>');
|
|
|
159 |
echo('<br>see getRow in action:<br>');
|
|
|
160 |
$db->setFetchmode(MDB_FETCHMODE_ASSOC);
|
|
|
161 |
echo('<br>default fetchmode ist now MDB_FETCHMODE_ASSOC<br>');
|
|
|
162 |
echo(Var_Dump::display($db->getRow('SELECT * FROM numbers WHERE number = ?',array('integer','text','text'),$array)));
|
|
|
163 |
echo('default fetchmode ist now MDB_FETCHMODE_ORDERED<br>');
|
|
|
164 |
$db->setFetchmode(MDB_FETCHMODE_ORDERED);
|
|
|
165 |
echo('<br>see getCol in action:<br>');
|
|
|
166 |
echo(Var_Dump::display($db->getCol('SELECT * FROM numbers','text', NULL, NULL, 1)).'<br>');
|
|
|
167 |
echo('<br>see getAll in action:<br>');
|
|
|
168 |
echo(Var_Dump::display($db->getAll('SELECT * FROM test',array('integer','text','text'))).'<br>');
|
|
|
169 |
echo('<br>see getAssoc in action:<br>');
|
|
|
170 |
echo(Var_Dump::display($db->getAssoc('SELECT * FROM test',array('integer','text','text'), NULL, NULL, MDB_FETCHMODE_ASSOC)).'<br>');
|
|
|
171 |
echo('tableInfo on a string:<br>');
|
|
|
172 |
echo(Var_Dump::display($db->tableInfo('numbers')).'<br>');
|
|
|
173 |
echo('<br>just a simple update query:<br>');
|
|
|
174 |
echo(Var_Dump::display($db->query('UPDATE numbers set trans_en ='.$db->getIntegerValue(0))).'<br>');
|
|
|
175 |
echo('<br>affected rows:<br>');
|
|
|
176 |
echo($db->affectedRows().'<br>');
|
|
|
177 |
// subselect test
|
|
|
178 |
$sub_select = $db->subSelect('SELECT test_name from test WHERE test_name = '.$db->getTextValue('gummihuhn'), TRUE);
|
|
|
179 |
echo(Var_Dump::display($sub_select).'<br>');
|
|
|
180 |
$query_with_subselect = 'SELECT * FROM test WHERE test_name IN ('.$sub_select.')';
|
|
|
181 |
// run the query and get a result handler
|
|
|
182 |
echo($query_with_subselect.'<br>');
|
|
|
183 |
$result = $db->query($query_with_subselect);
|
|
|
184 |
$array = $db->fetchAll($result);
|
|
|
185 |
echo('<br>all with subselect:<br>');
|
|
|
186 |
echo(Var_Dump::display($array).'<br>');
|
|
|
187 |
echo('<br>drop index (will fail if the index was never created):<br>');
|
|
|
188 |
echo(Var_Dump::display($db->dropIndex('test', 'test_id_index')).'<br>');
|
|
|
189 |
$index_def = array(
|
|
|
190 |
'FIELDS' => array(
|
|
|
191 |
'test_id' => array(
|
|
|
192 |
'sorting' => 'ascending'
|
|
|
193 |
)
|
|
|
194 |
)
|
|
|
195 |
);
|
|
|
196 |
echo('<br>create index:<br>');
|
|
|
197 |
echo(Var_Dump::display($db->createIndex('test', 'test_id_index', $index_def)).'<br>');
|
|
|
198 |
if($db_type == 'mysql') {
|
|
|
199 |
$manager->captureDebugOutput(TRUE);
|
|
|
200 |
$manager->database->setOption('log_line_break', '<br>');
|
|
|
201 |
// ok now lets create a new xml schema file from the existing DB
|
|
|
202 |
// we will not use the 'metapear_test_db.schema' for this
|
|
|
203 |
// this feature is especially interesting for people that have an existing Db and want to move to MDB's xml schema management
|
|
|
204 |
// you can also try MDB_MANAGER_DUMP_ALL and MDB_MANAGER_DUMP_CONTENT
|
|
|
205 |
echo(Var_Dump::display($manager->dumpDatabase(
|
|
|
206 |
array(
|
|
|
207 |
'Output_Mode' => 'file',
|
|
|
208 |
'Output' => $db_name.'2.schema'
|
|
|
209 |
),
|
|
|
210 |
MDB_MANAGER_DUMP_STRUCTURE
|
|
|
211 |
)).'<br>');
|
|
|
212 |
if($manager->options['debug']) {
|
|
|
213 |
echo($manager->debugOutput().'<br>');
|
|
|
214 |
}
|
|
|
215 |
// this is the database definition as an array
|
|
|
216 |
echo(Var_Dump::display($manager->database_definition).'<br>');
|
|
|
217 |
}
|
|
|
218 |
echo('<br>just a simple delete query:<br>');
|
|
|
219 |
echo(Var_Dump::display($db->query('DELETE FROM numbers')).'<br>');
|
|
|
220 |
// You can disconnect from the database with:
|
|
|
221 |
$db->disconnect()
|
|
|
222 |
?>
|