W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
OrientDBHooks只是數(shù)據(jù)庫(kù)術(shù)語(yǔ)中的觸發(fā)器,它們?cè)谟脩魬?yīng)用程序中的每個(gè)CRUD操作之前和之后啟用內(nèi)部事件。您可以使用鉤子編寫(xiě)自定義驗(yàn)證規(guī)則,強(qiáng)制實(shí)施安全性,或安排外部事件,例如根據(jù)關(guān)系型DBMS進(jìn)行復(fù)制。
CREATE CLASS Invoice EXTENDS OTriggered ALTER CLASS Invoice CUSTOM onAfterCreate = invoiceCreated
讓我們?cè)贘avascript中創(chuàng)建invoiceCreated函數(shù),在服務(wù)器控制臺(tái)中打印創(chuàng)建的發(fā)票號(hào)。
CREATE FUNCTION invoiceCreated "print(' Invoice created: ' + doc.field ('number'));" LANGUAGE Javascript
現(xiàn)在通過(guò)創(chuàng)建一個(gè)新的Invoice文檔來(lái)嘗試掛鉤。
INSERT INTO Invoice CONTENT {number: 100, notes: 'This is a test}
如果成功執(zhí)行這個(gè)命令,你會(huì)得到下面的輸出。
Invoice created: 100
只能針對(duì)一個(gè)或多個(gè)文檔定義一個(gè)特殊操作。要做到這一點(diǎn),讓你的類來(lái)擴(kuò)展OTriggered類。
例如,讓我們執(zhí)行一個(gè)觸發(fā),例如JavaScript功能,針對(duì)存在的Profile類,所有的財(cái)產(chǎn)賬戶='高級(jí)'的文件。觸發(fā)器將被調(diào)用,以防止文件刪除。
ALTER CLASS Profile SUPERCLASS OTriggered UPDATE Profile SET onBeforeDelete = 'preventDeletion' WHERE account = 'Premium'
讓我們創(chuàng)建preventDeletion()JavaScript函數(shù)。
CREATE FUNCTION preventDeletion "throw new java.lang.RuntimeException('Cannot delete Premium profile ' + doc)" LANGUAGE Javascript
然后通過(guò)嘗試刪除“Premium”帳戶來(lái)測(cè)試掛鉤。
DELETE FROM #12:1 java.lang.RuntimeException: Cannot delete Premium profile profile#12:1{onBeforeDelete:preventDeletion,account:Premium,name:Jill} v-1 (<Unknown source>#2) in <Unknown source> at line number 2
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.ReentrantLock; import com.orientechnologies.orient.core.hook.ODocumentHookAbstract; import com.orientechnologies.orient.core.hook.ORecordHook; import com.orientechnologies.orient.core.hook.ORecordHookAbstract; import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener; import com.orientechnologies.orient.core.db.ODatabase; import com.orientechnologies.orient.core.record.ORecord; import com.orientechnologies.orient.core.record.impl.ODocument; public class HookTest extends ODocumentHookAbstract implements ORecordHook { public HookTest() { } @Override public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() { return DISTRIBUTED_EXECUTION_MODE.BOTH; } public RESULT onRecordBeforeCreate( ODocument iDocument ) { System.out.println("Ran create hook"); return ORecordHook.RESULT.RECORD_NOT_CHANGED; } public RESULT onRecordBeforeUpdate( ODocument iDocument ) { System.out.println("Ran update hook"); return ORecordHook.RESULT.RECORD_NOT_CHANGED; } }
上面的示例代碼打印每次創(chuàng)建或更新類的創(chuàng)紀(jì)錄的時(shí)間適當(dāng)?shù)淖⑨尅?
讓我們添加更多的鉤子文件setCreatedUpdatedDates.java如下-
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.ReentrantLock; import com.orientechnologies.orient.core.hook.ODocumentHookAbstract; import com.orientechnologies.orient.core.hook.ORecordHook; import com.orientechnologies.orient.core.hook.ORecordHookAbstract; import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener; import com.orientechnologies.orient.core.db.ODatabase; import com.orientechnologies.orient.core.record.ORecord; import com.orientechnologies.orient.core.record.impl.ODocument; public class setCreatedUpdatedDates extends ODocumentHookAbstract implements ORecordHook { public setCreatedUpdatedDates() { } @Override public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() { return DISTRIBUTED_EXECUTION_MODE.BOTH; } public RESULT onRecordBeforeCreate( ODocument iDocument ) { if ((iDocument.getClassName().charAt(0) == 't') || (iDocument.getClassName().charAt(0)=='r')) { iDocument.field("CreatedDate", System.currentTimeMillis() / 1000l); iDocument.field("UpdatedDate", System.currentTimeMillis() / 1000l); return ORecordHook.RESULT.RECORD_CHANGED; } else { return ORecordHook.RESULT.RECORD_NOT_CHANGED; } } public RESULT onRecordBeforeUpdate( ODocument iDocument ) { if ((iDocument.getClassName().charAt(0) == 't') || (iDocument.getClassName().charAt(0)=='r')) { iDocument.field("UpdatedDate", System.currentTimeMillis() / 1000l); return ORecordHook.RESULT.RECORD_CHANGED; } else { return ORecordHook.RESULT.RECORD_NOT_CHANGED; } } }上面的代碼所做的是尋找以字母'r'或't'開(kāi)頭的任何類別,并且在創(chuàng)建記錄時(shí)設(shè)置CreatedDate和UpdatedDate,并在每次記錄更新時(shí)設(shè)置為UpdatedDate。
使用以下命令編譯Java代碼。
注意:將下載的jar文件和這些Java文件保存在同一個(gè)文件夾中。
$ jar cf hooks-1.0-SNAPSHOT.jar *.java將編譯的代碼移動(dòng)到OrientDB服務(wù)器可以找到它
$ cp hooks-1.0-SNAPSHOT.jar "$ORIENTDB_HOME/lib"在OrientDB服務(wù)器配置文件中啟用測(cè)試掛接
<hooks> <hook class = "HookTest" position = "REGULAR"/> </hooks> ... </orient-server>重新啟動(dòng)OrientDB服務(wù)器
INSERT INTO V SET ID = 1;
如果成功執(zhí)行這個(gè)命令,你會(huì)得到下面的輸出。
Ran create hook
現(xiàn)在運(yùn)行以下命令:
UPDATE V SET ID = 2 WHERE ID = 1;
如果成功執(zhí)行這個(gè)命令,你會(huì)得到下面的輸出。
Ran update hook在OrientDB服務(wù)器配置文件中啟用Real Hook
<hooks> <hook class="setCreatedUpdatedDates" position="REGULAR"/> </hooks> ... </orient-server>
創(chuàng)建以字母“R”或“T”開(kāi)頭的新類。
CREATE CLASS tTest EXTENDS V;
現(xiàn)在,插入一條記錄。
INSERT INTO tTest SET ID = 1 SELECT FROM tTest
如果成功執(zhí)行這個(gè)命令,你會(huì)得到下面的輸出。
----+-----+------+----+-----------+----------- # |@RID |@CLASS|ID |CreatedDate|UpdatedDate ----+-----+------+----+-----------+----------- 0 |#19:0|tTest |1 |1427597275 |1427597275 ----+-----+------+----+-----------+-----------即使您沒(méi)有指定要為CreatedDate和UpdatedDate設(shè)置的值,OrientDB也會(huì)為您自動(dòng)設(shè)置這些字段。
UPDATE tTest SET ID = 2 WHERE ID = 1; SELECT FROM tTest;
如果成功執(zhí)行這個(gè)命令,你會(huì)得到下面的輸出:
----+-----+------+----+-----------+----------- # |@RID |@CLASS|ID |CreatedDate|UpdatedDate ----+-----+------+----+-----------+----------- 0 |#19:0|tTest |2 |1427597275 |1427597306 ----+-----+------+----+-----------+-----------你可以看到OrientDB改變了UpUpdatedDate,但是讓CreatedDateremain保持不變。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: