65.9K
CodeProject 正在变化。 阅读更多。
Home

Siminov Hybrid ORM - 对象关系映射 (Android + Phonegap)

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4投票s)

2013年7月7日

Apache

4分钟阅读

viewsIcon

20612

Siminov 通过将 JavaScript/Java 对象映射到关系数据库,让应用程序开发者的工作变得轻松简单。

Siminov Framework

引言

根据定义,混合应用程序源于不同种类的技术、方法或元素的组合。对于移动应用程序而言,混合应用程序利用了原生技术和移动 Web 技术的优点。

在混合环境中,将 JavaScript/Java 对象映射到关系数据库非常困难,但 Siminov 通过将 JavaScript/Java 对象映射到关系数据库,使应用程序开发者的工作变得轻松简单。

Siminov 可以与 PhoneGap 一起使用。它使应用程序开发人员能够使用 JavaScript、HTML5 和 CSS3 构建移动设备应用程序,而不是使用 Java 等特定于设备的语言。

Siminov 不仅负责从 JavaScript/Java 类到数据库表(以及从 JavaScript/Java 数据类型到 SQL 数据类型)的映射,还提供数据查询和检索功能。与手动在 SQLite 中处理数据相比,它可以显著缩短开发时间。Siminov 的设计目标是通过消除使用 SQLite 进行手动、手工数据处理的需求,从而使开发人员摆脱 99% 的常见数据持久性相关的编程任务。但是,与许多其他持久性解决方案不同,Siminov 不会向您隐藏 SQLite 的强大功能,并保证您在关系技术和知识方面的投入始终有效。

您需要的所有功能

易于配置

Siminov 混合 ORM 提供了一组易于定义的描述符,这些描述符适用于原生和 Web。它们可以大致分为

  • 应用程序描述符:应用程序描述符是将应用程序连接到 Siminov 框架的描述符。它提供有关应用程序的基本信息,定义应用程序的行为。
  • <!-- Design Of ApplicationDescriptor.si.xml -->
    
    <siminov>
        
        <!-- General Application Description Properties -->    
            <!-- Mandatory Field -->
        <property name="name">application_name</property>    
        
            <!-- Optional Field -->
        <property name="description">application_description</property>
        
            <!-- Mandatory Field (Default is 0.0) -->
        <property name="version">application_version</property>
    
        <!-- Siminov Framework Performance Properties -->    
            <!-- Optional Field (Default is true)-->
        <property name="load_initially">true/false</property>
    
        <!-- Database Descriptors Used By Application (zero-to-many) -->    
            <!-- Optional Field's -->
        <database-descriptors>
            <database-descriptor>full_path_of_database_descriptor_file</database-descriptor>
        </database-descriptors>
        
        <!-- Event Handlers Implemented By Application (zero-to-many) -->    
            <!-- Optional Field's -->
        <event-handlers>
            <event-handler>full_java_class_path_of_event_handler/javascript_class_path_of
              _event_handler (ISiminovHandler/IDatabaseHandler)</event-handler>
        </event-handlers>
    </siminov>
  • 数据库描述符: 数据库描述符定义数据库的模式。
  • <!-- Design Of DatabaseDescriptor.si.xml -->
    
    <database-descriptor>
    
            <!-- General Database Descriptor Properties -->    
                <!-- Mandatory Field -->
        <property name="database_name">name_of_database_file</property>
    
                <!-- Optional Field (Default is sqlite)-->
        <property name="type">type_of_database</property>
    
            <!-- Optional Field -->
        <property name="description">database_description</property>
    
            <!-- Optional Field (Default is false) -->r/>    
             <property name="is_locking_required">true/false</property>
        
            <!-- Optional Field (Default is false) -->
        <property name="external_storage">true/false</property>
    
                    
        <!-- Database Mapping Descriptor Paths Needed Under 
                      This Database Descriptor -->    br/>            <!-- Optional Field -->
        <database-mappings>
                    <database-mapping path="full_path_of_database_mapping_descriptor_file" />
        </database-mappings>
    
    
        <!-- Libraries Needed Under This Database Descriptor -->    
                <!-- Optional Field -->
        <libraries>
            <library>full_path_of_library_descriptor_file</library>
        </libraries>br/>    
    </database-descriptor>
  • 库描述符: 库描述符定义库的属性。
  • <!-- Design Of LibraryDescriptor.si.xml -->
    
    <library>
    
            <!-- General Library Properties -->
                <!-- Mandatory Field -->
        <property name="name">name_of_library</property>
        
            <!-- Optional Field -->r/>    
               <property name="description">description_of_library</property>
    
        <!-- Database Mapping Descriptor Paths Needed Under 
                  This Library Descriptor -->    br/>        <!-- Optional Field -->
        <database-mappings>
            <database-mapping path="full_path_of_database_mapping_descriptor_file" />
        </database-mappings>br/>
        <!-- Hybrid Adapters Needed Under This Library Descriptor -->
            
        <!-- Optional Field -->
            <!-- Hybrid Adapters -->
        <adapters>
            <adapter path="full_path_of_hybrid_adapter_file"></adapter>
        </adapters>
         
    </library>
  • 数据库映射描述符:数据库映射描述符执行 ORM,它将 POJO 类映射到数据库表。
  • <!-- Design Of DatabaseMappingDescriptor.si.xml -->
    
    <database-mapping>
    
        <!-- General Properties Of Table And Class -->
        
            <!-- TABLE_NAME: Mandatory Field -->
                    <!-- CLASS_NAME: Mandatory Field -->
        <table table_name="name_of_table" 
          class_name="mapped_pojo_java_class_name/mapped_pojo_javascript_class_name">
            
            <!-- Column Properties Required Under This Table -->
            
            <!-- Optional Field -->
            
                <!-- VARIABLE_NAME: Mandatory Field -->
                            <!-- COLUMN_NAME: Mandatory Field -->
            <column variable_name="class_variable_name" column_name="column_name_of_table">
                
                    <!-- Mandatory Field -->
                <property name="type">java_variable_data_type/javascript_variable_data_type</property>
                
                    <!-- Optional Field (Default is false) -->
                <property name="primary_key">true/false</property>
                
                    <!-- Optional Field (Default is false) -->
                <property name="not_null">true/false</property>
                
                    <!-- Optional Field (Default is false) --> 
                       <property name="unique">true/false</property>
                
                    <!-- Optional Field -->
                <property name="check">
                  condition_to_be_checked (Eg: variable_name 'condition' value; variable_name > 0)
                </property>
                
                    <!-- Optional Field -->
                <property name="default">default_value_of_column (Eg: 0.1)</property>
            
            </column>       
            
            <!-- Index Properties -->
                    
            <!-- Optional Field -->
                <!-- NAME: Mandatory Field -->
                <!-- UNIQUE: Optional Field (Default is false) -->
            <index name="name_of_index" unique="true/false">
                <column>column_name_needs_to_add</column>
            </index>
            
            <!-- Map Relationship Properties -->
                    
            <!-- Optional Field -->    
            <relationships>
                
                    <!-- REFER: Mandatory Field -->
                    <!-- REFER_TO: Mandatory Field -->
                <one-to-one refer="class_variable_name" 
                  refer_to="map_to_pojo_java_class_name/map_to_pojo_javascript_class_name" 
                  on_update="cascade/restrict/no_action/set_null/set_default" 
                  on_delete="cascade/restrict/no_action/set_null/set_default">
                        
                        <!-- Optional Field (Default is false) -->
                    <property name="load">true/false</property>
                </one-to-one>        
                
                    <!-- REFER: Mandatory Field -->
                    <!-- REFER_TO: Mandatory Field -->
                <one-to-many refer="class_variable_name" 
                  refer_to="map_to_pojo_java_class_name/map_to_pojo_javascript_class_name" 
                  on_update="cascade/restrict/no_action/set_null/set_default" 
                  on_delete="cascade/restrict/no_action/set_null/set_default">
                        
                        <!-- Optional Field (Default is false) -->
                    <property name="load">true/false</property>
                </one-to-many>        
    
                        <!-- REFER: Mandatory Field -->
                    <!-- REFER_TO: Mandatory Field -->
                <many-to-one refer="class_variable_name" 
                  refer_to="map_to_pojo_java_class_name/map_to_pojo_javascript_class_name" 
                  on_update="cascade/restrict/no_action/set_null/set_default" 
                  on_delete="cascade/restrict/no_action/set_null/set_default">
                        
                        <!-- Optional Field (Default is false) -->
                    <property name="load">true/false</property>
                </many-to-one>        
    
                    <!-- REFER: Mandatory Field -->
                    <!-- REFER_TO: Mandatory Field -->
                <many-to-many refer="class_variable_name" 
                  refer_to="map_to_pojo_java_class_name/map_to_pojo_javascript_class_name" 
                  on_update="cascade/restrict/no_action/set_null/set_default" 
                  on_delete="cascade/restrict/no_action/set_null/set_default">
                        
                        <!-- Optional Field (Default is false) -->
                    <property name="load">true/false</property>
                </many-to-many>        
                                        
            </relationships>
    
        </table>
    
    </database-mapping>
  • 混合描述符:混合描述符描述将 Web 映射到原生和反之亦然所需的属性。这是一个可选的描述符。
  • <!-- Design of HybridDescriptor.si.xml -->
     
    <hybird-descriptor>
    
        <!-- Adapters -->
        <adapters>
            
            <!-- Adapter -->
            <adapter>
                
                <!-- General Adapter Properties -->
                    <!-- Mandatory Field -->
                <property name="name">adapter_name</property>
                    
                    <!-- Optional Field -->
                    <property name="description">adapter_description</property>
                
                    <!-- Mandatory Field -->
                <property name="type">WEB-TO-NATIVE|NATIVE-TO-WEB</property>
                
                    <!-- Optional Field -->
                <property name="map_to">name_of_adapter_class</property>
        
                    <!-- Optional Field (DEFAULT: FALSE)--> <property name="cache">true/false</property>
                
                <!-- Handlers -->
                    <!-- Handler -->
                <handlers>
                    
                    <handler>
                        
                        <!-- General Handler Properties -->
                            <!-- Mandatory Field -->
                                            <property name="name">handler_name</property>
                        
                            <!-- Optional Field -->
                        <property name="description">handler_description</property>                
                                            
                            <!-- Mandatory Field -->
                            <property name="map_to">name_of_handler_method</property>                
                                                                           
                        <!-- Parameters -->
                        <parameters>
                            
                            <!-- Parameter -->
                            <parameter>
                                
                                    <!-- Mandatory Field -->
                                <property name="name">name_of_parameter</property>
                                
                                    <!-- Mandatory Field -->
                                    <property name="type">parameter_type</property>
                                
                                    <!-- Optional Field -->
                                <property name="description">description_of_parameter</property>
                                
                            </parameter>
                            
                        </parameters>
                        
                        <return>
                            
                                <!-- Mandatory Field -->
                            <property name="type">return_type</property>
                            
                                <!-- Optional Field -->
                            <property name="description">return_data_description</property>
                            
                        </return>     
                    </handler>
                        
                </handlers>
        
            </adapter>
    
            
            <!-- Adapter Paths -->
                    <adapter path="adapter_path" />
                        
        </adapters>
        
    
        <!-- Library Needed Under This HybridDescriptor -->
        <libraries>
            
            <library>full_path_of_library_descriptor_file</library>
            
        </libraries>
        
    </hybird-descriptor>

应用程序部署

应用程序所需的所有资源都由 Siminov 框架创建和管理。(例如:创建应用程序数据库、部署应用程序、管理关系、生成数据库查询)。

支持多个模式

如果应用程序需要,Siminov 框架提供了一种支持多个模式的简便方法。开发人员可以使用描述符定义数据库的属性。

事件通知器

Siminov 通过事件通知提供通知,如果框架触发任何事件,应用程序将收到通知。

  • Siminov 事件:ISiminov 是一个事件处理程序,当框架中发生任何操作时,它会自动触发。应用程序必须为 ISiminov 事件通知器提供实现,并将其注册到 siminov。
  • public interface ISiminovEvents {
    
        public void firstTimeSiminovInitialized();
    
        public void siminovInitialized();
    
        public void siminovStopped();
    }
  • 数据库事件:IDatabase 是一个事件处理程序,当数据库发生操作时,它会自动触发。应用程序必须为 IDatabase 事件通知器提供实现,并将其注册到 siminov。
  • public interface IDatabase {
    
        public void openOrCreate(final DatabaseDescriptor databaseDescriptor) throws DatabaseException;
    
        public void close(final DatabaseDescriptor databaseDescriptor) throws DatabaseException;
    
        public void executeQuery(final DatabaseDescriptor databaseDescriptor, 
          final DatabaseMappingDescriptor databaseMappingDescriptor, 
          final String query) throws DatabaseException;
    
        public void executeBindQuery(final DatabaseDescriptor databaseDescriptor, 
          final DatabaseMappingDescriptor databaseMappingDescriptor, 
          final String query, final Iterator columnValues) throws DatabaseException;
    
        public Iterator executeFetchQuery(final DatabaseDescriptor databaseDescriptor, 
          final DatabaseMappingDescriptor databaseMappingDescriptor, 
          final String query) throws DatabaseException;
    
        public void executeMethod(final String methodName, 
          final Object parameters) throws DatabaseException;
    }

数据库 API

  • 创建和删除数据库
  • public class Database {
        public static IDatabase createDatabase(DatabaseDesriptor) throws DatabaseException;
    }
  • 创建和删除表
  • 创建和删除索引
  • Select
  • Native: public ISelect select() throws DatabaseException;                    
    Web: this.select() = function();
  • 保存
  • Native: public final void save() throws DatabaseException;

    示例:保存 Java 对象

    Liquor beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.save();
    } catch(DatabaseException de) {
        //Log it.
    }
    Web: this.save = function();

    示例:保存 JavaScript 对象

    var beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.save();
    } catch(DatabaseException de) {
        //Log it.
    }
  • 更新
  • Native: public final void update() throws DatabaseException;

    示例:更新 Java 对象

    Liquor beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.update();
    } catch(DatabaseException de) {
        //Log it.
    }
    Web: this.update = function();

    示例:更新 JavaScript 对象

    var beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.update();
    } catch(DatabaseException de) {
        //Log it.
    }
  • 保存或更新
  • Native: public final void saveOrUpdate() throws DatabaseException;

    示例:保存或更新 Java 对象

    Liquor beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.saveOrUpdate();
    } catch(DatabaseException de) {
        //Log it.
    }
    Web: this.saveOrUpdate() = function();

    示例:保存或更新 JavaScript 对象

    var beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.saveOrUpdate();
    } catch(DatabaseException de) {
        //Log it.
    }
  • 删除
  • Native: public final void delete() throws DatabaseException { }

    示例:删除 Java 对象

    Liquor beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.delete();
    } catch(DatabaseException de) {
        //Log it.
    }
    Web: this.delete() = function();

    示例:删除 JavaScript 对象

    var beer = new Liquor();
    beer.setLiquorType(Liquor.LIQUOR_TYPE_BEER);
    beer.setDescription(applicationContext.getString(R.string.beer_description));
    beer.setHistory(applicationContext.getString(R.string.beer_history));
    beer.setLink(applicationContext.getString(R.string.beer_link));
    beer.setAlcholContent(applicationContext.getString(R.string.beer_alchol_content));
    
    try {
        beer.delete();
    } catch(DatabaseException de) {
        //Log it.
    }

聚合 API

  • Count
  • Native: public ICount count() throws DatabaseException;
    Web: this.count() = function();
  • Average
  • Native: public IAverage avg() throws DatabaseException;
    Web: this.avg() = function();
  • Sum
  • Native: public ISum sum() throws DatabaseException;
    Web: this.sum() = function();
  • 总计
  • Native: public ITotal total() throws DatabaseException;
    Web: this.total() = function();
  • 最低
  • Native: public IMin min() throws DatabaseException;
    Web: this.min() = function();
  • 最大
  • Native: public IMax max() throws DatabaseException;
    Web: this.max() = function();
  • Group Concat (字符串拼接)
  • Native: public IGroupConcat groupConcat() throws DatabaseException;
    Web: this.groupConcat() = function();

数据库事务 API

  • 开始事务
  • Native: public static final void beginTransaction(
      final DatabaseDescriptor databaseDescriptor) throws DatabaseException;
    Web: this.beginTransaction = function(databaseDescriptor);
  • 提交事务
  • Native: public static final void commitTransaction(
      final DatabaseDescriptor databaseDescriptor) throws DatabaseException;
    Web: this.commitTransaction = function(databaseDescriptor);
  • 结束事务
  • Native: public static final void endTransaction(
      final DatabaseDescriptor databaseDescriptor) throws DatabaseException;
    Web: this.endTransaction = function(databaseDescriptor);

数据库加密 (SQLCipher)

当谈到数据库时,数据安全起着重要作用。它可以保护您的数据库免受破坏性力量和未经授权用户的恶意行为的影响。

Siminov 提供了 SQLCipher 的实现,以保护应用程序数据库免受任何未经授权的用户的侵害。

处理基于库的 ORM

Android 库项目是一个开发项目,其中包含共享的 android 源代码和资源。其他 android 应用程序项目可以引用库项目,并在构建时将其编译后的源代码包含在其 .apk 文件中。

Siminov 提供了为您的库项目配置 ORM 的机制。

许可证

[SIMINOV FRAMEWORK]
Copyright [2013] [Siminov Software Solution|support@siminov.com]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://apache.ac.cn/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

结论

如果您希望弥合对象关系阻抗失配并提高您的生产力和数据访问性能,您应该考虑 Siminov 混合 ORM。它提供了您在 ORM 中需要的所有功能,例如易于配置、应用程序部署、支持多个模式、事件通知器、数据库 API、聚合 API、数据库事务 API、数据库加密 (SQLCipher)、处理基于库的 ORM。

© . All rights reserved.