Exercise: Creating the Database Functions

Một phần của tài liệu Beginning PHP and Postgre SQL E-Commerce From Novice to Professional phần 7 ppt (Trang 24 - 27)

1. Load pgAdmin III, and connect to the hatshopdatabase.

2. Click Tools ➤Query tool (or click the SQL button on the toolbar). A new query window should appear.

3. Use the query tool to execute this code, which creates the customer_logintype and customer_get_login_infofunction in your hatshopdatabase:

-- Create customer_login_info type CREATE TYPE customer_login_info AS (

customer_id INTEGER, password VARCHAR(50) );

-- Create customer_get_login_info function

CREATE FUNCTION customer_get_login_info(VARCHAR(100)) RETURNS customer_login_info LANGUAGE plpgsql AS $$

DECLARE

inEmail ALIAS FOR $1;

outCustomerLoginInfoRow customer_login_info;

BEGIN

SELECT INTO outCustomerLoginInfoRow customer_id, password FROM customer

WHERE email = inEmail;

RETURN outCustomerLoginInfoRow;

END;

$$;

When a user logs in to the site, you must check his or her password. The customer_get_login_info function returns the customer ID and the hashed password for a user with a specific email.

4. Use the query tool to execute this code, which creates the customer_addfunction in your hatshop database:

-- Create customer_add function CREATE FUNCTION customer_add(

VARCHAR(50), VARCHAR(100), VARCHAR(50)) RETURNS INTEGER LANGUAGE plpgsql AS $$

DECLARE

inName ALIAS FOR $1;

inEmail ALIAS FOR $2;

inPassword ALIAS FOR $3;

outCustomerId INTEGER;

BEGIN

INSERT INTO customer (name, email, password) VALUES (inName, inEmail, inPassword);

SELECT INTO outCustomerId

currval('customer_customer_id_seq');

RETURN outCustomerId;

END;

$$;

The customer_addfunction is called when a user registers on the site. This method returns the customer ID for that user to be saved in the session.

5. Use the query tool to execute this code, which creates the customer_get_customerfunction in your hatshopdatabase:

-- Create customer_get_customer function CREATE FUNCTION customer_get_customer(INTEGER) RETURNS customer LANGUAGE plpgsql AS $$

DECLARE

inCustomerId ALIAS FOR $1;

outCustomerRow customer;

BEGIN

SELECT INTO outCustomerRow

customer_id, name, email, password, credit_card,

address_1, address_2, city, region, postal_code, country, shipping_region_id, day_phone, eve_phone, mob_phone FROM customer

WHERE customer_id = inCustomerId;

RETURN outCustomerRow;

END;

$$;

The customer_get_customerfunction returns full customer details for a given customer ID.

6. Use the query tool to execute this code, which creates the customer_update_accountfunction in your hatshopdatabase:

-- Create customer_update_account function

CREATE FUNCTION customer_update_account(INTEGER, VARCHAR(50), VARCHAR(100), VARCHAR(50), VARCHAR(100), VARCHAR(100), VARCHAR(100)) RETURNS VOID LANGUAGE plpgsql AS $$

DECLARE

inCustomerId ALIAS FOR $1;

inName ALIAS FOR $2;

inEmail ALIAS FOR $3;

inPassword ALIAS FOR $4;

inDayPhone ALIAS FOR $5;

inEvePhone ALIAS FOR $6;

inMobPhone ALIAS FOR $7;

BEGIN

UPDATE customer

SET name = inName, email = inEmail,

password = inPassword, day_phone = inDayPhone, eve_phone = inEvePhone, mob_phone = inMobPhone WHERE customer_id = inCustomerId;

END;

$$;

The customer_update_accountfunction updates the customer’s account details in the database.

7. Use the query tool to execute this code, which creates the customer_update_credit_cardfunction in your hatshopdatabase:

-- Create customer_update_credit_card function

CREATE FUNCTION customer_update_credit_card(INTEGER, TEXT) RETURNS VOID LANGUAGE plpgsql AS $$

DECLARE

inCustomerId ALIAS FOR $1;

inCreditCard ALIAS FOR $2;

BEGIN

UPDATE customer

SET credit_card = inCreditCard WHERE customer_id = inCustomerId;

END;

$$;

The customer_update_credit_cardfunction updates the customer’s credit card information in the database. It only updates the credit_cardcolumn for the customer, which contains the encrypted version of the XML document containing the customer’s complete credit card details.

8. Use the query tool to execute this code, which creates the customer_get_shipping_regionsfunction in your hatshopdatabase:

-- Create customer_get_shipping_regions function CREATE FUNCTION customer_get_shipping_regions() RETURNS SETOF shipping_region LANGUAGE plpgsql AS $$

DECLARE

outShippingRegion shipping_region;

BEGIN

FOR outShippingRegion IN

SELECT shipping_region_id, shipping_region FROM shipping_region

LOOP

RETURN NEXT outShippingRegion;

END LOOP;

RETURN;

END;

$$;

The customer_get_shipping_regionsfunction returns the shipping regions in the database for the customer address details page.

9. Use the query tool to execute this code, which creates the customer_update_addressfunction in your hatshopdatabase:

-- Create customer_update_address function

CREATE FUNCTION customer_update_address(INTEGER, VARCHAR(100), VARCHAR(100), VARCHAR(100), VARCHAR(100), VARCHAR(100), VARCHAR(100), INTEGER)

RETURNS VOID LANGUAGE plpgsql AS $$

DECLARE

inCustomerId ALIAS FOR $1;

inAddress1 ALIAS FOR $2;

inAddress2 ALIAS FOR $3;

inCity ALIAS FOR $4;

inRegion ALIAS FOR $5;

inPostalCode ALIAS FOR $6;

inCountry ALIAS FOR $7;

inShippingRegionId ALIAS FOR $8;

BEGIN

UPDATE customer

SET address_1 = inAddress1, address_2 = inAddress2, city = inCity, region = inRegion, postal_code = inPostalCode,

country = inCountry, shipping_region_id = inShippingRegionId WHERE customer_id = inCustomerId;

END;

$$;

The customer_update_addressfunction updates the customer’s address in the database.

Một phần của tài liệu Beginning PHP and Postgre SQL E-Commerce From Novice to Professional phần 7 ppt (Trang 24 - 27)

Tải bản đầy đủ (PDF)

(63 trang)