如何重构它以进行单个函数调用?

发布于 2024-12-17 05:09:37 字数 1167 浏览 0 评论 0原文

我已经使用它一段时间了,根据需要更新 mysql。但是我不太确定语法......并且需要将 sql 迁移到数组。

这一行

database::query("CREATE TABLE $name($query)");

特别是“这是否翻译成

CREATE TABLE bookmark(name VARCHAR(64), url VARCHAR(256), tag VARCHAR(256), id INT)

这是我的……猜测” 。这是正确的吗?

class table extends database
{
    private function create($name, $query)
    {
        database::query("CREATE TABLE $name($query)");
    }

    public function make($type)
    {
        switch ($type) 
        {
            case "credentials":
                self::create('credentials', 'id INT NOT NULL AUTO_INCREMENT, flname VARCHAR(60), email VARCHAR(32), pass VARCHAR(40), PRIMARY KEY(id)');
                break;
            case "booomark":
                self::create('boomark', 'name VARCHAR(64), url VARCHAR(256), tag VARCHAR(256), id INT');
                break;
            case "tweet":
                self::create('tweet', 'time INT, fname VARCHAR(32), message VARCHAR(128), email VARCHAR(64)');
                break;
            default:
                throw new Exception('Invalid Table Type');
        }
    }
}

I've been using this for a while updating mysql as needed. However I'm not too sure on the syntax..and need to migrate the sql to an array.

Particulary the line

database::query("CREATE TABLE $name($query)");

Does this translate to

CREATE TABLE bookmark(name VARCHAR(64), url VARCHAR(256), tag VARCHAR(256), id INT)

This is my ...guess. Is this correct?

class table extends database
{
    private function create($name, $query)
    {
        database::query("CREATE TABLE $name($query)");
    }

    public function make($type)
    {
        switch ($type) 
        {
            case "credentials":
                self::create('credentials', 'id INT NOT NULL AUTO_INCREMENT, flname VARCHAR(60), email VARCHAR(32), pass VARCHAR(40), PRIMARY KEY(id)');
                break;
            case "booomark":
                self::create('boomark', 'name VARCHAR(64), url VARCHAR(256), tag VARCHAR(256), id INT');
                break;
            case "tweet":
                self::create('tweet', 'time INT, fname VARCHAR(32), message VARCHAR(128), email VARCHAR(64)');
                break;
            default:
                throw new Exception('Invalid Table Type');
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-12-24 05:09:37

如果我明白你想说的话,你所要做的就是将 create() 方法中的代码更改为类似于 make(),使 < code>create() 具有 make() 签名的公共方法,并从类中删除 make() 方法:

public function create($table_array)  
{  
    foreach($table_array as $table) {
        $type = $table["type"];

        switch ($type)   
        {  
            case "credentials":  
                database::query('CREATE TABLE credentials(id INT NOT NULL AUTO_INCREMENT, flname VARCHAR(60), email VARCHAR(32), pass VARCHAR(40), PRIMARY KEY(id))');  
                break;  
            case "booomark":  
                database::query('CREATE TABLE boomark(name VARCHAR(64), url VARCHAR(256), tag VARCHAR(256), id INT)');  
                break;  
            case "tweet":  
                database::query('CREATE TABLE tweet(time INT, fname VARCHAR(32), message VARCHAR(128), email VARCHAR(64))');  
                break;  
            default:  
                throw new Exception('Invalid Table Type');  
        }
    }
}

If I have understood what you want to say, all you have to do is change the code in the create() method to be similar to the make(), making create() a public method with the make() signature and removing the make() method from the class:

public function create($table_array)  
{  
    foreach($table_array as $table) {
        $type = $table["type"];

        switch ($type)   
        {  
            case "credentials":  
                database::query('CREATE TABLE credentials(id INT NOT NULL AUTO_INCREMENT, flname VARCHAR(60), email VARCHAR(32), pass VARCHAR(40), PRIMARY KEY(id))');  
                break;  
            case "booomark":  
                database::query('CREATE TABLE boomark(name VARCHAR(64), url VARCHAR(256), tag VARCHAR(256), id INT)');  
                break;  
            case "tweet":  
                database::query('CREATE TABLE tweet(time INT, fname VARCHAR(32), message VARCHAR(128), email VARCHAR(64))');  
                break;  
            default:  
                throw new Exception('Invalid Table Type');  
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文