| | |
| | | |
| | | $this->db_dsnw = $db_dsnw; |
| | | $this->db_dsnr = $db_dsnr; |
| | | |
| | | $dsn_array = DB::parseDSN($db_dsnw); |
| | | $this->db_provider = $dsn_array['phptype']; |
| | | } |
| | | |
| | | // PHP 4 compatibility |
| | |
| | | // Connect to specific database |
| | | function dsn_connect($dsn) |
| | | { |
| | | $dsn_array = DB::parseDSN($dsn); |
| | | $this->db_provider = $dsn_array['phptype']; |
| | | |
| | | // Use persistent connections if available |
| | | $dbh = DB::connect($dsn, array('persistent' => $true)); |
| | | |
| | | |
| | | if (DB::isError($dbh)) |
| | | raise_error(array('code' => 500, |
| | | 'type' => 'db', |
| | | 'line' => __LINE__, |
| | | 'file' => __FILE__, |
| | | 'message' => $dbh->getMessage()), TRUE, FALSE); |
| | | |
| | | else if ($this->db_provider=='sqlite') |
| | | { |
| | | if (!is_file($dsn_array['database']) || !filesize($dsn_array['database'])) |
| | | $this->_sqlite_create_database($dbh, 'SQL/sqlite.initial.sql'); |
| | | $dsn_array = DB::parseDSN($dsn); |
| | | if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials)) |
| | | $this->_sqlite_create_database($dbh, $this->sqlite_initials); |
| | | } |
| | | |
| | | return $dbh; |
| | |
| | | // Connect to appropiate databse |
| | | function db_connect ($mode) |
| | | { |
| | | $this->db_mode = $mode; |
| | | |
| | | // Already connected |
| | | |
| | | if ($this->db_connected) |
| | | { |
| | | // no replication, current connection is ok |
| | |
| | | |
| | | $this->db_handle = $this->dsn_connect($dsn); |
| | | $this->db_connected = true; |
| | | $this->db_mode = $mode; |
| | | } |
| | | |
| | | // Query database (read operations) |
| | |
| | | $result = $this->db_handle->query($query); |
| | | |
| | | if (DB::isError($result)) |
| | | raise_error(array('code' => 500, 'type' => 'db', |
| | | raise_error(array('code' => 500, |
| | | 'type' => 'db', |
| | | 'line' => __LINE__, |
| | | 'file' => __FILE__, |
| | | 'message' => $result->getMessage()), TRUE, FALSE); |
| | |
| | | |
| | | function db_execute ($query) |
| | | { |
| | | db_connect('w'); |
| | | $this->db_connect('w'); |
| | | |
| | | if ($this->db_provider == 'sqlite') |
| | | $query = $this->_sqlite_prepare_query($query); |
| | |
| | | |
| | | function insert_id($sequence = '') |
| | | { |
| | | if (!$this->db_link || $this->db_mode=='r') |
| | | if (!$this->db_handle || $this->db_mode=='r') |
| | | return FALSE; |
| | | |
| | | switch($this->db_provider) |
| | |
| | | case 'pgsql': |
| | | // PostgreSQL uses sequences |
| | | $result =& $this->db_handle->getOne("SELECT CURRVAL('$sequence')"); |
| | | if (DB::isError($result)) |
| | | if (DB::isError($result)) { |
| | | raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, |
| | | 'message' => $result->getMessage()), TRUE, TRUE); |
| | | } |
| | | return $result; |
| | | |
| | | case 'mysql': // This is unfortuneate |
| | | return mysql_insert_id($this->db_handle); |
| | | return mysql_insert_id(); |
| | | |
| | | case 'sqlite': |
| | | return sqlite_last_insert_rowid($this->db_handle->connection); |
| | |
| | | $result = $this->_get_result($res_id); |
| | | |
| | | if (DB::isError($result)) |
| | | { |
| | | raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, |
| | | 'message' => $this->db_link->getMessage()), TRUE, TRUE); |
| | | 'message' => $this->db_link->getMessage()), TRUE, FALSE); |
| | | return FALSE; |
| | | } |
| | | |
| | | return $result->fetchRow(DB_FETCHMODE_ASSOC); |
| | | } |
| | |
| | | if (empty($fileName) || !is_string($fileName)) |
| | | return ; |
| | | |
| | | $fd = fopen($fileName, 'r'); |
| | | if (!$fd) |
| | | return ; |
| | | |
| | | $data = ''; |
| | | while ($line = fgets($fd, 4096)) |
| | | $data .= $line; |
| | | if ($fd = fopen($fileName, 'r')) |
| | | { |
| | | $data = fread($fd, filesize($fileName)); |
| | | fclose($fd); |
| | | } |
| | | |
| | | fclose($fd); |
| | | sqlite_exec($dbh->connection, $data); |
| | | if (strlen($data)) |
| | | sqlite_exec($dbh->connection, $data); |
| | | } |
| | | |
| | | // transform a query so that it is sqlite2 compliant |