public function Connect() { // Lookup Adapter-Specific Connection Properties $strServer = $this->Server; $strName = $this->Database; $strUsername = $this->Username; $strPassword = $this->Password; $strPort = $this->Port; //echo "DB vars: ".$this->__get('Username'); // Connect to the Database Server $this->objOracle = ociplogon($strUsername, $strPassword, "{$strServer}/{$strName}", 'UTF8'); if (!$this->objOracle) { throw new QOracleDatabaseException("Unable to connect to Database", -1, null); } if ($objOracle_error = oci_error()) { throw new QOracleDatabaseException($objOracle_error['message'], $objOracle_error['code'], null); } // Set to AutoCommit //$this->NonQuery('SET AUTOCOMMIT=1;'); //Set SORT with special characteres //$this->NonQuery("ALTER SESSION SET NLS_SORT=SPANISH"); // Update "Connected" Flag $this->blnConnectedFlag = true; $this->NonQuery("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD hh24:mi:ss'"); //$this->NonQuery("ALTER SESSION SET NLS_LANGUAGE='AMERICAN'"); // to use . instead of , for floating point //$this->NonQuery("ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,'"); }
/** * Connect to server */ function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) { $this->persistency = $persistency; $this->user = $sqluser; $this->server = $sqlserver . ($port ? ':' . $port : ''); $this->dbname = $database; $this->db_connect_id = $this->persistency ? @ociplogon($this->user, $sqlpassword, $this->server) : @ocinlogon($this->user, $sqlpassword, $this->server); return $this->db_connect_id ? $this->db_connect_id : $this->sql_error(''); }
function db_connect() { global $user,$pwd,$sid; $conn=@ociplogon($user,$pwd,$sid); if(!$conn) trigger_error(db_error($conn),E_USER_ERROR); $stmt=ociparse($conn,"alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'"); ociexecute($stmt); ocifreestatement($stmt); return $conn; }
/** * Open a connection to db */ public function connect() { $this->conn = @ociplogon($this->user, $this->pwd, $this->sid); if (!$this->conn) { trigger_error($this->lastError(), E_USER_ERROR); } $stmt = ociparse($this->conn, "alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'"); ociexecute($stmt); ocifreestatement($stmt); return $this->conn; }
function da_sql_pconnect($config) { if ($config[sql_use_http_credentials] == 'yes') { global $HTTP_SERVER_VARS; $SQL_user = $HTTP_SERVER_VARS["PHP_AUTH_USER"]; $SQL_passwd = $HTTP_SERVER_VARS["PHP_AUTH_PW"]; } else { $SQL_user = $config[sql_username]; $SQL_passwd = $config[sql_password]; } $link = @ociplogon($SQL_user, $SQL_passwd, $config[sql_database]); $res = @da_sql_query($link, $config, "ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS.FF TZH:TZM'"); return $link; }
/** * Connect to server */ function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) { $this->persistency = $persistency; $this->user = $sqluser; $this->server = $sqlserver . ($port ? ':' . $port : ''); $this->dbname = $database; $connect = $database; // support for "easy connect naming" if ($sqlserver !== '' && $sqlserver !== '/') { if (substr($sqlserver, -1, 1) == '/') { $sqlserver == substr($sqlserver, 0, -1); } $connect = $sqlserver . ($port ? ':' . $port : '') . '/' . $database; } $this->db_connect_id = $new_link ? @ocinlogon($this->user, $sqlpassword, $connect, 'UTF8') : ($this->persistency ? @ociplogon($this->user, $sqlpassword, $connect, 'UTF8') : @ocilogon($this->user, $sqlpassword, $connect, 'UTF8')); return $this->db_connect_id ? $this->db_connect_id : $this->sql_error(''); }
/** * {@inheritDoc} */ function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false) { $this->persistency = $persistency; $this->user = $sqluser; $this->server = $sqlserver . ($port ? ':' . $port : ''); $this->dbname = $database; $connect = $database; // support for "easy connect naming" if ($sqlserver !== '' && $sqlserver !== '/') { if (substr($sqlserver, -1, 1) == '/') { $sqlserver == substr($sqlserver, 0, -1); } $connect = $sqlserver . ($port ? ':' . $port : '') . '/' . $database; } if ($new_link) { if (!function_exists('ocinlogon')) { $this->connect_error = 'ocinlogon function does not exist, is oci extension installed?'; return $this->sql_error(''); } $this->db_connect_id = @ocinlogon($this->user, $sqlpassword, $connect, 'UTF8'); } else { if ($this->persistency) { if (!function_exists('ociplogon')) { $this->connect_error = 'ociplogon function does not exist, is oci extension installed?'; return $this->sql_error(''); } $this->db_connect_id = @ociplogon($this->user, $sqlpassword, $connect, 'UTF8'); } else { if (!function_exists('ocilogon')) { $this->connect_error = 'ocilogon function does not exist, is oci extension installed?'; return $this->sql_error(''); } $this->db_connect_id = @ocilogon($this->user, $sqlpassword, $connect, 'UTF8'); } } return $this->db_connect_id ? $this->db_connect_id : $this->sql_error(''); }
ocidefinebyname(); ocierror(); ociexecute(); ocifetch(); ocifetchinto(); ocifetchstatement(); ocifreecollection(); ocifreecursor(); ocifreedesc(); ocifreestatement(); ociinternaldebug(); ociloadlob(); ocilogoff(); ocilogon(); ocinewcollection(); ocinewcursor(); ocinewdescriptor(); ocinlogon(); ocinumcols(); ociparse(); ociplogon(); ociresult(); ocirollback(); ocirowcount(); ocisavelob(); ocisavelobfile(); ociserverversion(); ocisetprefetch(); ocistatementtype(); ociwritelobtofile(); ociwritetemporarylob();
/** * Persistent database connection * * @access private called by the base class * @return resource */ function db_pconnect() { return @ociplogon($this->username, $this->password, $this->hostname); }
function DBconnect(&$error) { $result = true; global $DB; $DB['DB'] = null; $DB['TRANSACTIONS'] = 0; //Stats $DB['SELECT_COUNT'] = 0; $DB['EXECUTE_COUNT'] = 0; //SDI('type: '.$DB['TYPE'].'; server: '.$DB['SERVER'].'; port: '.$DB['PORT'].'; db: '.$DB['DATABASE'].'; usr: '******'USER'].'; pass: '******'PASSWORD']); if (!isset($DB['TYPE'])) { $error = "Unknown database type."; $result = false; } else { switch ($DB['TYPE']) { case 'MYSQL': $mysql_server = $DB['SERVER'] . (!empty($DB['PORT']) ? ':' . $DB['PORT'] : ''); if (!($DB['DB'] = mysql_pconnect($mysql_server, $DB['USER'], $DB['PASSWORD']))) { $error = 'Error connecting to database [' . mysql_error() . ']'; $result = false; } else { if (!mysql_select_db($DB['DATABASE'])) { $error = 'Error database selection [' . mysql_error() . ']'; $result = false; } } break; case 'POSTGRESQL': $pg_connection_string = (!empty($DB['SERVER']) ? 'host=\'' . $DB['SERVER'] . '\' ' : '') . 'dbname=\'' . $DB['DATABASE'] . '\' ' . (!empty($DB['USER']) ? 'user=\'' . $DB['USER'] . '\' ' : '') . (!empty($DB['PASSWORD']) ? 'password=\'' . $DB['PASSWORD'] . '\' ' : '') . (!empty($DB['PORT']) ? 'port=' . $DB['PORT'] : ''); $DB['DB'] = pg_connect($pg_connection_string); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'ORACLE': $DB['DB'] = ociplogon($DB['USER'], $DB['PASSWORD'], $DB['DATABASE']); // $DB['DB']= ociplogon($DB['USER'], $DB['PASSWORD'], '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.$DB['SERVER'].')(PORT=1521))(CONNECT_DATA=(SERVICE_NAME='.$DB['DATABASE'].')))'); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'SQLITE3': $DB['TRANSACTIONS'] = 0; if (!function_exists('init_db_access')) { function init_db_access() { global $DB, $ZBX_SEM_ID; $ZBX_SEM_ID = false; if (function_exists('ftok') && function_exists('sem_get') && file_exists($DB['DATABASE'])) { $ZBX_SEM_ID = sem_get(ftok($DB['DATABASE'], 'z'), 1); } } } if (!function_exists('lock_db_access')) { function lock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_acquire')) { sem_acquire($ZBX_SEM_ID); } } } if (!function_exists('unlock_db_access')) { function unlock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_release')) { sem_release($ZBX_SEM_ID); } } } if (!function_exists('free_db_access')) { function free_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_remove')) { sem_remove($ZBX_SEM_ID); } $ZBX_SEM_ID = false; } } if (file_exists($DB['DATABASE'])) { $DB['DB'] = sqlite3_open($DB['DATABASE']); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } } else { $error = 'Missed database'; $result = false; } init_db_access(); break; default: $error = 'Unsupported database'; $result = false; } } if (false == $result) { $DB['DB'] = null; } return $result; }
/** * Makes a persistent database connection. * Makes a persistent database connection. * @access public * @return mixed The connection's link identifier, if successful; False, otherwise. */ function pConnect() { // if connected, need to disconnect first if ($this->isConnected()) { return false; } $linkId = ociplogon($this->_dbUser, $this->_dbPass, $this->_dbName); // see if successful if ($linkId) { // reset the query counters $this->_successfulQueries = 0; $this->_failedQueries = 0; $this->_linkId = $linkId; return $linkId; } else { throw new ConnectionDatabaseException($this->getConnectionErrorInfo() . "Cannot connect to database."); $this->_linkId = false; return false; } }
/** * Connects to the database using options in the given configuration array. * * @return boolean True if the database could be connected, else false * @access public */ function connect() { $config = $this->config; $this->connected = false; $config['charset'] = !empty($config['charset']) ? $config['charset'] : null; if (!$config['persistent']) { $this->connection = @ocilogon($config['login'], $config['password'], $config['database'], $config['charset']); } else { $this->connection = @ociplogon($config['login'], $config['password'], $config['database'], $config['charset']); } if ($this->connection) { $this->connected = true; if (!empty($config['nls_sort'])) { $this->execute('ALTER SESSION SET NLS_SORT=' . $config['nls_sort']); } if (!empty($config['nls_comp'])) { $this->execute('ALTER SESSION SET NLS_COMP=' . $config['nls_comp']); } $this->execute("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'"); } else { $this->connected = false; $this->_setError(); return false; } return $this->connected; }
function DBconnect(&$error) { $result = true; global $DB; $DB['DB'] = null; $DB['TRANSACTIONS'] = 0; //Stats $DB['SELECT_COUNT'] = 0; $DB['EXECUTE_COUNT'] = 0; //SDI('type: '.$DB['TYPE'].'; server: '.$DB['SERVER'].'; port: '.$DB['PORT'].'; db: '.$DB['DATABASE'].'; usr: '******'USER'].'; pass: '******'PASSWORD']); if (!isset($DB['TYPE'])) { $error = "Unknown database type."; $result = false; } else { $DB['TYPE'] = zbx_strtoupper($DB['TYPE']); switch ($DB['TYPE']) { case 'MYSQL': $mysql_server = $DB['SERVER'] . (!empty($DB['PORT']) ? ':' . $DB['PORT'] : ''); if (!($DB['DB'] = mysql_connect($mysql_server, $DB['USER'], $DB['PASSWORD']))) { $error = 'Error connecting to database [' . mysql_error() . ']'; $result = false; } else { if (!mysql_select_db($DB['DATABASE'])) { $error = 'Error database in selection [' . mysql_error() . ']'; $result = false; } else { DBexecute('SET NAMES utf8'); DBexecute('SET CHARACTER SET utf8'); } } break; case 'POSTGRESQL': $pg_connection_string = (!empty($DB['SERVER']) ? 'host=\'' . $DB['SERVER'] . '\' ' : '') . 'dbname=\'' . $DB['DATABASE'] . '\' ' . (!empty($DB['USER']) ? 'user=\'' . $DB['USER'] . '\' ' : '') . (!empty($DB['PASSWORD']) ? 'password=\'' . $DB['PASSWORD'] . '\' ' : '') . (!empty($DB['PORT']) ? 'port=' . $DB['PORT'] : ''); $DB['DB'] = pg_connect($pg_connection_string); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'ORACLE': $connect = ''; if (!empty($DB['SERVER'])) { $connect = '//' . $DB['SERVER']; if ($DB['PORT'] != '0') { $connect .= ':' . $DB['PORT']; } if ($DB['DATABASE']) { $connect .= '/' . $DB['DATABASE']; } } $DB['DB'] = ociplogon($DB['USER'], $DB['PASSWORD'], $connect); // $DB['DB']= ociplogon($DB['USER'], $DB['PASSWORD'], '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.$DB['SERVER'].')(PORT=1521))(CONNECT_DATA=(SERVICE_NAME='.$DB['DATABASE'].')))'); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'IBM_DB2': $connect = ''; $connect .= 'DATABASE=' . $DB['DATABASE'] . ';'; $connect .= 'HOSTNAME=' . $DB['SERVER'] . ';'; $connect .= 'PORT=' . $DB['PORT'] . ';'; $connect .= 'PROTOCOL=TCPIP;'; $connect .= 'UID=' . $DB['USER'] . ';'; $connect .= 'PWD=' . $DB['PASSWORD'] . ';'; $DB['DB'] = db2_connect($connect, $DB['USER'], $DB['PASSWORD']); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } else { $options = array('db2_attr_case' => DB2_CASE_LOWER); db2_set_option($DB['DB'], $options, 1); if (isset($DB['SCHEMA']) && $DB['SCHEMA'] != '') { DBexecute("SET CURRENT SCHEMA='" . $DB['SCHEMA'] . "'"); } } break; case 'SQLITE3': $DB['TRANSACTIONS'] = 0; if (!function_exists('init_db_access')) { function init_db_access() { global $DB, $ZBX_SEM_ID; $ZBX_SEM_ID = false; if (function_exists('ftok') && function_exists('sem_get') && file_exists($DB['DATABASE'])) { $ZBX_SEM_ID = sem_get(ftok($DB['DATABASE'], 'z'), 1); } } } if (!function_exists('lock_db_access')) { function lock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_acquire')) { sem_acquire($ZBX_SEM_ID); } } } if (!function_exists('unlock_db_access')) { function unlock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_release')) { sem_release($ZBX_SEM_ID); } } } if (!function_exists('free_db_access')) { function free_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_remove')) { sem_remove($ZBX_SEM_ID); } $ZBX_SEM_ID = false; } } if (file_exists($DB['DATABASE'])) { $DB['DB'] = sqlite3_open($DB['DATABASE']); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } } else { $error = 'Missing database'; $result = false; } init_db_access(); break; default: $error = 'Unsupported database'; $result = false; } } if (false == $result) { $DB['DB'] = null; } return $result; }
/** * Get a database connection. This function shouldn't be used by you, as a connection to the database is established automatically. * * @param boolean Whether to create a persistant connection * @param string The database name * @param string The database host (the server) * @param string The database connection username * @param string The database connection password * @param boolean Whether to on error echo an error and return with a NULL, rather than giving a critical error * @return ?array A database connection (NULL: failed) */ function db_get_connection($persistent, $db_name, $db_host, $db_user, $db_password, $fail_ok = false) { if ($db_host != 'localhost') { fatal_exit(do_lang_tempcode('ONLY_LOCAL_HOST_FOR_TYPE')); } // Potential cacheing global $CACHE_DB; if (isset($CACHE_DB[$db_name][$db_host])) { return $CACHE_DB[$db_name][$db_host]; } if (!function_exists('ocilogon')) { $error = 'The oracle PHP extension not installed (anymore?). You need to contact the system administrator of this server.'; if ($fail_ok) { echo $error; return NULL; } critical_error('PASSON', $error); } $db = $persistent ? @ociplogon($db_user, $db_password, $db_name) : @ocilogon($db_user, $db_password, $db_name); if ($db === false) { $error = 'Could not connect to database-server (' . ocierror() . ')'; if ($fail_ok) { echo $error; return NULL; } critical_error('PASSON', $error); //warn_exit(do_lang_tempcode('CONNECT_DB_ERROR')); } if (!$db) { fatal_exit(do_lang('CONNECT_DB_ERROR')); } $CACHE_DB[$db_name][$db_host] = $db; return $db; }
/** * Persistent database connection * * @access private called by the base class * @return resource */ function db_pconnect() { // ====== Modificación por CRIE - UTP ======= // Se agrega el parámetro utf8 a la función ocilogon para // forzar el uso de la codificación UTF-8 en Oracle return @ociplogon($this->username, $this->password, $this->hostname, 'utf8'); // ========= Fin Modificación ========= }
/** * Creates global database connection * * @param string $error returns a message in case of an error * @return bool */ function DBconnect(&$error) { global $DB; if (isset($DB['DB'])) { $error = _('Cannot create another database connection.'); return false; } $result = true; $DB['DB'] = null; // global db handler $DB['TRANSACTIONS'] = 0; // level of a nested transation $DB['TRANSACTION_NO_FAILED_SQLS'] = true; // true - if no statements failed in transaction, false - there are failed statements $DB['SELECT_COUNT'] = 0; // stats $DB['EXECUTE_COUNT'] = 0; if (!isset($DB['TYPE'])) { $error = 'Unknown database type.'; $result = false; } else { $DB['TYPE'] = zbx_strtoupper($DB['TYPE']); switch ($DB['TYPE']) { case ZBX_DB_MYSQL: $mysql_server = $DB['SERVER'] . (!empty($DB['PORT']) ? ':' . $DB['PORT'] : ''); if (!($DB['DB'] = mysql_connect($mysql_server, $DB['USER'], $DB['PASSWORD']))) { $error = 'Error connecting to database [' . mysql_error() . ']'; $result = false; } else { if (!mysql_select_db($DB['DATABASE'])) { $error = 'Error database in selection [' . mysql_error() . ']'; $result = false; } else { DBexecute('SET NAMES utf8'); } } break; case ZBX_DB_POSTGRESQL: $pg_connection_string = (!empty($DB['SERVER']) ? 'host=\'' . pg_connect_escape($DB['SERVER']) . '\' ' : '') . 'dbname=\'' . pg_connect_escape($DB['DATABASE']) . '\' ' . (!empty($DB['USER']) ? 'user=\'' . pg_connect_escape($DB['USER']) . '\' ' : '') . (!empty($DB['PASSWORD']) ? 'password=\'' . pg_connect_escape($DB['PASSWORD']) . '\' ' : '') . (!empty($DB['PORT']) ? 'port=' . pg_connect_escape($DB['PORT']) : ''); $DB['DB'] = pg_connect($pg_connection_string); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } elseif (false !== ($pgsql_version = pg_parameter_status('server_version'))) { if ((int) $pgsql_version >= 9) { // change the output format for values of type bytea from hex (the default) to escape DBexecute('set bytea_output = escape'); } } break; case ZBX_DB_ORACLE: $connect = ''; if (!empty($DB['SERVER'])) { $connect = '//' . $DB['SERVER']; if ($DB['PORT'] != '0') { $connect .= ':' . $DB['PORT']; } if ($DB['DATABASE']) { $connect .= '/' . $DB['DATABASE']; } } $DB['DB'] = ociplogon($DB['USER'], $DB['PASSWORD'], $connect); if ($DB['DB']) { DBexecute('ALTER SESSION SET NLS_NUMERIC_CHARACTERS=' . zbx_dbstr('. ')); } else { $error = 'Error connecting to database'; $result = false; } break; case ZBX_DB_DB2: $connect = ''; $connect .= 'DATABASE=' . $DB['DATABASE'] . ';'; $connect .= 'HOSTNAME=' . $DB['SERVER'] . ';'; $connect .= 'PORT=' . $DB['PORT'] . ';'; $connect .= 'PROTOCOL=TCPIP;'; $connect .= 'UID=' . $DB['USER'] . ';'; $connect .= 'PWD=' . $DB['PASSWORD'] . ';'; $DB['DB'] = db2_connect($connect, $DB['USER'], $DB['PASSWORD']); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } else { $options = array('db2_attr_case' => DB2_CASE_LOWER); db2_set_option($DB['DB'], $options, 1); if (isset($DB['SCHEMA']) && $DB['SCHEMA'] != '') { DBexecute('SET CURRENT SCHEMA=' . zbx_dbstr($DB['SCHEMA'])); } } break; case ZBX_DB_SQLITE3: if (file_exists($DB['DATABASE'])) { init_sqlite3_access(); lock_sqlite3_access(); try { $DB['DB'] = new SQLite3($DB['DATABASE'], SQLITE3_OPEN_READWRITE); } catch (Exception $e) { $error = 'Error connecting to database'; $result = false; } unlock_sqlite3_access(); } else { $error = 'Missing database'; $result = false; } break; default: $error = 'Unsupported database'; $result = false; } } if (false == $result) { $DB['DB'] = null; } return $result; }
private function _connect($sHost, $sUser, $sPass, $sPort = false, $sPersistent = false) { if ($sPort) { $sHost = $sHost . ':' . $sPort; } if ($hLink = $sPersistent ? @ociplogon($sUser, $sPass, $sHost) : @ocinlogon($sUser, $sPass, $sHost)) { return $hLink; } return false; }