Tag Archives: oracle

Active Directory kullanıcılarını bir tablodan SELECT etmek:

Oracle veri tabanından domain sunucunuza erişip kullanıcıları görme konusunu detaylı örneklerle anlatan sayısız doküman var.

Bu örnekler genel anlamda DBMS_LDAP paketini kullanarak domain sunucunuza nasıl erişeceğinizi, eriştikten sonra nasıl browse edeceğinizi gösteriyor.

Test etmek için domain sunucunuzun adres ve port bilgilerini edinmelisiniz. Tabii bir de username/password.

Ben bu örneklerde tarif edilen yöntemi kendi tablomu doldurmasını sağlayacak şekilde biraz değiştirdim. Bunu yaparken de neredeyse tamamen Billy~Verreynne ‘in https://community.oracle.com/thread/4064849 adresindeki önerilerini kullandım.

Eklediğim bir kaç küçük şey oldu:

  • ldap_host, ldap_user, ldap_passw gibi değişkenleri plain-text göndermek yerine encrypt etmek istedim.
  • bu örnek attribute_name ve attribute_value isimli iki kolonda alt alta sıralıyordu oysa ben isim, email, departman, title, telefon gibi bilgileri de içeren bir tablom olsun, bu tablo ben her sorguladığımda canlı olarak LDAP üzerinden beslensin istiyordum.

Yaptıklarımsa şunlar;

  • önce objelerimi hazırladım.
    • TLDAPATTR için kullandığım kolonları özelleştirebilirsiniz. Ama tabii bu durumda DBMS_LDAP kullandığımız fonksiyonda da uygun değişiklikler yapmamız gerekecek.
CREATE OR REPLACE TYPE tstrings IS TABLE OF VARCHAR2(4000);
 
CREATE OR REPLACE TYPE tldapattr IS object 
(
attribute_name VARCHAR2(50), 
attribute_value VARCHAR2(4000), 
dispname VARCHAR2(255), 
email VARCHAR2(255), 
department VARCHAR2(255), 
memberof VARCHAR2(255), 
manager VARCHAR2(255), 
extension VARCHAR2(255
);
 
CREATE OR REPLACE TYPE tldapattrlist IS TABLE OF tldapattr;
  • sonra type objemde görmek istediğim kolonları besleyecek şekilde değiştirerek fonksiyonumu create ettim.
CREATE OR REPLACE FUNCTION ldapattr (filter VARCHAR2, attributes tstrings DEFAULT NULL) 
RETURN tldapattrlist pipelined AUTHID definer IS
 
ldap_host    VARCHAR2(256);
ldap_port    VARCHAR2(256) := '389';
ldap_user    VARCHAR2(256); 
ldap_passw   VARCHAR2(256);
ldap_base    VARCHAR2(256) := 'OU=buraya_Organization_Unit_bilgisi_yaz,dc=sizin_domain_controlleriniz,dc=muhtemelen_com'; 
LDAP_ATTR    CONSTANT tstrings := NEW tstrings('displayname' ,'mail', 'department', 'memberof', 'manager','telephonenumber');
 
retVal          INTEGER;  
ldapSession     DBMS_LDAP.SESSION;  
attrList        DBMS_LDAP.string_collection;  
valList         DBMS_LDAP.string_collection;  
ldapMessage     DBMS_LDAP.message;  
berElement      DBMS_LDAP.ber_element;  
ldapTimeout     DBMS_LDAP.timeval;  
 
attrName        VARCHAR2(256);  
attrDisplay     VARCHAR2(4000);  
name            VARCHAR2(256);  
attrNum         INTEGER;  
i               INTEGER; 
 
v_dispname     VARCHAR2(255);
v_email        VARCHAR2(255);  
v_department   VARCHAR2(255);
v_memberof     VARCHAR2(255);
v_manager      VARCHAR2(255);
v_extension    VARCHAR2(255);
BEGIN
SELECT 'foo' INTO ldap_host FROM dual;
SELECT 'goo' INTO ldap_user FROM dual;
SELECT 'hoo' INTO ldap_passw FROM dual;
 
pipe ROW(TLDAPATTR('PARAM.<filter>', filter, v_dispname, v_email, v_department, v_memberof, v_manager, v_extension));  
         DBMS_LDAP.USE_EXCEPTION := TRUE;
 
-- create LDAP session  
         ldapSession := DBMS_LDAP.init(hostname => LDAP_HOST, portnum  => LDAP_PORT);  
         retval := DBMS_LDAP.simple_bind_s(ld=> ldapSession,dn=> LDAP_USER,passwd=> LDAP_PASSW);  
 
-- build a distinct ordered list of attributes  
         i := 0;  
         FOR c IN (SELECT DISTINCT column_value AS ATTR FROM TABLE(NVL(attributes,LDAP_ATTR)) ORDER BY 1) LOOP  
                 i := i + 1;  
                 attrList(i) := c.attr;  
                 attrDisplay := attrDisplay || c.attr || ' ';  
         END LOOP;  
pipe ROW(TLDAPATTR('PARAM:<attribute list>', TRIM(attrDisplay), v_dispname, v_email, v_department, v_memberof, v_manager, v_extension));  
 
-- use supplied filter to search  
        ldapTimeout.seconds := 5;  
        ldapTimeout.useconds := 0;
 
        retval := DBMS_LDAP.search_st(
		ld => ldapSession,
		base => ldap_base,
		scope => DBMS_LDAP.SCOPE_SUBTREE,
		filter => filter,
		attrs => attrList,
		attronly => 0,
		tv => ldapTimeout,
		res => ldapMessage);
 
-- is the search successful?  
        retval := DBMS_LDAP.count_entries(ld => ldapSession, msg => ldapMessage);  
        IF retval = 0 THEN  
pipe ROW(TLDAPATTR('Error', 'No matches found for filter ['||filter||'].',v_dispname, v_email, v_department, v_memberof, v_manager, v_extension));  
retVal := DBMS_LDAP.unbind_s(ld => ldapSession);  
RETURN;  
        END IF;  
-- Get all the entries returned by our search.  
        ldapMessage := DBMS_LDAP.first_entry(ld  => ldapSession, msg => ldapMessage);  
 
        WHILE ldapMessage IS NOT NULL  
        LOOP  
-- Get all the attributes for this entry.  
        attrName := DBMS_LDAP.first_attribute(ld => ldapSession, ldapentry => ldapMessage, ber_elem  => berElement);  
--degerleri her defasinda resetle
v_dispname   := NULL;
v_email      := NULL;
v_department := NULL;
v_memberof   := NULL;
v_manager 	 := NULL;
v_extension  := NULL;
-- output a null row for formatting an empty line between records  
                pipe ROW( NULL );  
-- output DN as unique record identifier  
                pipe ROW(TLDAPATTR('distinguishedName', DBMS_LDAP.get_dn(ldapSession, ldapMessage), v_dispname, v_email, v_department, v_memberof, v_manager, v_extension));   
-- now output attribute name-values found for the DN  
attrNum := 1;
		LOOP  
		EXIT WHEN attrName IS NULL;
		EXIT WHEN attrNum > attrList.COUNT;   
-- Get all the values for this attribute.  
		valList := DBMS_LDAP.get_values (ld => ldapSession, ldapentry => ldapMessage, attr => attrName);
			FOR i IN valList.FIRST .. valList.LAST
			LOOP
				IF attrName    = 'displayName' THEN v_dispname := SUBSTR(valList(i),1,200);
				ELSIF attrName = 'mail' THEN v_email := SUBSTR(valList(i),1,200);
				ELSIF attrName = 'department' THEN v_department := SUBSTR(valList(i),1,200);
				ELSIF attrName = 'memberOf' THEN v_memberof := SUBSTR(valList(i),1,200);
				ELSIF attrName = 'manager' THEN v_manager := SUBSTR(valList(i),1,200);
				ELSIF attrName = 'telephoneNumber' THEN v_extension := SUBSTR(valList(i),1,200);
				END IF ;
			END LOOP values_loop;
name := valList.FIRST;  
WHILE name IS NOT NULL LOOP  
pipe ROW(TLDAPATTR(attrName,valList(name),v_dispname, v_email, v_department, v_memberof, v_manager, v_extension));  
name := valList.Next(name);  
END LOOP;  
 
attrName := DBMS_LDAP.next_attribute(ld => ldapSession, ldapentry => ldapMessage, ber_elem  => berElement);
attrNum := attrNum + 1;  
END LOOP;  
 
ldapMessage := DBMS_LDAP.next_entry(ld  => ldapSession, msg => ldapMessage);  
END LOOP;  
 
-- Disconnect from the LDAP server.  
retVal := DBMS_LDAP.unbind_s(ld => ldapSession);  
RETURN;  
END;

bunları yaptıktan sonra şöyle test edebiliyor olmalısınız:

SELECT * 
FROM TABLE(ldapattr(filter => '(&(objectclass=user) (!(useraccountcontrol=514)))'))

514 pasif kullanıcıları dışarıda bırakmak için. Search stringi kendi ihtiyaçlarınıza göre değiştirebilirsiniz. Son olarak datayı derli toplu görmek için bir VIEW oluşturun.

Böylece a/d kullanıcılarınız, bu kullanıcıların bağlı olduğu yöneticiler, departmanları, masa telefonları gibi bilgilere dilediğiniz zaman gerçek zamanlı olarak erişebilirsiniz.

Advertisement

APEX Page Validation: a global check for special characters

I’ve been struggling with a long form which has tens of text areas on my APEX env.

At some point I needed to apply some restrictions on input fields. Basically to refrain any unwanted non-unicode characters to be inserted into table.

APEX has great Built-in restrictions and they work like a charm. However, it has only a few options. It didn’t help my case when I had to set a restriction to reject all characters but a-Z letters, all the numbers, spaces AND punctuations.

Perhaps there are easier ways to do that. Actually I’m sure there are easier ways to do that. But here is what I ‘ve done and feel free to use & feedback.

DECLARE
l_query VARCHAR2(100);
l_res clob;
v_out clob;
v_special NUMBER;
CURSOR cur_main IS
SELECT item
FROM (
SELECT item_name item 
FROM APEX_APPLICATION_PAGE_ITEMS 
WHERE application_id = :app_id AND page_id = :app_page_id 
AND LOWER(display_as) LIKE '%text%'
);
BEGIN
v_out:= '';
   FOR rec_main IN cur_main LOOP
   l_query := 'select v('''||NVL(rec_main.item, 'null')||''') from dual';
   EXECUTE IMMEDIATE l_query INTO l_res;
   v_out := v_out||l_res;
   END LOOP;
SELECT COUNT(1) 
INTO v_special 
FROM dual 
WHERE regexp_like(REPLACE(v_out, CHR(13)||CHR(10), '_carriegeReturn_'), 
                 '[^a-zA-Z0-9 .,:;?%&+-_]');
   IF v_special >0 THEN
   RETURN FALSE;
   ELSE
   RETURN TRUE;
  END IF;
END;

let’s unwrap it a bit:

  1. cur_main is the cursor where I get all my text-based input fields on the page.
  2. l_query holds my dynamic sql string.
  3. v_out is concatenated string which consists of each item’s value on the screen for current page. note that it won’t work based on the data from session state.
  4. I simply hate being have to use regular expressions. So I copied a simple syntax and modified for my scenario.

Rest are just true/false conditions.

In my case it’s a Page Validation Process. To pass this validation no rows should be returned. Otherwise it will raise the error message.

Cheers!

note to mezelf: chunk CLOB?

Ever had a trouble while you were trying to print a variable, got an ORA-06502 error?

ORA-06502: PL/SQL: numeric or value error string

Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2).

Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.

Here is what I came across to. A big and special thanks to oracle forums, of course :)


PROCEDURE chunk_clob(p_clob CLOB) IS
v_buf_size VARCHAR2(32000);
v_offset PLS_INTEGER DEFAULT 1;
v_size PLS_INTEGER;
v_len PLS_INTEGER;
v_chunk PLS_INTEGER;
v_clob CLOB := p_clob;
BEGIN
v_chunk := dbms_lob.getchunksize(v_clob);
v_len := dbms_lob.getlength(v_clob);
WHILE v_offset v_chunk
THEN v_size := v_chunk;
ELSE v_size := v_len - (v_offset - 1);
END IF;
v_buf_size := NULL;
dbms_lob.READ(v_clob, v_size, v_offset, v_buf_size);
htp.prn(v_buf_size);
v_offset := v_offset + v_size;
END LOOP;
END;

– OKAY what is this and what am I going to do with this?

It ‘ll take care of your oversized strings so you get to see what your VAR has in it.


my_pck.chunk_clob(l_show_html);

Oracle Database In-Memory and Hash Keys

Data Warehousing with Oracle

Hash keys are often used as primary keys in Data Vault environments. When loading dimensional data marts from a Data Vault schema, it seems to be obvious to use them as dimension keys, too. At least in combination with Oracle Database In-Memory, this is not a good idea.

View original post 14,906 more words

Work with WITH option in Oracle 12c!

oracle 12c: using functions with WITH

Lczinkoc's Blog

Starting Oracle 9.0  – according to SQL 1999 standard – Oracle introduced the WITH option which can be considered
as the extension of the inline views.
Let’s see an “old” example:

(The origin of this statement is Oracle course,titled:
Oracle Database: SQL Fundamentals I
Volume II • Student Guide).
In Oracle 12c we have new enhancements for this kind of SQL statement:
We can define “in-line” functions or procedures after the WITH clause.
In the following example we are looking for those departments, where
total salary of the department is greater than the maximum of the average of the total salaries:

The result is:

The execution plan is the following ( not very simple!):

We can use the WITH option with plsql_declarations clause
in DML statements as well.
Let’s consider the following example where we want to compute the
tax amount of the salaries and modify the tax_amount column,
but…

View original post 124 more words

Analytic Views: Powerful Oracle 12.2 Feature for Business Intelligence

Oracle 12.2: Analytic Views

Data Warehousing with Oracle

Analytic Views are one of the main features for Business Intelligence introduced with Oracle 12c Release 2. They provide a lot of new possibilities for analytical queries on star schemas. This blog post gives an overview of the new functionality.

View original post 2,453 more words

APEX: dbms_scheduler

Bir sorgu hazirladiniz ve bu sorgudan donen sonucu periyodik olarak bir tabloya yazmak istiyorsunuz..

Ya da bu bir rapor ve bu rapora bakarak periyodik olarak bir kisi ya da ekibe mail gondermek istiyorsunuz..

APEX icinden nasil mail gonderilecegine dair kucuk bir ornegi su yazimda anlatmaya calismistim.

Simdi ise bunu “otomatik” hale getirmeyi deneyelim.

Farkli yontemler elbette var, ama ben -veri tabaninizin “eski” bir versiyonda olmadigini varsayarak- dogrudan dbms_scheduler uzerinden bir procedure ile ilerleyecegim.

i. Oncelikle sorgumuzu bir procedure haline getirerek objemizi olusturalim. Bunu APEX ‘in etkilesimli menulerine yaptirabilecegimiz gibi SQL konsolunda elle de yapabiliriz.

create or replace procedure “ORNEK_BASITMIS” is 

Sorgunun detaylari bu yazinin konusu degil. Fakat soz gelimi, belirli bir data setini dinamik olarak sececegimiz alicilara gondermek istiyorsak

  • Bir cursor acabilir,
  • Sonra bu cursor ‘i loop icinde %NOTFOUND oluncaya kadar fetch ederek aksiyonumuzu aldirabiliriz: mesela her defasinda bir email gondermek gibi.

Schedule etmeye karar verdiginiz islem sisteme periyodik olarak email gonderme isi vermekse ve ornek mail gonderme kismini burada isledigimiz gibi apex_mail.send uzerinden kotariyorsaniz, dikkat etmemiz gereken aslinda belki de tek onemli nokta su:

Hazirladiniz SP icinde BEGIN END arasinda APEX Workspace ID ‘nizi assign emelisiniz..

Aksi takdirde schedule ettiginiz Job ‘un calisma vakti geldiginde

ORA-20001: This procedure must be invoked from within an application session.

gibi bir hata ile karsilasirsiniz :)

apex_about

APEX Workspace ID

Bu ID bilgisine ustteki gibi erisebilirsiniz. Set etmek icin asagidaki gibi bir ifade kullaniyoruz:

wwv_flow_api.set_security_group_id();

Stored Procedure ‘imizi hazirladik ve ORNEK_PROCEDURE ismi ile create ettik diyelim. Simdi sirasi ile DBMS_SCHEDULER ile once bir program create etmeliyiz:

dbms_create_prog
DBMS_SCHEDULER.CREATE_PROGRAM

Ardindan bir schedule create ediyoruz:

dbms_create_sched
DBMS_SCHEDULER.CREATE_SCHEDULE

repeate_interval degiskenine dikkat edelim. Burada frekans, saat, dakika, saniye turunden bir cok deger verme sansimiz var..

Dolayisiyla, Ayin belirli bir gunu, Haftanin belirli gunleri, gunun belirli saatleri gibi spesifik periyod tanimlari yapabiliyoruz. Detayli bilgiye Oracle dokumanlarindan erisebilirsiniz.

Simdi de job create edelim:

dbms_create_job
DBMS_SCHEDULER.CREATE_JOB

Artik veri tabaninda bir SP ‘miz var ve bu SP ‘nin istedigimiz zaman araliklarinda otomatik calismasi icin bir Program tanimimiz, bir Schedule ‘imiz ve bir de Job ‘imiz var..

Simdi program ve job ‘imizi “acalim”.

dbms_enable
Enable

Hazir.. Artik repeat_interval olarak verdigimiz vakit geldigi zaman job ‘in calismadigini kontrol edebiliriz.

Mesela az once enable ettigimiz Job ‘u suradan gorebiliriz:

select * from dba_scheduler_jobs;

Zamani geldiginde islemin calisip calismadigini ise su tablodan takip edebiliriz:

select * from user_scheduler_job_run_details;

Yaptiklarimizi drop etmek icinse yine DBMS_SCHEDULER icindeki

  • drop_job
  • drop_schedule
  • drop_program

yapilarini kullanabiliriz. Fakat tahmin edebileceginiz gibi bunlari “sirali” yapmalisiniz, aksi takdirde sistem dependency nedeniyle kizacaktir :)

dbms_drops

DROP etmek..

Gorusmek uzere!

APEX: Send Mail Thru Report Page

Hi,

You published a useful interactive report for your users and now want to let them get that as email also through that page.

Here you can find a basic example..

  • Go to your report page from Application Builder panel
  • Create a page item button 
    • Pick Defined by Dynamic Action as button action
  • Right click to your brand new Dynamic Action and select Create.

dyn_act
APEX Dynamic Action

As you can see above, we have to create this action with some conditions. Such as:

  • Event: Click
  • Selection Type: Button
  • Button: address your button which you ‘ve just created. This item will trigger your email sending process.

Once we completed these steps, we can add a True action. You ‘ll see a button named Add True Action at your Dynamic Action Edit page. You can also create one by choosing from right click menu.

true_act
Adding a TRUE action

You may want to uncheck Fire On Page Load. Because.. Well we want to trigger it by clicking our button, manually :)

So.. Now we need this PL/SQL code which will take care of sending email via Oracle APEX.

I ‘ll share 2 basic examples with you. One for plain-text emails, one for HTML based ones.

I personally prefer to send mails in HTML format… But it ‘s up to you and depends on your needs..

 

It is pretty easy, right? Just give the right parameters and a simple sentence as email body. You can always use variables to collect these informations like i did..

If you want to go with HTML format, then you ‘ll need some extra time to create a good-looking and small-sized body. That ‘s all :)

Once you store these codes you ‘ve wrote and apply the changes onto your report page, you can start using your button. It should be sending emails to given address. Click on your button and wait couple of minutes to receive it.

I am not going to remind you not to copy everything from my sample snapshots. You do need to change email addresses etc. :)

Should you want to add some extra features on this dynamic action, you can add another True action(s) as well.

You may need to.. Otherwise someone would be able to click on that button more than necessary and we don ‘t want to spam people :)

Let ‘s add a Confirmation action..

 

conf_act

  • Right Click, Create
  • Action: CONFIRM

Now you ‘ll be prompted to click OK everytime you click on your email button..

O.K. but not enough? Well let ‘s hide the button after it ‘s got clicked.

hide

  • Now we have a button on our report page
  • Our authorized report viewers can click on it to send email
  • We have a confirmation dialog before trigger
  • Our email send button will be hidden once it used.

Dag!