/[drupal]/contributions/sandbox/crell/pdo/test.php
ViewVC logotype

Contents of /contributions/sandbox/crell/pdo/test.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Fri Feb 15 06:04:20 2008 UTC (21 months, 1 week ago) by crell
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +7 -1 lines
File MIME type: text/x-php
- Confirm add and drop fields.
1 <?php
2
3 // Load the library.
4 require_once('database.inc');
5
6 /**
7 * Some testing and demo code.
8 */
9
10 /**
11 * The $db_url array, which defines our various and sundry connections.
12 */
13 /*
14 $db_url['default']['default'] = 'mysql://test:test@localhost/drupal6test';
15 $db_url['default']['slave'][] = 'mysql://test:test@localhost/drupal6test';
16 $db_url['default']['slave'][] = 'mysql://test:test@localhost/drupal6test';
17 $db_url['default']['slave'][] = 'mysql://test:test@localhost/drupal6test';
18 */
19
20 $databases['default']['default'] = array(
21 'driver' => 'mysql',
22 'username' => 'test',
23 'password' => 'test',
24 'host' => 'localhost',
25 'database' => 'drupal6test',
26 'transactions' => FALSE,
27 );
28 $databases['default']['slave'][] = array(
29 'driver' => 'mysql',
30 'username' => 'test',
31 'password' => 'test',
32 'host' => 'localhost',
33 'database' => 'drupal6test',
34 'transactions' => FALSE,
35 );
36 $databases['default']['slave'][] = array(
37 'driver' => 'mysql',
38 'username' => 'test',
39 'password' => 'test',
40 'host' => 'localhost',
41 'database' => 'drupal6test',
42 );
43 $databases['default']['cache'] = array(
44 'driver' => 'sqlite',
45 'file' => 'cachedb.sqlite',
46 );
47
48
49
50 class Dummy { }
51
52 db_query("SELECT * FROM {system} WHERE status=:status", array(':status' => 1), array('target' => 'slave', 'fetch' => 'Dummy'));
53
54 $result = db_query("SELECT * FROM {system} WHERE status=:status", array(':status' => 1));
55 foreach ($result as $record) {
56 //debug($record);
57 }
58
59 $result2 = db_query("SELECT * FROM {system} WHERE status=:status", array(':status' => 1), array('target' => 'slave'));
60 //debug($result2->fetchAll());
61
62 $result3 = db_query("SELECT * FROM {system} WHERE status='%s'", array(1));
63 //debug($result3->fetchAll());
64
65 $ret = array();
66
67 $schema['test'] = array(
68 'fields' => array(
69 'id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
70 'age' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
71 ),
72 'primary key' => array('id'),
73 'unique keys' => array(
74 'age' => array('age')
75 ),
76 'indexes' => array(
77 'ages' => array('age'),
78 ),
79 );
80
81 if (db_table_exists('test')) {
82 db_drop_table($ret, 'test');
83 }
84
85 foreach ($schema as $name => $data) {
86 db_create_table($ret, $name, $data);
87 }
88
89 $spec = array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '');
90 db_add_field($ret, 'test', 'name', $spec);
91
92 debug($ret);
93
94 /*
95 //db_query("DROP TABLE IF EXISTS {test}");
96 db_query("CREATE TABLE {test} (
97 id INT AUTO_INCREMENT NOT NULL,
98 name VARCHAR(255) NOT NULL DEFAULT '',
99 age INT NOT NULL DEFAULT 0,
100 PRIMARY KEY (id)
101 );");
102 */
103
104 debug(db_table_exists('test'));
105
106 db_insert('test_id', 'test', array('delay' => TRUE))->fields(array('name' => 'Larry' , 'age' => 26))->execute();
107 debug(db_insert('test_id', 'test')->fields(array('name' => 'George' , 'age' => 28))->execute());
108 db_insert('test_id', 'test')->fields(array('name' => 'Dan' , 'age' => 24))->execute();
109 db_insert('test_id', 'test')->fields(array('name' => 'Robert' , 'age' => 22))->fields(array('name' => 'Bill' , 'age' => 20))->execute();
110
111 //debug(db_query("SELECT * FROM {test}")->fetchAll());
112
113 //debug(db_delete('test', array('age' => 24)));
114
115 //debug(db_query("SELECT * FROM {test}")->fetchAll());
116
117 $fields['name'] = 'Jimmy';
118 /*$where = array(
119 'name' => 'Larry',
120 'not' => array('age' => 26),
121 'or' => array('age' => 20)
122 );*/
123
124 debug(db_update('test_id', 'test')->fields($fields)->condition('name', 'Larry')->execute());
125
126 debug(db_delete('test_id', 'test')->condition('name', 'Dan')->execute());
127
128
129 $ret = array();
130 db_drop_field($ret, 'test', 'name');
131 debug($ret);
132
133 //$where = array('<' => array('age' => 26));
134 //debug(db_update('test', $fields, $where));
135
136 //debug(db_query_range("SELECT * FROM {test} WHERE age >= ? ORDER BY age", array(20), 0, 3)->fetchAll());
137
138 //db_query_temporary("SELECT * FROM {test} WHERE age >= ? ORDER BY age", array(24), 'temp');
139 //debug(db_query("SHOW TABLES")->fetchAll());
140 //debug(db_query("SELECT * from temp"));
141
142
143 //$fields = array('name' => 'Jimmy' , 'age' => 21);
144 //$where = array('id' => 1);
145 //db_replace('test', $fields, $where);
146
147 //debug(db_query("SELECT * FROM {test}")->fetchAll());
148
149 //debug(db_query_range("SELECT * FROM {test} WHERE age >= ? ORDER BY age", array(20), 0, 3));
150
151 //$where = array('name' => 'Larry' , 'not' => array('age' => 26) , 'or' => array('age' => 20 , 'name' => 'Bob') , '<' => array('age' => 26) , 'name' => array('John' , 'Paul' , 'George' , 'Ringo') , 'NOT' => array('or' => array('age' => 24 , 'name' => 'George' , '>=' => array('age' => 20))));
152
153 //debug(db_query("SELECT * FROM {system} WHERE name='user'")->fetchOne());
154
155 //debug(db_query("SELECT * FROM {system} WHERE type='module'")->fetchAssoc('name'));
156
157 //debug(db_query("SELECT * FROM {system} WHERE type='module'", array(), array('fetch'=>'Dummy'))->fetchAll());
158
159 $uid = 123;
160 $query = db_select('tracker_pages')
161 ->distinct()
162 ->fields('n.nid', 'n.title', 'n.type', 'n.changed', 'n.uid', 'u.name', 'GREATEST(n.changed, l.last_comment_timestamp) AS last_updated', 'l.comment_count')
163 ->join('node', 'n')
164 ->join('node_comment_statistics', 'l', 'n.nid = l.nid')
165 ->join('users', 'u', 'n.uid = u.uid')
166 ->leftJoin('comments', 'c', 'n.nid = c.nid AND (c.status = :c_status OR c.status IS NULL)')
167 ->condition('n.status', 1)
168 ->where('n.status = :status', array(':status' => 2))
169 ->condition(db_or()->condition('c.uid', $uid)->condition('n.uid', $uid))
170 //->removeCondition('n.status')
171 ->condition('n.promote', 'IN', array(0, 1, 2))
172 ->condition('n.comment', 'IN', array(5, 6))
173 ->orderBy('last_updated', 'DESC')
174 //->execute()
175 ;
176
177 print $query;
178 print "\n";

  ViewVC Help
Powered by ViewVC 1.1.2