The invention relates to the technical field of software development, in particular to a method for preventing over-long SQL statements from being spelled in batch processing. The method of the invention includes the following steps: 1. Query the longest byte number of SQL statements supported by database A; 2. Create SQL prefix for a table and record the prefix length L; 3. Circulate through the list of objects that need batch processing, record the length of SQL string S, and stitch the fields of current object that need to be persisted to database to SQL string; 4. Calculate the stitched SQL bytes; Number B; 5. Compare the number of bytes A with the number of bytes B. Decide the specific processing steps according to the size of A and B and the objects that need to be processed later. 6. Repeat steps 3 to 5 until the object list data persistence is completed. The invention solves the problem that the list data that needs to be persisted to the database is too large in batch processing, which makes the spliced SQL too long, and causes the database to throw an exception while executing sql, which causes the batch processing operation to fail.
【技术实现步骤摘要】
一种批量处理时防止拼写sql语句过长的方法
本专利技术涉及软件开发
,具体涉及一种批量处理时防止拼写的sql语句过长的方法。
技术介绍
在软件开发过程中遇到批量处理时,现有三种处理方案:一、遍历循环需要批量处理的对象列表,然后进行单个对象的持久化处理;二、遍历循环需要批量处理的对象列表拼接得到一条处理sql;三、遍历循环需要批量处理的对象列表,生成n(n>0)个固定长度对象列表,最后将这n个对象列表通过方案二的方式进行持久化处理。对于现有方案一在于遍历过程中的每个对象都进行一次数据库持久化,其缺陷是最耗时。方案二本是最节约时间但其缺陷在于当批量处理的对象列表出现过多时,拼接的sql语句很容易达到数据库最大限制sql的字节数,使得程序直接抛出异常而终止。方案三则介于方案一和方案二之间,缺陷是不能确定最大可以拼接的sql长度(不能确定sql是否有效或有效的sql不是最长)来充分节约时间。
技术实现思路
本专利技术解决的技术问题在于提供一种批量处理时防止拼写sql语句过长的方法;解决批量处理时需要持久化到数据库的列表数据过大,使得拼接的sql过长;导致执行sql ...
【技术保护点】
1.一种批量处理时防止拼写sql语句过长的方法,其特征在于,所述的方法包括如下步骤:步骤1、查询数据库支持的sql语句最长字节数A;步骤2、创建针对某一表的sql前缀,并记录前缀长度L;步骤3、循环遍历需要批量处理的对象列表,记录sql字符串长度S,并拼接当前对象需要持久化到数据库的字段到sql字符串;步骤4、计算已拼接好的sql字节数B;步骤5、将字节数A与字节数B进行比较,根据A与B的大小及需要后续处理的对象决定具体处理步骤;步骤6、重复执行步骤3到步骤5直至对象列表数据持久化完成。
【技术特征摘要】
1.一种批量处理时防止拼写sql语句过长的方法,其特征在于,所述的方法包括如下步骤:步骤1、查询数据库支持的sql语句最长字节数A;步骤2、创建针对某一表的sql前缀,并记录前缀长度L;步骤3、循环遍历需要批量处理的对象列表,记录sql字符串长度S,并拼接当前对象需要持久化到数据库的字段到sql字符串;步骤4、计算已拼接好的sql字节数B;步骤5、将字节数A与字节数B进行比较,根据A与B的大小及需要后续处理的对象决定具体处理步骤;步骤6、重复执行步骤3到步骤5直至对象列表数据持久化完成。2.根据权利要求1所述的方法,其特征在于:所述的步骤5,若A大于B且对象并非最后一个时继续步骤3和步骤4;若A大于B且对象为最后一个时,执行sql持久化到数据库表中。3.根据权利要求1所述的方法,其特征在于:所述的步骤5,若A小于等于B时,则执行如下步骤:S51,s...
【专利技术属性】
技术研发人员:梁茂钦,涂旭平,季统凯,
申请(专利权)人:国云科技股份有限公司,
类型:发明
国别省市:广东,44
还没有人留言评论。发表了对其他浏览者有用的留言会获得科技券。