Code Monkey home page Code Monkey logo

ormlite-extension's Introduction

Extensions OrmLite for Kotlin

Automatic work with foreign models and automatic generation extensions for easy work with https://ormlite.com/.

Just super KISS work with ormlite)

OrmliteBase:

  • Connection (init, close)
  • Auto gen ComponentScanDb
  • Create table if not exist
  • Clear all tables in db

Extensions:

  • One-to-one relationships (Model)
  • One-to-many relationships (Collection<Model>)
  • Clear tables with relationships
  • Create and update with relationships
  • Model get JSONObject with relationships
  • Other extensions functions

For connect:

// Gradle Groovy DSL
repositories {
    maven {
        url "https://artifactory.keygenqt.com/artifactory/open-source"
    }
}
dependencies {
    kapt 'com.keygenqt.artifactory:gen-ormlite-android:1.0.1'
}
// Gradle Kotlin DSL
repositories {
    maven("https://artifactory.keygenqt.com/artifactory/open-source")
}
dependencies {
    kapt("com.keygenqt.artifactory:gen-ormlite-android:1.0.1")
}

Methods extensions:

// model
Model().delete()
Model().createOrUpdate() // Model
Model().getJSONObject() // JSONObject

// find
Model.find("key", "value") // model
Model.findOneAND(hashMapOf("key" to "value")) // model
Model.findOneOR(hashMapOf("key" to "value")) // model
Model.findAll(hashMapOf("key" to "value")) // list
Model.findAND(hashMapOf("key" to "value")) // list
Model.findOR(hashMapOf("key" to "value")) // list

// Companion
Model.dao() // Dao<Model, String>
Model.clearTable()
Model.clearTableWithForeign()

Example:

class MainActivity : AppCompatActivity() {

    private lateinit var db: MyOrmliteBase

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = MyOrmliteBase(this, "name.db", 1)
        setContent {
            MyApplicationTheme {
                Surface(color = MaterialTheme.colors.background) {
                    Example()
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        db.close()
    }
}

@Composable
fun Example() {
    Column(
        modifier = Modifier.padding(16.dp)
    ) {

        // clear all tables in db
        OrmliteBase.clearDb()

        // clear table only model
        ModelUser.clearTable()

        // clear table model with foreign models
        ModelUser.clearTableWithForeign()

        val id = UUID.randomUUID().toString()

        // example create model with foreign
        ModelUser(
            id,
            "First name",
            "Last name",
            ModelUserAddress("Volgodonsk", "Russia"),
            arrayListOf(
                ModelUserParent("First name mather", "Last name mather"),
                ModelUserParent("First name father", "Last name father"),
            )
        ).createOrUpdate()

        // find model
        ModelUser.find("id", id)?.let {
            Text("It's exist - $id")
            Log.d("TAG", it.getJSONObject().toString())
        } ?: run {
            Text("It's not exist - $id")
        }

        // find model with AND
        ModelUser.findOneAND(hashMapOf("fname" to "First name", "lname" to "Last name"))?.let {
            Text("Find model 'findOneAND'")
            Log.d("TAG", it.getJSONObject().toString())
        }

        // find models list with AND
        ModelUser.findAND(hashMapOf("fname" to "First name", "lname" to "Last name")).firstOrNull()
            ?.let {
                Text("Find model 'findAND'")
                Log.d("TAG", it.getJSONObject().toString())
            }

        // find model with OR
        ModelUser.findOneOR(hashMapOf("fname" to "First name", "lname" to "Last name"))?.let {
            Text("Find model 'findOneOR'")
            Log.d("TAG", it.getJSONObject().toString())
        }

        // find models list with OR
        ModelUser.findOR(hashMapOf("fname" to "First name", "lname" to "Last name")).firstOrNull()?.let {
            Text("Find model 'findOR'")
            Log.d("TAG", it.getJSONObject().toString())
        }

        // find models list
        ModelUser.findAll().firstOrNull()?.let {
            Text("Find model 'findAll'")
            Log.d("TAG", it.getJSONObject().toString())
        }

        // update model
        ModelUser.find("id", id)?.let {
            it.fname = "First name UPDATE"
            it.address.city = "City UPDATE"
            it.parents?.firstOrNull()?.let { p ->
                p.fname = "First name mather UPDATE"
            }
            it.createOrUpdate()
        }

        // show update
        ModelUser.find("id", id)?.let {
            Text("Find after Update model")
            Log.d("TAG", it.getJSONObject().toString())
        }
    }
}

ormlite-extension's People

Contributors

keygenqt avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.