VMM OpenSource - sv/perf/vmm_sql_db_sqlite.sv

sv/perf/vmm_sql_db_sqlite.sv expanded source

00001 //
00002 // -------------------------------------------------------------
00003 //    Copyright 2004-2008 Synopsys, Inc.
00004 //    All Rights Reserved Worldwide
00005 // 
00006 //    Licensed under the Apache License, Version 2.0 (the
00007 //    "License"); you may not use this file except in
00008 //    compliance with the License.  You may obtain a copy of
00009 //    the License at
00010 // 
00011 //        http://www.apache.org/licenses/LICENSE-2.0
00012 // 
00013 //    Unless required by applicable law or agreed to in
00014 //    writing, software distributed under the License is
00015 //    distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
00016 //    CONDITIONS OF ANY KIND, either express or implied.  See
00017 //    the License for the specific language governing
00018 //    permissions and limitations under the License.
00019 // -------------------------------------------------------------
00020 //
00021 
00022 
00023 `ifndef VMM_SQL_DB_SQLITE__SV 
00024 `define VMM_SQL_DB_SQLITE__SV 
00025 
00026 import "DPI" function int vmm_sqlite_execute_dpi(string dbname, string sql);  
00027 import "DPI" function void vmm_sqlite_close_db_dpi(string dbname);
00028 import "DPI" function int vmm_sqlite_create_db_dpi(string dbname);  
00029 import "DPI" function int vmm_sqlite_init_dpi(); 
00030 import "DPI" function string vmm_sqlite_error_dpi();
00031 import "DPI" function int vmm_sqlite_unique_id_dpi(string dbname);  
00032 
00033 typedef class vmm_sql_db_sqlite; 
00034 
00035 //MUST be a power of 2.
00036 `define VMM_SQLITE_NUM_TRANS_TO_COMMIT 2047
00037 class vmm_sql_db_sqlite extends vmm_sql_db;
00038 
00039    local string dbname;
00040 
00041    local static bit init_done = 0;
00042    local bit last_failed;
00043    local static int num_transactions = 0;
00044 `ifndef VMM_SQLITE_TRANSACTION_DISABLE
00045    local static int num_transactions_before_commit = `VMM_SQLITE_NUM_TRANS_TO_COMMIT;
00046    local static bit uncommitted = 0;
00047 `endif
00048 
00049    extern function new(string dbname);
00050    extern virtual function int status();
00051    extern virtual function int statement(string sql_stmt);
00052    extern virtual function void commit();
00053    extern virtual function void close();
00054 
00055    // Return an ID that is unique in the DB
00056    extern local virtual function int get_unique_id();
00057 endclass: vmm_sql_db_sqlite
00058 
00059 //------------------------------------------------------------------
00060 //
00061 //  Implementation
00062 //
00063 
00064 function vmm_sql_db_sqlite::new(string dbname);
00065    super.new();
00066    this.dbname = Xexpand_nameX(dbname);
00067    this.last_failed = 0;
00068    this.log = new("SQLdb", this.dbname);
00069    if (!init_done) begin
00070       if (vmm_sqlite_init_dpi()) begin
00071          `vmm_fatal (this.log, vmm_sqlite_error_dpi());
00072          this.last_failed = 1;
00073       end else begin
00074          init_done = 1;
00075       end
00076    end    
00077    if (vmm_sqlite_create_db_dpi(this.dbname)) begin
00078       `vmm_error (this.log, vmm_sqlite_error_dpi());
00079       this.last_failed = 1;
00080    end else begin
00081       Xcreate_system_tablesX();
00082    end
00083 endfunction: new
00084 
00085 function int vmm_sql_db_sqlite::status();
00086    return(this.last_failed);
00087 endfunction: status
00088 
00089 function int vmm_sql_db_sqlite::statement(string sql_stmt);
00090 `ifndef VMM_SQLITE_TRANSACTION_DISABLE
00091    int num_transactions_modulo = num_transactions & num_transactions_before_commit;
00092    if ((num_transactions_modulo == 0) && (this.uncommitted == 0)) begin
00093       vmm_sqlite_execute_dpi(this.log.get_instance(), "BEGIN TRANSACTION;");
00094       this.uncommitted = 1;
00095    end
00096 `endif
00097    if(vmm_sqlite_execute_dpi(this.log.get_instance(), sql_stmt)) begin
00098       this.last_failed = 1;
00099       `vmm_error (this.log, vmm_sqlite_error_dpi());
00100    end else begin
00101       this.last_failed = 0;
00102    end
00103 `ifndef VMM_SQLITE_TRANSACTION_DISABLE
00104    if (num_transactions_modulo == num_transactions_before_commit) begin
00105       vmm_sqlite_execute_dpi(this.log.get_instance(), "END TRANSACTION;");
00106       this.uncommitted = 0;
00107    end
00108 `endif
00109    num_transactions++;
00110    return(this.last_failed);
00111 endfunction: statement
00112 
00113 function void vmm_sql_db_sqlite::commit();
00114 `ifndef VMM_SQLITE_TRANSACTION_DISABLE
00115    if (this.uncommitted) begin
00116       //commit, and begin a new transaction
00117       vmm_sqlite_execute_dpi(this.log.get_instance(), "END TRANSACTION;");
00118       vmm_sqlite_execute_dpi(this.log.get_instance(), "BEGIN TRANSACTION;");
00119    end
00120 `endif
00121 endfunction: commit
00122 
00123 function void vmm_sql_db_sqlite::close();
00124 `ifndef VMM_SQLITE_TRANSACTION_DISABLE
00125    //if commit has not already happend, commit transaction before closing. 
00126    if (this.uncommitted) begin
00127       vmm_sqlite_execute_dpi(this.log.get_instance(), "END TRANSACTION;");
00128       this.uncommitted = 0;
00129    end
00130 `endif
00131    vmm_sqlite_close_db_dpi(this.log.get_instance());
00132 endfunction: close
00133 
00134 
00135 function int vmm_sql_db_sqlite::get_unique_id();
00136    // Need to return a unique ID from the DB.
00137    // like the number of rows in the vmm_runs table...
00138    return vmm_sqlite_unique_id_dpi(this.dbname);
00139 endfunction: get_unique_id
00140 
00141 `endif