/** * A method to re-initialize connections to the CMSMS configured database. * This method should be used by any UDT's or other plugins that use any other method * than the standard CMSMS supplied database object to connect to a database. * */ function &adodb_connect() { global $gCms; $config =& $gCms->GetConfig(); $dbinstance =& ADONewConnection($config['dbms'], 'pear:date:extend:transaction'); $dbinstance->raiseErrorFn = "adodb_error"; $conn_func = isset($config['persistent_db_conn']) && $config['persistent_db_conn'] == true ? 'PConnect' : 'Connect'; if (!empty($config['db_port'])) { $dbinstance->port = $config['db_port']; } $connect_result = $dbinstance->{$conn_func}($config['db_hostname'], $config['db_username'], $config['db_password'], $config['db_name']); if (FALSE == $connect_result) { $str = "Attempt to connect to database {$config['db_name']} on {$config['db_username']}@{$config['db_hostname']} failed"; trigger_error($str, E_USER_ERROR); die($str); } $dbinstance->raiseErrorFn = null; $dbinstance->SetFetchMode(ADODB_FETCH_ASSOC); if ($config['debug'] == true) { $dbinstance->debug = true; #$dbinstance->LogSQL(); } if ($config['dbms'] == 'sqlite') { $dbinstance->Execute('PRAGMA short_column_names = 1;'); sqlite_create_function($config['use_adodb_lite'] ? $db->connectionId : $db->_connectionID, 'now', 'time', 0); } else { if (!empty($config['default_encoding']) && $config['default_encoding'] == 'utf-8' && $config['set_names'] == true) { $dbinstance->Execute("SET NAMES 'utf8'"); } } $db =& $dbinstance; return $db; }
function _sqlite_add_function(&$sqlite, &$f, &$r) { if (_sqlite_is_version(3, $sqlite)) { isset($r[1]) ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) : $sqlite->sqliteCreateFunction($f, $r[0]); } else { isset($r[1]) ? sqlite_create_function($sqlite, $f, $r[0], $r[1]) : sqlite_create_function($sqlite, $f, $r[0]); } }
/** * register sql standard functions for a connection * * @param db handel conn */ public function registerCallbackFunctions($conn) { sqlite_create_function($conn, 'cast', array($this, '_cast'), 2); sqlite_create_function($conn, 'sign', array($this, '_sign'), 1); sqlite_create_function($conn, 'dateadd', array($this, '_dateadd'), 3); sqlite_create_function($conn, 'locate', array($this, '_locate'), 3); sqlite_create_function($conn, 'nullif', array($this, '_nullif'), 2); }
/** * load the sqlite helper */ function _getDB() { $db =& plugin_load('helper', 'sqlite'); if (is_null($db)) { msg('The data plugin needs the sqlite plugin', -1); return false; } if ($db->init('data', dirname(__FILE__) . '/db/')) { sqlite_create_function($db->db, 'DATARESOLVE', array($this, '_resolveData'), 2); return $db; } else { return false; } }
function DBConnect($dbpath, $dbuname, $dbpass) { if (defined("DBDEBUG")) { $this->debug = constant("DBDEBUG"); } $this->dbpath = $dbpath; $this->db = @sqlite_open($dbpath, 0666, $msg_err); if (!$this->db) { die("<h1>Error connecting to the database!</h1>\n" . "<p>Have you edited dbinit.php and correctly defined " . "the database path?</p>\n"); } sqlite_query($this->db, "PRAGMA synchronous = FULL"); //we emulate via callback some missing MySQL function sqlite_create_function($this->db, "year", "__cb_sqlite_year", 1); sqlite_create_function($this->db, "month", "__cb_sqlite_month", 1); sqlite_create_function($this->db, "day", "__cb_sqlite_dayofmonth", 1); sqlite_create_function($this->db, "dayofmonth", "__cb_sqlite_dayofmonth", 1); sqlite_create_function($this->db, "from_unixtime", "__cb_sqlite_from_unixtime"); sqlite_create_function($this->db, "unix_timestamp", "__cb_sqlite_unix_timestamp"); sqlite_create_function($this->db, "now", "__cb_sqlite_now"); sqlite_create_function($this->db, "md5", "__cb_sqlite_md5"); }
/** * Initializes a database connection. * It returns the connection, if successful. * * @param string $db_server * @param string $db_name * @param string $db_user * @param string $db_passwd * @param string $db_prefix * @param array $db_options * * @return resource */ static function initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array()) { global $db_in_transact, $sqlite_error; if (substr($db_name, -3) != '.db') { $db_name .= '.db'; } // initialize the instance... if not done already! if (self::$_db === null) { self::$_db = new self(); } if (!empty($db_options['persist'])) { $connection = @sqlite_popen($db_name, 0666, $sqlite_error); } else { $connection = @sqlite_open($db_name, 0666, $sqlite_error); } // Something's wrong, show an error if its fatal (which we assume it is) if (!$connection) { if (!empty($db_options['non_fatal'])) { return null; } else { display_db_error(); } } $db_in_transact = false; // This is frankly stupid - stop SQLite returning alias names! @sqlite_query('PRAGMA short_column_names = 1', $connection); // Make some user defined functions! sqlite_create_function($connection, 'unix_timestamp', 'elk_udf_unix_timestamp', 0); sqlite_create_function($connection, 'inet_aton', 'elk_udf_inet_aton', 1); sqlite_create_function($connection, 'inet_ntoa', 'elk_udf_inet_ntoa', 1); sqlite_create_function($connection, 'find_in_set', 'elk_udf_find_in_set', 2); sqlite_create_function($connection, 'year', 'elk_udf_year', 1); sqlite_create_function($connection, 'month', 'elk_udf_month', 1); sqlite_create_function($connection, 'dayofmonth', 'elk_udf_dayofmonth', 1); sqlite_create_function($connection, 'concat', 'elk_udf_concat'); sqlite_create_function($connection, 'locate', 'elk_udf_locate', 2); sqlite_create_function($connection, 'regexp', 'elk_udf_regexp', 2); return $connection; }
function smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array()) { global $smcFunc, $mysql_set_mode, $db_in_transact, $sqlite_error; // Map some database specific functions, only do this once. if (!isset($smcFunc['db_fetch_assoc']) || $smcFunc['db_fetch_assoc'] != 'sqlite_fetch_array') { $smcFunc += array('db_query' => 'smf_db_query', 'db_quote' => 'smf_db_quote', 'db_fetch_assoc' => 'sqlite_fetch_array', 'db_fetch_row' => 'smf_db_fetch_row', 'db_free_result' => 'smf_db_free_result', 'db_insert' => 'smf_db_insert', 'db_insert_id' => 'smf_db_insert_id', 'db_num_rows' => 'sqlite_num_rows', 'db_data_seek' => 'sqlite_seek', 'db_num_fields' => 'sqlite_num_fields', 'db_escape_string' => 'sqlite_escape_string', 'db_unescape_string' => 'smf_db_unescape_string', 'db_server_info' => 'smf_db_libversion', 'db_affected_rows' => 'smf_db_affected_rows', 'db_transaction' => 'smf_db_transaction', 'db_error' => 'smf_db_last_error', 'db_select_db' => '', 'db_title' => 'SQLite', 'db_sybase' => true, 'db_case_sensitive' => true, 'db_escape_wildcard_string' => 'smf_db_escape_wildcard_string'); } if (substr($db_name, -3) != '.db') { $db_name .= '.db'; } if (!empty($db_options['persist'])) { $connection = @sqlite_popen($db_name, 0666, $sqlite_error); } else { $connection = @sqlite_open($db_name, 0666, $sqlite_error); } // Something's wrong, show an error if its fatal (which we assume it is) if (!$connection) { if (!empty($db_options['non_fatal'])) { return null; } else { db_fatal_error(); } } $db_in_transact = false; // This is frankly stupid - stop SQLite returning alias names! @sqlite_query('PRAGMA short_column_names = 1', $connection); // Make some user defined functions! sqlite_create_function($connection, 'unix_timestamp', 'smf_udf_unix_timestamp', 0); sqlite_create_function($connection, 'inet_aton', 'smf_udf_inet_aton', 1); sqlite_create_function($connection, 'inet_ntoa', 'smf_udf_inet_ntoa', 1); sqlite_create_function($connection, 'find_in_set', 'smf_udf_find_in_set', 2); sqlite_create_function($connection, 'year', 'smf_udf_year', 1); sqlite_create_function($connection, 'month', 'smf_udf_month', 1); sqlite_create_function($connection, 'dayofmonth', 'smf_udf_dayofmonth', 1); sqlite_create_function($connection, 'concat', 'smf_udf_concat'); sqlite_create_function($connection, 'locate', 'smf_udf_locate', 2); sqlite_create_function($connection, 'regexp', 'smf_udf_regexp', 2); return $connection; }
function SQL() { global $pref; $this->type = $pref['sql_type']; $this->connect(); if (DB::isError($this->dbh)) { // display custom error message if have problems with dbserver if (!$this->dberror($this->dbh->getMessage())) { die($this->dbh->getMessage()); } } $this->debug = 0; // Set some DB engine specific function names if ($pref['sql_type'] == 'sqlite') { $this->NOW = "datetime('now', 'localtime')"; $this->CURDATE = "datetime('now', strftime('-%H hours', 'now', 'localtime'), strftime('-%M minutes', 'now', 'localtime'), strftime('-%S seconds', 'now', 'localtime'), 'localtime')"; // Register some custom functions for sqlite // that will emulate some MySQL functions that we use sqlite_create_function($this->dbh->connection, 'CONCAT', array('SQL', 'concat')); } else { $this->NOW = 'NOW()'; $this->CURDATE = 'CURDATE()'; } }
function sqlitem_create_function($dhb, $function_name, $callback, $num_arg = null) { return sqlite_create_function($dhb, $function_name, $callback, $num_arg); }
<?php array_map('callableString', $array); call_user_func($array, 'nonCallableString'); //array_map($array, $nonCallableVar); array_filter($array, array('string', 'string')); array_reduce(array('string', 'string'), $array); preg_replace_callback('a', 'b', 'MyClass::myCallbackMethod'); sqlite_create_function('MyClass::myNonCallbackMethod', $b, $c); sqlite_create_aggregate($a, 'MyClass2::myNonCallbackMethod', $c);
<?php $db = sqlite_open(dirname(__FILE__) . "/db2.sqlite"); /* arguments: * 1 database resource * 2 sqlite function name * 3 php function name * 4 number of arguments (optional) */ sqlite_create_function($db, "my_date", "date", 2); /* * The above function makes PHP date() avaliable * from SQLite as my_date() and for highest * performance indicates that it requires * exactly 2 arguments */ $res = sqlite_array_query("\n\tSELECT title, \n\tmy_date((SELECT time_format FROM profile WHERE login='******'), timestamp) AS date\n\tFROM messages", $db, SQLITE_ASSOC); foreach ($res as $v) { echo $v['title'] . " was posted on " . $v['date'] . "<br />\n"; }
<?php require 's_common.inc'; $db = makedb(); $data = array(array("one", "uno"), array("two", "dos"), array("three", "tres")); sqlite_query("CREATE TABLE strings(a,b)", $db); function implode_args() { $args = func_get_args(); $sep = array_shift($args); return implode($sep, $args); } foreach ($data as $row) { sqlite_query("INSERT INTO strings VALUES('" . sqlite_escape_string($row[0]) . "','" . sqlite_escape_string($row[1]) . "')", $db); } sqlite_create_function($db, "implode", "implode_args"); $r = sqlite_query("SELECT implode('-', a, b) from strings", $db); while ($row = sqlite_fetch_array($r, SQLITE_NUM)) { var_dump($row); } $r = sqlite_close($db); ?>
$sql = 'SELECT md5rev(filename) FROM files'; $rows = sqlite_array_query($dbhandle, $sql); } else { echo 'Error opening sqlite db: ' . $sqliteerror; exit; } $rows = sqlite_array_query($dbhandle, "SELECT php('md5', filename) from files"); class sqlite_function { function md5($value) { return md5($value); } } $dbhandle = sqlite_open('SQLiteDB'); sqlite_create_function($dbhandle, 'md5', array('sqlite_function', 'md5'), 1); // From now on, you can use md5 function inside your SQL statements $dbhandle = sqlite_open('mysqlitedb'); $query = sqlite_query($dbhandle, "UPDATE users SET email='*****@*****.**' WHERE username='******'"); if (!$query) { exit('Error in query.'); } else { echo 'Number of rows modified: ', sqlite_changes($dbhandle); } $dbhandle = new SQLiteDatabase('mysqlitedb'); $query = $dbhandle->query("UPDATE users SET email='*****@*****.**' WHERE username='******'"); if (!$query) { exit('Error in query.'); } else { echo 'Number of rows modified: ', $dbhandle->changes(); }
/** * This method expand the SQLite ability * * The functions added with this method, can be used in the sql query. * The functgion of PHP, they can be called with the sintax: * php('functionName', param, param, ...) * * @param string $name name of SQLite function * @param string $originalFunction name of the original funtion to call * @param int $args number of aguments * @access public * @return void */ function addFunction($name, $originalFunction, $args) { sqlite_create_function($this->_conn, $name, $originalFunction, $args); }
static function connect($dsn, $debug = false, $die = true, $prefix = null, $make_global = true) { /* $gCms = silk(); $persistent = false; if ($dbms == '') { $config = cms_config(); $dbms = $config['dbms']; $hostname = $config['db_hostname']; $username = $config['db_username']; $password = $config['db_password']; $dbname = $config['db_name']; $debug = $config['debug']; $persistent = $config['persistent_db_conn']; } */ if ($prefix !== null) { self::$prefix = $prefix; } $dbinstance = null; $_GLOBALS['ADODB_CACHE_DIR'] = join_path(ROOT_DIR, 'tmp', 'cache'); require_once join_path(SILK_LIB_DIR, 'adodb', 'adodb-exceptions.inc.php'); require_once join_path(SILK_LIB_DIR, 'adodb', 'adodb.inc.php'); try { $dbinstance = ADONewConnection($dsn); $dbinstance->fnExecute = 'count_execs'; $dbinstance->fnCacheExecute = 'count_cached_execs'; } catch (exception $e) { if ($die) { echo "<strong>Database Connection Failed</strong><br />"; echo "Error: {$dbinstance->_errorMsg}<br />"; echo "Function Performed: {$e->fn}<br />"; echo "Host/DB: {$e->host}/{$e->database}<br />"; die; } else { return null; } } $dbinstance->SetFetchMode(ADODB_FETCH_ASSOC); $dbinstance->debug = $debug; if (isset($dbms) && $dbms == 'sqlite') { $dbinstance->Execute("PRAGMA short_column_names = 1;"); sqlite_create_function($dbinstance->_connectionID, 'now', 'time', 0); } if ($make_global) { self::$instance = $dbinstance; } //Initialize the CMS_DB_PREFIX define self::get_prefix(); return $dbinstance; }
/** * Registers a User Defined Function for use in SQL statements */ public function create_function($function_name, $callback, $num_args) { sqlite_create_function($this->db, $function_name, $callback, $num_args); }
/** * Adds a number of math functions to SQLite that MSSQL, MySQL and PostgreSQL have by default * * @return void */ private function createSQLiteFunctions() { $function = array(); $functions[] = array('acos', 'acos', 1); $functions[] = array('asin', 'asin', 1); $functions[] = array('atan', 'atan', 1); $functions[] = array('atan2', 'atan2', 2); $functions[] = array('ceil', 'ceil', 1); $functions[] = array('ceiling', 'ceil', 1); $functions[] = array('cos', 'cos', 1); $functions[] = array('cot', array('fSQLTranslation', 'sqliteCotangent'), 1); $functions[] = array('degrees', 'rad2deg', 1); $functions[] = array('exp', 'exp', 1); $functions[] = array('floor', 'floor', 1); $functions[] = array('ln', 'log', 1); $functions[] = array('log', array('fSQLTranslation', 'sqliteLogBaseFirst'), 2); $functions[] = array('ltrim', 'ltrim', 1); $functions[] = array('pi', 'pi', 0); $functions[] = array('power', 'pow', 2); $functions[] = array('radians', 'deg2rad', 1); $functions[] = array('rtrim', 'rtrim', 1); $functions[] = array('sign', array('fSQLTranslation', 'sqliteSign'), 1); $functions[] = array('sqrt', 'sqrt', 1); $functions[] = array('sin', 'sin', 1); $functions[] = array('tan', 'tan', 1); $functions[] = array('trim', 'trim', 1); if ($this->database->getExtension() == 'sqlite') { $functions[] = array('current_date', array('fSQLTranslation', 'sqliteDate'), 0); $functions[] = array('current_time', array('fSQLTranslation', 'sqliteTime'), 0); $functions[] = array('current_timestamp', array('fSQLTranslation', 'sqliteTimestamp'), 0); // If SQLite was compiled with ISO-8859-* string handling, we override as best we can // with custom functions that return the correct values. We can't fix LIKE and GLOB // but they don't matter as much since the encoding only affects case transformations. if (strtolower(sqlite_libencoding()) != 'utf-8') { $functions[] = array('length', array('fSQLTranslation', 'sqliteLength'), 1); $functions[] = array('substr', array('fSQLTranslation', 'sqliteSubstr'), 3); } } foreach ($functions as $function) { if ($this->database->getExtension() == 'pdo') { $this->database->getConnection()->sqliteCreateFunction($function[0], $function[1], $function[2]); } else { sqlite_create_function($this->database->getConnection(), $function[0], $function[1], $function[2]); } } }
public function registerRegex() { sqlite_create_function($this->db, 'REGEX', 'sqlite_regex_match', 2); }
/** * Registers a User Defined Function for use in SQL statements */ function create_function($function_name, $callback, $num_args) { if ($this->extension == DOKU_EXT_SQLITE) { sqlite_create_function($this->db, $function_name, $callback, $num_args); } else { $this->db->sqliteCreateFunction($function_name, $callback, $num_args); } }
/** * Registers an user defined function for use in SQL statements. * @param string function name * @param mixed callback * @param int num of arguments * @return void */ public function registerFunction($name, $callback, $numArgs = -1) { sqlite_create_function($this->connection, $name, $callback, $numArgs); }
getResultResource(){return$this->resultSet;}function registerFunction($name,$callback,$numArgs=-1){sqlite_create_function($this->connection,$name,$callback,$numArgs);}function
/** * Add some proprietary database functions to the current SQLite handle * in order to make it MySQL compatible */ private function sqlite_prepare() { // we emulate via callback some missing MySQL functions sqlite_create_function($this->db_handle->connection, 'unix_timestamp', array('rcube_mdb2', 'sqlite_unix_timestamp')); sqlite_create_function($this->db_handle->connection, 'now', array('rcube_mdb2', 'sqlite_now')); }
public function init($db) { ini_set('sqlite.assoc_case', 0); $db->execute("PRAGMA foreign_keys = ON;"); @sqlite_create_function($db->_connectionID, 'UNIX_TIMESTAMP', '_unix_timestamp', 1); @sqlite_create_function($db->_connectionID, 'now', '_now', 0); @sqlite_create_function($db->_connectionID, 'floor', '_floor', 1); @sqlite_create_function($db->_connectionID, 'log', '_log'); @sqlite_create_function($db->_connectionID, 'isnull', '_isnull', 1); @sqlite_create_function($db->_connectionID, 'md5', '_md5', 1); @sqlite_create_function($db->_connectionID, 'concat', '_concat', 2); @sqlite_create_function($db->_connectionID, 'lower', '_lower', 1); }
/** * Add some proprietary database functions to the current SQLite handle * in order to make it MySQL compatible * * @access private */ private function _sqlite_prepare() { include_once 'include/rcube_sqlite.inc'; // we emulate via callback some missing MySQL function sqlite_create_function($this->db_handle->connection, 'from_unixtime', 'rcube_sqlite_from_unixtime'); sqlite_create_function($this->db_handle->connection, 'unix_timestamp', 'rcube_sqlite_unix_timestamp'); sqlite_create_function($this->db_handle->connection, 'now', 'rcube_sqlite_now'); sqlite_create_function($this->db_handle->connection, 'md5', 'rcube_sqlite_md5'); }
/** * Adds a number of math functions to SQLite that MSSQL, MySQL and PostgreSQL have by default * * @return void */ private function createSQLiteFunctions() { $function = array(); $functions[] = array('acos', 'acos', 1); $functions[] = array('asin', 'asin', 1); $functions[] = array('atan', 'atan', 1); $functions[] = array('atan2', 'atan2', 2); $functions[] = array('ceil', 'ceil', 1); $functions[] = array('ceiling', 'ceil', 1); $functions[] = array('cos', 'cos', 1); $functions[] = array('cot', array('fSQLTranslation', 'sqliteCotangent'), 1); $functions[] = array('degrees', 'rad2deg', 1); $functions[] = array('exp', 'exp', 1); $functions[] = array('floor', 'floor', 1); $functions[] = array('ln', 'log', 1); $functions[] = array('log', array('fSQLTranslation', 'sqliteLogBaseFirst'), 2); $functions[] = array('ltrim', 'ltrim', 1); $functions[] = array('pi', 'pi', 0); $functions[] = array('power', 'pow', 2); $functions[] = array('radians', 'deg2rad', 1); $functions[] = array('rtrim', 'rtrim', 1); $functions[] = array('sign', array('fSQLTranslation', 'sqliteSign'), 1); $functions[] = array('sqrt', 'sqrt', 1); $functions[] = array('sin', 'sin', 1); $functions[] = array('tan', 'tan', 1); $functions[] = array('trim', 'trim', 1); if ($this->database->getExtension() == 'sqlite') { $functions[] = array('current_date', array('fSQLTranslation', 'sqliteDate'), 0); $functions[] = array('current_time', array('fSQLTranslation', 'sqliteTime'), 0); $functions[] = array('current_timestamp', array('fSQLTranslation', 'sqliteTimestamp'), 0); } foreach ($functions as $function) { if ($this->database->getExtension() == 'pdo') { $this->database->getConnection()->sqliteCreateFunction($function[0], $function[1], $function[2]); } else { sqlite_create_function($this->database->getConnection(), $function[0], $function[1], $function[2]); } } }
<?php require 's_common.inc'; $db = makedb(); sqlite_create_function($db, 'md5rev', 'md5_and_reverse', 1); sqlite_create_aggregate($db, 'max_len', 'max_len_step', 'max_len_finalize'); // generic php callback $rows = sqlite_array_query($db, "SELECT php('md5', my_string) from mytable"); var_dump($rows); $rows = sqlite_array_query($db, "SELECT php('max', my_float, 2) from mytable"); var_dump($rows); // callback error $rows = sqlite_array_query($db, "SELECT php('unknown', my_string) from mytable"); var_dump($rows); /// PHP UDF function md5_and_reverse($string) { echo "in callback, original string is {$string}\n"; return strrev(md5($string)); } $sql = 'SELECT md5rev(my_string) FROM mytable'; $rows = sqlite_array_query($db, $sql); var_dump($rows); /// aggregate $data = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); sqlite_query($db, "CREATE TABLE strings(a)"); foreach ($data as $str) { $str = sqlite_escape_string($str); sqlite_query($db, "INSERT INTO strings VALUES ('{$str}')"); } function max_len_step(&$context, $string)
public function sqliteCreateFunction($function_name, $callback, $num_args = false) { if ($num_args === false) { sqlite_create_function($this->_connection, $function_name, $callback); } else { sqlite_create_function($this->_connection, $function_name, $callback, $num_args); } }
/** * Add some proprietary database functions to the current SQLite handle * in order to make it MySQL compatible * * @access private */ function _sqlite_prepare() { include_once 'include/rcube_sqlite.inc'; // we emulate via callback some missing MySQL function sqlite_create_function($this->db_handle->connection, "from_unixtime", "rcube_sqlite_from_unixtime"); sqlite_create_function($this->db_handle->connection, "unix_timestamp", "rcube_sqlite_unix_timestamp"); sqlite_create_function($this->db_handle->connection, "now", "rcube_sqlite_now"); sqlite_create_function($this->db_handle->connection, "md5", "rcube_sqlite_md5"); }
function _createFunctions() { @sqlite_create_function($this->_connectionID, 'adodb_date', 'adodb_date', 1); @sqlite_create_function($this->_connectionID, 'adodb_date2', 'adodb_date2', 2); }
function create_function($function_name, $callback, $num_arg = null) { if (DEBUG) { return sqlite_create_function($this->connId, $function_name, $callback, $num_arg); } else { return @sqlite_create_function($this->connId, $function_name, $callback, $num_arg); } }