Location: PHPKode > scripts > DATA > data/example.sqltypes.php
<?php
/**
 * @ignore 
 */

/**
 * @ignore
 */
require_once('data.mysql5.lib.php');

echo "Connecting to database...\n";
$DB->connect();
$DB->selectSchema('test');

echo "Creating test table...\n";
$DB->query('DROP TABLE IF EXISTS `test_table`');
$DB->query('
    CREATE TABLE `test_table` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(20) NOT NULL,
      `charfield` char(20) NOT NULL,
      `nullcharfield` char(20) NULL,
      `varcharfield` varchar(20) NOT NULL,
      `intfield` int NOT NULL,
      `nullintfield` int NULL,
      `smallintfield` smallint NOT NULL,
      `decimalfield` decimal(7,3) NOT NULL,
      `datefield` date NULL,
      `timefield` time NULL,
      `datetimefield` datetime NULL,
      PRIMARY KEY  (`id`),
      UNIQUE KEY `name` (`name`)
    )
');

echo "Insert valid row...\n";
$DB['test_table'][] = array(
    'name'          => 'test',
    'charfield'     => '20 char field',
    'nullcharfield' => null,
    'varcharfield'  => '20 varchar field',
    'intfield'      => 101,
    'nullintfield'  => null,
    'smallintfield' => 11,
    'decimalfield'  => '123.45',
    'datefield'     => DATA_SQLDate::today(),
    'timefield'     => DATA_SQLTime::now(),
    'datetimefield' => DATA_SQLDatetime::now(),
);
foreach ($DB['test_table']['test'] as $name => $value) {
    if (DATA_SQLType::isNull($value)) {
        echo "    $name: NULL\n";
    } else {
        echo "    $name: |$value|\n";
    }
}

echo "Insert invalid char(20) field...\n";
echo "  null value...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'charfield'     => null,
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}
echo "  'too much characters for this field'...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'charfield'     => 'too much characters for this field',
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}

echo "Insert invalid int field...\n";
echo "  null value...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'intfield'      => null,
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}
echo "  'not a number'...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'intfield'      => 'not a number',
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}
echo "  '12345678901234567890'...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'intfield'      => '12345678901234567890',
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}

echo "Insert invalid smallint field...\n";
echo "  '1234567'...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'smallintfield' => '1234567',
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}

echo "Insert invalid decimal(7,3) field...\n";
echo "  '12345'...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'decimalfield'  => '12345',
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}
echo "  '0.1234'...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'decimalfield'  => '0.1234',
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}

echo "Insert invalid date field...\n";
echo "  '2007-02-31'...\n";
try {
    $DB['test_table'][] = array(
        'name'          => 'test2',
        'datefield'     => '2007-02-31',
    );
} catch (DATA_SQLTypeConstraintFailed $e) {
    echo "    exception " . get_class($e) . " on table `" . $e->getTable() . "` and field `" . $e->getField() . "`\n";
}
?>
Return current item: DATA