Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add single/double string quote function.

  • Loading branch information...
commit c7c32a44a5961ede5ea371872e7b52c56fe2a398 1 parent ae03acf
@c9s authored
View
62 ext/php_sqlbuilder.c
@@ -14,7 +14,9 @@ PHPAPI zend_class_entry *spl_ce_ArrayObject;
static const zend_function_entry sqlbuilder_functions[] = {
- PHP_FE(sqlbuilder_test, NULL)
+ PHP_FE(sqlbuilder_single_quote, NULL)
+ PHP_FE(sqlbuilder_double_quote, NULL)
+ // PHP_FE(sqlbuilder_test, NULL)
{NULL, NULL, NULL}
};
@@ -42,14 +44,8 @@ ZEND_GET_MODULE(sqlbuilder)
PHP_MINIT_FUNCTION(sqlbuilder)
{
- PHP_MINIT(sqlbuilder_driver)(INIT_FUNC_ARGS_PASSTHRU);
- // PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
- // PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
- // PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
- // PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
- // PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
- // PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
- return SUCCESS;
+ PHP_MINIT(sqlbuilder_driver)(INIT_FUNC_ARGS_PASSTHRU);
+ return SUCCESS;
}
PHP_MINIT_FUNCTION(sqlbuilder_driver)
@@ -58,9 +54,53 @@ PHP_MINIT_FUNCTION(sqlbuilder_driver)
}
-PHP_FUNCTION(sqlbuilder_test)
+PHPAPI void str_column_double_quote(char * str, int str_len, zval * return_value)
+{
+ char *newstr;
+ int newstr_len;
+
+ newstr_len = str_len + 2;
+ newstr = emalloc( sizeof(char) * (newstr_len) );
+ memcpy(newstr, "\"", 1);
+ memcpy(newstr + 1, str, str_len);
+ memcpy(newstr + 1 + str_len, "\"", 1);
+ RETURN_STRINGL(newstr, newstr_len, 0);
+}
+
+PHPAPI void str_column_single_quote(char * str, int str_len, zval * return_value)
+{
+ char *newstr;
+ int newstr_len;
+
+ newstr_len = str_len + 2;
+ newstr = emalloc( sizeof(char) * (newstr_len) );
+ memcpy(newstr, "'", 1);
+ memcpy(newstr + 1, str, str_len);
+ memcpy(newstr + 1 + str_len, "'", 1);
+ RETURN_STRINGL(newstr, newstr_len, 0);
+}
+
+
+/* proto: sqlbuilder_single_quote('string') */
+PHP_FUNCTION(sqlbuilder_single_quote)
+{
+ char *str;
+ int str_len = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len ) == FAILURE) {
+ RETURN_FALSE;
+ }
+ str_column_single_quote(str, str_len, return_value);
+}
+
+PHP_FUNCTION(sqlbuilder_double_quote)
{
- RETURN_STRING("Hello World", 1);
+ char *str;
+ int str_len = 0;
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len ) == FAILURE) {
+ RETURN_FALSE;
+ }
+ str_column_double_quote(str, str_len, return_value);
}
View
3  ext/php_sqlbuilder.h
@@ -5,7 +5,8 @@
#define PHP_SQLBUILDER_VERSION "1.0"
#define PHP_SQLBUILDER_EXTNAME "sqlbuilder"
-PHP_FUNCTION(sqlbuilder_test);
+PHP_FUNCTION(sqlbuilder_single_quote);
+PHP_FUNCTION(sqlbuilder_double_quote);
PHP_MINIT_FUNCTION(sqlbuilder);
PHP_MINIT_FUNCTION(sqlbuilder_driver);
View
17 ext/tests/FunctionTest.php
@@ -0,0 +1,17 @@
+<?php
+
+class FunctionTest extends PHPUnit_Framework_TestCase
+{
+ public function testSingleQuote()
+ {
+ $val = sqlbuilder_single_quote('name');
+ is("'name'",$val);
+ }
+
+ public function testDoubleQuote()
+ {
+ $val = sqlbuilder_double_quote('name');
+ is("\"name\"",$val);
+ }
+}
+
View
20 ext/tests/bootstrap.php
@@ -0,0 +1,20 @@
+<?php
+require 'PHPUnit/TestMore.php';
+require 'PHPUnit/Framework/ExtensionTestCase.php';
+// require '../PHPUnit_Framework_ExtensionTestCase/src/PHPUnit/Framework/ExtensionTestCase.php';
+require 'Universal/ClassLoader/BasePathClassLoader.php';
+
+
+if ( ! extension_loaded('sqlbuilder') ) {
+ // require "src/FileUtil.php";
+}
+
+if ( !defined('BASEDIR') ) {
+ define('BASEDIR',dirname(dirname(__FILE__)));
+}
+$classLoader = new \Universal\ClassLoader\BasePathClassLoader(array(
+ BASEDIR . '/src',
+ BASEDIR . '/vendor/pear',
+));
+$classLoader->useIncludePath(false);
+$classLoader->register();
Please sign in to comment.
Something went wrong with that request. Please try again.