Giter Site home page Giter Site logo

docelic / text_to_sql_search.cr Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 31 KB

Translates text searches into SQL WHERE clauses

License: MIT License

Crystal 99.66% Makefile 0.34%
crystal text sql search search-in-sql text-to-sql form-to-sql text-to-search granite-orm database-search

text_to_sql_search.cr's Introduction

Welcome to Text To SQL Search

Text to SQL Search is a shard written in Crystal for translating textual searches into SQL WHERE clauses. All the necessary options to configure the specifics of the translation process exist.

The search specifications are generally intended to come from three sources: free-form search inputs on websites, submitted form fields, and command lines. However, there is nothing in the code that would prevent any other uses as long as the input is text.

This shard is for you when you use an SQL data store and want to generate WHERE clauses in a simple and user-intuitive way. It is much better than asking your users to write SQL directly or you manually writing conversions from form inputs into SQL and ORM wrappers' syntax.

The shard works with any database for which the generated SQL WHERE clause is valid. The resulting SQL is usable in Model.all("WHERE ...") invocations in ORM models such as Granite ORM, in any other ORMs that support direct SQL input, and even in manual SQL search invocations.

When search identifiers are recognized to be column names, generated SQL WHEREs default to searching in those columns. When search terms are not identifiers, they are searched in custom-definable sets of fields with custom-definable operators.

Supported Search Types

Let's start with a couple examples to get you going. The examples do not show specific configuration settings or char-by-char accurate SQL that is generated; they provide a general intro to what is supported and what you get out of the box.

Let's say we want to provide a text search for use in automobile sales. These examples work and are part of the spec suite:

INPUT: sedan    4 doors    > 2000 ccm    price < 20k    with    no    downpayment
WHERE: "type"='sedan' AND "doors"='4' AND "ccm">'2000' AND "price"<'20000' AND not("downpayment">'0')

INPUT: > 3000 ccm or with    stereo
WHERE: ccm>3000 or stereo>0

INPUT: ((4 door and color = blue) or !downpayment) and price < than 5000
WHERE: ((doors=4 AND color=blue) OR not(downpayment>0)) AND price<5000

INPUT: color = "metallic red" or year: 2015
WHERE: color='metallic red' or year > 2015

Installation

Ensure you have the necessary dependencies:

Then:

$ git clone https://github.com/docelic/text_to_sql_search.cr.git
$ cd text_to_sql_search.cr
$ make
$ make spec

Contributing

  1. Fork it (https://github.com/docelic/text_to_sql_search.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details

text_to_sql_search.cr's People

Contributors

docelic avatar

Stargazers

 avatar  avatar

Watchers

 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.