Esempio n. 1
0
  public PrimaryKey(
      String dbname,
      String tableName,
      String primaryKeyName,
      int increment,
      long curValue,
      String type,
      String prefix,
      String maxSql,
      String generator,
      Connection con) {
    this.dbname = dbname;
    this.generator = generator;
    dbAdapter = SQLManager.getInstance().getDBAdapter(dbname);
    this.tableName = tableName;
    this.primaryKeyName = primaryKeyName;
    this.increment = increment;
    this.curValue = curValue;
    JDBCPool pool = (JDBCPool) (SQLManager.getInstance().getPool(dbname));
    synsequece = pool.getJDBCPoolMetadata().synsequence();
    this.seqfunction = pool.getJDBCPoolMetadata().getSeqfunction();
    String mode = pool.getKeygenerate();
    if (mode.trim().equalsIgnoreCase("auto")) keygenerator_mode = 0;
    else if (mode.trim().equalsIgnoreCase("composite")) keygenerator_mode = 1;
    if (type != null && !type.trim().equals("")) this.type = type;
    if (prefix != null && !prefix.trim().equals("")) this.prefix = prefix;
    this.maxSql = maxSql;

    if (type.equals("sequence")) {
      ColumnMetaData cd = pool.getColumnMetaData(con, tableName, primaryKeyName);
      if (cd != null) {
        int type_ = cd.getDataType();
        this.metaType = PrimaryKey.getJavaType(type_);
      } else {
        metaType = "int";
      }
    } else if (type.equals("uuid")) {
      ColumnMetaData cd = pool.getColumnMetaData(con, tableName, primaryKeyName);
      if (cd != null) {
        int type_ = cd.getDataType();
        this.metaType = PrimaryKey.getJavaType(type_);
      } else {
        metaType = "string";
      }
    } else {
      this.metaType = type;
    }
  }
Esempio n. 2
0
    /**
     * added by biaoping.yin on 2005.03.29
     * 重构insert语句,添加主键信息,首先获取主键值
     * @param insertStmt 数据库插入语句
     * @param dbname 数据库名称
     * @return ret
     * ret[0]:存放insert语句
     * ret[1]:存放新的主键值,如果不是插入语句则为空
     * ret[2]:更新表tableinfo中插入表对应主键值语句,如果不是插入语句则为空
     * ret[3]:PrimaryKey对象,如果不是插入语句则为空
     * ret[4]:标识主键是自动产生还是用户指定,0-标识自动
     * 		  1-标识用户指定
     *        如果不是插入语句则为空
     * ret[6]:field字段数组;
     * @throws SQLException 
     * 
     */
    public static Object[] refactorInsertStatement(Connection con ,String insertStmt,String dbname,boolean prepared) throws SQLException
    {
        Object[] ret = new Object[6];

        //String tableName = this.getInsertTableName(insertStmt);
        //this.getInsertTableName(insertStmt);
        /**
         *  1.Insert
		 * 	2.into
		 * 	3.oa_meetingpromptsound
		 * 	4.( soundCode , soundName , soundFileName )
		 * 	5.values
		 * 	6.( '。.尹标平','bb','d()d' )
         */
        String tableInfos[] = StatementParser.parserInsert(insertStmt);
        if(tableInfos == null)
        {
            ret[0] = insertStmt;
            return ret;
        }
        String insert = tableInfos[0];
        String into   = tableInfos[1];
        String tableName = tableInfos[2];
        String fields = tableInfos[3];

        String values_key = tableInfos[4];
        String values = tableInfos[5];
        PrimaryKey primaryKey = null;
        try
		{

	        primaryKey = PrimaryKeyCacheManager.getInstance()
														  .getPrimaryKeyCache(dbname)
														  .getIDTable(con,tableName.toLowerCase());

		}
        catch(Exception e)//如果抛出sql异常,忽略,不影响整个事务的处理。
		{
        	log.info("[db:"
        	        + dbname
        	        + "] tableinfo not initialed or initial failed"
        	        + " or check the table "
        	        + "[tableinfo has been created] or table[" + tableName
        	        + "'s information has been inserted into tableinfo"
        	        + "]\r\n please check log to view detail.");
        	//e.printStackTrace();

        	ret[0] = insertStmt;
        	
			return ret;
		}
		if(primaryKey == null)
		{
			//System.out.println("表'" + tableName + "'没有定义主键或定义了多主键!");
			ret[0] = insertStmt;
			PrimaryKey t_p = new PrimaryKey(dbname,tableName,null,null,con);
	        ret[3] = t_p;
			return ret;
		}


        //定义表的主键名称变量
        String idName = primaryKey.getPrimaryKeyName();
        boolean contain = containKey(fields,idName);
        ret[4] = new Integer(0);
        ret[5] = StatementParser.parseField(fields);

        if(contain)
        {
        	String s_temp = values.trim();
        	s_temp = s_temp.substring(1,s_temp.length() -1);
        	String keyValue = getKeyValue(s_temp);
            ret[0] = insertStmt;
            ret[1] = keyValue;
            PrimaryKey t_p = new PrimaryKey(primaryKey.getDbname(),
            								primaryKey.getTableName(),
            								primaryKey.getPrimaryKeyName(),
            								keyValue,con);
            ret[3] = t_p;
            ret[4] = new Integer(1);            
            return ret;
        }
        //将表的主键字段属性插入到insert语句中
        StringBuffer temp = new StringBuffer(fields);
        temp.insert(1,idName + ",");
        fields = temp.toString();

        
        temp = new StringBuffer(values);
//      //定义主键值变量
        Sequence idValue = primaryKey.generateObjectKey(con);
        temp.insert(1,PrimaryKey.changeID(idValue.getPrimaryKey(),dbname,primaryKey.getType()) + ",");
        values = temp.toString();
        temp = new StringBuffer(insert);
        temp.append(" ")
			.append(into)
			.append(" ")
			.append(tableName)
			.append(fields)
			.append(" ")
			.append(values_key)
			.append(values);
        //started here
        //设置insert语句
        ret[0] = temp.toString();
        //设置新的主键值
        ret[1] = idValue.getPrimaryKey();
        //设置更新tableinfo的语句
        List datas = new ArrayList();
        datas.add(new Long(idValue.getSequence()));
        datas.add(tableName.toLowerCase());
        datas.add(new Long(idValue.getSequence()));
        
        UpdateSQL preparedUpdate = new UpdateSQL(dbname,tableName,UpdateSQL.TABLE_INFO_UPDATE,  datas);
//        ret[2] = "update tableinfo set table_id_value=" + idValue.getSequence() +" where table_name='"+ tableName.toLowerCase() + "' and table_id_value <" + idValue.getSequence()  ;
        ret[2] = preparedUpdate  ;

        //设置表主键信息封装类
        ret[3] = primaryKey;
//        ret[4] = new Integer(0);

        return ret;
    }