2008/05/19 | Example PHP data class
类别(零零碎碎) | 评论(0) | 阅读(113) | 发表于 15:42

<?php
// Following class is generated by ds2class.tcl v1.0
class ExampleClass1 {
    public $ tablename = 'ExampleClass1';
    public $ id;
    public $ num;
    public $ time;
    public $ name;
    public $ xml;
    function ExampleClass1($ _id,$ _num,$ _time,$ _name,$ _xml){
        $ this->id = $ _id;
        $ this->num = $ _num;
        $ this->time = $ _time;
        $ this->name = $ _name;
        $ this->xml = $ _xml;
    }
    public function get_sql_for_create_table(){
        return "CREATE TABLE ExampleClass1 (id INT AUTO_INCREMENT UNIQUE NOT NULL,PRIMARY KEY(id),num INT DEFAULT '0' NOT NULL,time DATETIME NULL,name VARCHAR(12) CHARACTER SET utf8 NULL,xml TEXT CHARACTER SET utf8 NULL)";
    }
}

class ExampleClass1Agent {
    public $ tablename = 'ExampleClass1Agent';
    public function addEntity($ num, $ time, $ name, $ xml) {
        try {
            $ dbh = new PDO('mysql:host=localhost;dbname=test', 'new', '', array(
              PDO::ATTR_PERSISTENT => true
            ));
            $ stmt = $ dbh->prepare("INSERT INTO ExampleClass1 (num, time, name, xml) VALUES (?, ?, ?, ?)");
            if(1 == $ stmt->execute(array($ num, $ time, $ name, $ xml))){;
                $ stmt = $ dbh->query("SELECT LAST_INSERT_ID();");
                if(count($ stmt) == 1){
                    $ rst= $ stmt->fetch(PDO::FETCH_ASSOC);
                    if(count($ rst) == 1) return $ rst["LAST_INSERT_ID()"]; else return 0;
                } else return 0;
            } else return 0;
        } catch (PDOException $ e) {

            print "Error!: " . $ e->getMessage() . "<br/>";
            die();
        }
    }
    public function deleteEntityByID($ id) {
        try {
            $ dbh = new PDO('mysql:host=localhost;dbname=test', 'new', '', array(
              PDO::ATTR_PERSISTENT => true
            ));
            $ stmt = $ dbh->prepare("DELETE FROM ExampleClass1 WHERE id= ?");
            return $ stmt->execute(array($ id));
        } catch (PDOException $ e) {
            print "Error!: " . $ e->getMessage() . "<br/>";
            die();
        }
    }
    public function deleteEntityByConditions($ keyvalues) {
        if(!is_array($ keyvalues)) return false;
        $ mykeys = array();
        $ myvalues = array();
        foreach(array_keys($ keyvalues) as $ key) {$ mykeys[] = $ key."= ?";$ myvalues[]=$ keyvalues[$ key];}
        try {
            $ dbh = new PDO('mysql:host=localhost;dbname=test', 'new', '', array(
              PDO::ATTR_PERSISTENT => true
            ));
            $ stmt = $ dbh->prepare("DELETE FROM ExampleClass1 WHERE " . implode(" AND ", array_values($ mykeys)));
            return $ stmt->execute($ myvalues);
        } catch (PDOException $ e) {
            print "Error!: " . $ e->getMessage() . "<br/>";
            die();
        }
    }
    public function modifyEntityByID($ id, $ keyvalues) {
        if(!is_array($ keyvalues)) return 0;
        $ mykeys = array();
        $ myvalues = array();
        foreach(array_keys($ keyvalues) as $ key) {$ mykeys[] = $ key."= ?";$ myvalues[]=$ keyvalues[$ key];}
        $ myvalues[] = $ id;
        try {
            $ dbh = new PDO('mysql:host=localhost;dbname=test', 'new', '', array(
              PDO::ATTR_PERSISTENT => true
            ));
            $ stmt = $ dbh->prepare("UPDATE ExampleClass1 SET " . implode(",", array_values($ mykeys)) . " WHERE id= ?");
        $ mykeys[] = $ id;
            return $ stmt->execute($ myvalues);
        } catch (PDOException $ e) {
            print "Error!: " . $ e->getMessage() . "<br/>";
            die();
        }
    }
    public function selectEntityByID($ id) {
        try {
            $ dbh = new PDO('mysql:host=localhost;dbname=test', 'new', '', array(
              PDO::ATTR_PERSISTENT => true
            ));
            $ stmt = $ dbh->prepare("SELECT * FROM ExampleClass1 WHERE id= ?");
            $ stmt->execute(array($ id));
            $ result = $ stmt->fetch(PDO::FETCH_ASSOC);
            if("" == $ result)return;
            $ theitem = new ExampleClass1($ result['id'],$ result['num'],$ result['time'],$ result['name'],$ result['xml']);
            return $ theitem;
        } catch (PDOException $ e) {
            print "Error!: " . $ e->getMessage() . "<br/>";
            die();
        }
    }
    public function selectEntityByConditions($ keyvalues) {
        if(!is_array($ keyvalues)) return false;
        $ mykeys = array();
        $ myvalues = array();
        foreach(array_keys($ keyvalues) as $ key) {$ mykeys[] = $ key."= ?";$ myvalues[]=$ keyvalues[$ key];}
        try {
            $ dbh = new PDO('mysql:host=localhost;dbname=test', 'new', '', array(
              PDO::ATTR_PERSISTENT => true
            ));
            $ stmt = $ dbh->prepare("SELECT * FROM ExampleClass1 WHERE " . implode(" AND ", array_values($ mykeys)));
            $ stmt->execute($ myvalues);
            $ result = $ stmt->fetchAll();
            if(0 == count($ result))return;
            $ ret = array();
            foreach($ result as $ record){
                $ ret[] = new ExampleClass1($ record['id'],$ record['num'],$ record['time'],$ record['name'],$ record['xml']);
            }
            return $ ret;
        } catch (PDOException $ e) {
            print "Error!: " . $ e->getMessage() . "<br/>";
            die();
        }
    }
}

 

/*
class ExampleClass1Agent {
    public $ tablename = 'ExampleClass1Agent';
    public function addEntity($ num, $ time, $ name, $ xml) {
        try {
            $ dbh = new PDO('mysql:host=localhost;dbname=test', 'new', '', array(
              PDO::ATTR_PERSISTENT => true
            ));
            $ stmt = $ dbh->prepare("INSERT INTO ExampleClass1 (num, time, name, xml) VALUES (?, ?, ?, ?)");
            if(1 == $ stmt->execute(array($ num, $ time, $ name, $ xml))){
                $ stmt = $ dbh->query("SELECT LAST_INSERT_ID();");
                if(count($ stmt) == 1){
                    $ rst= $ stmt->fetch(PDO::FETCH_ASSOC);
                    if(count($ rst) == 1) return $ rst["LAST_INSERT_ID()"]; else return 0;
                } else return 0;
            } else {
                return 0;
            }
        } catch (PDOException $ e) {
            print "Error!: " . $ e->getMessage() . "<br/>";
            die();
        }
    }
    */

function test_function($ testnum, $ testline) {
    echo "<P>Test" . $ testnum . ": $ testline</p>";
    eval("\$ ret = " . $ testline);
    echo "<P style='COLOR:#339; FONT: 75% bold MS Sans Serif;'>Result: " . $ ret;
    return $ ret;
}

function test_function2($ testnum, $ testline) {
    echo "<P>Test" . $ testnum . ": $ testline</p>";
    eval("\$ ret = " . $ testline);
    echo "<P style='COLOR:#339; FONT: 75% bold MS Sans Serif;'>Result: ";
    if(is_array($ ret)){foreach($ ret as $ item) {print_r($ item); echo "<Br>";}
    } else {print_r($ ret);}
    echo "</P>";
}

 

//echo ExampleClass1::get_create_table_script();
echo "<H1>PHP Data Class Test</H1>";
echo "<Hr>";
echo "<H2>ExampleClass1</H2>";
$ example1 = new ExampleClass1('1','6','2008-5-14 23:40:00','cc','dd');
echo "<H3>Create Object</H3>";
print_r($ example1);
echo "<H3>Function: get_sql_for_create_table</H3>";
echo $ example1->get_sql_for_create_table();
echo "<H2>ExampleClass1Agent</H2>";

$ exampleagent1 = new ExampleClass1Agent();
echo "<H3>Create Object</H3>";
print_r($ exampleagent1);
echo "<H3>Function: addEntity</H3>";
test_function(1,"ExampleClass1Agent::addEntity('6','2008-5-14 23:40:00','cc','dd');");
$ newid1 = test_function(2,"ExampleClass1Agent::addEntity('','','','');");
$ newid2 = test_function(3,"ExampleClass1Agent::addEntity('x','2008-5-14 23:40:00','cc','dd');");
test_function(4,"ExampleClass1Agent::addEntity('6','2008-5-14','cc','dd');");
test_function(5,"ExampleClass1Agent::addEntity('6','23:40:00','cc','dd');");
test_function(6,"ExampleClass1Agent::addEntity('6','2008-5-14 23:40:00','aaabbbcccdddeeefff','dd');");

echo "<H3>Function: modifyEntityByID</H3>";
test_function(1,"ExampleClass1Agent::modifyEntityByID('10',array('name' => 'i`m changed.', 'xml' => '<xx />'));");
test_function(2,"ExampleClass1Agent::modifyEntityByID('10',array(''));");
test_function(3,"ExampleClass1Agent::modifyEntityByID('9999',array('name' => 'i`m changed.'));");

echo "<H3>Function: deleteEntityByID</H3>";
test_function2(1,"ExampleClass1Agent::deleteEntityByID($ newid1);");
test_function2(2,"ExampleClass1Agent::deleteEntityByID($ newid2);");
test_function2(3,"ExampleClass1Agent::deleteEntityByID();");
test_function2(4,"ExampleClass1Agent::deleteEntityByID('');");
test_function2(5,"ExampleClass1Agent::deleteEntityByID('999999');");

echo "<H3>Function: deleteEntityByConditions</H3>";
test_function(1,"ExampleClass1Agent::deleteEntityByConditions(array('num' => '6', 'name' => 'cc'));");
test_function(2,"ExampleClass1Agent::deleteEntityByConditions(array());");

echo "<H3>Function: selectEntityByID</H3>";
test_function2(1,"ExampleClass1Agent::selectEntityByID('7');");
test_function2(2,"ExampleClass1Agent::selectEntityByID('999999');");

echo "<H3>Function: selectEntityByConditions</H3>";
test_function2(1,"ExampleClass1Agent::selectEntityByConditions(array('(id>80)' => '1','(id<100)' => '1'));");
test_function2(2,"ExampleClass1Agent::selectEntityByConditions(array());");
test_function2(3,"ExampleClass1Agent::selectEntityByConditions(array('xxx' => 'yyy'));");

//echo ExampleClass1Agent::addEntity('6','2008-5-14 23:40:00','cc','dd');
//echo ExampleClass1Agent::modifyEntityByID('10',array('name' => 'newton', 'xml' => "<main />"));
//echo ExampleClass1Agent::deleteEntityByConditions(array('num' => '25', 'name' => "Test It"));
?>

PHP Data Class Test


ExampleClass1

Create Object

ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 1 [num] => 6 [time] => 2008-5-14 23:40:00 [name] => cc [xml] => dd )

Function: get_sql_for_create_table

CREATE TABLE ExampleClass1 (id INT AUTO_INCREMENT UNIQUE NOT NULL,PRIMARY KEY(id),num INT DEFAULT '0' NOT NULL,time DATETIME NULL,name VARCHAR(12) CHARACTER SET utf8 NULL,xml TEXT CHARACTER SET utf8 NULL)

ExampleClass1Agent

Create Object

ExampleClass1Agent Object ( [tablename] => ExampleClass1Agent )

Function: addEntity

Test1: ExampleClass1Agent::addEntity('6','2008-5-14 23:40:00','cc','dd');

Result: 320

Test2: ExampleClass1Agent::addEntity('','','','');

Result: 321

Test3: ExampleClass1Agent::addEntity('x','2008-5-14 23:40:00','cc','dd');

Result: 322

Test4: ExampleClass1Agent::addEntity('6','2008-5-14','cc','dd');

Result: 323

Test5: ExampleClass1Agent::addEntity('6','23:40:00','cc','dd');

Result: 324

Test6: ExampleClass1Agent::addEntity('6','2008-5-14 23:40:00','aaabbbcccdddeeefff','dd');

Result: 325

Function: modifyEntityByID

Test1: ExampleClass1Agent::modifyEntityByID('10',array('name' => 'i`m changed.', 'xml' => ''));

Result: 1

Test2: ExampleClass1Agent::modifyEntityByID('10',array(''));

Result:

Test3: ExampleClass1Agent::modifyEntityByID('9999',array('name' => 'i`m changed.'));

Result: 1

Function: deleteEntityByID

Test1: ExampleClass1Agent::deleteEntityByID(321);

Result: 1

Test2: ExampleClass1Agent::deleteEntityByID(322);

Result: 1

Test3: ExampleClass1Agent::deleteEntityByID();

Result: 1

Test4: ExampleClass1Agent::deleteEntityByID('');

Result: 1

Test5: ExampleClass1Agent::deleteEntityByID('999999');

Result: 1

Function: deleteEntityByConditions

Test1: ExampleClass1Agent::deleteEntityByConditions(array('num' => '6', 'name' => 'cc'));

Result: 1

Test2: ExampleClass1Agent::deleteEntityByConditions(array());

Result:

Function: selectEntityByID

Test1: ExampleClass1Agent::selectEntityByID('7');

Result: ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 7 [num] => 22 [time] => 0000-00-00 00:00:00 [name] => Test It [xml] => )

Test2: ExampleClass1Agent::selectEntityByID('999999');

Result:

Function: selectEntityByConditions

Test1: ExampleClass1Agent::selectEntityByConditions(array('(id>80)' => '1','(id<100)' => '1'));

Result: ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 81 [num] => 6 [time] => 2008-05-14 23:40:00 [name] => aaabbbcccddd [xml] => dd )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 83 [num] => 0 [time] => 0000-00-00 00:00:00 [name] => [xml] => )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 84 [num] => 0 [time] => 2008-05-14 23:40:00 [name] => cc [xml] => dd )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 87 [num] => 6 [time] => 2008-05-14 23:40:00 [name] => aaabbbcccddd [xml] => dd )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 89 [num] => 0 [time] => 0000-00-00 00:00:00 [name] => [xml] => )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 90 [num] => 0 [time] => 2008-05-14 23:40:00 [name] => cc [xml] => dd )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 93 [num] => 6 [time] => 2008-05-14 23:40:00 [name] => aaabbbcccddd [xml] => dd )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 95 [num] => 0 [time] => 0000-00-00 00:00:00 [name] => [xml] => )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 96 [num] => 0 [time] => 2008-05-14 23:40:00 [name] => cc [xml] => dd )
ExampleClass1 Object ( [tablename] => ExampleClass1 [id] => 99 [num] => 6 [time] => 2008-05-14 23:40:00 [name] => aaabbbcccddd [xml] => dd )

Test2: ExampleClass1Agent::selectEntityByConditions(array());

Result:

Test3: ExampleClass1Agent::selectEntityByConditions(array('xxx' => 'yyy'));

Result:

0

评论Comments