TinySQL介绍

来源:互联网 发布:计量经济学数据库 编辑:程序博客网 时间:2024/05/21 14:54

TinySQL介绍

TinySQL 是一个小型的SQL 引擎,纯JAVA编写,它是一个非常小型的、基于文件的SQL引擎,引擎使用兼容MS Excel 的文件。
 
开发环境: JDBC
目标用户:
Developers
开源协议:
GNU Library or Lesser General Public License (LGPL)
操作系统:
OS Independent (Written in an interpreted language)
编程语言:
Java
主题:
: Database Engines/Servers
用户接口:
Command-line     
www.cc166.cn //编程开源大全KeKeYu.

TinySQL is small and handy SQL script which can quickly generate consistent code snippet that you can paste in your project. TinySQL小和方便的SQL脚本,可以快速生成一致的代码片段,您可以粘贴在您的项目。 It basically operates by reading schema of provided table and applying template to each column of table in order to generate code snippet.它基本上运作提供阅读表的架构和应用模板的每个表列,以生成的代码段。

With TinySQL , you can create complete or specific portion of business class, business service, or data access layer.有了TinySQL您可以创建完整的或具体的业务类,商业服务,或数据访问层的一部分。 You can also generate repetitive code snippet like storing all DataReader columns into respective property of business object, or passing all object properties to SqlParameter in data access layer, or creating stored procedures for a table or creating simple update or insert statement in stored procedure etc.您还可以生成像储存到业务对象,或在数据传递所有对象属性SqlParameter各自所有财产DataReader列接入层,或创建一个表的存储过程或创建简单的更新或插入等在存储过程中的重复代码段声明

TinySQL is part of my daily development practice. TinySQL是我每天的发展实践的一部分。 I am using early version of TinySQL for 2 years now and found myself more productive with this tool.我用了2年现在TinySQL的早期版本,并发现自己更多的使用此工具的生产。 I usually keep SQL Management Studio open (like most Microsoft application developers) all the time with a dedicated query pane for TinySQL.我通常保持的SQL Management Studio中打开(最喜欢的微软应用程序开发)所有与一个TinySQL专门的查询窗格时间。 Whenever I need to write any repetitive code based on schema of any table, I simply provide table name, tweak template per my requirement and execute the script.每当我需要编写任何重复代码的任何表的架构为基础的,我只是提供表名,调整模板按我的要求,执行该脚本。 I then copy and paste generated code in my project.然后我复制并粘贴在我的项目生成的代码。 Yes, I do require making minor changes sometime in generated code to make it usable in my project but still TinySQL serves its purpose.是的,我必须使生成的代码中的某个小的改动,使之在我的项目有用,但仍然TinySQL达到目的。

TinySQL is definitely not a replacement of sophisticated code generation tools like CodeSmith, TierDeveloper, LLBLGen etc. Those tools are very advance and can generate code for whole project including user interface, business layer, data access layers and even store procedures. TinySQL决不是一个复杂的代码像CodeSmith中,TierDeveloperLLBLGen等这些工具是非常前进,可以为整个项目包括用户界面,业务层代码生成工具更换,数据访问层,甚至是存储程序。 TinySQL can only generate basic code snippet that you can use in your existing code or project. TinySQL只能生成基本的代码段,您可以使用现有的代码或项目。

Download Link: TinySQL Implementation of TinySQL is very simple and straight forward.实施的TinySQL很简单,直截了当。 It reads schema definition of all columns of provided table and template; it then render code by rotating loop for each column.它读取模式,其提供表中所有列的定义和模板,它的旋转,然后使每一列循环代码。 Variable you have to take care in TinySQL are:可变你必须采取TinySQL照顾:

@TableName: table name for which you want to generate code @表名:表名您想要生成代码
@PrintTableName: how above table name should display in generated code. @ PrintTableName如何表的名称应显示在生成的代码。 You may have table “tblCategories” but you want “Category” for class name.您可能已经表“tblCategories”但你要分类的类的名称

@Template: Template for generated code @模板:对生成的代码模板

Open TinySQL in SQL Management Studio, provide table name and template, press F5 to execute TinySQL and you will have generated code in result pane.打开在SQL Management StudioTinySQL,提供表名和模板,按F5执行TinySQL,你会产生结果窗格中的代码。 Now you just have to copy generated code from result pane and paste in your project.现在你只需要复制的结果窗格并粘贴到您的项目生成的代码。

To make customization of template more flexible, I have used few tags as placeholder for rendering respective entity.为了使模板的定制更灵活,我作为占位符各实体提供一些标记。 These tags are:这些标记:

$table : Table name
$field : Column Name
$type : .NET Data Type
$default : .NET Default Value
$sp_type : SQL Data Type
$length : Column Max Length

{loop} : Start Loop
{/loop} : End Loop

Suppose I want to generate snippet which has list of all columns of provided table.假如我要生成段已提供的表中所有列的列表。 So value of TinySQL variables will be:所以,TinySQL变量的值将是:

SET @TableName = 'tblCategory'
SET @PrintTableName = 'Category'
SET @Template = '
** generate simple column list /v1.0
$table: {loop}$field, {/loop}
'

It will generate code something like this:这将生成的代码是这样的:

Category: Id, Language, ParentId, Name, SafeName, Right, Left, IsEnabled, DisplayOrder, Color,

Note: first line of template is comment portion which can be used as template description, author, template version, additional comment etc. 注:模板的第一行是注释部分,可用于为模板的描述,作者,模板的版本,更多的评论等

You can play with TinySQL to explorer more about this tool.你可以玩TinySQL以探险家此工具的详情。

Ok, so here are some practical examples of templates which explain the basic usage of TinySQL:好了,下面是一些实例模板,解释TinySQL的基本用法:

1. 1 Listing all fields of provided table. 清单提供的表的所有字段。

$table Fields:
{loop}$field,
{/loop}

This template will generate list of field in separate lines.这个模板将生成的字段列表中单独的行。

2. 2 Creating Simple SELECT statement: 创建简单的SELECT语句:

SELECT {loop}$field, {/loop}
FROM $table

As I mentioned before, you may require minor modification sometime in generated code to use it in your project.正如我前面提到的,您可能需要在某个小的修改生成的代码在项目中使用它。 Like in above case, you have to manual remove last comma from SELECT.在上面的例子一样,你必须手动删除选择上逗号。

3. 3 Creating UPDATE statement 创建UPDATE语句

UPDATE $table
SET {loop}$field = @$field,
{/loop}

Again, you require removing last comma from generated code.同样,您需要清除生成代码的最后逗号。

4. 4 Creating Update Stored Procedure: 创建更新存储过程:

** generate Data Provider: Update/v1.0
CREATE PROCEDURE [dbo].[usp_Update$table]{loop}
@$field = $sp_type,{/loop}
AS
BEGIN

UPDATE dbo.$table WITH (ROWLOCK)
SET {loop}
$field = @$field,{/loop}
WHERE --TODO

RETURN -1
END

5. 5 Creating simple insert statement: 创建简单的INSERT语句:

** generate simple insert statement/v1.0
INSERT INTO $table ({loop}[$field],{/loop}
VALUES ({loop}@$field,{/loop}

6. 6 Now, let's use TinySQL to generate code snippet in C# (or VB.NET) which assign entity values to respective parameters of stored procedure. 现在,让我们使用TinySQL生成的代码段在C#(或VB.NET)的分配实体值分别存储过程的参数。

** generate Data Provider: Update Function/v1.0
SqlDatabase database = new SqlDatabase(this._connectionString);
DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.$table_Update", _useStoredProcedure);
{loop}database.AddInParameter(commandWrapper, "@$field", DbType.$type, entity.$field );
{/loop}

7. 7 And as last example, here is template to generate business entity in C#. 作为最后一个例子,这里是模板生成业务实体在C#。

** generate basic business entity/v1.0<br>
#region Using Directives</p>
<p>using System;<br>
using System.ComponentModel;<br>
using System.Collections;<br>
using System.Xml.Serialization;<br>
using System.Runtime.Serialization;</p>

<p>#endregion</p>

<p>namespace MyNameSpace.Entities<br>
{<br>
///<summary><br>
/// An object representation of the “$table” table.<br>
///</summary><br>
[Serializable, DataObject]<br>
public class $table<br>
{<br>
#region Variable Declarations<br>
{loop}<br>
private $type _$field;{/loop}</p>

<p>        #endregion Variable Declarations</p>

<p>        #region Constructors</p>

<p>        public $table()<br>
{<br>
{loop}_$field = $default;<br>
{/loop}<br>
}</p>

<p>        #endregion</p>

<p>        #region Properties<br>
{loop}<br>
public $type $field<br>
{<br>
get<br>
{<br>
return this._$field;<br>
}<br>
set<br>
{<br>
this._$field = value;<br>
}<br>
}<br>
{/loop}<br>
#endregion<br>
}<br>
}

Like last example, you can even create complete business service or data access layer using TinySQL.最后一个例子一样,你甚至可以创建完整的商业服务或数据访问层使用TinySQL

I hope you will find this tool useful.我希望你会发现这个工具非常有用。

 

完整代码:

DECLARE @TableName VARCHAR(255)

DECLARE @PrintTableName VARCHAR(255)

DECLARE @Template VARCHAR(MAX)

 

SET @TableName = 'table_name'

SET @PrintTableName = ''

SET @Template = '

** generate simple column list /v1.0

$table: {loop}$field, {/loop}

'

 

/*********************************************************************/

* TinySQL Code Generator {Version: 1.0}

*

* (C) 2007 Firoz Ansari

* http://www.firoz.name

*

* Permission to use, copy, modify, and distribute this software for any

* purpose with or without fee is hereby granted, provided that the above

* copyright notice and this permission notice appear in all copies.

*

* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES

* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF

* MERCHANTABILITY AND FITNESS.

*

* Tag Hint:

* $table    : Table name

* $field    : Column Name

* $type     : .NET Data Type

* $sp_type  : SQL Data Type

* $default  : .NET Default Value

* $length   : Column Max Length

*

* {loop}    : Start Loop

* {/loop}   : End Loop

/*********************************************************************/

SET NOCOUNT ON

DECLARE @ColumnName VARCHAR(255)

DECLARE @DataType VARCHAR(255)

DECLARE @MaxLength INT

DECLARE @IsNull INT

DECLARE @RType VARCHAR(100)

DECLARE @RDefault VARCHAR(100)

DECLARE @Snippet VARCHAR(MAX)

DECLARE @TempSnippet VARCHAR(MAX)

DECLARE @RenderedSnippet VARCHAR(MAX)

 

DECLARE @CurrentPos INT

DECLARE @StartTag VARCHAR(10)

DECLARE @EndTag VARCHAR(10)

DECLARE @StartPos INT

DECLARE @EndPos INT

 

-- pull column information of selected table

DECLARE CurColumnList CURSOR FOR

SELECT [Column].Name AS [ColumnName], systypes.Name AS [DateType], [Column].max_length as [MaxLength], [Column].is_nullable as [IsNull]

FROM Sys.Columns [Column]

INNER JOIN SysTypes ON [Column].system_type_id = SysTypes.xtype

LEFT JOIN SysComments ON [Column].default_object_id = SysComments.id

LEFT OUTER JOIN sys.extended_properties ex ON ex.major_id = [Column].object_id AND ex.minor_id = [Column].column_id AND ex.name = 'MS_Description'

WHERE OBJECTPROPERTY([Column].object_id, 'IsMsShipped')=0 AND OBJECT_NAME([Column].object_id) = @TableName

ORDER BY OBJECT_NAME([Column].object_id), [Column].column_id

 

SET @StartTag = '{loop}'

SET @EndTag = '{/loop}'

 

-- find first start-loop tag in template

SET @StartPos = CharIndex(@StartTag, @Template, 0)

SET @CurrentPos = 1

WHILE @CurrentPos > 0 AND @StartPos > 0

BEGIN

        -- goto the end of start-loop tag string

        SET @StartPos = @StartPos+Len(@StartTag)

        SET @EndPos = CharIndex(@EndTag, @Template, @StartPos)

        SET @Snippet = SubString(@Template, @StartPos, @EndPos-@StartPos)

 

        SET @TempSnippet = ''

        OPEN CurColumnList

        FETCH NEXT FROM CurColumnList INTO @ColumnName, @DataType, @MaxLength, @IsNull

        WHILE @@FETCH_STATUS = 0

        BEGIN

               SET @RenderedSnippet = @Snippet

               SET @RenderedSnippet = REPLACE(@RenderedSnippet, '$table', @PrintTableName)

               SET @RenderedSnippet = REPLACE(@RenderedSnippet, '$field', @ColumnName)

               SET @RenderedSnippet = REPLACE(@RenderedSnippet, '$length', @MaxLength)

 

               -- set .net data type for respective data type of column

               SELECT @RType = CASE @DataType

                       WHEN 'bit' THEN 'Boolean'

                       WHEN 'tinyint' THEN 'Int16'

                       WHEN 'smallint' THEN 'Int32'

                       WHEN 'bigint' THEN 'Int64'

                       WHEN 'int' THEN 'Int32'

                       WHEN 'varchar' THEN 'String'

                       WHEN 'nvarchar' THEN 'String'

                       WHEN 'char' THEN 'String'

                       WHEN 'text' THEN 'String'

                       WHEN 'float' THEN 'Double'

                       WHEN 'decimal' THEN 'Double'

                       WHEN 'money' THEN 'Double'

                       WHEN 'smalldatetime' THEN 'DateTime'

                       WHEN 'datetime' THEN 'DateTime'

                       WHEN 'uniqueidentifier' THEN 'Guid'

                       ELSE '?'+@DataType

               END

 

               IF (UPPER(@DataType) != 'VARCHAR')

                       SET @RenderedSnippet = REPLACE(@RenderedSnippet, '$sp_type', UPPER(@DataType))

               ELSE

                       SET @RenderedSnippet = REPLACE(@RenderedSnippet, '$sp_type', UPPER(@DataType) + '(' + CAST(@MaxLength AS VARCHAR(10)) + ')')

 

               SET @RenderedSnippet = REPLACE(@RenderedSnippet, '$type', @RType)

 

                -- set .net default value for respective data type of column

               SELECT @RDefault = CASE @DataType

                       WHEN 'bigint' THEN '0'

                       WHEN 'int' THEN '0'

                       WHEN 'tinyint' THEN '0'

                       WHEN 'smallint' THEN '0'

                       WHEN 'bit' THEN '0'

                       WHEN 'varchar' THEN 'String.Empty'

                       WHEN 'nvarchar' THEN 'String.Empty'

                       WHEN 'char' THEN 'String.Empty'

                       WHEN 'text' THEN 'String.Empty'

                       WHEN 'float' THEN '0.0'

                       WHEN 'decimal' THEN '0.0'

                       WHEN 'money' THEN '0.0'

                       WHEN 'smalldatetime' THEN 'DateTime.MinTime'

                       WHEN 'datetime' THEN 'DateTime.MinTime'

                       WHEN 'uniqueidentifier' THEN 'Guid.Empty'

                       ELSE '?'+@DataType

               END

               SET @RenderedSnippet = REPLACE(@RenderedSnippet, '$default', @RDefault)

               SET @TempSnippet = @TempSnippet + @RenderedSnippet

               FETCH NEXT FROM CurColumnList INTO @ColumnName, @DataType, @MaxLength, @IsNull

        END

        CLOSE CurColumnList

 

        -- put back rendered snippet into template

        SET @Template = REPLACE(@Template, @StartTag+@Snippet+@EndTag, @TempSnippet)

 

        -- get next position for start loop tag in template

        Set @StartPos = CharIndex(@StartTag, @Template, @EndPos+1)

        SET @CurrentPos = @StartPos

END

 

-- if printable table name is not specified than use table name instead

IF (@PrintTableName = '')

        SET @PrintTableName = @TableName

 

-- replace $table tag to printable table name

SET @Template = REPLACE(@Template, '$table', @PrintTableName)

 

--ignore first line of template, which can be used as template description, author, template version, additional comment etc.

--simply remove this line if you dont want comment segment in template

SET @CurrentPos = CharIndex(CHAR(13)+CHAR(10), @Template, 0)

SET @CurrentPos = CharIndex(CHAR(13)+CHAR(10), @Template, @CurrentPos+2) + 2

SET @Template = SubString(@Template, @CurrentPos, Len(@Template))

--/

 

--print rendered code in result pane

PRINT @Template

 

DEALLOCATE CurColumnList

SET NOCOUNT OFF

参考网址:http://www.cnblogs.com/emanlee/archive/2008/01/01/1022172.html

http://www.firoz.name/wp-content/uploads/2007/09/TinySQL.txt

http://www.kekeyu.com/database/open_1312388402.html