/** * Given a table name, a two-dimensional array of data, and a database connection, * creates a table in the database. The array of data should look something like this. * * $testdata = array( * array('id', 'username', 'firstname', 'lastname', 'email'), * array(1, 'u1', 'user', 'one', '*****@*****.**'), * array(2, 'u2', 'user', 'two', '*****@*****.**'), * array(3, 'u3', 'user', 'three', '*****@*****.**'), * array(4, 'u4', 'user', 'four', '*****@*****.**'), * array(5, 'u5', 'user', 'five', '*****@*****.**'), * ); * * The first 'row' of the test data gives the column names. The type of each column * is set to either INT or VARCHAR($strlen), guessed by inspecting the first row of * data. Unless the col name is 'id' in which case the col type will be SERIAL. * The remaining 'rows' of the data array are values loaded into the table. All columns * are created with a default of 0xdefa or 'Default' as appropriate. * * This function should not be used in real code. Only for testing and debugging. * * @param string $tablename the name of the table to create. E.g. 'mdl_unittest_user'. * @param array $data a two-dimensional array of data, in the format described above. * @param object $db an AdoDB database connection. * @param int $strlen the width to use for string fields. */ function load_test_table($tablename, $data, $db = null, $strlen = 255) { global $CFG; $colnames = array_shift($data); $coldefs = array(); foreach (array_combine($colnames, $data[0]) as $colname => $value) { if ($colname == 'id') { switch ($CFG->dbfamily) { case 'mssql': $type = 'INTEGER IDENTITY(1,1)'; break; case 'oracle': $type = 'INTEGER'; break; default: $type = 'SERIAL'; } } else { if (is_int($value)) { $type = 'INTEGER DEFAULT 57082'; // 0xdefa if ($CFG->dbfamily == 'mssql') { $type = 'INTEGER NULL DEFAULT 57082'; } } else { $type = "VARCHAR({$strlen}) DEFAULT 'Default'"; if ($CFG->dbfamily == 'mssql') { $type = "VARCHAR({$strlen}) NULL DEFAULT 'Default'"; } else { if ($CFG->dbfamily == 'oracle') { $type = "VARCHAR2({$strlen}) DEFAULT 'Default'"; } } } } $coldefs[] = "{$colname} {$type}"; } _private_execute_sql("CREATE TABLE {$tablename} (" . join(',', $coldefs) . ');', $db); if ($CFG->dbfamily == 'oracle') { $sql = "CREATE SEQUENCE {$tablename}_id_seq;"; _private_execute_sql($sql, $db); $sql = "CREATE OR REPLACE TRIGGER {$tablename}_id_trg BEFORE INSERT ON {$tablename} FOR EACH ROW BEGIN IF :new.id IS NULL THEN SELECT {$tablename}_ID_SEQ.nextval INTO :new.id FROM dual; END IF; END; "; _private_execute_sql($sql, $db); } array_unshift($data, $colnames); load_test_data($tablename, $data, $db); }
* @uses collect(function($value){return $value+1}, range(1,5)); */ function collect($func, $array) { $out = array(); foreach ($array as $value) { array_push($out, $func($value)); } return $out; } require_once dirname(__FILE__) . '/config/db_connect.php'; //load the test database if (!defined('DATABASE_CREATED')) { $m = new Migration(); $m->drop_database(MYSQL_DATABASE); $m->create_database(MYSQL_DATABASE); define('DATABASE_CREATED', true); } //load the table if it hasn't been loaded if (!defined('TABLE_CREATED')) { $g = new TestMigration(); $g->down(); $g->up(); define('TABLE_CREATED', true); } load_test_data(); foreach (array('Tim', 'Steve', 'Joe', 'Bob', 'John', 'Scott', 'Randy', 'Jessica', 'Julie') as $user) { $func_name = strtolower($user); $func = "function {$func_name}() {\n\t\t\treturn User::_find_by(array('conditions' => \"name = '" . $user . "'\"));\n\t\t};"; eval($func); }
/** * Given a table name, a two-dimensional array of data, and a database connection, * creates a table in the database. The array of data should look something like this. * * $testdata = array( * array('id', 'username', 'firstname', 'lastname', 'email'), * array(1, 'u1', 'user', 'one', '*****@*****.**'), * array(2, 'u2', 'user', 'two', '*****@*****.**'), * array(3, 'u3', 'user', 'three', '*****@*****.**'), * array(4, 'u4', 'user', 'four', '*****@*****.**'), * array(5, 'u5', 'user', 'five', '*****@*****.**'), * ); * * The first 'row' of the test data gives the column names. The type of each column * is set to either INT or VARCHAR($strlen), guessed by inspecting the first row of * data. Unless the col name is 'id' in which case the col type will be SERIAL. * The remaining 'rows' of the data array are values loaded into the table. All columns * are created with a default of 0xdefa or 'Default' as appropriate. * * This function should not be used in real code. Only for testing and debugging. * * @param string $tablename the name of the table to create. E.g. 'mdl_unittest_user'. * @param array $data a two-dimensional array of data, in the format described above. * @param object $db an AdoDB database connection. * @param int $strlen the width to use for string fields. */ function load_test_table($tablename, $data, $db = null, $strlen = 255) { $colnames = array_shift($data); $coldefs = array(); foreach (array_combine($colnames, $data[0]) as $colname => $value) { if ($colname == 'id') { $type = 'SERIAL'; } else { if (is_int($value)) { $type = 'INTEGER DEFAULT 57082'; // 0xdefa } else { $type = "VARCHAR({$strlen}) DEFAULT 'Default'"; } } $coldefs[] = "{$colname} {$type}"; } _private_execute_sql("CREATE TABLE {$tablename} (" . join(',', $coldefs) . ');', $db); array_unshift($data, $colnames); load_test_data($tablename, $data, $db); }