Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Escape array keys used in sqlp @KEYS/@LIST macros and add missing @FILTER whitelist, fix for GHSA-fwp9-mpg4-hh5w

Givana62d0824b664
diff --git a/admin/sql/mysqli/admin_failed_login.sql b/admin/sql/mysqli/admin_failed_login.sql
index 724eede..3f64828 100644
--- a/admin/sql/mysqli/admin_failed_login.sql
+++ b/admin/sql/mysqli/admin_failed_login.sql
@@ -5,13 +5,13 @@
CREATE PROCEDURE getAll(
IN start INT,
IN limit INT,
+ IN cursor INT, -- current cursor (admin_id) for cursor pagination
IN admin_id INT,
IN count INT,
IN updated_at CHAR,
-- return array of admin_failed_login
OUT fetch_all,
- -- return admin_failed_login count for count query
OUT fetch_one
)
BEGIN
@@ -33,18 +33,34 @@
AND admin_failed_login.updated_at = :updated_at
END @IF
- ORDER BY admin_failed_login.admin_id, admin_failed_login.updated_at
+ -- cursor pagination
+
+ @IF isset(:cursor)
+
+ THEN
+
+ AND admin_failed_login.admin_id > :cursor
+
+ END @IF
+
+ ORDER BY admin_failed_login.admin_id, admin_failed_login.updated_at
+
+ -- limit
+ @IF isset(:limit)
+ THEN
+ LIMIT :limit
+
+ @IF isset(:start)
+ THEN
+ OFFSET :start
+ END @IF
- -- limit
- @IF isset(:limit)
- THEN
- @SQL_LIMIT(:start, :limit)
- END @IF;
+ END @IF;
-- SELECT FOUND_ROWS() as count;
SELECT count(*) FROM (
- @SQL_COUNT(admin_failed_login.admin_id, admin) -- this takes previous query removes limit and replaces select columns with parameter admin_id
+ @SQL_COUNT(admin_failed_login.admin_id, admin_failed_login) -- this takes previous query removes limit and replaces select columns with parameter admin_id
) as count;
@@ -119,6 +135,7 @@
IN username CHAR,
IN updated_at CHAR,
IN last_ip INT,
+ IN email CHAR,
OUT fetch_one,
OUT insert_id
)
@@ -141,11 +158,6 @@
AND email = :email
END @IF
- @IF isset(:admin_id)
- THEN
- AND admin_id = :admin_id
- END @IF
-
LIMIT 1;
@IF isset(@result.admin)
@@ -165,7 +177,7 @@
-- Add new admin_failed_login
CREATE PROCEDURE add(
- IN admin_failed_login ARRAY,
+ IN admin_failed_login ROW,
OUT insert_id
)
BEGIN
@@ -185,54 +197,39 @@
CREATE PROCEDURE edit(
IN admin_id INT,
- IN admin_failed_login ARRAY,
+ IN admin_failed_login ROW,
OUT affected_rows
)
BEGIN
-- allow only table fields and set defaults for missing values
@FILTER(:admin_failed_login, admin_failed_login)
+ @IF !empty(:admin_failed_login)
+ THEN
UPDATE admin_failed_login
SET @LIST(:admin_failed_login)
- WHERE admin_id = :admin_id;
+ WHERE admin_id = :admin_id
+ END @IF;
END
-- delete admin_failed_login
- PROCEDURE delete(
+ CREATE PROCEDURE delete(
IN admin_id ARRAY,
- IN updated_at CHAR,
- IN count INT,
OUT affected_rows
)
BEGIN
- DELETE admin_failed_login FROM admin_failed_login
- INNER JOIN admin.admin_id ON admin = admin_failed_login.admin_id
- WHERE
+ DELETE FROM admin_failed_login
+ WHERE 1 = 1
@IF isset(:admin_id) AND !empty(:admin_id)
- THEN
- AND admin_failed_login.admin_id = :admin_id
- END @IF
-
- @IF isset(:username)
- THEN
- AND admin.username = :username
- END @IF
+ THEN
+ AND admin_failed_login.admin_id IN (:admin_id)
+ END @IF
- @IF isset(:email)
- THEN
- AND admin.email = :email
- END @IF
-
- @IF isset(:admin_id)
- THEN
- AND admin.admin_id IN (:admin_id)
- END @IF
-
- END
+ END
diff --git a/admin/sql/mysqli/order.sql b/admin/sql/mysqli/order.sql
index 062f798..4e8c6f5 100644
--- a/admin/sql/mysqli/order.sql
+++ b/admin/sql/mysqli/order.sql
@@ -1,501 +1,513 @@
--- Orders
-
- -- get all orders
-
- CREATE PROCEDURE getAll(
- -- variables
- IN language_id INT,
- IN site_id INT,
- IN user_id INT
-
- IN order_status CHAR,
- IN order_status_id INT,
-
- IN payment_status CHAR,
- IN payment_status_id INT,
-
- IN shipping_status CHAR,
- IN shipping_status_id INT,
-
- IN email CHAR,
- IN phone_number CHAR,
- IN search CHAR,
-
- -- pagination
- IN start INT,
- IN limit INT,
- IN order_by CHAR,
- IN direction CHAR,
-
-
- -- return
- OUT fetch_all, -- orders
- OUT fetch_one -- count
- )
- BEGIN
-
- SELECT `order`.*,os.name as order_status,ops.name as payment_status,oss.name as shipping_status FROM `order` AS `order`
-
- LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
- LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND oss.language_id = :language_id)
- LEFT JOIN payment_status AS ops ON (`order`.order_status_id = ops.payment_status_id AND os.language_id = :language_id)
-
- WHERE 1 = 1
-
- AND `order`.site_id = :site_id
-
- @IF isset(:user_id)
- THEN
- AND `order`.user_id = :user_id
- END @IF
-
- @IF isset(:order_status)
- THEN
- AND os.name = :order_status
- END @IF
-
- @IF isset(:order_status_id)
- THEN
- AND `order`.order_status_id = :order_status_id
- END @IF
-
- @IF isset(:shipping_status)
- THEN
- AND oss.name = :shipping_status
- END @IF
-
- @IF isset(:shipping_status_id)
- THEN
- AND `order`.shipping_status_id = :shipping_status_id
- END @IF
-
- @IF isset(:payment_status)
- THEN
- AND ops.name = :payment_status
- END @IF
-
- @IF isset(:payment_status_id)
- THEN
- AND `order`.payment_status_id = :payment_status_id
- END @IF
-
- @IF isset(:email) AND !empty(:email)
- THEN
- AND `order`.email = :email
- END @IF
-
- @IF isset(:phone_number) AND !empty(:phone_number)
- THEN
- AND `order`.phone_number = :phone_number
- END @IF
-
- -- search
- @IF isset(:search) AND !empty(:search)
- THEN
- AND
- `order`.first_name LIKE CONCAT('%',:search,'%') OR
- `order`.last_name LIKE CONCAT('%',:search,'%')
- END @IF
-
- -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
- @IF isset(:order_by)
- THEN
- -- ORDER BY $order_by $direction
- ORDER BY order.@ESC(:order_by) @ESC(:direction)
- @ELSE
- ORDER BY order_id DESC
- END @IF
-
- @SQL_LIMIT(:start, :limit);
-
- SELECT count(*) FROM (
-
- @SQL_COUNT(order_id, order) -- this takes previous query removes limit and replaces select columns with parameter order_id
-
- ) as count;
-
- END
-
-
- CREATE PROCEDURE get(
- IN order_id INT,
- IN customer_order_id CHAR,
- IN user_id INT,
- IN email CHAR,
- IN language_id INT,
- OUT fetch_row,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- )
- BEGIN
-
- -- order
- SELECT `order`.*,
- os.name as order_status,
- ops.name as payment_status,
- oss.name as shipping_status,
- bc.name as billing_country,
- sc.name as shipping_country,
- br.name as billing_region,
- sr.name as shipping_region
-
- FROM `order`
-
- LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
- LEFT JOIN payment_status AS ops ON (`order`.payment_status_id = ops.payment_status_id AND os.language_id = :language_id)
- LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND os.language_id = :language_id)
- -- country
- LEFT JOIN country AS bc ON (`order`.billing_country_id = bc.country_id)
- LEFT JOIN country AS sc ON (`order`.shipping_country_id = sc.country_id)
- -- Region
- LEFT JOIN region AS br ON (`order`.billing_region_id = br.region_id)
- LEFT JOIN region AS sr ON (`order`.shipping_region_id = sr.region_id)
-
- WHERE 1 = 1
-
- @IF isset(:user_id)
- THEN
- AND `order`.user_id = :user_id
- END @IF
-
- @IF isset(:order_id)
- THEN
- AND `order`.order_id = :order_id
- END @IF
-
- @IF isset(:customer_order_id)
- THEN
- AND `order`.customer_order_id = :customer_order_id
- END @IF
-
- @IF isset(:email)
- THEN
- AND `order`.email = :email
- END @IF
-
- LIMIT 1;
-
-
- SELECT `key` as array_key,value as array_value FROM order_meta as _
- WHERE _.order_id = :order_id;
-
- -- products
- SELECT *,products.name as name, products.order_product_id as array_key,
- (SELECT JSON_ARRAYAGG( JSON_OBJECT('order_product_option_id', opo.order_product_option_id, 'product_option_id' , opo.product_option_id, 'product_option_value_id' , opo.product_option_value_id, 'option', opo.option, 'name' , opo.name, 'price' , opo.price, 'type' , opo.type) )
- FROM order_product_option AS opo
- WHERE opo.order_product_id = products.order_product_id
- ) as option_value
- FROM order_product as products
- LEFT JOIN product ON product.product_id = products.product_id
- LEFT JOIN product_content ON product_content.product_id = products.product_id
-
- @IF isset(:language_id)
- THEN
- AND product_content.language_id = :language_id
- END @IF
-
- -- use @result.order.order_id instead of :order_id to work when :customer_order_id is used
- WHERE products.order_id = @result.order.order_id;
-
- -- log
- SELECT *,os.name as order_status
- FROM order_log as log
- LEFT JOIN order_status AS os ON (log.order_status_id = os.order_status_id AND os.language_id = :language_id)
- WHERE log.order_id = @result.order.order_id;
-
- -- meta
- SELECT * FROM order_meta as meta
- WHERE meta.order_id = @result.order.order_id;
-
- -- total
- SELECT * FROM order_total as total
- WHERE total.order_id = @result.order.order_id;
-
- -- shipment
- SELECT * FROM order_shipment as shipment
- WHERE shipment.order_id = @result.order.order_id;
-
- -- voucher
- SELECT * FROM order_voucher as voucher
- WHERE voucher.order_id = @result.order.order_id;
-
- END
-
-
- PROCEDURE getData(
- IN order_id INT,
- IN language_id INT,
- IN billing_country_id INT,
- IN shipping_country_id INT,
-
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- )
- BEGIN
-
- -- order status
- SELECT
-
- order_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM order_status as order_status_id WHERE language_id = :language_id;
-
- -- payment status
- SELECT
-
- payment_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM payment_status as payment_status_id WHERE language_id = :language_id;
-
- -- shipping status
- SELECT
-
- shipping_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM shipping_status as shipping_status_id WHERE language_id = :language_id;
-
- -- billing_country
- SELECT
- country_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM country as billing_country_id WHERE status = 1;
-
- -- shipping_country
- SELECT
- country_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM country as shipping_country_id WHERE status = 1;
-
- -- billing_region
- SELECT
- region_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM region as billing_region_id
- WHERE status = 1
-
- @IF isset(:billing_country_id)
- THEN
- AND billing_region_id.country_id = :billing_country_id
- END @IF;
-
- -- shipping_region
- SELECT
- region_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM region as shipping_region_id
- WHERE status = 1
-
- @IF isset(:shipping_country_id)
- THEN
- AND shipping_region_id.country_id = :shipping_country_id
- END @IF;
-
- END
-
- -- delete order
-
- PROCEDURE delete(
- IN order_id ARRAY,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows
- )
- BEGIN
-
- DELETE FROM order_product WHERE order_id IN (:order_id);
- DELETE FROM order_product_option WHERE order_id IN (:order_id);
- DELETE FROM order_subscription WHERE order_id IN (:order_id);
- DELETE FROM order_shipment WHERE order_id IN (:order_id);
- DELETE FROM order_voucher WHERE order_id IN (:order_id);
- DELETE FROM order_total WHERE order_id IN (:order_id);
- DELETE FROM voucher WHERE order_id IN (:order_id);
- DELETE FROM voucher_log WHERE order_id IN (:order_id);
- DELETE FROM order_log WHERE order_id IN (:order_id);
- DELETE FROM `order` WHERE order_id IN (:order_id);
-
- END
-
-
- -- add order
-
- CREATE PROCEDURE add(
- IN order ARRAY,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id
- )
- BEGIN
-
- :products = @FILTER(:order.products, order_product, false, true)
- :totals = @FILTER(:order.totals, order_total, false, true)
-
- @FILTER(:order, order)
-
- INSERT INTO `order`
-
- ( @KEYS(:order) )
-
- VALUES ( :order );
-
- -- insert order products
- @EACH(:products)
- INSERT INTO order_product
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each );
-
- -- insert product option
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each );
-
- -- insert order totals
- @EACH(:totals)
- INSERT INTO order_total
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each );
-
- END
-
- -- edit order
-
- CREATE PROCEDURE edit(
- IN order_id INT,
- IN order ARRAY,
- OUT affected_rows
- )
- BEGIN
-
- @FILTER(:order, order)
-
- UPDATE `order`
-
- SET @LIST(:order)
-
- WHERE order_id = :order_id;
-
- END
-
- -- products
-
- CREATE PROCEDURE addProduct(
- IN product ARRAY,
- IN product_options ARRAY,
- IN order_id INT,
- OUT insert_id,
- OUT insert_id
- )
- BEGIN
-
- :product = @FILTER(:product, order_product, false)
- :product_options = @FILTER(:product_options, order_product_option, false, true)
-
- INSERT INTO order_product
- ( order_id, @KEYS(:product) )
- VALUES ( :order_id, :product );
-
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, order_product_id, @KEYS(:each) )
- VALUES ( :order_id, @result.order_product, :each );
- END
-
- CREATE PROCEDURE editProduct(
- IN order_product_id INT,
- IN product ARRAY,
- IN product_options ARRAY,
- IN order_id INT,
- OUT affected_rows,
- OUT affected_rows
- )
- BEGIN
-
- :product = @FILTER(:product, order_product)
- :product_options = @FILTER(:product_options, order_product_option, false, true)
-
- UPDATE `order_product`
-
- SET @LIST(:product)
-
- WHERE order_product_id = :order_product_id;
-
- -- insert product option
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, order_product_id, @KEYS(:each) )
- VALUES ( :order_id, :order_product_id, :each )
- ON DUPLICATE KEY UPDATE @LIST(:each);
- END
-
- PROCEDURE deleteProduct(
- IN order_product_id ARRAY,
- OUT affected_rows,
- )
- BEGIN
-
- DELETE FROM order_product WHERE order_product_id IN (:order_product_id);
-
- END
-
- -- totals
-
- CREATE PROCEDURE addTotal(
- IN total ARRAY,
- IN order_id INT,
- OUT insert_id
- )
- BEGIN
-
- :total = @FILTER(:total, order_total)
-
- INSERT INTO order_total
- ( order_id, @KEYS(:total) )
- VALUES ( :order_id, :total );
-
- END
-
- CREATE PROCEDURE editTotal(
- IN order_total_id INT,
- IN total ARRAY,
- OUT affected_rows
- )
- BEGIN
-
- :total = @FILTER(:total, order_total)
-
- UPDATE `order_total`
-
- SET @LIST(:total)
-
- WHERE order_total_id = :order_total_id;
-
- END
-
- PROCEDURE deleteTotal(
- IN order_total_id ARRAY,
- OUT affected_rows,
- )
- BEGIN
-
- DELETE FROM order_total WHERE order_total_id IN (:order_total_id);
-
- END
+-- Orders
+
+ -- get all orders
+
+ CREATE PROCEDURE getAll(
+ -- variables
+ IN language_id INT,
+ IN site_id INT,
+ IN user_id INT,
+
+ IN order_status CHAR,
+ IN order_status_id INT,
+
+ IN payment_status CHAR,
+ IN payment_status_id INT,
+
+ IN shipping_status CHAR,
+ IN shipping_status_id INT,
+
+ IN email CHAR,
+ IN phone_number CHAR,
+ IN search CHAR,
+
+ -- pagination
+ IN start INT,
+ IN limit INT,
+ IN cursor INT, -- current cursor (order_id) for cursor pagination
+ IN order_by CHAR,
+ IN direction CHAR,
+
+
+ -- return
+ OUT fetch_all, -- orders
+ OUT fetch_one -- count
+ )
+ BEGIN
+
+ SELECT `order`.*,os.name as order_status,ops.name as payment_status,oss.name as shipping_status FROM `order` AS `order`
+
+ LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
+ LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND oss.language_id = :language_id)
+ LEFT JOIN payment_status AS ops ON (`order`.payment_status_id = ops.payment_status_id AND ops.language_id = :language_id)
+
+ WHERE 1 = 1
+
+ AND `order`.site_id = :site_id
+
+ @IF isset(:user_id)
+ THEN
+ AND `order`.user_id = :user_id
+ END @IF
+
+ @IF isset(:order_status)
+ THEN
+ AND os.name = :order_status
+ END @IF
+
+ @IF isset(:order_status_id)
+ THEN
+ AND `order`.order_status_id = :order_status_id
+ END @IF
+
+ @IF isset(:shipping_status)
+ THEN
+ AND oss.name = :shipping_status
+ END @IF
+
+ @IF isset(:shipping_status_id)
+ THEN
+ AND `order`.shipping_status_id = :shipping_status_id
+ END @IF
+
+ @IF isset(:payment_status)
+ THEN
+ AND ops.name = :payment_status
+ END @IF
+
+ @IF isset(:payment_status_id)
+ THEN
+ AND `order`.payment_status_id = :payment_status_id
+ END @IF
+
+ @IF isset(:email) AND !empty(:email)
+ THEN
+ AND `order`.email = :email
+ END @IF
+
+ @IF isset(:phone_number) AND !empty(:phone_number)
+ THEN
+ AND `order`.phone_number = :phone_number
+ END @IF
+
+ -- search
+ @IF isset(:search) AND !empty(:search)
+ THEN
+ AND
+ (`order`.first_name LIKE CONCAT('%',:search,'%') OR
+ `order`.last_name LIKE CONCAT('%',:search,'%'))
+ END @IF
+
+ -- cursor pagination
+
+ @IF isset(:cursor)
+
+ THEN
+
+ AND `order`.order_id > :cursor
+
+ END @IF
+
+ -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
+ @IF isset(:order_by)
+ THEN
+ -- ORDER BY $order_by $direction
+ ORDER BY order.@ESC(:order_by) @ESC(:direction)
+ @ELSE
+ ORDER BY order_id DESC
+ END @IF
+
+ LIMIT :limit OFFSET :start;
+
+ SELECT count(*) FROM (
+
+ @SQL_COUNT(order_id, order) -- this takes previous query removes limit and replaces select columns with parameter order_id
+
+ ) as count;
+
+ END
+
+
+ CREATE PROCEDURE get(
+ IN order_id INT,
+ IN customer_order_id CHAR,
+ IN user_id INT,
+ IN email CHAR,
+ IN language_id INT,
+ OUT fetch_row,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all
+ )
+ BEGIN
+
+ -- order
+ SELECT `order`.*,
+ os.name as order_status,
+ ops.name as payment_status,
+ oss.name as shipping_status,
+ bc.name as billing_country,
+ sc.name as shipping_country,
+ br.name as billing_region,
+ sr.name as shipping_region
+
+ FROM `order`
+
+ LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
+ LEFT JOIN payment_status AS ops ON (`order`.payment_status_id = ops.payment_status_id AND ops.language_id = :language_id)
+ LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND os.language_id = :language_id)
+ -- country
+ LEFT JOIN country AS bc ON (`order`.billing_country_id = bc.country_id)
+ LEFT JOIN country AS sc ON (`order`.shipping_country_id = sc.country_id)
+ -- Region
+ LEFT JOIN region AS br ON (`order`.billing_region_id = br.region_id)
+ LEFT JOIN region AS sr ON (`order`.shipping_region_id = sr.region_id)
+
+ WHERE 1 = 1
+
+ @IF isset(:user_id)
+ THEN
+ AND `order`.user_id = :user_id
+ END @IF
+
+ @IF isset(:order_id)
+ THEN
+ AND `order`.order_id = :order_id
+ END @IF
+
+ @IF isset(:customer_order_id)
+ THEN
+ AND `order`.customer_order_id = :customer_order_id
+ END @IF
+
+ @IF isset(:email)
+ THEN
+ AND `order`.email = :email
+ END @IF
+
+ LIMIT 1;
+
+
+ SELECT `key` as array_key,value as array_value FROM order_meta as _
+ WHERE _.order_id = :order_id;
+
+ -- products
+ SELECT *,products.name as name, products.order_product_id as array_key,
+ (SELECT JSON_ARRAYAGG( JSON_OBJECT('order_product_option_id', opo.order_product_option_id, 'product_option_id' , opo.product_option_id, 'product_option_value_id' , opo.product_option_value_id, 'option', opo.option, 'name' , opo.name, 'price' , opo.price, 'type' , opo.type) )
+ FROM order_product_option AS opo
+ WHERE opo.order_product_id = products.order_product_id
+ ) as option_value
+ FROM order_product as products
+ LEFT JOIN product ON product.product_id = products.product_id
+ LEFT JOIN product_content ON product_content.product_id = products.product_id
+
+ @IF isset(:language_id)
+ THEN
+ AND product_content.language_id = :language_id
+ END @IF
+
+ -- use @result.order.order_id instead of :order_id to work when :customer_order_id is used
+ WHERE products.order_id = @result.order.order_id;
+
+ -- log
+ SELECT *,os.name as order_status
+ FROM order_log as log
+ LEFT JOIN order_status AS os ON (log.order_status_id = os.order_status_id AND os.language_id = :language_id)
+ WHERE log.order_id = @result.order.order_id;
+
+ -- meta
+ SELECT * FROM order_meta as meta
+ WHERE meta.order_id = @result.order.order_id;
+
+ -- total
+ SELECT * FROM order_total as total
+ WHERE total.order_id = @result.order.order_id;
+
+ -- shipment
+ SELECT * FROM order_shipment as shipment
+ WHERE shipment.order_id = @result.order.order_id;
+
+ -- voucher
+ SELECT * FROM order_voucher as voucher
+ WHERE voucher.order_id = @result.order.order_id;
+
+ END
+
+
+ CREATE PROCEDURE getData(
+ IN language_id INT,
+ IN billing_country_id INT,
+ IN shipping_country_id INT,
+
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all
+ )
+ BEGIN
+
+ -- order status
+ SELECT
+
+ order_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM order_status as order_status_id WHERE language_id = :language_id;
+
+ -- payment status
+ SELECT
+
+ payment_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM payment_status as payment_status_id WHERE language_id = :language_id;
+
+ -- shipping status
+ SELECT
+
+ shipping_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM shipping_status as shipping_status_id WHERE language_id = :language_id;
+
+ -- billing_country
+ SELECT
+ country_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM country as billing_country_id WHERE status = 1;
+
+ -- shipping_country
+ SELECT
+ country_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM country as shipping_country_id WHERE status = 1;
+
+ -- billing_region
+ SELECT
+ region_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM region as billing_region_id
+ WHERE status = 1
+
+ @IF isset(:billing_country_id)
+ THEN
+ AND billing_region_id.country_id = :billing_country_id
+ END @IF;
+
+ -- shipping_region
+ SELECT
+ region_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM region as shipping_region_id
+ WHERE status = 1
+
+ @IF isset(:shipping_country_id)
+ THEN
+ AND shipping_region_id.country_id = :shipping_country_id
+ END @IF;
+
+ END
+
+ -- delete order
+
+ CREATE PROCEDURE delete(
+ IN order_id ARRAY,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_product WHERE order_id IN (:order_id);
+ DELETE FROM order_product_option WHERE order_id IN (:order_id);
+ DELETE FROM order_subscription WHERE order_id IN (:order_id);
+ DELETE FROM order_shipment WHERE order_id IN (:order_id);
+ DELETE FROM order_voucher WHERE order_id IN (:order_id);
+ DELETE FROM order_total WHERE order_id IN (:order_id);
+ DELETE FROM voucher WHERE order_id IN (:order_id);
+ DELETE FROM voucher_log WHERE order_id IN (:order_id);
+ DELETE FROM order_log WHERE order_id IN (:order_id);
+ DELETE FROM `order` WHERE order_id IN (:order_id);
+
+ END
+
+
+ -- add order
+
+ CREATE PROCEDURE add(
+ IN order ARRAY,
+ OUT insert_id,
+ OUT insert_id,
+ OUT insert_id,
+ OUT insert_id
+ )
+ BEGIN
+
+ :products = @FILTER(:order.products, order_product, false, true)
+ :totals = @FILTER(:order.totals, order_total, false, true)
+
+ @FILTER(:order, order)
+
+ INSERT INTO `order`
+
+ ( @KEYS(:order) )
+
+ VALUES ( :order );
+
+ -- insert order products
+ @EACH(:products)
+ INSERT INTO order_product
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each );
+
+ -- insert product option
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each );
+
+ -- insert order totals
+ @EACH(:totals)
+ INSERT INTO order_total
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each );
+
+ END
+
+ -- edit order
+
+ CREATE PROCEDURE edit(
+ IN order_id INT,
+ IN order ROW,
+ OUT affected_rows
+ )
+ BEGIN
+
+ @FILTER(:order, order)
+
+ UPDATE `order`
+
+ SET @LIST(:order)
+
+ WHERE order_id = :order_id;
+
+ END
+
+ -- products
+
+ CREATE PROCEDURE addProduct(
+ IN product ROW,
+ IN product_options ROWS,
+ IN order_id INT,
+ OUT insert_id,
+ OUT insert_id
+ )
+ BEGIN
+
+ :product = @FILTER(:product, order_product, false)
+ :product_options = @FILTER(:product_options, order_product_option, false, true)
+
+ INSERT INTO order_product
+ ( order_id, @KEYS(:product) )
+ VALUES ( :order_id, :product );
+
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, order_product_id, @KEYS(:each) )
+ VALUES ( :order_id, @result.order_product, :each );
+ END
+
+ CREATE PROCEDURE editProduct(
+ IN order_product_id INT,
+ IN product ROW,
+ IN product_options ROWS,
+ IN order_id INT,
+ OUT affected_rows,
+ OUT affected_rows
+ )
+ BEGIN
+
+ :product = @FILTER(:product, order_product)
+ :product_options = @FILTER(:product_options, order_product_option, false, true)
+
+ UPDATE `order_product`
+
+ SET @LIST(:product)
+
+ WHERE order_product_id = :order_product_id;
+
+ -- insert product option
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, order_product_id, @KEYS(:each) )
+ VALUES ( :order_id, :order_product_id, :each )
+ ON DUPLICATE KEY UPDATE @LIST(:each);
+ END
+
+ CREATE PROCEDURE deleteProduct(
+ IN order_product_id ARRAY,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_product WHERE order_product_id IN (:order_product_id);
+
+ END
+
+ -- totals
+
+ CREATE PROCEDURE addTotal(
+ IN total ROW,
+ IN order_id INT,
+ OUT insert_id
+ )
+ BEGIN
+
+ :total = @FILTER(:total, order_total)
+
+ INSERT INTO order_total
+ ( order_id, @KEYS(:total) )
+ VALUES ( :order_id, :total );
+
+ END
+
+ CREATE PROCEDURE editTotal(
+ IN order_total_id INT,
+ IN total ROW,
+ OUT affected_rows
+ )
+ BEGIN
+
+ :total = @FILTER(:total, order_total)
+
+ UPDATE `order_total`
+
+ SET @LIST(:total)
+
+ WHERE order_total_id = :order_total_id;
+
+ END
+
+ CREATE PROCEDURE deleteTotal(
+ IN order_total_id ARRAY,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_total WHERE order_total_id IN (:order_total_id);
+
+ END
diff --git a/admin/sql/mysqli/product.sql b/admin/sql/mysqli/product.sql
index 1ee10a0..2ef5593 100644
--- a/admin/sql/mysqli/product.sql
+++ b/admin/sql/mysqli/product.sql
@@ -2,7 +2,7 @@
-- get one product
- PROCEDURE get(
+ CREATE PROCEDURE get(
IN product_id INT,
IN admin_id INT,
IN slug CHAR,
@@ -10,7 +10,6 @@
IN language_id INT,
IN promotion INT, -- include promotional price
IN user_group_id INT, -- user_group_id for group promotional price
- IN variant INT, -- include variants
IN variant_price INT, -- include variants min max prices
IN points INT, -- include points
IN stock_status INT, -- include stock_status
@@ -78,7 +77,7 @@
FROM product_points pp
WHERE pp.product_id = _.product_id
AND pp.user_group_id = :user_group_id
- AS points
+ ) AS points
END @IF
@@ -304,8 +303,7 @@
END
- PROCEDURE getData(
- IN product_id INT,
+ CREATE PROCEDURE getData(
IN manufacturer_id INT,
IN vendor_id INT,
@@ -316,9 +314,8 @@
OUT fetch_all,
OUT fetch_all,
OUT fetch_all,
-
- OUT fetch_one,
OUT fetch_one,
+ OUT fetch_one
)
BEGIN
@@ -401,9 +398,17 @@
-- Delete product
- PROCEDURE delete(
+ CREATE PROCEDURE delete(
IN product_id ARRAY,
IN admin_id INT,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
OUT affected_rows
)
BEGIN
@@ -448,7 +453,21 @@
THEN
INNER JOIN product ON (product.product_id = product_image.product_id AND product.admin_id = :admin_id)
END @IF
- WHERE product_image.product_id IN (:product_id);
+ WHERE product_image.product_id IN (:product_id);
+
+ DELETE product_question FROM product_question
+ @IF isset(:admin_id)
+ THEN
+ INNER JOIN product ON (product.product_id = product_question.product_id AND product.admin_id = :admin_id)
+ END @IF
+ WHERE product_question.product_id IN (:product_id);
+
+ DELETE product_review FROM product_review
+ @IF isset(:admin_id)
+ THEN
+ INNER JOIN product ON (product.product_id = product_review.product_id AND product.admin_id = :admin_id)
+ END @IF
+ WHERE product_review.product_id IN (:product_id);
DELETE product FROM product WHERE product_id IN (:product_id)
@IF isset(:admin_id)
@@ -460,9 +479,9 @@
-- Edit product
- PROCEDURE edit(
- IN product ARRAY,
- IN product_content ARRAY,
+ CREATE PROCEDURE edit(
+ IN product ROW,
+ IN product_content ROWS,
IN taxonomy_item_id ARRAY,
IN product_id INT,
IN site_id ARRAY,
@@ -542,7 +561,7 @@
-- Edit post content
CREATE PROCEDURE editContent(
- IN product_content ARRAY,
+ IN product_content ROW,
IN product_id INT,
IN language_id INT,
OUT affected_rows
@@ -562,14 +581,14 @@
-- Add new product
CREATE PROCEDURE add(
- IN product ARRAY,
- IN product_content ARRAY,
+ IN product ROW,
+ IN product_content ROWS,
IN taxonomy_item_id ARRAY,
IN site_id ARRAY,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id
+ OUT insert_id, -- product
+ OUT affected_rows, -- product_content
+ OUT affected_rows, -- taxonomy_item_id
+ OUT affected_rows -- product_to_site
)
BEGIN
@@ -612,7 +631,7 @@
-- Edit product
- PROCEDURE productImage(
+ CREATE PROCEDURE productImage(
IN product_image ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -635,7 +654,7 @@
-- Edit product related
- PROCEDURE productRelated(
+ CREATE PROCEDURE productRelated(
IN product_related ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -658,8 +677,8 @@
-- Edit product variant
- PROCEDURE productVariant(
- IN product_variant ARRAY,
+ CREATE PROCEDURE productVariant(
+ IN product_variant ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -679,8 +698,8 @@
-- Edit product subscription
- PROCEDURE productSubscription(
- IN product_subscription ARRAY,
+ CREATE PROCEDURE productSubscription(
+ IN product_subscription ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -700,8 +719,8 @@
-- Edit product discount
- PROCEDURE productDiscount(
- IN product_discount ARRAY,
+ CREATE PROCEDURE productDiscount(
+ IN product_discount ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -721,8 +740,8 @@
-- Edit product promotion
- PROCEDURE productPromotion(
- IN product_promotion ARRAY,
+ CREATE PROCEDURE productPromotion(
+ IN product_promotion ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -742,8 +761,8 @@
-- Edit product points
- PROCEDURE productPoints(
- IN product_points ARRAY,
+ CREATE PROCEDURE productPoints(
+ IN product_points ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -763,10 +782,9 @@
-- Edit product attribute
- PROCEDURE productAttribute(
- IN product_attribute ARRAY,
+ CREATE PROCEDURE productAttribute(
+ IN product_attribute ROWS,
IN product_id INT,
- IN language_id INT,
OUT affected_rows,
OUT affected_rows
)
@@ -787,7 +805,7 @@
-- Edit product digital asset
- PROCEDURE productDigitalAsset(
+ CREATE PROCEDURE productDigitalAsset(
IN product_digital_asset ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -810,7 +828,7 @@
-- get all products
- PROCEDURE getAll(
+ CREATE PROCEDURE getAll(
-- variables
IN language_id INT,
@@ -822,8 +840,6 @@
IN manufacturer_id ARRAY,
IN vendor_id ARRAY,
IN option_value_id ARRAY,
- IN attribute_id ARRAY,
- IN field_id ARRAY,
IN related INT,
IN variant INT,
IN status INT,
@@ -840,6 +856,7 @@
-- pagination
IN start INT,
IN limit INT,
+ IN cursor INT, -- current cursor (product_id) for cursor pagination
IN type CHAR,
IN order_by CHAR,
IN direction CHAR,
@@ -862,8 +879,7 @@
-- return array of products for products query
OUT fetch_all,
- -- return products count for count query
- OUT fetch_one,
+ OUT fetch_one
)
BEGIN
@@ -879,7 +895,7 @@
@IF !empty(:author)
THEN
- ,ad.display_name, ad.first_name, ad.last_name, ad.email, ad.last_name, ad.phone_number, ad.phone_number, ad.url, ad.avatar, ad.bio, ad.status, ad.created_at
+ ,ad.display_name, ad.first_name, ad.last_name, ad.email, ad.phone_number, ad.url, ad.avatar, ad.bio, ad.status, ad.created_at
END @IF
-- include image gallery
@@ -904,6 +920,11 @@
OR pd2.from_date < NOW())
AND (pd2.to_date = NULL
OR pd2.to_date > NOW()))
+ -- cursor pagination
+ @IF isset(:cursor)
+ THEN
+ AND product.product_id > :cursor
+ END @IF
ORDER BY pd2.priority ASC, pd2.price ASC
LIMIT 1) AS discount
@@ -945,7 +966,7 @@
FROM product_points pp
WHERE pp.product_id = product.product_id
AND pp.user_group_id = :user_group_id
- AS points
+ ) AS points
END @IF
@@ -994,7 +1015,7 @@
THEN
,(SELECT AVG(rating) AS total
- FROM review r1
+ FROM product_review r1
WHERE r1.product_id = product.product_id
AND r1.status = '1'
GROUP BY r1.product_id)
@@ -1008,7 +1029,7 @@
THEN
,(SELECT COUNT(*) AS total
- FROM review r2
+ FROM product_review r2
WHERE r2.product_id = product.product_id
AND r2.status = '1'
GROUP BY r2.product_id) AS reviews
@@ -1140,7 +1161,7 @@
@IF isset(:quantity) && :quantity !== ""
THEN
- AND product.quantity = :quantity
+ AND product.stock_quantity = :quantity
END @IF
@IF isset(:model) && :model !== ""
@@ -1204,7 +1225,7 @@
@IF !empty(:taxonomy_item_id)
THEN
- AND pt.taxonomy_item_id = :taxonomy_item_id
+ AND pt.taxonomy_item_id IN(:taxonomy_item_id)
END @IF
@@ -1224,13 +1245,13 @@
@IF !empty(:product_attribute_id)
THEN
- pa.product_attribute_id IN (:product_attribute_id)
+ pa.attribute_id IN (:product_attribute_id)
END @IF
@IF !empty(:product_attribute)
THEN
- pa.text IN (:product_attribute)
+ pa.value IN (:product_attribute)
END @IF
@@ -1249,10 +1270,17 @@
END @IF
- @IF isset(:limit)
- THEN
- @SQL_LIMIT(:start, :limit)
- END @IF;
+ -- limit
+ @IF isset(:limit)
+ THEN
+ LIMIT :limit
+
+ @IF isset(:start)
+ THEN
+ OFFSET :start
+ END @IF
+
+ END @IF;
-- SELECT FOUND_ROWS() as count;
diff --git a/admin/sql/pgsql/admin_failed_login.sql b/admin/sql/pgsql/admin_failed_login.sql
index 7e4824f..03b3253 100644
--- a/admin/sql/pgsql/admin_failed_login.sql
+++ b/admin/sql/pgsql/admin_failed_login.sql
@@ -5,13 +5,13 @@
CREATE PROCEDURE getAll(
IN start INT,
IN limit INT,
+ IN cursor INT, -- current cursor (admin_id) for cursor pagination
IN admin_id INT,
IN count INT,
IN updated_at CHAR,
-- return array of admin_failed_login
OUT fetch_all,
- -- return admin_failed_login count for count query
OUT fetch_one
)
BEGIN
@@ -33,12 +33,22 @@
AND admin_failed_login.updated_at = :updated_at
END @IF
+ -- cursor pagination
+
+ @IF isset(:cursor)
+
+ THEN
+
+ AND admin_failed_login.admin_id > :cursor
+
+ END @IF
+
ORDER BY admin_failed_login.admin_id, admin_failed_login.updated_at
-- limit
@IF isset(:limit)
THEN
- @SQL_LIMIT(:start, :limit)
+ LIMIT :limit OFFSET :start
END @IF;
-- SELECT FOUND_ROWS() as count;
@@ -119,8 +129,9 @@
IN username CHAR,
IN updated_at CHAR,
IN last_ip INT,
+ IN email CHAR,
OUT fetch_one,
- OUT insert_id
+ OUT affected_rows
)
BEGIN
@@ -165,8 +176,8 @@
-- Add new admin_failed_login
CREATE PROCEDURE add(
- IN admin_failed_login ARRAY,
- OUT insert_id
+ IN admin_failed_login ROW,
+ OUT fetch_one
)
BEGIN
@@ -177,7 +188,8 @@
( @KEYS(:admin_failed_login) )
- VALUES ( :admin_failed_login )
+ VALUES ( :admin_failed_login )
+ RETURNING admin_failed_login_id;
END
@@ -185,54 +197,39 @@
CREATE PROCEDURE edit(
IN admin_id INT,
- IN admin_failed_login ARRAY,
+ IN admin_failed_login ROW,
OUT affected_rows
)
BEGIN
-- allow only table fields and set defaults for missing values
@FILTER(:admin_failed_login, admin_failed_login)
+ @IF !empty(:admin_failed_login)
+ THEN
UPDATE admin_failed_login
SET @LIST(:admin_failed_login)
- WHERE admin_id = :admin_id;
+ WHERE admin_id = :admin_id
+ END @IF;
END
-- delete admin_failed_login
- PROCEDURE delete(
+ CREATE PROCEDURE delete(
IN admin_id ARRAY,
- IN updated_at CHAR,
- IN count INT,
OUT affected_rows
)
BEGIN
- DELETE admin_failed_login FROM admin_failed_login
- INNER JOIN admin.admin_id ON admin = admin_failed_login.admin_id
- WHERE
+ DELETE FROM admin_failed_login
+ WHERE 1 = 1
@IF isset(:admin_id) AND !empty(:admin_id)
- THEN
- AND admin_failed_login.admin_id = :admin_id
- END @IF
+ THEN
+ AND admin_failed_login.admin_id IN (:admin_id)
+ END @IF
- @IF isset(:username)
- THEN
- AND admin.username = :username
- END @IF
-
- @IF isset(:email)
- THEN
- AND admin.email = :email
- END @IF
-
- @IF isset(:admin_id)
- THEN
- AND admin.admin_id IN (:admin_id)
- END @IF
-
- END
+ END
diff --git a/admin/sql/pgsql/order.sql b/admin/sql/pgsql/order.sql
index 8573618..756b274 100644
--- a/admin/sql/pgsql/order.sql
+++ b/admin/sql/pgsql/order.sql
@@ -1,501 +1,513 @@
--- Orders
-
- -- get all orders
-
- CREATE PROCEDURE getAll(
- -- variables
- IN language_id INT,
- IN site_id INT,
- IN user_id INT
-
- IN order_status CHAR,
- IN order_status_id INT,
-
- IN payment_status CHAR,
- IN payment_status_id INT,
-
- IN shipping_status CHAR,
- IN shipping_status_id INT,
-
- IN email CHAR,
- IN phone_number CHAR,
- IN search CHAR,
-
- -- pagination
- IN start INT,
- IN limit INT,
- IN order_by CHAR,
- IN direction CHAR,
-
-
- -- return
- OUT fetch_all, -- orders
- OUT fetch_one -- count
- )
- BEGIN
-
- SELECT "order".*,os.name as order_status,ops.name as payment_status,oss.name as shipping_status FROM "order" AS "order"
-
- LEFT JOIN order_status AS os ON ("order".order_status_id = os.order_status_id AND os.language_id = :language_id)
- LEFT JOIN shipping_status AS oss ON ("order".shipping_status_id = oss.shipping_status_id AND oss.language_id = :language_id)
- LEFT JOIN payment_status AS ops ON ("order".order_status_id = ops.payment_status_id AND os.language_id = :language_id)
-
- WHERE 1 = 1
-
- AND "order".site_id = :site_id
-
- @IF isset(:user_id)
- THEN
- AND "order".user_id = :user_id
- END @IF
-
- @IF isset(:order_status)
- THEN
- AND os.name = :order_status
- END @IF
-
- @IF isset(:order_status_id)
- THEN
- AND "order".order_status_id = :order_status_id
- END @IF
-
- @IF isset(:shipping_status)
- THEN
- AND oss.name = :shipping_status
- END @IF
-
- @IF isset(:shipping_status_id)
- THEN
- AND "order".shipping_status_id = :shipping_status_id
- END @IF
-
- @IF isset(:payment_status)
- THEN
- AND ops.name = :payment_status
- END @IF
-
- @IF isset(:payment_status_id)
- THEN
- AND "order".payment_status_id = :payment_status_id
- END @IF
-
- @IF isset(:email) AND !empty(:email)
- THEN
- AND "order".email = :email
- END @IF
-
- @IF isset(:phone_number) AND !empty(:phone_number)
- THEN
- AND "order".phone_number = :phone_number
- END @IF
-
- -- search
- @IF isset(:search) AND !empty(:search)
- THEN
- AND
- "order".first_name LIKE '%' || :search || '%' OR
- "order".last_name LIKE '%' || :search || '%'
- END @IF
-
- -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
- @IF isset(:order_by)
- THEN
- -- ORDER BY $order_by $direction
- ORDER BY "order".@ESC(:order_by) @ESC(:direction)
- @ELSE
- ORDER BY "order".order_id DESC
- END @IF
-
- @SQL_LIMIT(:start, :limit);
-
- SELECT count(*) FROM (
-
- @SQL_COUNT(order_id, order) -- this takes previous query removes limit and replaces select columns with parameter order_id
-
- ) as count;
-
- END
-
-
- CREATE PROCEDURE get(
- IN order_id INT,
- IN customer_order_id CHAR,
- IN user_id INT,
- IN email CHAR,
- IN language_id INT,
- OUT fetch_row,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- )
- BEGIN
-
- -- order
- SELECT "order".*,
- os.name as order_status,
- ops.name as payment_status,
- oss.name as shipping_status,
- bc.name as billing_country,
- sc.name as shipping_country,
- br.name as billing_region,
- sr.name as shipping_region
-
- FROM "order"
-
- LEFT JOIN order_status AS os ON ("order".order_status_id = os.order_status_id AND os.language_id = :language_id)
- LEFT JOIN payment_status AS ops ON ("order".payment_status_id = ops.payment_status_id AND os.language_id = :language_id)
- LEFT JOIN shipping_status AS oss ON ("order".shipping_status_id = oss.shipping_status_id AND os.language_id = :language_id)
- -- country
- LEFT JOIN country AS bc ON ("order".billing_country_id = bc.country_id)
- LEFT JOIN country AS sc ON ("order".shipping_country_id = sc.country_id)
- -- Region
- LEFT JOIN region AS br ON ("order".billing_region_id = br.region_id)
- LEFT JOIN region AS sr ON ("order".shipping_region_id = sr.region_id)
-
- WHERE 1 = 1
-
- @IF isset(:user_id)
- THEN
- AND "order".user_id = :user_id
- END @IF
-
- @IF isset(:order_id)
- THEN
- AND "order".order_id = :order_id
- END @IF
-
- @IF isset(:customer_order_id)
- THEN
- AND "order".customer_order_id = :customer_order_id
- END @IF
-
- @IF isset(:email)
- THEN
- AND "order".email = :email
- END @IF
-
- LIMIT 1;
-
- SELECT "key" as array_key,"value" as array_value FROM order_meta as _
- WHERE _.order_id = :order_id;
-
- -- products
- SELECT *,products.name as name,
- (SELECT json_agg(json_build_object('order_product_option_id', opo.order_product_option_id, 'product_option_id' , opo.product_option_id, 'product_option_value_id' , opo.product_option_value_id, 'option', opo.option, 'name' , opo.name, 'price' , opo.price, 'type' , opo.type) )
- FROM order_product_option AS opo
- WHERE opo.order_product_id = products.order_product_id
- ) as option_value
- FROM order_product as products
- LEFT JOIN product ON product.product_id = products.product_id
- LEFT JOIN product_content ON product_content.product_id = products.product_id
-
- @IF isset(:language_id)
- THEN
- AND product_content.language_id = :language_id
- END @IF
-
- -- use @result.order.order_id instead of :order_id to work when :customer_order_id is used
- WHERE products.order_id = @result.order.order_id;
-
- -- log
- SELECT *,os.name as order_status
- FROM order_log as log
- LEFT JOIN order_status AS os ON (log.order_status_id = os.order_status_id AND os.language_id = :language_id)
- WHERE log.order_id = @result.order.order_id;
-
- -- meta
- SELECT * FROM order_meta as meta
- WHERE meta.order_id = @result.order.order_id;
-
- -- total
- SELECT * FROM order_total as total
- WHERE total.order_id = @result.order.order_id;
-
- -- shipment
- SELECT * FROM order_shipment as shipment
- WHERE shipment.order_id = @result.order.order_id;
-
- -- voucher
- SELECT * FROM order_voucher as voucher
- WHERE voucher.order_id = @result.order.order_id;
-
- END
-
-
- PROCEDURE getData(
- IN order_id INT,
- IN language_id INT,
- IN billing_country_id INT,
- IN shipping_country_id INT,
-
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- )
- BEGIN
-
- -- order status
- SELECT
-
- order_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM order_status as order_status_id WHERE language_id = :language_id;
-
- -- payment status
- SELECT
-
- payment_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM payment_status as payment_status_id WHERE language_id = :language_id;
-
- -- shipping status
- SELECT
-
- shipping_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM shipping_status as shipping_status_id WHERE language_id = :language_id;
-
- -- billing_country
- SELECT
- country_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM country as billing_country_id WHERE status = 1;
-
- -- shipping_country
- SELECT
- country_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM country as shipping_country_id WHERE status = 1;
-
- -- billing_region
- SELECT
- region_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM region as billing_region_id
- WHERE status = 1
-
- @IF isset(:billing_country_id)
- THEN
- AND billing_region_id.country_id = :billing_country_id
- END @IF;
-
- -- shipping_region
- SELECT
- region_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM region as shipping_region_id
- WHERE status = 1
-
- @IF isset(:shipping_country_id)
- THEN
- AND shipping_region_id.country_id = :shipping_country_id
- END @IF;
-
- END
-
- -- delete order
-
- PROCEDURE delete(
- IN order_id ARRAY,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows
- )
- BEGIN
-
- DELETE FROM order_product WHERE order_id IN (:order_id);
- DELETE FROM order_product_option WHERE order_id IN (:order_id);
- DELETE FROM order_subscription WHERE order_id IN (:order_id);
- DELETE FROM order_shipment WHERE order_id IN (:order_id);
- DELETE FROM order_voucher WHERE order_id IN (:order_id);
- DELETE FROM order_total WHERE order_id IN (:order_id);
- DELETE FROM voucher WHERE order_id IN (:order_id);
- DELETE FROM voucher_log WHERE order_id IN (:order_id);
- DELETE FROM order_log WHERE order_id IN (:order_id);
- DELETE FROM "order" WHERE order_id IN (:order_id);
-
- END
-
-
- -- add order
-
- CREATE PROCEDURE add(
- IN order ARRAY,
- OUT fetch_one,
- OUT fetch_one,
- OUT fetch_one,
- OUT fetch_one
- )
- BEGIN
-
- :products = @FILTER(:order.products, order_product, false, true)
- :totals = @FILTER(:order.totals, order_total, false, true)
-
- @FILTER(:order, order)
-
- INSERT INTO "order"
-
- ( @KEYS(:order) )
-
- VALUES ( :order ) RETURNING order_id;
-
- -- insert order products
- @EACH(:products)
- INSERT INTO order_product
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each ) RETURNING order_product_id;
-
- -- insert product option
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each ) RETURNING order_product_option_id;
-
- -- insert order products
- @EACH(:totals)
- INSERT INTO order_total
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each ) RETURNING order_total_id;
-
- END
-
- -- edit order
-
- CREATE PROCEDURE edit(
- IN order_id INT,
- IN order ARRAY,
- OUT affected_rows
- )
- BEGIN
-
- @FILTER(:order, order)
-
- UPDATE "order"
-
- SET @LIST(:order)
-
- WHERE order_id = :order_id;
-
- END
-
- -- products
-
- CREATE PROCEDURE addProduct(
- IN product ARRAY,
- IN product_options ARRAY,
- IN order_id INT,
- OUT insert_id,
- OUT insert_id
- )
- BEGIN
-
- :product = @FILTER(:product, order_product, false)
- :product_options = @FILTER(:product_options, order_product_option, false, true)
-
- INSERT INTO order_product
- ( order_id, @KEYS(:product) )
- VALUES ( :order_id, :product ) RETURNING order_product_id;
-
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, order_product_id, @KEYS(:each) )
- VALUES ( :order_id, @result.order_product, :each ) RETURNING order_product_option_id;
-
- END
-
- CREATE PROCEDURE editProduct(
- IN order_product_id INT,
- IN product ARRAY,
- IN product_options ARRAY,
- IN order_id INT,
- OUT affected_rows,
- OUT affected_rows
- )
- BEGIN
-
- :product = @FILTER(:product, order_product)
- :product_options = @FILTER(:product_options, order_product_option, false, true)
-
- UPDATE "order_product"
-
- SET @LIST(:product)
-
- WHERE order_product_id = :order_product_id;
-
- -- insert product option
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, order_product_id, @KEYS(:each) )
- VALUES ( :order_id, :order_product_id, :each )
- ON CONFLICT ("order_id", "order_product_id") DO UPDATE SET SET @LIST(:each);
- END
-
- PROCEDURE deleteProduct(
- IN order_product_id ARRAY,
- OUT affected_rows,
- )
- BEGIN
-
- DELETE FROM order_product WHERE order_product_id IN (:order_product_id);
-
- END
-
- -- totals
-
- CREATE PROCEDURE addTotal(
- IN total ARRAY,
- IN order_id INT,
- OUT insert_id
- )
- BEGIN
-
- :total = @FILTER(:total, order_total)
-
- INSERT INTO order_total
- ( order_id, @KEYS(:total) )
- VALUES ( :order_id, :total ) RETURNING order_total_id;
-
- END
-
- CREATE PROCEDURE editTotal(
- IN order_total_id INT,
- IN total ARRAY,
- OUT affected_rows
- )
- BEGIN
-
- :total = @FILTER(:total, order_total)
-
- UPDATE "order_total"
-
- SET @LIST(:total)
-
- WHERE order_total_id = :order_total_id;
-
- END
-
- PROCEDURE deleteTotal(
- IN order_total_id ARRAY,
- OUT affected_rows,
- )
- BEGIN
-
- DELETE FROM order_total WHERE order_total_id IN (:order_total_id);
-
- END
+-- Orders
+
+ -- get all orders
+
+ CREATE PROCEDURE getAll(
+ -- variables
+ IN language_id INT,
+ IN site_id INT,
+ IN user_id INT
+
+ IN order_status CHAR,
+ IN order_status_id INT,
+
+ IN payment_status CHAR,
+ IN payment_status_id INT,
+
+ IN shipping_status CHAR,
+ IN shipping_status_id INT,
+
+ IN email CHAR,
+ IN phone_number CHAR,
+ IN search CHAR,
+
+ -- pagination
+ IN start INT,
+ IN limit INT,
+ IN cursor INT, -- current cursor (order_id) for cursor pagination
+ IN order_by CHAR,
+ IN direction CHAR,
+
+
+ -- return
+ OUT fetch_all, -- orders
+ OUT fetch_one -- count
+ )
+ BEGIN
+
+ SELECT "order".*,os.name as order_status,ops.name as payment_status,oss.name as shipping_status FROM "order" AS "order"
+
+ LEFT JOIN order_status AS os ON ("order".order_status_id = os.order_status_id AND os.language_id = :language_id)
+ LEFT JOIN shipping_status AS oss ON ("order".shipping_status_id = oss.shipping_status_id AND oss.language_id = :language_id)
+ LEFT JOIN payment_status AS ops ON ("order".order_status_id = ops.payment_status_id AND os.language_id = :language_id)
+
+ WHERE 1 = 1
+
+ AND "order".site_id = :site_id
+
+ @IF isset(:user_id)
+ THEN
+ AND "order".user_id = :user_id
+ END @IF
+
+ @IF isset(:order_status)
+ THEN
+ AND os.name = :order_status
+ END @IF
+
+ @IF isset(:order_status_id)
+ THEN
+ AND "order".order_status_id = :order_status_id
+ END @IF
+
+ @IF isset(:shipping_status)
+ THEN
+ AND oss.name = :shipping_status
+ END @IF
+
+ @IF isset(:shipping_status_id)
+ THEN
+ AND "order".shipping_status_id = :shipping_status_id
+ END @IF
+
+ @IF isset(:payment_status)
+ THEN
+ AND ops.name = :payment_status
+ END @IF
+
+ @IF isset(:payment_status_id)
+ THEN
+ AND "order".payment_status_id = :payment_status_id
+ END @IF
+
+ @IF isset(:email) AND !empty(:email)
+ THEN
+ AND "order".email = :email
+ END @IF
+
+ @IF isset(:phone_number) AND !empty(:phone_number)
+ THEN
+ AND "order".phone_number = :phone_number
+ END @IF
+
+ -- search
+ @IF isset(:search) AND !empty(:search)
+ THEN
+ AND
+ "order".first_name LIKE '%' || :search || '%' OR
+ "order".last_name LIKE '%' || :search || '%'
+ END @IF
+
+ -- cursor pagination
+
+ @IF isset(:cursor)
+
+ THEN
+
+ AND "order".order_id > :cursor
+
+ END @IF
+
+ -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
+ @IF isset(:order_by)
+ THEN
+ -- ORDER BY $order_by $direction
+ ORDER BY "order".@ESC(:order_by) @ESC(:direction)
+ @ELSE
+ ORDER BY "order".order_id DESC
+ END @IF
+
+ LIMIT :limit OFFSET :start;
+
+ SELECT count(*) FROM (
+
+ @SQL_COUNT(order_id, order) -- this takes previous query removes limit and replaces select columns with parameter order_id
+
+ ) as count;
+
+ END
+
+
+ CREATE PROCEDURE get(
+ IN order_id INT,
+ IN customer_order_id CHAR,
+ IN user_id INT,
+ IN email CHAR,
+ IN language_id INT,
+ OUT fetch_row,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all
+ )
+ BEGIN
+
+ -- order
+ SELECT "order".*,
+ os.name as order_status,
+ ops.name as payment_status,
+ oss.name as shipping_status,
+ bc.name as billing_country,
+ sc.name as shipping_country,
+ br.name as billing_region,
+ sr.name as shipping_region
+
+ FROM "order"
+
+ LEFT JOIN order_status AS os ON ("order".order_status_id = os.order_status_id AND os.language_id = :language_id)
+ LEFT JOIN payment_status AS ops ON ("order".payment_status_id = ops.payment_status_id AND os.language_id = :language_id)
+ LEFT JOIN shipping_status AS oss ON ("order".shipping_status_id = oss.shipping_status_id AND os.language_id = :language_id)
+ -- country
+ LEFT JOIN country AS bc ON ("order".billing_country_id = bc.country_id)
+ LEFT JOIN country AS sc ON ("order".shipping_country_id = sc.country_id)
+ -- Region
+ LEFT JOIN region AS br ON ("order".billing_region_id = br.region_id)
+ LEFT JOIN region AS sr ON ("order".shipping_region_id = sr.region_id)
+
+ WHERE 1 = 1
+
+ @IF isset(:user_id)
+ THEN
+ AND "order".user_id = :user_id
+ END @IF
+
+ @IF isset(:order_id)
+ THEN
+ AND "order".order_id = :order_id
+ END @IF
+
+ @IF isset(:customer_order_id)
+ THEN
+ AND "order".customer_order_id = :customer_order_id
+ END @IF
+
+ @IF isset(:email)
+ THEN
+ AND "order".email = :email
+ END @IF
+
+ LIMIT 1;
+
+ SELECT "key" as array_key,"value" as array_value FROM order_meta as _
+ WHERE _.order_id = :order_id;
+
+ -- products
+ SELECT *,products.name as name,
+ (SELECT json_agg(json_build_object('order_product_option_id', opo.order_product_option_id, 'product_option_id' , opo.product_option_id, 'product_option_value_id' , opo.product_option_value_id, 'option', opo.option, 'name' , opo.name, 'price' , opo.price, 'type' , opo.type) )
+ FROM order_product_option AS opo
+ WHERE opo.order_product_id = products.order_product_id
+ ) as option_value
+ FROM order_product as products
+ LEFT JOIN product ON product.product_id = products.product_id
+ LEFT JOIN product_content ON product_content.product_id = products.product_id
+
+ @IF isset(:language_id)
+ THEN
+ AND product_content.language_id = :language_id
+ END @IF
+
+ -- use @result.order.order_id instead of :order_id to work when :customer_order_id is used
+ WHERE products.order_id = @result.order.order_id;
+
+ -- log
+ SELECT *,os.name as order_status
+ FROM order_log as log
+ LEFT JOIN order_status AS os ON (log.order_status_id = os.order_status_id AND os.language_id = :language_id)
+ WHERE log.order_id = @result.order.order_id;
+
+ -- meta
+ SELECT * FROM order_meta as meta
+ WHERE meta.order_id = @result.order.order_id;
+
+ -- total
+ SELECT * FROM order_total as total
+ WHERE total.order_id = @result.order.order_id;
+
+ -- shipment
+ SELECT * FROM order_shipment as shipment
+ WHERE shipment.order_id = @result.order.order_id;
+
+ -- voucher
+ SELECT * FROM order_voucher as voucher
+ WHERE voucher.order_id = @result.order.order_id;
+
+ END
+
+
+ CREATE PROCEDURE getData(
+ IN language_id INT,
+ IN billing_country_id INT,
+ IN shipping_country_id INT,
+
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all
+ )
+ BEGIN
+
+ -- order status
+ SELECT
+
+ order_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM order_status as order_status_id WHERE language_id = :language_id;
+
+ -- payment status
+ SELECT
+
+ payment_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM payment_status as payment_status_id WHERE language_id = :language_id;
+
+ -- shipping status
+ SELECT
+
+ shipping_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM shipping_status as shipping_status_id WHERE language_id = :language_id;
+
+ -- billing_country
+ SELECT
+ country_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM country as billing_country_id WHERE status = 1;
+
+ -- shipping_country
+ SELECT
+ country_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM country as shipping_country_id WHERE status = 1;
+
+ -- billing_region
+ SELECT
+ region_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM region as billing_region_id
+ WHERE status = 1
+
+ @IF isset(:billing_country_id)
+ THEN
+ AND billing_region_id.country_id = :billing_country_id
+ END @IF;
+
+ -- shipping_region
+ SELECT
+ region_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM region as shipping_region_id
+ WHERE status = 1
+
+ @IF isset(:shipping_country_id)
+ THEN
+ AND shipping_region_id.country_id = :shipping_country_id
+ END @IF;
+
+ END
+
+ -- delete order
+
+ CREATE PROCEDURE delete(
+ IN order_id ARRAY,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_product WHERE order_id IN (:order_id);
+ DELETE FROM order_product_option WHERE order_id IN (:order_id);
+ DELETE FROM order_subscription WHERE order_id IN (:order_id);
+ DELETE FROM order_shipment WHERE order_id IN (:order_id);
+ DELETE FROM order_voucher WHERE order_id IN (:order_id);
+ DELETE FROM order_total WHERE order_id IN (:order_id);
+ DELETE FROM voucher WHERE order_id IN (:order_id);
+ DELETE FROM voucher_log WHERE order_id IN (:order_id);
+ DELETE FROM order_log WHERE order_id IN (:order_id);
+ DELETE FROM "order" WHERE order_id IN (:order_id);
+
+ END
+
+
+ -- add order
+
+ CREATE PROCEDURE add(
+ IN order ARRAY,
+ OUT fetch_one,
+ OUT fetch_one,
+ OUT fetch_one,
+ OUT fetch_one
+ )
+ BEGIN
+
+ :products = @FILTER(:order.products, order_product, false, true)
+ :totals = @FILTER(:order.totals, order_total, false, true)
+
+ @FILTER(:order, order)
+
+ INSERT INTO "order"
+
+ ( @KEYS(:order) )
+
+ VALUES ( :order ) RETURNING order_id;
+
+ -- insert order products
+ @EACH(:products)
+ INSERT INTO order_product
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each ) RETURNING order_product_id;
+
+ -- insert product option
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each ) RETURNING order_product_option_id;
+
+ -- insert order products
+ @EACH(:totals)
+ INSERT INTO order_total
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each ) RETURNING order_total_id;
+
+ END
+
+ -- edit order
+
+ CREATE PROCEDURE edit(
+ IN order_id INT,
+ IN order ROW,
+ OUT affected_rows
+ )
+ BEGIN
+
+ @FILTER(:order, order)
+
+ UPDATE "order"
+
+ SET @LIST(:order)
+
+ WHERE order_id = :order_id;
+
+ END
+
+ -- products
+
+ CREATE PROCEDURE addProduct(
+ IN product ROW,
+ IN product_options ROWS,
+ IN order_id INT,
+ OUT insert_id,
+ OUT insert_id
+ )
+ BEGIN
+
+ :product = @FILTER(:product, order_product, false)
+ :product_options = @FILTER(:product_options, order_product_option, false, true)
+
+ INSERT INTO order_product
+ ( order_id, @KEYS(:product) )
+ VALUES ( :order_id, :product ) RETURNING order_product_id;
+
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, order_product_id, @KEYS(:each) )
+ VALUES ( :order_id, @result.order_product, :each ) RETURNING order_product_option_id;
+
+ END
+
+ CREATE PROCEDURE editProduct(
+ IN order_product_id INT,
+ IN product ROW,
+ IN product_options ROWS,
+ IN order_id INT,
+ OUT affected_rows,
+ OUT affected_rows
+ )
+ BEGIN
+
+ :product = @FILTER(:product, order_product)
+ :product_options = @FILTER(:product_options, order_product_option, false, true)
+
+ UPDATE "order_product"
+
+ SET @LIST(:product)
+
+ WHERE order_product_id = :order_product_id;
+
+ -- insert product option
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, order_product_id, @KEYS(:each) )
+ VALUES ( :order_id, :order_product_id, :each )
+ ON CONFLICT ("order_id", "order_product_id") DO UPDATE SET SET @LIST(:each);
+ END
+
+ CREATE PROCEDURE deleteProduct(
+ IN order_product_id ARRAY,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_product WHERE order_product_id IN (:order_product_id);
+
+ END
+
+ -- totals
+
+ CREATE PROCEDURE addTotal(
+ IN total ROW,
+ IN order_id INT,
+ OUT insert_id
+ )
+ BEGIN
+
+ :total = @FILTER(:total, order_total)
+
+ INSERT INTO order_total
+ ( order_id, @KEYS(:total) )
+ VALUES ( :order_id, :total ) RETURNING order_total_id;
+
+ END
+
+ CREATE PROCEDURE editTotal(
+ IN order_total_id INT,
+ IN total ROW,
+ OUT affected_rows
+ )
+ BEGIN
+
+ :total = @FILTER(:total, order_total)
+
+ UPDATE "order_total"
+
+ SET @LIST(:total)
+
+ WHERE order_total_id = :order_total_id;
+
+ END
+
+ CREATE PROCEDURE deleteTotal(
+ IN order_total_id ARRAY,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_total WHERE order_total_id IN (:order_total_id);
+
+ END
diff --git a/admin/sql/pgsql/product.sql b/admin/sql/pgsql/product.sql
index d559112..87a6b0d 100644
--- a/admin/sql/pgsql/product.sql
+++ b/admin/sql/pgsql/product.sql
@@ -2,7 +2,7 @@
-- get one product
- PROCEDURE get(
+ CREATE PROCEDURE get(
IN product_id INT,
IN admin_id INT,
IN slug CHAR,
@@ -10,7 +10,6 @@
IN language_id INT,
IN promotion INT, -- include promotional price
IN user_group_id INT, -- user_group_id for group promotional price
- IN variant INT, -- include variants
IN variant_price INT, -- include variants min max prices
IN points INT, -- include points
IN stock_status INT, -- include stock_status
@@ -78,7 +77,7 @@
FROM product_points pp
WHERE pp.product_id = _.product_id
AND pp.user_group_id = :user_group_id
- AS points
+ ) AS points
END @IF
@@ -304,8 +303,7 @@
END
- PROCEDURE getData(
- IN product_id INT,
+ CREATE PROCEDURE getData(
IN manufacturer_id INT,
IN vendor_id INT,
@@ -316,9 +314,8 @@
OUT fetch_all,
OUT fetch_all,
OUT fetch_all,
-
- OUT fetch_one,
OUT fetch_one,
+ OUT fetch_one
)
BEGIN
@@ -401,9 +398,17 @@
-- Delete product
- PROCEDURE delete(
+ CREATE PROCEDURE delete(
IN product_id ARRAY,
IN admin_id INT,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
OUT affected_rows
)
BEGIN
@@ -448,7 +453,21 @@
THEN
INNER JOIN product ON (product.product_id = product_image.product_id AND product.admin_id = :admin_id)
END @IF
- WHERE product_image.product_id IN (:product_id);
+ WHERE product_image.product_id IN (:product_id);
+
+ DELETE product_question FROM product_question
+ @IF isset(:admin_id)
+ THEN
+ INNER JOIN product ON (product.product_id = product_question.product_id AND product.admin_id = :admin_id)
+ END @IF
+ WHERE product_question.product_id IN (:product_id);
+
+ DELETE product_review FROM product_review
+ @IF isset(:admin_id)
+ THEN
+ INNER JOIN product ON (product.product_id = product_review.product_id AND product.admin_id = :admin_id)
+ END @IF
+ WHERE product_review.product_id IN (:product_id);
DELETE product FROM product WHERE product_id IN (:product_id)
@IF isset(:admin_id)
@@ -460,9 +479,9 @@
-- Edit product
- PROCEDURE edit(
- IN product ARRAY,
- IN product_content ARRAY,
+ CREATE PROCEDURE edit(
+ IN product ROW,
+ IN product_content ROWS,
IN taxonomy_item_id ARRAY,
IN product_id INT,
IN site_id ARRAY,
@@ -539,7 +558,7 @@
-- Edit post content
CREATE PROCEDURE editContent(
- IN product_content ARRAY,
+ IN product_content ROW,
IN product_id INT,
IN language_id INT,
OUT affected_rows
@@ -559,14 +578,14 @@
-- Add new product
CREATE PROCEDURE add(
- IN product ARRAY,
- IN product_content ARRAY,
+ IN product ROW,
+ IN product_content ROWS,
IN taxonomy_item_id ARRAY,
IN site_id ARRAY,
OUT fetch_one,
- OUT fetch_one,
- OUT fetch_one,
- OUT fetch_one
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows
)
BEGIN
@@ -596,7 +615,7 @@
( taxonomy_item_id, product_id)
VALUES ( :each, @result.product_data)
- ON CONFLICT("taxonomy_item_id", "product_id") DO UPDATE SET @LIST(:each);
+ ON CONFLICT("taxonomy_item_id", "product_id") DO UPDATE SET "taxonomy_item_id" = :each;
-- :product = @FILTER(:product_data, product)
@@ -611,7 +630,7 @@
-- Edit product
- PROCEDURE productImage(
+ CREATE PROCEDURE productImage(
IN product_image ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -634,7 +653,7 @@
-- Edit product related
- PROCEDURE productRelated(
+ CREATE PROCEDURE productRelated(
IN product_related ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -657,8 +676,8 @@
-- Edit product variant
- PROCEDURE productVariant(
- IN product_variant ARRAY,
+ CREATE PROCEDURE productVariant(
+ IN product_variant ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -670,7 +689,7 @@
@EACH(:product_variant)
INSERT INTO product_variant
- ( product_variant_id, product_id)
+ ( @KEYS(:each), product_id)
VALUES ( :each, :product_id );
@@ -678,8 +697,8 @@
-- Edit product subscription
- PROCEDURE productSubscription(
- IN product_subscription ARRAY,
+ CREATE PROCEDURE productSubscription(
+ IN product_subscription ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -699,8 +718,8 @@
-- Edit product discount
- PROCEDURE productDiscount(
- IN product_discount ARRAY,
+ CREATE PROCEDURE productDiscount(
+ IN product_discount ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -720,8 +739,8 @@
-- Edit product promotion
- PROCEDURE productPromotion(
- IN product_promotion ARRAY,
+ CREATE PROCEDURE productPromotion(
+ IN product_promotion ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -741,8 +760,8 @@
-- Edit product points
- PROCEDURE productPoints(
- IN product_points ARRAY,
+ CREATE PROCEDURE productPoints(
+ IN product_points ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -762,10 +781,9 @@
-- Edit product attribute
- PROCEDURE productAttribute(
- IN product_attribute ARRAY,
+ CREATE PROCEDURE productAttribute(
+ IN product_attribute ROWS,
IN product_id INT,
- IN language_id INT,
OUT affected_rows,
OUT affected_rows
)
@@ -786,7 +804,7 @@
-- Edit product digital asset
- PROCEDURE productDigitalAsset(
+ CREATE PROCEDURE productDigitalAsset(
IN product_digital_asset ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -809,7 +827,7 @@
-- get all products
- PROCEDURE getAll(
+ CREATE PROCEDURE getAll(
-- variables
IN language_id INT,
@@ -837,6 +855,7 @@
-- pagination
IN start INT,
IN limit INT,
+ IN cursor INT, -- current cursor (product_id) for cursor pagination
IN type CHAR,
IN order_by CHAR,
IN direction CHAR,
@@ -859,8 +878,7 @@
-- return array of products for products query
OUT fetch_all,
- -- return products count for count query
- OUT fetch_one,
+ OUT fetch_one
)
BEGIN
@@ -900,6 +918,11 @@
OR pd2.from_date < NOW())
AND (pd2.to_date = NULL
OR pd2.to_date > NOW()))
+ -- cursor pagination
+ @IF isset(:cursor)
+ THEN
+ AND product.product_id > :cursor
+ END @IF
ORDER BY pd2.priority ASC, pd2.price ASC
LIMIT 1) AS discount
@@ -941,7 +964,7 @@
FROM product_points pp
WHERE pp.product_id = product.product_id
AND pp.user_group_id = :user_group_id
- AS points
+ ) AS points
END @IF
@@ -990,7 +1013,7 @@
THEN
,(SELECT AVG(rating) AS total
- FROM review r1
+ FROM product_review r1
WHERE r1.product_id = product.product_id
AND r1.status = '1'
GROUP BY r1.product_id)
@@ -1004,7 +1027,7 @@
THEN
,(SELECT COUNT(*) AS total
- FROM review r2
+ FROM product_review r2
WHERE r2.product_id = product.product_id
AND r2.status = '1'
GROUP BY r2.product_id) AS reviews
@@ -1127,7 +1150,7 @@
@IF isset(:quantity) && :quantity !== ""
THEN
- AND product.quantity = :quantity
+ AND product.stock_quantity = :quantity
END @IF
@IF isset(:model) && :model !== ""
@@ -1191,7 +1214,7 @@
@IF !empty(:taxonomy_item_id)
THEN
- AND pt.taxonomy_item_id = :taxonomy_item_id
+ AND pt.taxonomy_item_id IN(:taxonomy_item_id)
END @IF
@@ -1211,13 +1234,13 @@
@IF !empty(:product_attribute_id)
THEN
- pa.product_attribute_id IN (:product_attribute_id)
+ pa.attribute_id IN (:product_attribute_id)
END @IF
@IF !empty(:product_attribute)
THEN
- pa.text IN (:product_attribute)
+ pa.value IN (:product_attribute)
END @IF
@@ -1236,10 +1259,17 @@
END @IF
- @IF isset(:limit)
- THEN
- @SQL_LIMIT(:start, :limit)
- END @IF;
+ -- limit
+ @IF isset(:limit)
+ THEN
+ LIMIT :limit
+
+ @IF isset(:start)
+ THEN
+ OFFSET :start
+ END @IF
+
+ END @IF;
-- SELECT FOUND_ROWS() as count;
diff --git a/admin/sql/sqlite/admin_failed_login.sql b/admin/sql/sqlite/admin_failed_login.sql
index e9c962a..5a6c0b0 100644
--- a/admin/sql/sqlite/admin_failed_login.sql
+++ b/admin/sql/sqlite/admin_failed_login.sql
@@ -5,13 +5,13 @@
CREATE PROCEDURE getAll(
IN start INT,
IN limit INT,
+ IN cursor INT, -- current cursor (admin_id) for cursor pagination
IN admin_id INT,
IN count INT,
IN updated_at CHAR,
-- return array of admin_failed_login
OUT fetch_all,
- -- return admin_failed_login count for count query
OUT fetch_one
)
BEGIN
@@ -33,12 +33,22 @@
AND admin_failed_login.updated_at = :updated_at
END @IF
+ -- cursor pagination
+
+ @IF isset(:cursor)
+
+ THEN
+
+ AND admin_failed_login.admin_id > :cursor
+
+ END @IF
+
ORDER BY admin_failed_login.admin_id, admin_failed_login.updated_at
-- limit
@IF isset(:limit)
THEN
- @SQL_LIMIT(:start, :limit)
+ LIMIT :limit OFFSET :start
END @IF;
-- SELECT FOUND_ROWS() as count;
@@ -119,6 +129,7 @@
IN username CHAR,
IN updated_at CHAR,
IN last_ip INT,
+ IN email CHAR,
OUT fetch_one,
OUT insert_id
)
@@ -165,7 +176,7 @@
-- Add new admin_failed_login
CREATE PROCEDURE add(
- IN admin_failed_login ARRAY,
+ IN admin_failed_login ROW,
OUT insert_id
)
BEGIN
@@ -185,54 +196,39 @@
CREATE PROCEDURE edit(
IN admin_id INT,
- IN admin_failed_login ARRAY,
+ IN admin_failed_login ROW,
OUT affected_rows
)
BEGIN
-- allow only table fields and set defaults for missing values
@FILTER(:admin_failed_login, admin_failed_login)
+ @IF !empty(:admin_failed_login)
+ THEN
UPDATE admin_failed_login
SET @LIST(:admin_failed_login)
- WHERE admin_id = :admin_id;
+ WHERE admin_id = :admin_id
+ END @IF;
END
-- delete admin_failed_login
- PROCEDURE delete(
+ CREATE PROCEDURE delete(
IN admin_id ARRAY,
- IN updated_at CHAR,
- IN count INT,
OUT affected_rows
)
BEGIN
- DELETE admin_failed_login FROM admin_failed_login
- INNER JOIN admin.admin_id ON admin = admin_failed_login.admin_id
- WHERE
+ DELETE FROM admin_failed_login
+ WHERE 1 = 1
@IF isset(:admin_id) AND !empty(:admin_id)
- THEN
- AND admin_failed_login.admin_id = :admin_id
- END @IF
+ THEN
+ AND admin_failed_login.admin_id IN (:admin_id)
+ END @IF
- @IF isset(:username)
- THEN
- AND admin.username = :username
- END @IF
-
- @IF isset(:email)
- THEN
- AND admin.email = :email
- END @IF
-
- @IF isset(:admin_id)
- THEN
- AND admin.admin_id IN (:admin_id)
- END @IF
-
- END
+ END
diff --git a/admin/sql/sqlite/order.sql b/admin/sql/sqlite/order.sql
index fc55031..6fef807 100644
--- a/admin/sql/sqlite/order.sql
+++ b/admin/sql/sqlite/order.sql
@@ -1,500 +1,512 @@
--- Orders
-
- -- get all orders
-
- CREATE PROCEDURE getAll(
- -- variables
- IN language_id INT,
- IN site_id INT,
- IN user_id INT
-
- IN order_status CHAR,
- IN order_status_id INT,
-
- IN payment_status CHAR,
- IN payment_status_id INT,
-
- IN shipping_status CHAR,
- IN shipping_status_id INT,
-
- IN email CHAR,
- IN phone_number CHAR,
- IN search CHAR,
-
- -- pagination
- IN start INT,
- IN limit INT,
- IN order_by CHAR,
- IN direction CHAR,
-
-
- -- return
- OUT fetch_all, -- orders
- OUT fetch_one -- count
- )
- BEGIN
-
- SELECT `order`.*,os.name as order_status,ops.name as payment_status,oss.name as shipping_status FROM `order` AS `order`
-
- LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
- LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND oss.language_id = :language_id)
- LEFT JOIN payment_status AS ops ON (`order`.order_status_id = ops.payment_status_id AND os.language_id = :language_id)
-
- WHERE 1 = 1
-
- AND `order`.site_id = :site_id
-
- @IF isset(:user_id)
- THEN
- AND `order`.user_id = :user_id
- END @IF
-
- @IF isset(:order_status)
- THEN
- AND os.name = :order_status
- END @IF
-
- @IF isset(:order_status_id)
- THEN
- AND `order`.order_status_id = :order_status_id
- END @IF
-
- @IF isset(:shipping_status)
- THEN
- AND oss.name = :shipping_status
- END @IF
-
- @IF isset(:shipping_status_id)
- THEN
- AND `order`.shipping_status_id = :shipping_status_id
- END @IF
-
- @IF isset(:payment_status)
- THEN
- AND ops.name = :payment_status
- END @IF
-
- @IF isset(:payment_status_id)
- THEN
- AND `order`.payment_status_id = :payment_status_id
- END @IF
-
- @IF isset(:email) AND !empty(:email)
- THEN
- AND `order`.email = :email
- END @IF
-
- @IF isset(:phone_number) AND !empty(:phone_number)
- THEN
- AND `order`.phone_number = :phone_number
- END @IF
-
- -- search
- @IF isset(:search) AND !empty(:search)
- THEN
- AND
- `order`.first_name LIKE '%' || :search || '%' OR
- `order`.last_name LIKE '%' || :search || '%'
- END @IF
-
- -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
- @IF isset(:order_by)
- THEN
- -- ORDER BY $order_by $direction
- ORDER BY `order`.@ESC(:order_by) @ESC(:direction)
- @ELSE
- ORDER BY `order`.order_id DESC
- END @IF
-
- @SQL_LIMIT(:start, :limit);
-
- SELECT count(*) FROM (
-
- @SQL_COUNT(order_id, order) -- this takes previous query removes limit and replaces select columns with parameter order_id
-
- ) as count;
-
- END
-
-
- CREATE PROCEDURE get(
- IN order_id INT,
- IN customer_order_id CHAR,
- IN user_id INT,
- IN email CHAR,
- IN language_id INT,
- OUT fetch_row,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- )
- BEGIN
-
- -- order
- SELECT `order`.*,
- os.name as order_status,
- ops.name as payment_status,
- oss.name as shipping_status,
- bc.name as billing_country,
- sc.name as shipping_country,
- br.name as billing_region,
- sr.name as shipping_region
-
- FROM `order`
-
- LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
- LEFT JOIN payment_status AS ops ON (`order`.payment_status_id = ops.payment_status_id AND os.language_id = :language_id)
- LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND os.language_id = :language_id)
- -- country
- LEFT JOIN country AS bc ON (`order`.billing_country_id = bc.country_id)
- LEFT JOIN country AS sc ON (`order`.shipping_country_id = sc.country_id)
- -- Region
- LEFT JOIN region AS br ON (`order`.billing_region_id = br.region_id)
- LEFT JOIN region AS sr ON (`order`.shipping_region_id = sr.region_id)
-
- WHERE 1 = 1
-
- @IF isset(:user_id)
- THEN
- AND `order`.user_id = :user_id
- END @IF
-
- @IF isset(:order_id)
- THEN
- AND `order`.order_id = :order_id
- END @IF
-
- @IF isset(:customer_order_id)
- THEN
- AND `order`.customer_order_id = :customer_order_id
- END @IF
-
- @IF isset(:email)
- THEN
- AND `order`.email = :email
- END @IF
-
- LIMIT 1;
-
- SELECT `key` as array_key,`value` as array_value FROM order_meta as _
- WHERE _.order_id = :order_id;
-
- -- products
- SELECT *,products.name as name,
- (SELECT json_group_array(json_object('order_product_option_id', opo.order_product_option_id, 'product_option_id' , opo.product_option_id, 'product_option_value_id' , opo.product_option_value_id, 'option', opo.option, 'name' , opo.name, 'price' , opo.price, 'type' , opo.type) )
- FROM order_product_option AS opo
- WHERE opo.order_product_id = products.order_product_id
- ) as option_value
- FROM order_product as products
- LEFT JOIN product ON product.product_id = products.product_id
- LEFT JOIN product_content ON product_content.product_id = products.product_id
-
- @IF isset(:language_id)
- THEN
- AND product_content.language_id = :language_id
- END @IF
-
- -- use @result.order.order_id instead of :order_id to work when :customer_order_id is used
- WHERE products.order_id = @result.order.order_id;
-
- -- log
- SELECT *,os.name as order_status
- FROM order_log as log
- LEFT JOIN order_status AS os ON (log.order_status_id = os.order_status_id AND os.language_id = :language_id)
- WHERE log.order_id = @result.order.order_id;
-
- -- meta
- SELECT * FROM order_meta as meta
- WHERE meta.order_id = @result.order.order_id;
-
- -- total
- SELECT * FROM order_total as total
- WHERE total.order_id = @result.order.order_id;
-
- -- shipment
- SELECT * FROM order_shipment as shipment
- WHERE shipment.order_id = @result.order.order_id;
-
- -- voucher
- SELECT * FROM order_voucher as voucher
- WHERE voucher.order_id = @result.order.order_id;
-
- END
-
-
- PROCEDURE getData(
- IN order_id INT,
- IN language_id INT,
- IN billing_country_id INT,
- IN shipping_country_id INT,
-
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- OUT fetch_all,
- )
- BEGIN
-
- -- order status
- SELECT
-
- order_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM order_status as order_status_id WHERE language_id = :language_id;
-
- -- payment status
- SELECT
-
- payment_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM payment_status as payment_status_id WHERE language_id = :language_id;
-
- -- shipping status
- SELECT
-
- shipping_status_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
-
- FROM shipping_status as shipping_status_id WHERE language_id = :language_id;
-
- -- billing_country
- SELECT
- country_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM country as billing_country_id WHERE status = 1;
-
- -- shipping_country
- SELECT
- country_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM country as shipping_country_id WHERE status = 1;
-
- -- billing_region
- SELECT
- region_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM region as billing_region_id
- WHERE status = 1
-
- @IF isset(:billing_country_id)
- THEN
- AND billing_region_id.country_id = :billing_country_id
- END @IF;
-
- -- shipping_region
- SELECT
- region_id as array_key, -- order_id as key
- name as array_value -- only set name as value and return
- FROM region as shipping_region_id
- WHERE status = 1
-
- @IF isset(:shipping_country_id)
- THEN
- AND shipping_region_id.country_id = :shipping_country_id
- END @IF;
-
- END
-
- -- delete order
-
- PROCEDURE delete(
- IN order_id ARRAY,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows,
- OUT affected_rows
- )
- BEGIN
-
- DELETE FROM order_product WHERE order_id IN (:order_id);
- DELETE FROM order_product_option WHERE order_id IN (:order_id);
- DELETE FROM order_subscription WHERE order_id IN (:order_id);
- DELETE FROM order_shipment WHERE order_id IN (:order_id);
- DELETE FROM order_voucher WHERE order_id IN (:order_id);
- DELETE FROM order_total WHERE order_id IN (:order_id);
- DELETE FROM voucher WHERE order_id IN (:order_id);
- DELETE FROM voucher_log WHERE order_id IN (:order_id);
- DELETE FROM order_log WHERE order_id IN (:order_id);
- DELETE FROM `order` WHERE order_id IN (:order_id);
-
- END
-
-
- -- add order
-
- CREATE PROCEDURE add(
- IN order ARRAY,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id
- )
- BEGIN
-
- :products = @FILTER(:order.products, order_product, false, true)
- :totals = @FILTER(:order.totals, order_total, false, true)
-
- @FILTER(:order, order)
-
- INSERT INTO `order`
-
- ( @KEYS(:order) )
-
- VALUES ( :order );
-
- -- insert order products
- @EACH(:products)
- INSERT INTO order_product
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each );
-
- -- insert product option
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each );
-
- -- insert order totals
- @EACH(:totals)
- INSERT INTO order_total
- ( order_id, @KEYS(:each) )
- VALUES ( @result.order, :each );
-
- END
-
- -- edit order
-
- CREATE PROCEDURE edit(
- IN order_id INT,
- IN order ARRAY,
- OUT affected_rows
- )
- BEGIN
-
- @FILTER(:order, order)
-
- UPDATE `order`
-
- SET @LIST(:order)
-
- WHERE order_id = :order_id;
-
- END
-
- -- products
-
- CREATE PROCEDURE addProduct(
- IN product ARRAY,
- IN product_options ARRAY,
- IN order_id INT,
- OUT insert_id,
- OUT insert_id
- )
- BEGIN
-
- :product = @FILTER(:product, order_product, false)
- :product_options = @FILTER(:product_options, order_product_option, false, true)
-
- INSERT INTO order_product
- ( order_id, @KEYS(:product) )
- VALUES ( :order_id, :product );
-
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, order_product_id, @KEYS(:each) )
- VALUES ( :order_id, @result.order_product, :each );
- END
-
- CREATE PROCEDURE editProduct(
- IN order_product_id INT,
- IN product ARRAY,
- IN product_options ARRAY,
- IN order_id INT,
- OUT affected_rows,
- OUT affected_rows
- )
- BEGIN
-
- :product = @FILTER(:product, order_product)
- :product_options = @FILTER(:product_options, order_product_option, false, true)
-
- UPDATE `order_product`
-
- SET @LIST(:product)
-
- WHERE order_product_id = :order_product_id;
-
- -- insert product option
- @EACH(:product_options)
- INSERT INTO order_product_option
- ( order_id, order_product_id, @KEYS(:each) )
- VALUES ( :order_id, :order_product_id, :each )
- ON CONFLICT(`order_id`,`order_product_id`) DO UPDATE SET @LIST(:each);
- END
-
- PROCEDURE deleteProduct(
- IN order_product_id ARRAY,
- OUT affected_rows,
- )
- BEGIN
-
- DELETE FROM order_product WHERE order_product_id IN (:order_product_id);
-
- END
-
- -- totals
-
- CREATE PROCEDURE addTotal(
- IN total ARRAY,
- IN order_id INT,
- OUT insert_id
- )
- BEGIN
-
- :total = @FILTER(:total, order_total)
-
- INSERT INTO order_total
- ( order_id, @KEYS(:total) )
- VALUES ( :order_id, :total );
-
- END
-
- CREATE PROCEDURE editTotal(
- IN order_total_id INT,
- IN total ARRAY,
- OUT affected_rows
- )
- BEGIN
-
- :total = @FILTER(:total, order_total)
-
- UPDATE `order_total`
-
- SET @LIST(:total)
-
- WHERE order_total_id = :order_total_id;
-
- END
-
- PROCEDURE deleteTotal(
- IN order_total_id ARRAY,
- OUT affected_rows,
- )
- BEGIN
-
- DELETE FROM order_total WHERE order_total_id IN (:order_total_id);
-
- END
+-- Orders
+
+ -- get all orders
+
+ CREATE PROCEDURE getAll(
+ -- variables
+ IN language_id INT,
+ IN site_id INT,
+ IN user_id INT
+
+ IN order_status CHAR,
+ IN order_status_id INT,
+
+ IN payment_status CHAR,
+ IN payment_status_id INT,
+
+ IN shipping_status CHAR,
+ IN shipping_status_id INT,
+
+ IN email CHAR,
+ IN phone_number CHAR,
+ IN search CHAR,
+
+ -- pagination
+ IN start INT,
+ IN limit INT,
+ IN cursor INT, -- current cursor (order_id) for cursor pagination
+ IN order_by CHAR,
+ IN direction CHAR,
+
+
+ -- return
+ OUT fetch_all, -- orders
+ OUT fetch_one -- count
+ )
+ BEGIN
+
+ SELECT `order`.*,os.name as order_status,ops.name as payment_status,oss.name as shipping_status FROM `order` AS `order`
+
+ LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
+ LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND oss.language_id = :language_id)
+ LEFT JOIN payment_status AS ops ON (`order`.order_status_id = ops.payment_status_id AND os.language_id = :language_id)
+
+ WHERE 1 = 1
+
+ AND `order`.site_id = :site_id
+
+ @IF isset(:user_id)
+ THEN
+ AND `order`.user_id = :user_id
+ END @IF
+
+ @IF isset(:order_status)
+ THEN
+ AND os.name = :order_status
+ END @IF
+
+ @IF isset(:order_status_id)
+ THEN
+ AND `order`.order_status_id = :order_status_id
+ END @IF
+
+ @IF isset(:shipping_status)
+ THEN
+ AND oss.name = :shipping_status
+ END @IF
+
+ @IF isset(:shipping_status_id)
+ THEN
+ AND `order`.shipping_status_id = :shipping_status_id
+ END @IF
+
+ @IF isset(:payment_status)
+ THEN
+ AND ops.name = :payment_status
+ END @IF
+
+ @IF isset(:payment_status_id)
+ THEN
+ AND `order`.payment_status_id = :payment_status_id
+ END @IF
+
+ @IF isset(:email) AND !empty(:email)
+ THEN
+ AND `order`.email = :email
+ END @IF
+
+ @IF isset(:phone_number) AND !empty(:phone_number)
+ THEN
+ AND `order`.phone_number = :phone_number
+ END @IF
+
+ -- search
+ @IF isset(:search) AND !empty(:search)
+ THEN
+ AND
+ `order`.first_name LIKE '%' || :search || '%' OR
+ `order`.last_name LIKE '%' || :search || '%'
+ END @IF
+
+ -- cursor pagination
+
+ @IF isset(:cursor)
+
+ THEN
+
+ AND `order`.order_id > :cursor
+
+ END @IF
+
+ -- ORDER BY parameters can't be binded, because they are added to the query directly they must be properly sanitized by only allowing a predefined set of values
+ @IF isset(:order_by)
+ THEN
+ -- ORDER BY $order_by $direction
+ ORDER BY `order`.@ESC(:order_by) @ESC(:direction)
+ @ELSE
+ ORDER BY `order`.order_id DESC
+ END @IF
+
+ LIMIT :limit OFFSET :start;
+
+ SELECT count(*) FROM (
+
+ @SQL_COUNT(order_id, order) -- this takes previous query removes limit and replaces select columns with parameter order_id
+
+ ) as count;
+
+ END
+
+
+ CREATE PROCEDURE get(
+ IN order_id INT,
+ IN customer_order_id CHAR,
+ IN user_id INT,
+ IN email CHAR,
+ IN language_id INT,
+ OUT fetch_row,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all
+ )
+ BEGIN
+
+ -- order
+ SELECT `order`.*,
+ os.name as order_status,
+ ops.name as payment_status,
+ oss.name as shipping_status,
+ bc.name as billing_country,
+ sc.name as shipping_country,
+ br.name as billing_region,
+ sr.name as shipping_region
+
+ FROM `order`
+
+ LEFT JOIN order_status AS os ON (`order`.order_status_id = os.order_status_id AND os.language_id = :language_id)
+ LEFT JOIN payment_status AS ops ON (`order`.payment_status_id = ops.payment_status_id AND os.language_id = :language_id)
+ LEFT JOIN shipping_status AS oss ON (`order`.shipping_status_id = oss.shipping_status_id AND os.language_id = :language_id)
+ -- country
+ LEFT JOIN country AS bc ON (`order`.billing_country_id = bc.country_id)
+ LEFT JOIN country AS sc ON (`order`.shipping_country_id = sc.country_id)
+ -- Region
+ LEFT JOIN region AS br ON (`order`.billing_region_id = br.region_id)
+ LEFT JOIN region AS sr ON (`order`.shipping_region_id = sr.region_id)
+
+ WHERE 1 = 1
+
+ @IF isset(:user_id)
+ THEN
+ AND `order`.user_id = :user_id
+ END @IF
+
+ @IF isset(:order_id)
+ THEN
+ AND `order`.order_id = :order_id
+ END @IF
+
+ @IF isset(:customer_order_id)
+ THEN
+ AND `order`.customer_order_id = :customer_order_id
+ END @IF
+
+ @IF isset(:email)
+ THEN
+ AND `order`.email = :email
+ END @IF
+
+ LIMIT 1;
+
+ SELECT `key` as array_key,`value` as array_value FROM order_meta as _
+ WHERE _.order_id = :order_id;
+
+ -- products
+ SELECT *,products.name as name,
+ (SELECT json_group_array(json_object('order_product_option_id', opo.order_product_option_id, 'product_option_id' , opo.product_option_id, 'product_option_value_id' , opo.product_option_value_id, 'option', opo.option, 'name' , opo.name, 'price' , opo.price, 'type' , opo.type) )
+ FROM order_product_option AS opo
+ WHERE opo.order_product_id = products.order_product_id
+ ) as option_value
+ FROM order_product as products
+ LEFT JOIN product ON product.product_id = products.product_id
+ LEFT JOIN product_content ON product_content.product_id = products.product_id
+
+ @IF isset(:language_id)
+ THEN
+ AND product_content.language_id = :language_id
+ END @IF
+
+ -- use @result.order.order_id instead of :order_id to work when :customer_order_id is used
+ WHERE products.order_id = @result.order.order_id;
+
+ -- log
+ SELECT *,os.name as order_status
+ FROM order_log as log
+ LEFT JOIN order_status AS os ON (log.order_status_id = os.order_status_id AND os.language_id = :language_id)
+ WHERE log.order_id = @result.order.order_id;
+
+ -- meta
+ SELECT * FROM order_meta as meta
+ WHERE meta.order_id = @result.order.order_id;
+
+ -- total
+ SELECT * FROM order_total as total
+ WHERE total.order_id = @result.order.order_id;
+
+ -- shipment
+ SELECT * FROM order_shipment as shipment
+ WHERE shipment.order_id = @result.order.order_id;
+
+ -- voucher
+ SELECT * FROM order_voucher as voucher
+ WHERE voucher.order_id = @result.order.order_id;
+
+ END
+
+
+ CREATE PROCEDURE getData(
+ IN language_id INT,
+ IN billing_country_id INT,
+ IN shipping_country_id INT,
+
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all,
+ OUT fetch_all
+ )
+ BEGIN
+
+ -- order status
+ SELECT
+
+ order_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM order_status as order_status_id WHERE language_id = :language_id;
+
+ -- payment status
+ SELECT
+
+ payment_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM payment_status as payment_status_id WHERE language_id = :language_id;
+
+ -- shipping status
+ SELECT
+
+ shipping_status_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+
+ FROM shipping_status as shipping_status_id WHERE language_id = :language_id;
+
+ -- billing_country
+ SELECT
+ country_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM country as billing_country_id WHERE status = 1;
+
+ -- shipping_country
+ SELECT
+ country_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM country as shipping_country_id WHERE status = 1;
+
+ -- billing_region
+ SELECT
+ region_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM region as billing_region_id
+ WHERE status = 1
+
+ @IF isset(:billing_country_id)
+ THEN
+ AND billing_region_id.country_id = :billing_country_id
+ END @IF;
+
+ -- shipping_region
+ SELECT
+ region_id as array_key, -- order_id as key
+ name as array_value -- only set name as value and return
+ FROM region as shipping_region_id
+ WHERE status = 1
+
+ @IF isset(:shipping_country_id)
+ THEN
+ AND shipping_region_id.country_id = :shipping_country_id
+ END @IF;
+
+ END
+
+ -- delete order
+
+ CREATE PROCEDURE delete(
+ IN order_id ARRAY,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_product WHERE order_id IN (:order_id);
+ DELETE FROM order_product_option WHERE order_id IN (:order_id);
+ DELETE FROM order_subscription WHERE order_id IN (:order_id);
+ DELETE FROM order_shipment WHERE order_id IN (:order_id);
+ DELETE FROM order_voucher WHERE order_id IN (:order_id);
+ DELETE FROM order_total WHERE order_id IN (:order_id);
+ DELETE FROM voucher WHERE order_id IN (:order_id);
+ DELETE FROM voucher_log WHERE order_id IN (:order_id);
+ DELETE FROM order_log WHERE order_id IN (:order_id);
+ DELETE FROM `order` WHERE order_id IN (:order_id);
+
+ END
+
+
+ -- add order
+
+ CREATE PROCEDURE add(
+ IN order ARRAY,
+ OUT insert_id,
+ OUT insert_id,
+ OUT insert_id,
+ OUT insert_id
+ )
+ BEGIN
+
+ :products = @FILTER(:order.products, order_product, false, true)
+ :totals = @FILTER(:order.totals, order_total, false, true)
+
+ @FILTER(:order, order)
+
+ INSERT INTO `order`
+
+ ( @KEYS(:order) )
+
+ VALUES ( :order );
+
+ -- insert order products
+ @EACH(:products)
+ INSERT INTO order_product
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each );
+
+ -- insert product option
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each );
+
+ -- insert order totals
+ @EACH(:totals)
+ INSERT INTO order_total
+ ( order_id, @KEYS(:each) )
+ VALUES ( @result.order, :each );
+
+ END
+
+ -- edit order
+
+ CREATE PROCEDURE edit(
+ IN order_id INT,
+ IN order ROW,
+ OUT affected_rows
+ )
+ BEGIN
+
+ @FILTER(:order, order)
+
+ UPDATE `order`
+
+ SET @LIST(:order)
+
+ WHERE order_id = :order_id;
+
+ END
+
+ -- products
+
+ CREATE PROCEDURE addProduct(
+ IN product ROW,
+ IN product_options ROWS,
+ IN order_id INT,
+ OUT insert_id,
+ OUT insert_id
+ )
+ BEGIN
+
+ :product = @FILTER(:product, order_product, false)
+ :product_options = @FILTER(:product_options, order_product_option, false, true)
+
+ INSERT INTO order_product
+ ( order_id, @KEYS(:product) )
+ VALUES ( :order_id, :product );
+
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, order_product_id, @KEYS(:each) )
+ VALUES ( :order_id, @result.order_product, :each );
+ END
+
+ CREATE PROCEDURE editProduct(
+ IN order_product_id INT,
+ IN product ROW,
+ IN product_options ROWS,
+ IN order_id INT,
+ OUT affected_rows,
+ OUT affected_rows
+ )
+ BEGIN
+
+ :product = @FILTER(:product, order_product)
+ :product_options = @FILTER(:product_options, order_product_option, false, true)
+
+ UPDATE `order_product`
+
+ SET @LIST(:product)
+
+ WHERE order_product_id = :order_product_id;
+
+ -- insert product option
+ @EACH(:product_options)
+ INSERT INTO order_product_option
+ ( order_id, order_product_id, @KEYS(:each) )
+ VALUES ( :order_id, :order_product_id, :each )
+ ON CONFLICT(`order_id`,`order_product_id`) DO UPDATE SET @LIST(:each);
+ END
+
+ CREATE PROCEDURE deleteProduct(
+ IN order_product_id ARRAY,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_product WHERE order_product_id IN (:order_product_id);
+
+ END
+
+ -- totals
+
+ CREATE PROCEDURE addTotal(
+ IN total ROW,
+ IN order_id INT,
+ OUT insert_id
+ )
+ BEGIN
+
+ :total = @FILTER(:total, order_total)
+
+ INSERT INTO order_total
+ ( order_id, @KEYS(:total) )
+ VALUES ( :order_id, :total );
+
+ END
+
+ CREATE PROCEDURE editTotal(
+ IN order_total_id INT,
+ IN total ROW,
+ OUT affected_rows
+ )
+ BEGIN
+
+ :total = @FILTER(:total, order_total)
+
+ UPDATE `order_total`
+
+ SET @LIST(:total)
+
+ WHERE order_total_id = :order_total_id;
+
+ END
+
+ CREATE PROCEDURE deleteTotal(
+ IN order_total_id ARRAY,
+ OUT affected_rows
+ )
+ BEGIN
+
+ DELETE FROM order_total WHERE order_total_id IN (:order_total_id);
+
+ END
diff --git a/admin/sql/sqlite/product.sql b/admin/sql/sqlite/product.sql
index 15c61de..b7ad84b 100644
--- a/admin/sql/sqlite/product.sql
+++ b/admin/sql/sqlite/product.sql
@@ -2,7 +2,7 @@
-- get one product
- PROCEDURE get(
+ CREATE PROCEDURE get(
IN product_id INT,
IN admin_id INT,
IN slug CHAR,
@@ -10,7 +10,6 @@
IN language_id INT,
IN promotion INT, -- include promotional price
IN user_group_id INT, -- user_group_id for group promotional price
- IN variant INT, -- include variants
IN variant_price INT, -- include variants min max prices
IN points INT, -- include points
IN stock_status INT, -- include stock_status
@@ -78,7 +77,7 @@
FROM product_points pp
WHERE pp.product_id = _.product_id
AND pp.user_group_id = :user_group_id
- AS points
+ ) AS points
END @IF
@@ -304,8 +303,7 @@
END
- PROCEDURE getData(
- IN product_id INT,
+ CREATE PROCEDURE getData(
IN manufacturer_id INT,
IN vendor_id INT,
@@ -316,9 +314,8 @@
OUT fetch_all,
OUT fetch_all,
OUT fetch_all,
-
- OUT fetch_one,
OUT fetch_one,
+ OUT fetch_one
)
BEGIN
@@ -401,68 +398,91 @@
-- Delete product
- PROCEDURE delete(
+ CREATE PROCEDURE delete(
IN product_id ARRAY,
IN admin_id INT,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
+ OUT affected_rows,
OUT affected_rows
)
BEGIN
- DELETE product_to_taxonomy_item FROM product_to_taxonomy_item
- @IF isset(:admin_id)
- THEN
- INNER JOIN product ON (product.product_id = product_to_taxonomy_item.product_id AND product.admin_id = :admin_id)
- END @IF
- WHERE product_to_taxonomy_item.product_id IN (:product_id);
+ DELETE FROM product_to_taxonomy_item
+
+ WHERE product_to_taxonomy_item.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_to_taxonomy_item.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_to_taxonomy_item.product_id AND product.admin_id = :admin_id))
+ END @IF;
- DELETE product_to_site FROM product_to_site
- @IF isset(:admin_id)
- THEN
- INNER JOIN product ON (product.product_id = product_to_site.product_id AND product.admin_id = :admin_id)
- END @IF
- WHERE product_to_site.product_id IN (:product_id);
+ DELETE FROM product_to_site
+ WHERE product_to_site.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_to_site.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_to_site.product_id AND product.admin_id = :admin_id))
+ END @IF;
- DELETE product_option_value FROM product_option_value
- @IF isset(:admin_id)
- THEN
- INNER JOIN product ON (product.product_id = product_option_value.product_id AND product.admin_id = :admin_id)
- END @IF
- WHERE product_option_value.product_id IN (:product_id);
+ DELETE FROM product_option_value
+ WHERE product_option_value.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_option_value.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_option_value.product_id AND product.admin_id = :admin_id))
+ END @IF;
- DELETE product_option FROM product_option
- @IF isset(:admin_id)
- THEN
- INNER JOIN product ON (product.product_id = product_option.product_id AND product.admin_id = :admin_id)
- END @IF
- WHERE product_option.product_id IN (:product_id);
+ DELETE FROM product_option
+ WHERE product_option.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_option.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_option.product_id AND product.admin_id = :admin_id))
+ END @IF;
- DELETE product_content FROM product_content
- @IF isset(:admin_id)
- THEN
- INNER JOIN product ON (product.product_id = product_content.product_id AND product.admin_id = :admin_id)
- END @IF
- WHERE product_content.product_id IN (:product_id);
+ DELETE FROM product_content
+ WHERE product_content.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_content.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_content.product_id AND product.admin_id = :admin_id))
+ END @IF;
- DELETE product_image FROM product_image
- @IF isset(:admin_id)
- THEN
- INNER JOIN product ON (product.product_id = product_image.product_id AND product.admin_id = :admin_id)
- END @IF
- WHERE product_image.product_id IN (:product_id);
+ DELETE FROM product_image
+ WHERE product_image.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_image.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_image.product_id AND product.admin_id = :admin_id))
+ END @IF;
+
+ DELETE FROM product_question
+ WHERE product_question.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_question.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_question.product_id AND product.admin_id = :admin_id))
+ END @IF;
- DELETE product FROM product WHERE product_id IN (:product_id)
- @IF isset(:admin_id)
- THEN
- AND admin_id = :admin_id
- END @IF;
+ DELETE FROM product_review
+ WHERE product_review.product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND product_review.product_id IN (SELECT product_id FROM product WHERE (product.product_id = product_review.product_id AND product.admin_id = :admin_id))
+ END @IF;
+
+ DELETE FROM product WHERE product_id IN (:product_id)
+ @IF isset(:admin_id)
+ THEN
+ AND admin_id = :admin_id
+ END @IF;
END
-- Edit product
- PROCEDURE edit(
- IN product ARRAY,
- IN product_content ARRAY,
+ CREATE PROCEDURE edit(
+ IN product ROW,
+ IN product_content ROWS,
IN taxonomy_item_id ARRAY,
IN product_id INT,
IN site_id ARRAY,
@@ -542,7 +562,7 @@
-- Edit post content
CREATE PROCEDURE editContent(
- IN product_content ARRAY,
+ IN product_content ROW,
IN product_id INT,
IN language_id INT,
OUT affected_rows
@@ -562,14 +582,14 @@
-- Add new product
CREATE PROCEDURE add(
- IN product ARRAY,
- IN product_content ARRAY,
+ IN product ROW,
+ IN product_content ROWS,
IN taxonomy_item_id ARRAY,
IN site_id ARRAY,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id,
- OUT insert_id
+ OUT insert_id, -- product
+ OUT affected_rows, -- product_content
+ OUT affected_rows, -- taxonomy_item_id
+ OUT affected_rows -- product_to_site
)
BEGIN
@@ -616,7 +636,7 @@
-- Edit product
- PROCEDURE productImage(
+ CREATE PROCEDURE productImage(
IN product_image ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -639,7 +659,7 @@
-- Edit product related
- PROCEDURE productRelated(
+ CREATE PROCEDURE productRelated(
IN product_related ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -662,8 +682,8 @@
-- Edit product variant
- PROCEDURE productVariant(
- IN product_variant ARRAY,
+ CREATE PROCEDURE productVariant(
+ IN product_variant ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -675,7 +695,7 @@
@EACH(:product_variant)
INSERT INTO product_variant
- ( product_variant_id, product_id)
+ ( @KEYS(:each), product_id)
VALUES ( :each, :product_id );
@@ -683,8 +703,8 @@
-- Edit product subscription
- PROCEDURE productSubscription(
- IN product_subscription ARRAY,
+ CREATE PROCEDURE productSubscription(
+ IN product_subscription ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -704,8 +724,8 @@
-- Edit product discount
- PROCEDURE productDiscount(
- IN product_discount ARRAY,
+ CREATE PROCEDURE productDiscount(
+ IN product_discount ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -725,8 +745,8 @@
-- Edit product promotion
- PROCEDURE productPromotion(
- IN product_promotion ARRAY,
+ CREATE PROCEDURE productPromotion(
+ IN product_promotion ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -746,8 +766,8 @@
-- Edit product points
- PROCEDURE productPoints(
- IN product_points ARRAY,
+ CREATE PROCEDURE productPoints(
+ IN product_points ROWS,
IN product_id INT,
OUT affected_rows,
OUT affected_rows
@@ -767,10 +787,9 @@
-- Edit product attribute
- PROCEDURE productAttribute(
- IN product_attribute ARRAY,
+ CREATE PROCEDURE productAttribute(
+ IN product_attribute ROWS,
IN product_id INT,
- IN language_id INT,
OUT affected_rows,
OUT affected_rows
)
@@ -791,7 +810,7 @@
-- Edit product digital asset
- PROCEDURE productDigitalAsset(
+ CREATE PROCEDURE productDigitalAsset(
IN product_digital_asset ARRAY,
IN product_id INT,
OUT affected_rows,
@@ -814,7 +833,7 @@
-- get all products
- PROCEDURE getAll(
+ CREATE PROCEDURE getAll(
-- variables
IN language_id INT,
@@ -842,6 +861,7 @@
-- pagination
IN start INT,
IN limit INT,
+ IN cursor INT, -- current cursor (product_id) for cursor pagination
IN type CHAR,
IN order_by CHAR,
IN direction CHAR,
@@ -864,8 +884,7 @@
-- return array of products for products query
OUT fetch_all,
- -- return products count for count query
- OUT fetch_one,
+ OUT fetch_one
)
BEGIN
@@ -906,6 +925,11 @@
OR pd2.from_date < date('now'))
AND (pd2.to_date = NULL
OR pd2.to_date > date('now')))
+ -- cursor pagination
+ @IF isset(:cursor)
+ THEN
+ AND product.product_id > :cursor
+ END @IF
ORDER BY pd2.priority ASC, pd2.price ASC
LIMIT 1) AS discount
@@ -947,7 +971,7 @@
FROM product_points pp
WHERE pp.product_id = product.product_id
AND pp.user_group_id = :user_group_id
- AS points
+ ) AS points
END @IF
@@ -996,7 +1020,7 @@
THEN
,(SELECT AVG(rating) AS total
- FROM review r1
+ FROM product_review r1
WHERE r1.product_id = product.product_id
AND r1.status = '1'
GROUP BY r1.product_id)
@@ -1010,7 +1034,7 @@
THEN
,(SELECT COUNT(*) AS total
- FROM review r2
+ FROM product_review r2
WHERE r2.product_id = product.product_id
AND r2.status = '1'
GROUP BY r2.product_id) AS reviews
@@ -1128,7 +1152,7 @@
@IF isset(:quantity) && :quantity !== ""
THEN
- AND product.quantity = :quantity
+ AND product.stock_quantity = :quantity
END @IF
@IF isset(:model) && :model !== ""
@@ -1192,7 +1216,7 @@
@IF !empty(:taxonomy_item_id)
THEN
- AND pt.taxonomy_item_id = :taxonomy_item_id
+ AND pt.taxonomy_item_id IN(:taxonomy_item_id)
END @IF
@@ -1212,13 +1236,13 @@
@IF !empty(:product_attribute_id)
THEN
- pa.product_attribute_id IN (:product_attribute_id)
+ pa.attribute_id IN (:product_attribute_id)
END @IF
@IF !empty(:product_attribute)
THEN
- pa.text IN (:product_attribute)
+ pa.value IN (:product_attribute)
END @IF
@@ -1237,10 +1261,17 @@
END @IF
- @IF isset(:limit)
- THEN
- @SQL_LIMIT(:start, :limit)
- END @IF;
+ -- limit
+ @IF isset(:limit)
+ THEN
+ LIMIT :limit
+
+ @IF isset(:start)
+ THEN
+ OFFSET :start
+ END @IF
+
+ END @IF;
-- SELECT FOUND_ROWS() as count;
diff --git a/system/db/dbdriver.php b/system/db/dbdriver.php
old mode 100755
new mode 100644
index 77cb708..9b4ef66
--- a/system/db/dbdriver.php
+++ b/system/db/dbdriver.php
@@ -22,24 +22,46 @@
namespace Vvveb\System\Db;
-define('SQL_VAR_REGEX',
- '/:([a-zA-Z0-9\[][\.\'a-zA-Z0-9\[\]_-]+)/ms');
+define(
+ 'SQL_VAR_REGEX',
+ '/:([a-zA-Z0-9\[\]_][\.\'a-zA-Z0-9\[\]_-]+)/ms'
+);
class DBDriver {
- protected $quote;
+ /** @var string Quote character used to delimit identifiers (e.g. backtick for MySQL, double-quote for PostgreSQL). */
+ public string $quote = '';
- public function _filter($data, $columns, $addMissingDefaults = false) {
+ /**
+ * Filter a single data row against a column metadata definition.
+ *
+ * Removes keys not present in the column schema, applies default values for
+ * null entries, and truncates values exceeding their maximum length.
+ *
+ * @param array<string, mixed> $data The data row to filter.
+ * @param array<string, array{t?: string, d?: mixed, n?: bool, l?: int|null, e?: string}> $columns Column metadata from getColumnsMeta().
+ * @param bool $addMissingDefaults Whether to populate missing non-nullable columns with defaults.
+ *
+ * @return array<string, mixed> The filtered data row.
+ */
+ public function _filter(array $data, array $columns, bool $addMissingDefaults = false): array {
//remove fields that are not table columns, $colums is returned by sqlp->getColumnsMeta()
foreach ($data as $key => $name) {
- if (! isset($columns[$key])) {
+ if (!isset($columns[$key])) {
unset($data[$key]);
} else {
- if ($data[$key] === NULL) {
+ if ($data[$key] === null) {
//set default value is empty value provided
if (isset($columns[$key]['d'])) {
$data[$key] = $columns[$key]['d'];
+ if ($data[$key] === 'NULL') {
+ $data[$key] = null;
+ }
}
}
+ //if max length make sure that content does not exceed it
+ if (isset($columns[$key]['l']) && $columns[$key]['l'] && is_string($data[$key]) && $data[$key]) {
+ $data[$key] = substr($data[$key], 0, $columns[$key]['l']);
+ }
}
}
@@ -52,11 +74,11 @@ class DBDriver {
}
//todo: validate based on data type (t)
//if there is no data for column and column is not nullable set default
- if (! isset($data[$name]) && $options['n'] == false) {
+ if (!isset($data[$name]) && $options['n'] == false) {
if ($options['d']) {
$data[$name] = $options['d'];
} else {
- if ($options['d'] == NULL) {
+ if ($options['d'] == null) {
$data[$name] = '';
}
@@ -79,7 +101,14 @@ class DBDriver {
return $data;
}
- public function varType($var) {
+ /**
+ * Determine the SQL parameter type code for a given PHP value.
+ *
+ * @param mixed $var The value to classify.
+ *
+ * @return 'a'|'d'|'i'|'s' Type code: array<array-key, mixed>, decimal, integer, or string.
+ */
+ public function varType(mixed $var): string {
//$type = gettype($var);
if (is_array($var)) {
$type = 'a';
@@ -99,12 +128,26 @@ class DBDriver {
return $type;
}
- public function filter($data, $columns, $addMissingDefaults = false) {
+ /**
+ * Filter data rows (single or collection) against a column metadata definition.
+ *
+ * Detects whether the input is a single row or a collection of rows and
+ * delegates to _filter() accordingly.
+ *
+ * @param array<int|string, mixed> $data Data row or collection of rows.
+ * @param array<string, array{t?: string, d?: mixed, n?: bool, l?: int|null, e?: string}> $columns Column metadata.
+ * @param bool $addMissingDefaults Whether to populate missing defaults.
+ *
+ * @return array<int|string, mixed> Filtered data.
+ */
+ public function filter(array $data, array $columns, bool $addMissingDefaults = false): array {
//check if collection of rows or individual row
reset($data);
if (is_numeric(key($data))) {
//rows
+ $return = [];
+
foreach ($data as $key => $row) {
$return[$key] = $this->_filter($row, $columns, $addMissingDefaults);
}
@@ -116,12 +159,25 @@ class DBDriver {
}
}
- public function validate($data, $params, $table = '', $ignoreMissing = false) {
+ /**
+ * Validate input data against a set of rules.
+ *
+ * Supports simple type checks (int, string, array) and complex column-level
+ * validation with default values, max lengths, and nullable checks.
+ *
+ * @param array<string, mixed> $data The data to validate.
+ * @param array<string, string|array<string, array<array-key, mixed>>> $params Validation rules — type code or column metadata array.
+ * @param string $table Table name used to auto-skip primary key columns.
+ * @param bool $ignoreMissing Whether to skip missing-field errors.
+ *
+ * @return array<int, string> List of validation error messages.
+ */
+ public function validate(array $data, array $params, string $table = '', bool $ignoreMissing = false): array {
$errors = [];
foreach ($params as $name => $type) {
- if (! isset($data[$name])) {
- if (! $ignoreMissing) {
+ if (!isset($data[$name])) {
+ if (!$ignoreMissing) {
$errors[] = sprintf('%s missing!', $name);
}
@@ -144,13 +200,13 @@ class DBDriver {
continue;
}
- if (! isset($arr[$col])) {
+ if (!isset($arr[$col])) {
//skip columns that have default values
- if (($attr['d'] === 'NULL' || $attr['d'] === NULL) && ! $ignoreMissing) {
+ if (($attr['d'] === 'null' || $attr['d'] === null) && !$ignoreMissing) {
$errors[] = sprintf('%s missing!', $name . '[' . $col . ']');
}
} else {
- if (($attr['t'] == 'int' || $attr['t'] == 'decimal' || $attr['t'] == 'tinyint') && ! is_numeric($arr[$col])) {
+ if (($attr['t'] == 'int' || $attr['t'] == 'decimal' || $attr['t'] == 'tinyint') && !is_numeric($arr[$col])) {
$errors[] = sprintf('%s is not an integer!', $name . '[' . $col . ']');
}
@@ -165,12 +221,12 @@ class DBDriver {
} else {
switch ($type) {
case 'a':
- if (! isset($data[$name]) || ! is_array($data[$name])) {
+ if (!isset($data[$name]) || !is_array($data[$name])) {
$errors[] = sprintf('%s is not an array!', $name);
} else {
- if (substr_compare($name, '_id', -3 ,3) === 0 &&
- ! empty($data[$name]) &&
- ! is_numeric($data[$name][0])) {
+ if (substr_compare($name, '_id', -3, 3) === 0 &&
+ !empty($data[$name]) &&
+ !is_numeric($data[$name][0])) {
$errors[] = sprintf('%s is not an integer!', $name . '[0]');
}
}
@@ -178,14 +234,14 @@ class DBDriver {
break;
case 'i':
- if (! isset($data[$name]) || ! is_numeric($data[$name])) {
+ if (!isset($data[$name]) || !is_numeric($data[$name])) {
$errors[] = sprintf('%s is not an integer!', $name);
}
break;
case 's':
- if (! isset($data[$name]) || ! is_string($data[$name])) {
+ if (!isset($data[$name]) || !is_string($data[$name])) {
$errors[] = sprintf('%s is not a string!', $name);
}
@@ -197,46 +253,97 @@ class DBDriver {
return $errors;
}
- /*
- * Expands arrays a = ['param' => 'value', 'second' => value]; to a[param], a[second]
- */
- public function expandArray($array, $arrayName) {
+ /**
+ * Expand an associative array into a SQL comma-separated parameter list.
+ *
+ * Converts ['param' => 'value', 'second' => 'val'] to :arrayName['param'], :arrayName['second'].
+ *
+ * @param array<string, mixed> $array The array to expand.
+ * @param string $arrayName The parameter array name.
+ *
+ * @return array{string, array<string, string>} The SQL fragment and parameter type map.
+ */
+ public function expandArray(array $array, string $arrayName): array {
$first = true;
$sql = '';
$parameters = [];
- if (is_array($array)) {
- foreach ($array as $key => $value) {
- if (! $first) {
- $sql .= ',';
- }
- $arrayKey = "['" . $arrayName . "']['" . $key . "']";
- $sql .= ':' . $arrayKey;
- $type = $this->varType($value);
- $parameters[$arrayKey] = $type;
- $first = false;
+ foreach ($array as $key => $value) {
+ if (!$first) {
+ $sql .= ',';
}
+ $arrayKey = "['" . $arrayName . "']['" . $key . "']";
+ $sql .= ':' . $arrayKey;
+ $type = $this->varType($value);
+ $parameters[$arrayKey] = $type;
+ $first = false;
}
return [$sql, $parameters];
}
/**
- * Generate a SQL list used for inserts
- * input ['var1' => 1, 'var2'=> 2, 'var3'=> 3] output var1 = 1, var2 = 2, var3 = 3.
- * @param mixed $array
- * @param mixed $arrayName
+ * Sanitize array keys used as SQL identifiers (eg. from the @KEYS macro).
+ *
+ * The keys of user supplied arrays can end up as SQL identifiers in the
+ * expansion of the @KEYS macro, an attacker with a back-office account could
+ * close the quoted identifier with the quote character in the key and inject
+ * SQL. Escape the quote character inside the key by doubling it and drop
+ * non-scalar keys.
+ *
+ * @param array<array-key, mixed> $keys Array keys from user supplied data.
+ *
+ * @return array<string> Sanitized keys, safe to embed as quoted identifiers.
+ */
+ public function escapeKeys(array $keys): array {
+ $return = [];
+ $quote = $this->quote;
+
+ foreach ($keys as $key) {
+ if (!is_scalar($key)) {
+ continue;
+ }
+
+ $key = (string)$key;
+
+ if ($quote !== '') {
+ $key = str_replace($quote, $quote . $quote, $key);
+ }
+
+ $return[] = $key;
+ }
+
+ return $return;
+ }
+
+ /**
+ * Generate a SQL SET clause from an associative array for UPDATE statements.
+ *
+ * Converts ['var1' => 1, 'var2' => 2] to `var1` = :arrayName['var1'], `var2` = :arrayName['var2'].
*
- * @return mixed
+ * @param array<string, mixed> $array The key-value pairs to set.
+ * @param string $arrayName The parameter array name.
+ *
+ * @return array{string, array<string, string>} The SQL SET fragment and parameter type map.
*/
- public function expandList($array, $arrayName) {
+ public function expandList(array $array, string $arrayName): array {
$first = true;
$sql = '';
$parameters = [];
$quote = $this->quote;
foreach ($array as $key => $value) {
- if (! $first) {
+ if (!is_scalar($key)) {
+ continue;
+ }
+
+ $key = (string)$key;
+
+ if ($quote !== '') {
+ $key = str_replace($quote, $quote . $quote, $key);
+ }
+
+ if (!$first) {
$sql .= ',';
}
$arrayKey = "['$arrayName']['$key']";
@@ -249,122 +356,153 @@ class DBDriver {
return [$sql, $parameters];
}
- public function sqlCount($query, $column, $table) {
+ /**
+ * Convert a SELECT query into a COUNT query by stripping LIMIT, ORDER BY,
+ * and replacing the selected columns.
+ *
+ * @param string $query The original SELECT query.
+ * @param string $column The column expression to count (e.g. '*' or 'DISTINCT id').
+ * @param string $table The table name.
+ *
+ * @return string The modified COUNT query.
+ */
+ public function sqlCount(string $query, string $column, string $table): string {
//remove limit
//pgsql
$query = preg_replace('/LIMIT\s+(\d+|:\w+)\s+OFFSET\s*(\d+|:\w+)\s*;?$/', '', $query);
//mysql
$query = preg_replace('/LIMIT\s+(\d+|:\w+),\s*(\d+|:\w+)\s*;?$/', '', $query);
//remove order by
- $query = preg_replace('/ORDER BY\s+([a-z\._\-]+|,|\s+|DESC|ASC)+/', '', $query);
+ $query = preg_replace('/ORDER BY\s+([a-z\._\-`]+|,|\s+|DESC|ASC)+/', '', $query);
- $query = preg_replace("/^\s*SELECT .*?\s*FROM\s*$table\s/ms", "SELECT $column FROM $table ", $query);
-
- return $query;
+ return preg_replace("/^\s*SELECT .*?\s*FROM\s*$table\s/ms", "SELECT $column FROM $table ", $query);
}
- /*
- * Convert array dot notation to php notation
- * Ex: my.array.key to ['my']['array']['key']
+ /**
+ * Convert a dot-notation key to PHP nested-array notation.
+ *
+ * Converts 'my.array.key' to ['my']['array']['key'].
+ *
+ * @param string $key The dot-notation key.
+ *
+ * @return string The PHP array<array-key, mixed> key notation.
*/
- function sqlPhpArrayKey($key) {
+ public function sqlPhpArrayKey(string $key): string {
return '[\'' . str_replace('.', '\'][\'', $key) . '\']';
}
- /*
- * Replace :named_params with ?
+ /**
+ * Replace :named_params in SQL with positional placeholders (? or $N).
+ *
+ * Extracts parameter values and builds a type string for prepared statements.
+ * Supports nested array parameters, dot-notation keys, and IN-clause expansion.
+ *
+ * @param string $sql The SQL query with named parameters (modified in place).
+ * @param array<string, mixed> $params Named parameter values (modified in place).
+ * @param array<string, string> $paramTypes Optional explicit type overrides (modified in place).
+ * @param string $placeholder Placeholder style: '?' for MySQL/SQLite, '$' for PostgreSQL.
+ *
+ * @return array{array<int, mixed>, string} The extracted positional parameters and concatenated type string.
*/
- public function paramsToQmark(&$sql, &$params = [], &$paramTypes = [], $placeholder = '?') {
+ public function paramsToQmark(string &$sql, array &$params = [], array &$paramTypes = [], string $placeholder = '?'): array {
$parameters = [];
+ $types = '';
$index = 1;
$sql = preg_replace_callback(
- SQL_VAR_REGEX,
- function ($matches) use (&$params, &$parameters, &$types, $paramTypes, &$index, $placeholder) {
- $varName = $matches[1];
+ SQL_VAR_REGEX,
+ function($matches) use (&$params, &$parameters, &$types, $paramTypes, &$index, $placeholder) {
+ $varName = $matches[1];
- if (strpos($varName, '.') !== false) {
- $varName = $this->sqlPhpArrayKey($varName);
- }
- //if parameters is array element
- if ($varName[0] == '[') {
- if (preg_match_all('/[\w_-]+/', $varName, $arrayKeys)) {
- $type = $paramTypes[$varName] ?? 's';
+ if (str_contains($varName, '.')) {
+ $varName = $this->sqlPhpArrayKey($varName);
+ }
+ //if parameters is array element
+ if ($varName[0] == '[') {
+ if (preg_match_all('/[\w_-]+/', $varName, $arrayKeys)) {
+ $type = $paramTypes[$varName] ?? 's';
- $types .= $type;
+ $types .= $type;
- $key1 = $arrayKeys[0][0];
- $key2 = $arrayKeys[0][1];
+ $key1 = $arrayKeys[0][0];
+ $key2 = $arrayKeys[0][1];
- $parameter = &$params[$key1][$key2];
- //if (strpos($parameter, ')') && )
- $parameters[] = $parameter;
- }
- } else {
- if (isset($params[$varName])) {
- $parameter = &$params[$varName];
-
- if (isset($paramTypes[$varName])) {
- $type = $paramTypes[$varName];
- } else {
- $type = $this->varType($parameter);
+ $parameter = &$params[$key1][$key2];
+ $parameters[] = $parameter;
}
+ } else {
+ if (isset($params[$varName])) {
+ $parameter = &$params[$varName];
- if ($type == 'a') {
- $return = false;
+ if (isset($paramTypes[$varName])) {
+ $type = $paramTypes[$varName];
+ } else {
+ $type = $this->varType($parameter);
+ }
- foreach ($parameter as $key => $value) {
- $parameters[] = $value;
+ if ($type == 'a') {
+ $return = false;
- if ($placeholder == '$') {
- $holder = '$' . $index;
- } else {
- $holder = '?';
- }
+ foreach ($parameter as $key => $value) {
+ $parameters[] = $value;
- if ($return) {
- $return .= ",$holder";
- } else {
- $return = $holder;
+ if ($placeholder == '$') {
+ $holder = '$' . $index;
+ } else {
+ $holder = '?';
+ }
+
+ if ($return) {
+ $return .= ",$holder";
+ } else {
+ $return = $holder;
+ }
+
+ $type = $this->varType($value);
+ $types .= $type;
+
+ $index++;
}
- $type = $this->varType($value);
- $types .= $type;
+ return $return;
+ } else {
+ $types .= $type ?: $this->varType($parameter);
- $index++;
+ $parameters[] = $parameter;
}
-
- return $return;
} else {
- if (! $type) {
- $type = $this->varType($parameter);
- }
- $types .= $type;
-
- $parameters[] = $parameter;
+ return 'null';
}
- } else {
- return 'null';
}
- }
- if ($placeholder == '$') {
- $holder = '$' . $index;
- } else {
- $holder = '?';
- }
+ if ($placeholder == '$') {
+ $holder = '$' . $index;
+ } else {
+ $holder = '?';
+ }
- $return = $holder;
- $index++;
+ $return = $holder;
+ $index++;
- return $return;
- },
- $sql);
+ return $return;
+ },
+ $sql
+ ) ?? $sql;
return [$parameters, $types];
}
- public function debugSql($sql, $params = [], $paramTypes = [], $placeholder = '?') {
- list($parameters, $types) = $this->paramsToQmark($sql, $params, $paramTypes, $placeholder);
+ /**
+ * Generate a debug-safe SQL string with all placeholders replaced by actual values.
+ *
+ * @param string $sql The SQL query with positional placeholders.
+ * @param array<array-key, mixed> $params The parameter values.
+ * @param array<array-key, string> $paramTypes Optional explicit type overrides.
+ * @param string $placeholder Placeholder style: '?' or '$'.
+ *
+ * @return string The SQL string with embedded literal values for debugging.
+ */
+ public function debugSql(string $sql, array $params = [], array $paramTypes = [], string $placeholder = '?'): string {
+ [$parameters, $types] = $this->paramsToQmark($sql, $params, $paramTypes, $placeholder);
if ($placeholder == '$') {
$regex = '\$\d+';
@@ -372,103 +510,23 @@ class DBDriver {
$regex = "\\$placeholder";
}
$index = 0;
- $sql = preg_replace_callback(
- "/$regex/",
- function ($matches) use (&$parameters, &$sql, &$index) {
- $value = $parameters[$index++];
- if (is_array($value)) {
- $value = var_export($value, 1);
- } else {
- if (! is_numeric($value)) {
- $value = "'$value'";
- }
- }
+ return preg_replace_callback(
+ "/$regex/",
+ function($matches) use (&$parameters, &$index) {
+ $value = $parameters[$index++];
- return $value;
- }, $sql);
-
- return $sql;
- $sql = preg_replace_callback(
- SQL_VAR_REGEX,
- function ($matches) use (&$params, &$types, $paramTypes) {
- //if parameters is array element
- $varName = $matches[1];
- $value = $params[$varName] ?? '';
-
- if (strpos($varName, '.') !== false) {
- $varName = $this->sqlPhpArrayKey($varName);
- }
-
- if ($varName[0] == '[') {
- if (preg_match_all('/[\w_-]+/', $varName, $arrayKeys)) {
- $type = $paramTypes[$varName] ?? 's';
-
- $types .= $type;
-
- $key1 = $arrayKeys[0][0];
- $key2 = $arrayKeys[0][1];
-
- $parameter = &$params[$key1][$key2];
- //if (strpos($parameter, ')') && )
- if ($type == 's') {
- $parameter = '"' . (string)$parameter . '"';
- }
-
- return $parameter;
- }
- } else {
- if (isset($params[$varName])) {
- $parameter = &$params[$varName];
-
- if (isset($paramTypes[$varName])) {
- $type = $paramTypes[$varName];
- } else {
- $type = $this->varType($parameter);
- }
-
- if ($type == 'a') {
- $return = false;
-
- if ($parameter) {
- foreach ($parameter as $key => $value) {
- if ($return) {
- $return .= ',';
- }
-
- if ($value) {
- $return .= '"' . $value . '"';
- }
- }
- }
-
- return $return;
- } else {
- if (! $type) {
- $type = 's';
- }
- $types .= $type;
- }
-
- if (! $type) {
- $type = 's';
- }
- $types .= $type;
-
- if ($type == 's') {
- $parameter = '"' . $parameter . '"';
- }
-
- return $parameter;
+ if (is_array($value)) {
+ $value = var_export($value, true);
} else {
- return 'null';
+ if (!is_numeric($value)) {
+ $value = "'$value'";
+ }
}
- }
-
- return '?';
- },
- $sql);
- return $sql;
+ return $value;
+ },
+ $sql
+ );
}
}
diff --git a/system/sqlp/config.php b/system/sqlp/config.php
index c0dde25..9e336d0 100755
--- a/system/sqlp/config.php
+++ b/system/sqlp/config.php
@@ -26,19 +26,19 @@ return
'functionRegex' => '/(CREATE\s+)?PROCEDURE\s+(?<name>\w+)\((?<params>.*?)\)\s+BEGIN\s+(?<statement>.*?)END(?!\s*@)/ms',
- 'paramRegex' => '/(IN|OUT|INOUT|LOCAL)\s+([\.@\w]+)\s*(\w+)?(\(\d+\))?[,$\n\s]?/ms',
+ 'paramRegex' => '/(IN|OUT|INOUT|LOCAL)\s+([\.@\w]+)\s*(\w+)?(\(\d+\))?,?(\s*--[^$\n]+)?[$\n]?/ms',
- 'valuesRegex' => '/\s*@VALUES\s*\(\s*:(?<list>[\w\[\]]+)\s*\)\s*/ms',
+ 'valuesRegex' => '/\s*@VALUES\s*\(\s*:(?<list>[\w\[\]]+)\s*\)\s*/ms',
- 'eachRegex' => '/\s*@EACH\s*\(\s*(\w+)\s*\,\s*([\w\.]+)\s*\)\s*/ms',
+ 'eachRegex' => '/\s*@EACH\s*\(\s*(\w+)\s*\,\s*([\w\.]+)\s*\)\s*/ms',
- 'eachVarRegex' => '/\s*@EACH\s*\(\s*:(.+?)\s*\)\s*/ms',
+ 'eachVarRegex' => '/\s*@EACH\s*\(\s*:(.+?)\s*\)\s*/ms',
- 'filterRegex' => '/\s*:?(?<return>[\w_\.]+)?\s*=?\s*@FILTER\s*\(\s*:(?<data>[\w\._]+)\s*\,\s*(?<columns>[\w_]+),?\s*(?<addmissing>true|false)?,?\s*(?<array>true|false)?\s*\)\s*/ms',
+ 'filterRegex' => '/\s*:?(?<return>[\w_\.]+)?\s*=?\s*@FILTER\s*\(\s*:(?<data>[\w\._]+)\s*\,\s*(?<columns>[\w_]+),?\s*(?<addmissing>true|false)?,?\s*(?<array>true|false)?\s*\)\s*/ms',
- 'varRegex' => '/:([\w+\.])/ms',
+ 'varRegex' => '/:([\w\.]+)/ms',
- 'importRegex' => '/import\(([\w\-\_\.\/]+?)\);?/',
+ 'importRegex' => '/import\(([\w\-\_\.\/]+?)\);?/',
//Generated model templates
@@ -46,15 +46,14 @@ return
//Lexer
'tokenMap' => [
- '@IF\s+.+?\s*THEN' => 'T_IF_START',
- '@ELSE' => 'T_ELSE',
- 'END @IF' => 'T_IF_END',
- '@KEYS\(.+?\)' => 'T_KEYS',
- '@LIST\(.+?\)' => 'T_LIST',
+ '@IF\s+.+?\s*THEN' => 'T_IF_START',
+ '@ELSE' => 'T_ELSE',
+ 'END @IF' => 'T_IF_END',
+ '@KEYS\(.+?\)' => 'T_KEYS',
+ '@LIST\(.+?\)' => 'T_LIST',
//'@EACH\(.+?\)' => 'T_EACH_START',
//'END @EACH' => 'T_EACH_END',
'@SQL_COUNT\(.+?\)' => 'T_SQL_COUNT',
- '@SQL_LIMIT\(.+?\)' => 'T_SQL_LIMIT',
'@ESC\(.+?\)' => 'T_ESC',
'.+?' => 'T_SQL',
],
@@ -64,74 +63,64 @@ return
'T_IF_START' => [
'/\s*@IF\s*(?<cond>.+?)\s*THEN\s*/', //regex
<<<'PHP'
- ';
- if ($%cond) {
- $sql .= '
-PHP
+ ';
+ if ($%cond) {
+ $sql .= '
+ PHP
,
],
'T_ELSE' => <<<'PHP'
- ';
- } else {
- $sql .= '
-PHP
+ ';
+ } else {
+ $sql .= '
+ PHP
,
'T_IF_END' => '\';
} //end if
-
+
$sql .= \'',
//end if
- 'T_KEYS' => [
+ 'T_KEYS' => [
'/\s*@KEYS\s*\(\s*:(?<keys>[\w_\.]+)\s*\)\s*/ms',
<<<'PHP'
- ';
- $q = $this->db->quote;
- $sql .= $q . implode("$q,$q", array_keys($params['%keys']));
- $sql .= $q . '
-PHP
+ ';
+ $q = $this->db->quote;
+ $sql .= $q . implode("$q,$q", $this->db->escapeKeys(array_keys($params['%keys'])));
+ $sql .= $q . '
+ PHP
],
- 'T_LIST' => [
+ 'T_LIST' => [
'/\s*@LIST\s*\(\s*:(?<list>[\w_\.]+)\s*\)\s*/ms',
<<<'PHP'
- ';
-
- list($_sql, $_params) = $this->db->expandList($params['%list'], '%list');
+ ';
- $sql .= ' ' . $_sql;
+ list($_sql, $_params) = $this->db->expandList($params['%list'], '%list');
- if (is_array($_params)) $paramTypes = array_merge($paramTypes, $_params);
+ $sql .= ' ' . $_sql;
- $sql .= ' ' . '
-PHP
- ],
+ if (is_array($_params)) $paramTypes = array_merge($paramTypes, $_params);
- 'T_SQL_COUNT' => [
- '/\s*@SQL_COUNT\s*\(\s*(?<column>.+?),\s*(?<table>.+?)\s*\)\s*/ms',
- <<<'PHP'
- ';
- $sql .= $this->db->sqlCount($prevSql, '%column', $this->db->prefix . '%table');
- $sql .= '
-PHP
+ $sql .= ' ' . '
+ PHP
],
- 'T_SQL_LIMIT' => [
- '/\s*@SQL_LIMIT\s*\(\s*(?<start>.+?),\s*(?<limit>.+?)\s*\)\s*/ms',
+ 'T_SQL_COUNT' => [
+ '/\s*@SQL_COUNT\s*\(\s*(?<column>.+?),\s*(?<table>.+?)\s*\)\s*/ms',
<<<'PHP'
- ';
- $sql .= $this->db->sqlLimit('%start', '%limit');
- $sql .= '
-PHP
+ ';
+ $sql .= $this->db->sqlCount($prevSql, '%column', $this->db->prefix . '%table');
+ $sql .= '
+ PHP
],
-
- 'T_ESC' => [
+ 'T_ESC' => [
'/@ESC\s*\(\s*:?(?<column>.+?)\s*\)/ms',
<<<'PHP'
- ';
- $sql .= preg_replace('/\W+/', '', $params['%column'] ?? '');
- $sql .= '
-PHP
+ ';
+ $sql .= preg_replace('/\W+/', '', $params['%column'] ?? '');
+ $sql .= '
+ PHP
],
//'T_EACH_START' => 'EACH()',