Class DASemantext

java.lang.Object
io.odysz.semantic.DASemantext
All Implemented Interfaces:
io.odysz.semantics.ISemantext
Direct Known Subclasses:
DBSynmantext

public class DASemantext extends Object implements io.odysz.semantics.ISemantext
A basic semantic context for generating sql. Handling semantics defined in runtime-root/semantics.xml file.

For example, pageSql(String, int, int) is an example that must handled by context, but not interested by semantic.jserv. When composing SQL like select statement, if the results needing to be paged at server side, the paging sql statement is different for different DB. But semantic-transact don't care DB type or JDBC connection, so it's the context that will handling this. See the Connects.pagingSql(dbtype, String, int, int).

Author:
odys-z@github.com
  • Field Details

    • semants

      protected DATranscxt.SemanticsMap semants
      Semantic Configurations
    • metas

      protected HashMap<String,io.odysz.semantics.meta.TableMeta> metas
    • usr

      protected io.odysz.semantics.IUser usr
    • connId

      protected String connId
    • basePath

      protected String basePath
    • onRowsOk

      protected ArrayList<io.odysz.transact.sql.Statement.IPostOptn> onRowsOk
    • onSelecteds

      protected LinkedHashMap<String,io.odysz.transact.sql.Statement.IPostSelectOptn> onSelecteds
    • onTableOk

      protected LinkedHashMap<String,io.odysz.transact.sql.Statement.IPostOptn> onTableOk
    • sqliteDumyUser

      protected static io.odysz.semantics.IUser sqliteDumyUser
      for generating sqlite auto seq
    • file_sys

      public static int file_sys
  • Constructor Details

    • DASemantext

      protected DASemantext(String connId, DATranscxt.SemanticsMap semanticsMap, io.odysz.semantics.IUser usr, String rtPath) throws SemanticException, SQLException
      Initialize a context for semantics handling. This class handling semantics comes form path, usually an xml like test/res/semantics.xml.
      Parameters:
      connId -
      semanticsMap - semantic configurations, usually load by DATranscxt.

      sample code:

      DATranscxt.initConfigs("inet", rootINF + "/semantics.xml");
      usr -
      rtPath - runtime root path, for docker layer, it's typically the volume folder.
      Throws:
      SemanticException - metas is null
      SQLException
  • Method Details

    • onInsert

      public io.odysz.semantics.ISemantext onInsert(io.odysz.transact.sql.Insert insert, String tabl, List<ArrayList<Object[]>> rows) throws io.odysz.transact.x.TransException
      When inserting, process data row with configured semantics, like auto-pk, fk-ins, etc..
      Specified by:
      onInsert in interface io.odysz.semantics.ISemantext
      Throws:
      io.odysz.transact.x.TransException
      See Also:
      • ISemantext.onInsert(io.odysz.transact.sql.Insert, java.lang.String, java.util.List)
    • onUpdate

      public io.odysz.semantics.ISemantext onUpdate(io.odysz.transact.sql.Update update, String tabl, ArrayList<Object[]> nvs) throws io.odysz.transact.x.TransException
      Specified by:
      onUpdate in interface io.odysz.semantics.ISemantext
      Throws:
      io.odysz.transact.x.TransException
    • onDelete

      public io.odysz.semantics.ISemantext onDelete(io.odysz.transact.sql.Delete delete, String tabl, io.odysz.transact.sql.parts.condition.Condit whereCondt) throws io.odysz.transact.x.TransException
      Specified by:
      onDelete in interface io.odysz.semantics.ISemantext
      Throws:
      io.odysz.transact.x.TransException
    • onPost

      public io.odysz.semantics.ISemantext onPost(io.odysz.transact.sql.Statement<?> stmt, String tabl, ArrayList<Object[]> row, ArrayList<String> sqls) throws io.odysz.transact.x.TransException
      Specified by:
      onPost in interface io.odysz.semantics.ISemantext
      Throws:
      io.odysz.transact.x.TransException
    • insert

      public io.odysz.semantics.ISemantext insert(io.odysz.transact.sql.Insert insert, String tabl, io.odysz.semantics.IUser usr) throws SQLException
      Specified by:
      insert in interface io.odysz.semantics.ISemantext
      Throws:
      SQLException
    • update

      public io.odysz.semantics.ISemantext update(io.odysz.transact.sql.Update update, String tabl, io.odysz.semantics.IUser usr) throws SQLException
      Specified by:
      update in interface io.odysz.semantics.ISemantext
      Throws:
      SQLException
    • dbtype

      public io.odysz.common.dbtype dbtype()
      Specified by:
      dbtype in interface io.odysz.semantics.ISemantext
    • connId

      public String connId()
      Specified by:
      connId in interface io.odysz.semantics.ISemantext
    • connId

      public io.odysz.semantics.ISemantext connId(String conn)
      Specified by:
      connId in interface io.odysz.semantics.ISemantext
    • clone

      public io.odysz.semantics.ISemantext clone(io.odysz.semantics.IUser usr)
      Specified by:
      clone in interface io.odysz.semantics.ISemantext
    • clone

      protected io.odysz.semantics.ISemantext clone(DASemantext srctx, io.odysz.semantics.IUser usr)
    • resulvedVals

      public List<Object> resulvedVals(String tabl, String col)
      Specified by:
      resulvedVals in interface io.odysz.semantics.ISemantext
    • resulves

      public io.odysz.semantics.SemanticObject resulves()
      Get the resolved value in autoVals a.k.a return value of Statement.doneOp(io.odysz.transact.sql.Statement.IPostOptn).
      Specified by:
      resulves in interface io.odysz.semantics.ISemantext
      Returns:
      autoVals
      See Also:
      • ISemantext.resulves()
    • genId

      public String genId(String conn, String tabl, String col, String... preval) throws SQLException, io.odysz.transact.x.TransException
      Specified by:
      genId in interface io.odysz.semantics.ISemantext
      Throws:
      SQLException
      io.odysz.transact.x.TransException
    • genIdPrefix

      protected static String genIdPrefix(String conn, String target, String idField, String subCate) throws SQLException, io.odysz.transact.x.TransException
      Generate new Id with the help of db function f_incSeq(varchar idName)
      Sql script for stored function:
      Mysql:
      create FUNCTION f_incSeq2 (seqId varchar(100), prefix varchar(4)) RETURNS int(11)
      begin
              DECLARE seqName varchar(100);
              DECLARE cnt INT DEFAULT 0;
      
              if prefix = '' then set seqName = seqId;
              else set seqName = concat(seqId, '.', prefix);
              end if;
      
              select count(seq) into cnt from ir_autoSeqs where sid = seqName;
      
              if cnt = 0
              then
                      insert into ir_autoSeqs(sid, seq, remarks) values (seqName, 0, now());
              end if;
      
              select seq into cnt from ir_autoSeqs where sid = seqName;
              update ir_autoSeqs set seq = cnt + 1 where sid = seqName;
              return cnt;
      end;
      select f_incSeq2('%s.%s', '%s') newId
      Oracle:
      CREATE OR REPLACE FUNCTION GZDX_YJPT.f_incSeq2(seqId in varchar, prefix in varchar) RETURN integer
      IS
              PRAGMA AUTONOMOUS_TRANSACTION;
              seqName varchar(100);
              cnt integer DEFAULT 0;
      begin
              if prefix = '' then seqName := seqId;
              else seqName := concat(concat(seqId, '.'), prefix);
              end if;
      
              select count(seq) into cnt from ir_autoSeqs where sid = seqName;
      
              if cnt = 0
              then
                      insert into ir_autoSeqs(sid, seq, remarks) values (seqName, 0, to_char(sysdate, 'MM-DD-YYYY HH24:MI:SS'));
                      commit;
              end if;
      
              select seq into cnt from ir_autoSeqs where sid = seqName;
              update ir_autoSeqs set seq = cnt + 1, remarks = to_char(sysdate, 'MM-DD-YYYY HH24:MI:SS') where sid = seqName;
              commit;
              return cnt;
      end;
               
      select f_incSeq2('%s.%s', '%s') newId from dual

      auto ID for sqlite is handled by genSqliteId(String, String, String) - needing table initialization.

      Parameters:
      conn - connection id
      target - target table
      idField - table id column (no multi-column id supported)
      subCate -
      Returns:
      new Id (shortened in radix 64 by Radix64)
      Throws:
      SQLException
      io.odysz.transact.x.TransException
    • radix64_32

      public static String radix64_32(long v)
      Try generate a radix 64 string of v. String length is controlled by connfigs.xml/k=db-len, overriding default 8 for windows, or 6 for others. If configs.xml/k=filesys is "windows", then generate a radix 32 string.
      Parameters:
      v -
      Returns:
      radix 64/32
    • totalSql

      public String totalSql(String rawSql) throws io.odysz.transact.x.TransException
      Throws:
      io.odysz.transact.x.TransException
    • pageSql

      public String pageSql(String rawSql, int page, int size) throws io.odysz.transact.x.TransException
      Throws:
      io.odysz.transact.x.TransException
    • pagingSql

      public static String pagingSql(io.odysz.common.dbtype dt, String sql, int pageIx, int pgSize) throws io.odysz.transact.x.TransException
      Wrap sql only for rows in a page, in stream mode.
      Parameters:
      dt -
      sql -
      pageIx -
      pgSize -
      Returns:
      pagination wrapped sql
      Throws:
      io.odysz.transact.x.TransException
    • totalSql

      public static String totalSql(io.odysz.common.dbtype dt, String sql) throws io.odysz.transact.x.TransException
      Parameters:
      dt -
      sql -
      Returns:
      SQL: select count(*) as total from [sql]
      Throws:
      io.odysz.transact.x.TransException
    • clear

      public void clear()
      Deprecated.
    • tablType

      public io.odysz.semantics.meta.TableMeta tablType(String tabl)
      Specified by:
      tablType in interface io.odysz.semantics.ISemantext
    • relativpath

      public String relativpath(String... sub) throws io.odysz.transact.x.TransException
      Specified by:
      relativpath in interface io.odysz.semantics.ISemantext
      Throws:
      io.odysz.transact.x.TransException
    • containerRoot

      public String containerRoot()
      Specified by:
      containerRoot in interface io.odysz.semantics.ISemantext
    • onCommitted

      public void onCommitted(io.odysz.semantics.ISemantext ctx, String tabl) throws io.odysz.transact.x.TransException, SQLException
      Specified by:
      onCommitted in interface io.odysz.semantics.ISemantext
      Throws:
      io.odysz.transact.x.TransException
      SQLException
    • addOnRowsCommitted

      public void addOnRowsCommitted(io.odysz.transact.sql.Statement.IPostOptn op)
      Specified by:
      addOnRowsCommitted in interface io.odysz.semantics.ISemantext
    • addOnTableCommitted

      public void addOnTableCommitted(String tabl, io.odysz.transact.sql.Statement.IPostOptn op)
      Specified by:
      addOnTableCommitted in interface io.odysz.semantics.ISemantext
    • onTableCommittedHandler

      public io.odysz.transact.sql.Statement.IPostOptn onTableCommittedHandler(String tabl)
      Specified by:
      onTableCommittedHandler in interface io.odysz.semantics.ISemantext
    • hasOnSelectedHandler

      public boolean hasOnSelectedHandler(String name)
      Specified by:
      hasOnSelectedHandler in interface io.odysz.semantics.ISemantext
    • onSelected

      public void onSelected(Object resultset) throws SQLException, io.odysz.transact.x.TransException
      Specified by:
      onSelected in interface io.odysz.semantics.ISemantext
      Throws:
      SQLException
      io.odysz.transact.x.TransException
    • addOnSelectedHandler

      public void addOnSelectedHandler(String name, io.odysz.transact.sql.Statement.IPostSelectOptn op)
      Specified by:
      addOnSelectedHandler in interface io.odysz.semantics.ISemantext
    • composeVal

      public io.odysz.transact.sql.parts.AbsPart composeVal(Object v, String tabl, String col)
      Specified by:
      composeVal in interface io.odysz.semantics.ISemantext
    • getTableMeta

      public io.odysz.semantics.meta.TableMeta getTableMeta(String tbl)
      Specified by:
      getTableMeta in interface io.odysz.semantics.ISemantext