function check_table_existence($conn_handle, $table_name) { $sql_stmt = "SELECT class_name FROM db_class WHERE class_name = ?"; $cubrid_req = cubrid_prepare($conn_handle, $sql_stmt); if (!$cubrid_req) { return -1; } $cubrid_retval = cubrid_bind($cubrid_req, 1, $table_name); if (!$cubrid_req) { cubrid_close_request($cubrid_req); return -1; } $cubrid_retval = cubrid_execute($cubrid_req); if (!$cubrid_retval) { cubrid_close_request($cubrid_req); return -1; } $row_num = cubrid_num_rows($cubrid_req); if ($row_num < 0) { cubrid_close_request($cubrid_req); return -1; } cubrid_close_request($cubrid_req); if ($row_num > 0) { return 1; } else { return 0; } }
function DROP_TABLE_IF_EXISTS($tablename, $link_identifier) { $id_result = cubrid_mysql_query_native("SELECT class_name FROM db_class WHERE class_name='" . $tablename . "'", $link_identifier); $rows = cubrid_num_rows($id_result); if ($rows > 1) { cubrid_execute($link_identifier, "DROP TABLE " . $tablename); } }
exit(1); } $retval = check_table_existence($conn, "largetable"); if ($retval == -1) { exit(1); } elseif ($retval == 1) { printf("this table is created\n"); } else { printf("#####start: create largetable#####\n"); $cubrid_req = cubrid_execute($conn, "CREATE TABLE largetable(a int AUTO_INCREMENT, b clob)"); if (!$cubrid_req) { printf("Failed to create test table: [%d] %s\n", cubrid_error_code(), cubrid_error_msg()); exit(1); } $req = cubrid_prepare($conn, "insert into largetable(b) values (?)"); $importName = array("largeFile/large.txt"); for ($i = 0; $i < count($importName); $i++) { $lob = cubrid_lob2_new($conn, "CLOB"); cubrid_lob2_import($lob, $importName[$i]); cubrid_lob2_bind($req, 1, $lob, "CLOB"); cubrid_execute($req); cubrid_lob2_close($lob); } cubrid_close_prepare($req); if (!cubrid_commit($conn)) { exit(1); } } ?>
public static function query($query, $compatibility = true) { /// Bypassing compatiblitiy issue : will be replace to NAF2. if ($compatibility) { $query = str_replace('UNIX_TIMESTAMP()', Timestamp::getUNIXtime(), $query); // compatibility issue. if (stripos($query, "ORDER BY") !== false) { $origPagingInst = array('/(ASC|DESC) LIMIT (\\d+) OFFSET 0/si', '/(ASC|DESC) LIMIT (\\d+) OFFSET (\\d+)/si', '/(ASC|DESC) LIMIT 1(^[0-9])/si', '/(ASC|DESC) LIMIT (\\d+)/si', '/RAND\\(\\) LIMIT (\\d+)/si'); $descPagingInst = array('$1 FOR ORDERBY_NUM() BETWEEN 1 AND $2', '$1 FOR ORDERBY_NUM() BETWEEN ($3+1) AND ($2+$3)', '$1 FOR ORDERBY_NUM() = 1', '$1 FOR ORDERBY_NUM() BETWEEN 1 AND $2', 'RANDOM() FOR ORDERBY_NUM() BETWEEN 1 AND $1'); } else { if (stripos($query, "GROUP BY") !== false) { $origPagingInst = array('/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT (\\d+) OFFSET 0/si', '/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT (\\d+) OFFSET ([0-9]+)/si', '/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT 1(^[0-9])/si', '/GROUP BY(.*)(ORDER BY)(.*)(ASC|DESC) LIMIT (\\d+)/si', '/GROUP BY(.*)(ORDER BY)(.*)RAND\\(\\) LIMIT (\\d+)/si'); $descPagingInst = array('GROUP BY $1 HAVING GROUPBY_NUM() = $5 $2 $3 $4', 'GROUP BY $1 HAVING GROUPBY_NUM() BETWEEN ($6+1) AND $5 $2 $3 $4', 'GROUP BY $1 HAVING GROUPBY_NUM() = 1 $2 $3 $4', 'GROUP BY $1 HAVING GROUPBY_NUM() BETWEEN 1 AND $5 $2 $3 $4', 'GROUP BY $1 HAVING GROUPBY_NUM() BETWEEN 1 AND $4 $2 RANDOM() $3'); } else { $origPagingInst = array('/WHERE(.*)LIMIT (\\d+) OFFSET 0/si', '/WHERE(.*)LIMIT (\\d+) OFFSET ([0-9]+)/si', '/WHERE(.*)LIMIT 1(^[0-9])/si', '/WHERE(.*)LIMIT (\\d+)/si', '/SUM\\((size|value)\\)/si'); $descPagingInst = array('WHERE ROWNUM BETWEEN 1 AND $2 AND $1', 'WHERE ROWNUM BETWEEN ($3+1) AND ($2+$3) AND $1', 'WHERE ROWNUM = 1 AND $1', 'WHERE ROWNUM BETWEEN 1 AND $2 AND $1', 'SUM("$1")'); } } $query = preg_replace($origPagingInst, $descPagingInst, $query); // CONCAT $ppos = -1; $length = strlen($query); do { $pos = strpos($query, '\'', $ppos + 1); if ($pos === false) { $pos = strlen($query); } while (true) { $concat = stripos($query, 'CONCAT', $ppos + 1); if ($concat === false || $concat >= $pos) { break; } $depth = 0; $quote = null; for ($i = $concat + 6; $i < $length; $i++) { if ($quote === null) { if ($query[$i] == '\'' || $query[$i] == '"') { $quote = $query[$i]; } elseif ($query[$i] == ',') { $query = substr($query, 0, $i) . ' || ' . substr($query, $i + 1); } elseif ($query[$i] == '(') { $depth++; } elseif ($query[$i] == ')') { if (--$depth == 0) { break; } } } else { if ($query[$i] == $quote && $query[$i - 1] != '\\') { $quote = null; } } } $query = substr($query, 0, $concat) . substr($query, $concat + 6); $pos = strpos($query, '\'', $ppos + 1); $length = strlen($query); } $ppos = $pos; while ($ppos < $length) { $ppos = strpos($query, '\'', $ppos + 1); if ($query[$ppos - 1] != '\\') { break; } } } while ($ppos < $length); } if (function_exists('__tcSqlLogBegin')) { __tcSqlLogBegin($query); $result = cubrid_execute(self::$dbProperties['handle'], $query); __tcSqlLogEnd($result, 0); } else { $result = cubrid_execute(self::$dbProperties['handle'], $query); } self::$lastQueryType = strtolower(substr($query, 0, 6)); if (in_array(self::$lastQueryType, array('insert', 'update', 'delete', 'replac'))) { self::commit(); self::clearCache(); } return $result; }
/** * Execute the query * this method is private * @param string $query * @param resource $connection * @return resource */ function __query($query, $connection) { if ($this->use_prepared_statements == 'Y') { $req = @cubrid_prepare($connection, $query); if (!$req) { $this->_setError(); return false; } $position = 0; if ($this->param) { foreach ($this->param as $param) { $value = $param->getUnescapedValue(); $type = $param->getType(); if ($param->isColumnName()) { continue; } switch ($type) { case 'number': $bind_type = 'numeric'; break; case 'varchar': $bind_type = 'string'; break; default: $bind_type = 'string'; } if (is_array($value)) { foreach ($value as $v) { $bound = @cubrid_bind($req, ++$position, $v, $bind_type); if (!$bound) { $this->_setError(); return false; } } } else { $bound = @cubrid_bind($req, ++$position, $value, $bind_type); if (!$bound) { $this->_setError(); return false; } } } } $result = @cubrid_execute($req); if (!$result) { $this->_setError(); return false; } return $req; } // Execute the query $result = @cubrid_execute($connection, $query); // error check if (!$result) { $this->_setError(); return false; } // Return the result return $result; }
<?php include_once "connect.inc"; header('Content-type: text/html; charset=euc-kr'); //var_dump($connect_url); $conn = cubrid_connect_with_url($connect_url); $sql = "SELECT a from foo"; $result = cubrid_execute($conn, $sql); $pResult = cubrid_fetch($result); echo $pResult[0]; echo "<br/>"; cubrid_close_request($result); cubrid_rollback($conn); cubrid_disconnect($conn);
exit(0); } cubrid_close_request($result); cubrid_commit($dh); $result = cubrid_execute($dh, "CREATE UNIQUE INDEX \"" . $db["table"] . "_idx\"\n ON \"" . $db["table"] . "\" (\"index\" ASC);"); if ($result === false) { printf("Cannot create index.\n"); cubrid_rollback($dh); } else { cubrid_close_request($result); cubrid_commit($dh); } } $error_count = 0; $inserted = 0; foreach ($addr_list as $val) { $result = cubrid_execute($dh, sprintf("INSERT INTO \"%s\" (\"index\", \"addr1\", \"addr2\",\n \"addr3\", \"addr4\", \"zipcode\") VALUES\n (%d, '%s', '%s', '%s', '%s', '%s');", $db["table"], $val[5], $val[1], $val[2], $val[3], $val[4], $val[0])); if ($result === false) { $error_count++; printf("Insert Error! Index: %d, Error Count: %d\n", $val[5], $error_count); continue; } $inserted++; if ($inserted % 1000 === 0) { printf("%d record inserted.\n", $inserted); } cubrid_close_request($result); } cubrid_commit($dh); cubrid_disconnect($dh); printf("Done. %d record inserted, %d errors.\n", $inserted, $error_count);
private function _execute($sql, $params = array()) { if (!$this->isConnected()) { throw new \org\autoset\santorini\exception\DataAccessException("데이터베이스에 연결되어 있지 않습니다."); return false; } $sql = trim($sql); //$this->_resultMode = MYSQLI_STORE_RESULT;//(strtolower(substr($sql,0,6)) == 'select') ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT; if (sizeof($params) > 0) { $sql = $this->_prepareSql($sql, $params); } if ($this->debug) { echo '<fieldset><legend><b>(mysqli) :</b></legend><xmp>' . $sql . '</xmp>'; } $this->logging($sql); $result = @cubrid_execute($this->_conn, $sql); //, CUBRID_ASYNC);//, $this->_resultMode); if ($this->getErrorNo() > 0) { $result = false; } if ($this->debug && !$result) { echo '<fieldset style="margin:5px;"><legend><b><span style="color:red">Error ' . $this->getErrorNo() . '</span></b></legend><xmp> ' . $this->getError() . '</xmp></fieldset>'; } if ($this->debug) { echo '</fieldset>'; } if ($this->isTransMode() && !$result) { $this->_transFailedCnt++; } if (!$result) { $this->logging("!!ERROR!!\t" . $this->getError()); throw new \Exception($this->getError()); } return $result; }
public function testCubridCci6_t10() { // case : ac on // multi_connection tname? if (OUTPUT_FUNCTION_NAME == true) { echo "\r\nRunning: " . __FUNCTION__ . " = "; } try { $this->sql = "create table t10(code int unique, s string) "; cubrid_execute($this->con, $this->sql); for ($count = 0; $count < 10; $count++) { $this->sql = "delete from t10 "; cubrid_execute($this->con, $this->sql); cubrid_commit($this->con); for ($i = 0; $i < 1000; $i++) { $this->sql = "insert into t10 values({$i}, 'aaa')"; $this->req = cubrid_execute($this->con, $this->sql); } $this->sql = "select count(*) from t10"; $this->req = cubrid_execute($this->con, $this->sql); $str = cubrid_fetch_row($this->req); echo $str[0]; $this->assertEquals($str[0], 1000); echo "\r\n-----Running:---------------{$count} "; } echo "\r\n#### case Cci6_t10 OK #### "; } catch (Exception $e) { echo "\r\n#### Catch Cci6_t10 Exception #### "; } $this->sql = "drop table t10"; cubrid_execute($this->con, $this->sql); cubrid_commit($this->con); $this->req = null; }
/** * @brief : 쿼리문의 실행 및 결과의 fetch 처리 * * query : query문 실행하고 result return\n * fetch : reutrn 된 값이 없으면 NULL\n * rows이면 array object\n * row이면 object\n * return\n **/ function _query($query) { //echo "(((".$this->backtrace().")))"; if (!$query || !$this->isConnected()) { return; } // 쿼리 시작을 알림 $this->actStart($query); // 쿼리 문 실행 $result = @cubrid_execute($this->fd, $query); // 오류 체크 if (cubrid_error_code()) { $this->setError(cubrid_error_code(), cubrid_error_msg()); } // 쿼리 실행 종료를 알림 $this->actFinish(); // 결과 리턴 return $result; }
printf("[002] Expecting NULL, got %s,%s\n", gettype($tmp2), $tmp2); } $conn = cubrid_connect($host, $port, $db, $user, $passwd); cubrid_execute($conn, "drop table if exists fetch_object_tb"); cubrid_execute($conn, "CREATE TABLE fetch_object_tb(c1 string, c2 char(20), c3 int, c4 double, c5 time, c6 date)"); cubrid_execute($conn, "insert into fetch_object_tb values('string1','char11111',1,11.11,TIME '02:10:22',DATE '08/14/1977')"); cubrid_execute($conn, "insert into fetch_object_tb values('string2','char2',2,222222,TIME '1:15',DATE '00-10-31')"); cubrid_execute($conn, "insert into fetch_object_tb values('string4','char4',4,44,TIME '4:15',DATE '04-10-21')"); cubrid_execute($conn, "insert into fetch_object_tb values('string3','char3',3,33,TIME '3:15',DATE '03-10-31')"); cubrid_execute($conn, "insert into fetch_object_tb values('string5','char5',5,55,TIME '5:15',DATE '05-10-01')"); cubrid_execute($conn, "insert into fetch_object_tb values('string6','char6',6,66,TIME '6:15',DATE '06-10-11')"); cubrid_execute($conn, "insert into fetch_object_tb values('string7','char7',7,77,TIME '7:15',DATE '07-10-11')"); cubrid_execute($conn, "insert into fetch_object_tb values('string8','char8',8,88,TIME '8:15',DATE '08-10-11')"); cubrid_execute($conn, "insert into fetch_object_tb values('string9','char9',9,99,TIME '9:15',DATE '09-10-11')"); cubrid_execute($conn, "insert into fetch_object_tb values('string10','char10',10,1010,TIME '10:15',DATE '010-10-11')"); $res = cubrid_execute($conn, "SELECT * FROM fetch_object_tb order by c1"); if (!$res) { printf("[003] [%d] %s\n", cubrid_error_code(), cubrid_error_msg()); exit(1); } printf("cubrid_fetch_object(res) start\n"); var_dump(cubrid_fetch_object($res)); class cubrid_fetch_object_test { public $c1 = NULL; public $c2 = NULL; public function toString() { var_dump($this); } }