Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
    // $Id: example.php,v 1.24 2006/05/31 14:38:06 lsmith Exp $
4
    //
5
    // MDB2 and MDB2_Schema example script.
6
    //
7
 
8
    ini_set('include_path', '../..'.PATH_SEPARATOR.ini_get('include_path'));
9
 
10
    // require the MDB2 code
11
    require_once 'MDB2.php';
12
 
13
    // define and set a PEAR error handler
14
    // will be called whenever an unexpected PEAR_Error occurs
15
    function handle_pear_error ($error_obj)
16
    {
17
        print '<pre><b>PEAR-Error</b><br />';
18
        echo $error_obj->getMessage().': '.$error_obj->getUserinfo();
19
        print '</pre>';
20
    }
21
    PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handle_pear_error');
22
 
23
    // just for kicks you can mess up this part to see some pear error handling
24
    $user = 'root';
25
    $pass = '';
26
    $host = 'localhost';
27
    $mdb2_name = 'metapear_test_db';
28
    $mdb2_type = !empty($_GET['db_type']) ? $_GET['db_type'] : 'mysql';
29
    echo($mdb2_type.'<br>');
30
 
31
    // Data Source Name: This is the universal connection string
32
    $dsn['username'] = $user;
33
    $dsn['password'] = $pass;
34
    $dsn['hostspec'] = $host;
35
    $dsn['phptype'] = $mdb2_type;
36
    // MDB2::factory will return a PEAR::MDB2 instance on success
37
    // or a Pear MDB2 error object on error
38
    // You can alternatively build a dsn here
39
    // $dsn = "$mdb2_type://$user:$pass@$host/$mdb2_name";
40
    Var_Dump($dsn);
41
    $mdb2 =& MDB2::factory($dsn);
42
    // With PEAR::isError you can differentiate between an error or
43
    // a valid connection.
44
    if (PEAR::isError($mdb2)) {
45
        die (__LINE__.$mdb2->getMessage());
46
    }
47
 
48
    // this loads the MDB2_Schema manager
49
    // this is a separate package you must install
50
    require_once 'MDB2/Schema.php';
51
    // you can either pass a dsn string, a dsn array or an exisiting mdb2 connection
52
    $schema =& MDB2_Schema::factory($mdb2);
53
    $input_file = 'metapear_test_db.schema';
54
    // lets create the database using 'metapear_test_db.schema'
55
    // if you have allready run this script you should have 'metapear_test_db.schema.before'
56
    // in that case MDB2 will just compare the two schemas and make any
57
    // necessary modifications to the existing database
58
    Var_Dump($schema->updateDatabase($input_file, $input_file.'.before'));
59
    echo('updating database from xml schema file<br>');
60
 
61
    echo('switching to database: '.$mdb2_name.'<br>');
62
    $mdb2->setDatabase($mdb2_name);
63
    // happy query
64
    $query ='SELECT * FROM test';
65
    echo('query for the following examples:'.$query.'<br>');
66
    // run the query and get a result handler
67
    $result = $mdb2->query($query);
68
    // lets just get row:0 and free the result
69
    $array = $result->fetchRow();
70
    $result->free();
71
    echo('<br>row:<br>');
72
    echo(Var_Dump($array).'<br>');
73
    $result = $mdb2->query($query);
74
    // lets just get row:0 and free the result
75
    $array = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
76
    $result->free();
77
    echo('<br>row (object:<br>');
78
    echo(Var_Dump($array).'<br>');
79
    // run the query and get a result handler
80
    $result = $mdb2->query($query);
81
    // lets just get row:0 and free the result
82
    $array = $result->fetchRow();
83
    $result->free();
84
    echo('<br>row from object:<br>');
85
    echo(Var_Dump($array).'<br>');
86
    // run the query and get a result handler
87
    $result = $mdb2->query($query);
88
    // lets just get column:0 and free the result
89
    $array = $result->fetchCol(2);
90
    $result->free();
91
    echo('<br>get column #2 (counting from 0):<br>');
92
    echo(Var_Dump($array).'<br>');
93
    // run the query and get a result handler
94
    $result = $mdb2->query($query);
95
    Var_Dump($mdb2->loadModule('Reverse', null, true));
96
    echo('tableInfo:<br>');
97
    echo(Var_Dump($mdb2->reverse->tableInfo($result)).'<br>');
98
    $types = array('integer', 'text', 'timestamp');
99
    $result->setResultTypes($types);
100
    $array = $result->fetchAll(MDB2_FETCHMODE_FLIPPED);
101
    $result->free();
102
    echo('<br>all with result set flipped:<br>');
103
    echo(Var_Dump($array).'<br>');
104
    // save some time with this function
105
    // lets just get all and free the result
106
    $array = $mdb2->queryAll($query);
107
    echo('<br>all with just one call:<br>');
108
    echo(Var_Dump($array).'<br>');
109
    // run the query with the offset 1 and count 1 and get a result handler
110
    Var_Dump($mdb2->loadModule('Extended', null, false));
111
    $result = $mdb2->extended->limitQuery($query, null, 1, 1);
112
    // lets just get everything but with an associative array and free the result
113
    $array = $result->fetchAll(MDB2_FETCHMODE_ASSOC);
114
    echo('<br>associative array with offset 1 and count 1:<br>');
115
    echo(Var_Dump($array).'<br>');
116
    // lets create a sequence
117
    echo(Var_Dump($mdb2->loadModule('Manager', null, true)));
118
    echo('<br>create a new seq with start 3 name real_funky_id<br>');
119
    $err = $mdb2->manager->createSequence('real_funky_id', 3);
120
    if (PEAR::isError($err)) {
121
            echo('<br>could not create sequence again<br>');
122
    }
123
    echo('<br>get the next id:<br>');
124
    $value = $mdb2->nextId('real_funky_id');
125
    echo($value.'<br>');
126
    // lets try an prepare execute combo
127
    $alldata = array(
128
                     array(1, 'one', 'un'),
129
                     array(2, 'two', 'deux'),
130
                     array(3, 'three', 'trois'),
131
                     array(4, 'four', 'quatre')
132
    );
133
    $stmt = $mdb2->prepare('INSERT INTO numbers VALUES(?,?,?)', array('integer', 'text', 'text'), MDB2_PREPARE_MANIP);
134
    foreach ($alldata as $row) {
135
        echo('running execute<br>');
136
        $stmt->bindValueArray($row);
137
        $stmt->execute();
138
    }
139
    $array = array(4);
140
    echo('<br>see getOne in action:<br>');
141
    echo(Var_Dump($mdb2->extended->getOne('SELECT trans_en FROM numbers WHERE number = ?',null,$array,array('integer'))).'<br>');
142
    $mdb2->setFetchmode(MDB2_FETCHMODE_ASSOC);
143
    echo('<br>default fetchmode ist now MDB2_FETCHMODE_ASSOC<br>');
144
    echo('<br>see getRow in action:<br>');
145
    echo(Var_Dump($mdb2->extended->getRow('SELECT * FROM numbers WHERE number = ?',array('integer','text','text'),$array, array('integer'))));
146
    echo('default fetchmode ist now MDB2_FETCHMODE_ORDERED<br>');
147
    $mdb2->setFetchmode(MDB2_FETCHMODE_ORDERED);
148
    echo('<br>see getCol in action:<br>');
149
    echo(Var_Dump($mdb2->extended->getCol('SELECT * FROM numbers WHERE number != ?',null,$array,array('integer'), 1)).'<br>');
150
    echo('<br>see getAll in action:<br>');
151
    echo(Var_Dump($mdb2->extended->getAll('SELECT * FROM test WHERE test_id != ?',array('integer','text','text'), $array, array('integer'))).'<br>');
152
    echo('<br>see getAssoc in action:<br>');
153
    echo(Var_Dump($mdb2->extended->getAssoc('SELECT * FROM test WHERE test_id != ?',array('integer','text','text'), $array, array('integer'), MDB2_FETCHMODE_ASSOC)).'<br>');
154
    echo('tableInfo on a string:<br>');
155
    echo(Var_Dump($mdb2->reverse->tableInfo('numbers')).'<br>');
156
    echo('<br>just a simple update query:<br>');
157
    echo('<br>affected rows:<br>');
158
    echo(Var_Dump($mdb2->exec('UPDATE numbers set trans_en ='.$mdb2->quote(0, 'integer'))).'<br>');
159
    // subselect test
160
    $sub_select = $mdb2->subSelect('SELECT test_name from test WHERE test_name = '.$mdb2->quote('gummihuhn', 'text'), 'text');
161
    echo(Var_Dump($sub_select).'<br>');
162
    $query_with_subselect = 'SELECT * FROM test WHERE test_name IN ('.$sub_select.')';
163
    // run the query and get a result handler
164
    echo($query_with_subselect.'<br>');
165
    $result = $mdb2->query($query_with_subselect);
166
    $array = $result->fetchAll();
167
    $result->free();
168
    echo('<br>all with subselect:<br>');
169
    echo('<br>drop index (will fail if the index was never created):<br>');
170
    echo(Var_Dump($mdb2->manager->dropIndex('test', 'test_id_index')).'<br>');
171
    $index_def = array(
172
        'fields' => array(
173
            'test_id' => array(
174
                'sorting' => 'ascending'
175
            )
176
        )
177
    );
178
    echo('<br>create index:<br>');
179
    echo(Var_Dump($mdb2->manager->createIndex('test', 'test_id_index', $index_def)).'<br>');
180
 
181
    if ($mdb2_type == 'mysql') {
182
        $schema->db->setOption('debug', true);
183
        $schema->db->setOption('log_line_break', '<br>');
184
        // ok now lets create a new xml schema file from the existing DB
185
        $database_definition = $schema->getDefinitionFromDatabase();
186
        // we will not use the 'metapear_test_db.schema' for this
187
        // this feature is especially interesting for people that have an existing Db and want to move to MDB2's xml schema management
188
        // you can also try MDB2_MANAGER_DUMP_ALL and MDB2_MANAGER_DUMP_CONTENT
189
        echo(Var_Dump($schema->dumpDatabase(
190
            $database_definition,
191
            array(
192
                'output_mode' => 'file',
193
                'output' => $mdb2_name.'2.schema'
194
            ),
195
            MDB2_SCHEMA_DUMP_STRUCTURE
196
        )).'<br>');
197
        if ($schema->db->getOption('debug') === true) {
198
            echo($schema->db->getDebugOutput().'<br>');
199
        }
200
        // this is the database definition as an array
201
        echo(Var_Dump($database_definition).'<br>');
202
    }
203
 
204
    echo('<br>just a simple delete query:<br>');
205
    echo(Var_Dump($mdb2->exec('DELETE FROM numbers')).'<br>');
206
    // You can disconnect from the database with:
207
    $mdb2->disconnect()
208
?>