dbi's People
Forkers
isgashodbi's Issues
Extending dbi for more functionality and ease of use
Here is a pseudo code/demo that includes some pretend features that make this slightly more extendable.
#[dbi_trait(impl_for(new="UserDao"))]
pub trait UserDaoImpl {
#[schema (production)]
-- helps with testing in 'test' environments and testing fn's in isolation
#[base (SELECT u.user_id, u.name FROM #schema.users u )
-- its good practice not to use 'SELECT * FROM', because if someone adds a column to that table, it can break your code
#[sql_query(base + "WHERE u.user_id = :id", use_named_params=true)]
fn find_by_id(self, id: i32) -> Box<Future<Item=Option<User>, Error=my::errors::Error> + Send>;
#[sql_query(base + "WHERE u.user_id = ?", mapper="|row| { let (id, full_name) = my::from_row_opt(row)?; Ok(User {id, full_name}) }")]
fn find_by_id_faster(self, id: i32) -> Box<Future<Item=Option<User>, Error=my::errors::Error> + Send>;
#[sql_query(base + "JOIN #schema.parent p USING (parent_id) WHERE p.parent_id = ?", mapper="|row| { let (id, full_name) = my::from_row_opt(row)?; Ok(Parent {id, full_name}) }")]
fn find_by_parent_id_faster(self, id: i32) -> Box<Future<Item=Option<User>, Error=my::errors::Error> + Send>;
#[sql_query("SELECT u.name FROM users u")]
fn find_all_names(self) -> Box<futures::Future<Item=Vec<String>, Error=my::errors::Error> + Send>;
#[sql_update("INSERT INTO #schema.users (name,email_id,parent_id) VALUES (:name, :email_id, :parent_id)
ON DUPLICATE KEY UPDATE names=VALUES(name),parent_id=VALUES(parent_id)", use_named_params=true)]
fn create_user_named(self, name: String) -> Box<futures::Future<Item=Option<u64>, Error=my::errors::Error> + Send>;
-- INSERT or UPDATE (UPSERT)
-- If not exist: INSERT, if exists UPDATE non-PRIMARY/UNIQUE columns
-- if exists and data is the same, database says 'OK', but does nothing and wastes no IO.
#[sql_create(CREATE TABLE IF NOT EXISTS #schema.users
user_id int unsigned not null auto_increment,
name varchar(255) not null,
email_id int unsigned default null,
parent_id int unsigned default null,
updated_at DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(user_id),
UNIQUE KEY unq_email(email_id)
KEY idx_name(name),
KEY idx_updated(updated_at)) engine = innodb default charset=utf8]
fn setup_table()
-- easier for isolated testing for the functions
-- also helpful to see how the table is built while writing the queries
-- note: updated_at is for tracking down issues, not loading into the app.
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.