user656925
user656925

Reputation:

how do I refactor this to make single function calls?

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');
        }
    }
}

Upvotes: 0

Views: 57

Answers (1)

Ricardo Souza
Ricardo Souza

Reputation: 16446

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');  
        }
    }
}

Upvotes: 1

Related Questions