Exemplo n.º 1
0
 /**
  * Prepare database connection.
  * 
  * @param string $dsn (default = SETTINGS_DSN)
  * @param map $opt (default = [ 'table' => 'language', 'language' => SETTINGS_LANGUAGE, 'default' => 'txt' ])
  * @param string $language (default = SETTINGS_LANGUAGE)
  */
 public function setDSN($dsn = SETTINGS_DSN, $opt = ['table' => 'language', 'use' => SETTINGS_LANGUAGE, 'default' => 'txt'])
 {
     $this->db = Database::getInstance($dsn);
     $query_map = ['@query_prefix' => '', 'escape_name@table' => $opt['table'], 'escape_name@use' => $opt['use'], 'escape_name@default' => $opt['default'], 'select' => "SELECT {:=@use} AS lang, {:=@default} AS default, txt FROM {:=@table} WHERE id='{:=id}'", 'insert' => "INSERT INTO {:=@table} (id, lchange, txt) VALUES ('{:=id}', NOW(), '{:=txt}')", 'delete' => "DELETE FROM {:=@table} WHERE id='{:=id}'"];
     $this->db->setQueryHash($query_map);
     $this->createTable($opt['table']);
 }
Exemplo n.º 2
0
    /**
     * Set database connection string. Change table name and add extra cols if necessary.
     * 
     * @param string $dsn
     * @param string $table (default = category)
     * @param map $extra_cols
     */
    public function setDSN($dsn, $table = 'category', $extra_cols = [])
    {
        $db = new Database();
        $db->setDSN($dsn);
        $table = ADatabase::escape_name($table);
        $db->setQuery('drop', "DROP TABLE IF EXISTS {$table}");
        $query = <<<END
CREATE TABLE {$table} (
id int NOT NULL AUTO_INCREMENT,
pid int,
name varchar(255) NOT NULL,

PRIMARY KEY (id),
FOREIGN KEY (pid) REFERENCES {$table}(id) ON DELETE CASCADE ON UPDATE CASCADE
);
END;
        $db->setQuery('create', $query);
        $db->setQuery('insert', "INSERT INTO {$table} (pid, name) VALUES ('{:=pid}', '{:=name}')");
        $db->setQuery('select_name_pid', "SELECT * FROM {$table} WHERE name='{:=name}' AND pid='{:=pid}'");
        $db->setQuery('select_id', "SELECT * FROM {$table} WHERE id='{:=id}'");
    }
Exemplo n.º 3
0
 /**
  * Set database connection string.
  *
  * @tok {sql_name:}a b{:sql_name} -> `a b`
  * @see ADatabase::setDSN()
  * @param string $arg
  * @return empty string
  */
 public function tok_sql_dsn($dsn)
 {
     $this->db = Database::getInstance($dsn);
     return '';
 }