Design and implementation of storage process of database technology (III.)

Source: Internet
Author: User

Declaration of originality

The source of this post is http://blog.csdn.net/zhujunxxxxx/article/details/39251241 if reproduced please indicate the source. This article author original, email [email protected], if you have any questions, please contact the author

A Java-simulated stored procedure

The design methods and data structures of stored procedures are described in the first two blogs. But without an example I'm afraid the article is not easy to understand.

For this blogger dedicated to an example, I use Java simulation of a stored procedure to achieve, because Java I do not do lexical parsing, so I have to construct the syntax tree (this is really tired)

All of my expression calculations are computed by the data engine (in fact, many of the database's stored procedures are like PostgreSQL)

First, give me the data structure.

Data structure Classes
Package Java_test;import Java.util.list;enum Nodetype{t_none,t_var,t_stmt,t_create,t_declare,t_set,t_expr,t_if,t_ Elseif,t_when,t_case,t_while,t_for,t_exec,t_proc}enum Datatype{tt_int,tt_char,tt_bool,tt_date,tt_time,tt_ Approxnum,tt_null,tt_var,tt_expr,tt_sign}class Node {int dno; NodeType Node_type;} Class Varnode extends Node{stringvar_name;datatypedata_type;intisconst;intnotnull; stringdefault_val;/* default value */intisnull; String setvalue;/* value */exprnodeexpr_value after assignment;} Class Declarenode extends node{list<node>var_list;/* This Declare statement contains variables */}class Declnode extends node{/* node that defines a stored procedure */stringproc_name;/* stored procedure name */list<node> params;/* stored procedure parameter */}/* endpoint */class Procnode extends node{ declnodedeclnode;/* defines the specific block of the stored procedure's node */list<node> proc_body;/* stored procedure, I think it has various if-else declare set SQL When-case for While the list*/list<node>except_list;/* exception list */}class Stmtnode extends node{/* a specific SQL statement node */intsql_type; Stringstr_sql; list<node>params;/* the parameters of this SQL statement */}class Setnode extends Node{list<node>var_list;/* the variable that this SET statement contains, and the specific value */}class Exprnode extends node{intis_const;/* is constant-1 0 1*/int is_wrap;/* is there a parenthesis */ datatypedata_type;/* type */stringconst_value;/* constant expression */stringsign; Stringparam; The variable */exprnode lnext of stringstr_value;/* useless */varnodevar_value;/* variable expression; Exprnode Rnext;} Class Ifnode extends node{exprnodeexpr; list<node>then_body; list<node>elseif_body; List<node>else_body;} Class Elseifnode extends node{exprnodeexpr; List<node>body;} Class Casenode extends node{exprnodeexpr; list<node>case_list;inthave_else; List<node>else_stmts;} Class Whennode extends node{exprnodeexpr; List<node>stmts;} Class Whilenode extends node{exprnodeexpr; List<node>body;} Class Fornode extends Node{exprnodelower; Exprnodeupper; Exprnodestep; List<node>body;} Class Procexecstate{int DNO; Procnode Proc;intrettype; Node Lastnode; Node NextNode; String error;} Class Dataum{datatype type; String Value;boolean is_init;public Dataum (DataType t) {this.type=t;is_init=false;} Public DatAUM (DataType t,string v) {this.type=t;this.value=v;is_init=true;}} 


This class is our basic data structure.

Then there is an interpreter class that is specifically used to explain the execution of the syntax tree

Interpreter class
Package Java_test;import Java.util.hashmap;import java.util.iterator;import java.util.list;import java.util.Map; Enum Procsqlstate{proc_sql_rc_error (0), PROC_SQL_RC_OK (1), Proc_sql_rc_exit (2), Proc_sql_rc_continue (3), PROC_SQL_    Rc_return (4);    private int nCode;    Private procsqlstate (int _ncode) {this. NCode = _ncode;    } public int ToInt () {return this.ncode;    } @Override Public String toString () {return string.valueof (This.ncode); }}public class Language {public static mysqlhelper mysql=new mysqlhelper ();p ublic static hashmap<string,object> Var_table =new hashmap<string,object> ();p ublic static int Exec_stmts (Procexecutestate estate,list<node> STMTS) {if (stmts==null) return 0;for (Node node:stmts) {int rc=exec_stmt (estate,node); if (rc==0) return 0;} return 1;} public static int exec_stmt (Procexecutestate estate,node stmt) {int Rc=-1;switch (stmt.node_type) {case T_IF:RC=EXEC_ Stmt_if (Estate, (Ifnode) stmt); Break;case t_while:rc=exec_sTmt_while (Estate, (Whilenode) stmt); Break;case t_case:rc=exec_stmt_case (Estate, (Casenode) stmt); Break;case t_ Declare:rc=exec_stmt_declare (Estate, (Declarenode) stmt); Break;case t_set:rc=exec_stmt_assign (Estate, (Setnode) stmt); Break;case T_stmt:rc=exec_stmt_sql (Estate, (Stmtnode) stmt); break;default:break;} return RC;} public static int Exec_stmt_declare (Procexecutestate estate,declarenode stmt) {for (Node node:stmt.var_list) {Varnode vn Ode= (Varnode) node;if (vnode.isconst!=1) {Dataum data=new Dataum (vnode.data_type); Add_var (Vnode.var_name,data);}} return 1;} public static int exec_stmt_assign (Procexecutestate estate,setnode stmt) {for (Node node:stmt.var_list) {Varnode vnode= ( Varnode) node;if (vnode.isconst!=1) {set_value (Vnode.var_name,vnode.expr_value);}} return 1;} public static int exec_stmt_if (Procexecutestate Estate,ifnode ifnode) {Boolean Value;value=exec_eval_bool (estate, IFNODE.EXPR), if (value) return Exec_stmts (Estate,ifnode.then_body); for (Node item:ifnode.elseif_body) {Elseifnode Eifnode= (ElseIfnode) Item;value=exec_eval_bool (estate,eifnode.expr), if (value) return Exec_stmts (estate,eifnode.body);} Return Exec_stmts (estate, Ifnode.else_body); }public static int exec_stmt_while (Procexecutestate estate,whilenode whilenode) {for (;;) {Intrc;booleanvalue;value = Exec_eval_bool (estate, whilenode.expr), if (!value) BREAK;RC = Exec_stmts (Estate, Whilenode.body); Procsqlstate Erc=null;switch (RC) {case 0:erc=procsqlstate.proc_sql_rc_error;break;case 1:erc=procsqlstate.proc_sql _rc_ok;break;case 2:erc=procsqlstate.proc_sql_rc_exit;break;case 3:erc=procsqlstate.proc_sql_rc_continue;break; Case 4:erc=procsqlstate.proc_sql_rc_return;break;default:break;} Switch (ERC)//Here is where the while loop exits exit break return continue waiting to be perfected {case proc_sql_rc_ok:break;case proc_sql_rc_exit:break; Case Proc_sql_rc_continue:break;case Proc_sql_rc_return:break;default:break;}} return 1;} public static int Exec_stmt_case (Procexecutestate estate,casenode casenode) {Exprnode val=null;//case The value of the variable of expr Next each Whennode expr and it does compare asExecute if the result is true, otherwise do not execute if (casenode.expr!=null) {//val=exec_eval_expr (estate,casenode.expr); val=casenode.expr;//exec_ Assign_value here assigns the variable value}for (Node item:casenode.case_list) {whennode whennode= (whennode) Item;boolean value =exec_eval_ Compare (VAL,WHENNODE.EXPR), if (value) {return Exec_stmts (estate, Whennode.stmts);}} if (casenode.have_else==1) {return Exec_stmts (estate, CASENODE.ELSE_STMTS);} return 1;} public static int Exec_stmt_sql (Procexecutestate estate,stmtnode stmt) {return mysql.exec_update (stmt.str_sql);} Executes an expression and returns the result public static Boolean exec_eval_bool (Procexecutestate estate,exprnode expr) {String Sql=string.format (" Select%s ", getexprstring (expr));//Gets the expression string return Integer.parseint (mysql.exec (SQL). toString ()) ==1?true:false;// From database query to result}public static Boolean exec_eval_compare (Exprnode expr1,exprnode expr2) {String sql1=string.format ("Select% S ", getexprstring (EXPR1));//Gets an expression string ret1=mysql.exec (SQL1). toString (); String Sql2=string.format ("Select%s", Getexprstring (EXPR2));//Gets an expression string of2=mysql.exec (SQL2). toString (); return ret1.equals (Ret2);} public static int Exec_eval_int (procexecutestate estate,exprnode expr) {String Sql=string.format ("Select%s", Getexprstring (expr)); return Integer.parseint (Mysql.exec (SQL). toString ());} public static Exprnode exec_eval_expr (procexecutestate estate,exprnode expr) {return null;} public static void Add_var (String Var_name,dataum data) {if (!var_table.containskey (Var_name)) {Var_table.put (var_name , data);}} public static void Set_value (String var_name,object value) {Exprnode v= (exprnode) Value;dataum data;if (v.is_const==1)// The value of the expression is computed if constant is the constant value data=new Dataum (V.data_type, v.const_value); else{string sql=string.format ("Select%s", Getexprstring (v));//Gets the expression string ret=mysql.exec (SQL). toString ();//query from database to result data=new Dataum (V.data_type, ret);} if (!var_table.containskey (Var_name)) {var_table.put (var_name, data);} Else{var_table.remove (Var_name); Var_table.put (var_name, data);}} private static Dataum Get_var_value (String var_name) {return (Dataum) Var_table.get (var_name);} Gets the string form of the expression node should also replace the inside variable with a true value of public static string Expr_str= "";p ublic static string getexprstring (Exprnode expr) { Expr_str= ""; inorder (expr); return expr_str;} The middle-order traversal expression private static void Inorder (Exprnode expr) {if (expr==null) return;if (expr.is_wrap==1) expr_str+= "("; Inorder ( Expr.lnext); if (expr.is_const==-1)//+-*/% >> etc expr_str+=expr.sign;else if (expr.is_const==1) expr_str+= Expr.const_value;else if (expr.is_const==0) {Dataum data=get_var_value (expr.var_value.var_name); if (data!=null) { Expr_str+=data.value;} Else{expr_str+=expr.var_value.var_name; System.out.println (expr.var_value.var_name+ "not Assigned");}} Inorder (Expr.rnext); if (expr.is_wrap==1) expr_str+= ")";}  public static void Show_var () {Iterator iter = Var_table.entryset (). Iterator ();  while (Iter.hasnext ()) {map.entry<string, object> Entry = (map.entry<string, object>) Iter.next (); String key = Entry.getkey ();D Ataum data= (Dataum) entry.getvalue (); System.out.println (key+ "" +data.value);}}

It can be seen that the execution of a stored procedure is basically a recursive process

The source of this post is http://blog.csdn.net/zhujunxxxxx/article/details/39251241 if reproduced please indicate the source. This article author original, email [email protected], if you have any questions, please contact the author

About the author

The author is a software engineering born bitter programmer, undergraduate stage is also software engineering, currently graduate students in reading, Love new technology, love programming, humor, love open source.

Personal website: http://www.zhujuncoding.com/

github:https://github.com/zhujunxxxxx/

Email: [email protected]

Design and implementation of storage process of database technology (III.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.