Skip to content

Commit

Permalink
Merge branch 'master' of github.com:choptastic/sql_bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
choptastic committed May 30, 2018
2 parents f471907 + 34eb072 commit fa5f977
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.markdown
@@ -1,5 +1,8 @@
## v0.3.1

<<<<<<< HEAD
## v0.4.0 (in progress)
* **BACKWARDS INCOMPATIBLE CHANGE**: Renamed `update` and `insert` to `qupdate` and `qinsert`.
* Created a new `update/[2,3]` and `insert/2` to which are friendly maps to
`plu` and `pli` respectively.
* Handling nested transactions and nested checkouts (just for mysql-otp right now)
* Give a warning message if query breaks in mysql-otp. Previously just
returned `{error, Something}` but this generates an OTP warning as well.
Expand Down
15 changes: 12 additions & 3 deletions README.markdown
Expand Up @@ -354,15 +354,14 @@ they only return a single row. They all start with `fr` for "first record"

### Insert

* `db:qi` or `db:insert` Runs the specified query and returns the `insert_id`
* `db:qi` or `db:qinsert` Runs the specified query and returns the `insert_id`

### Update

* `db:qu` or `db:update`: Run the specified query and returns the number of affected rows.
* `db:qu` or `db:qupdate`: Run the specified query and returns the number of affected rows.

### Update or Delete from a Proplist, Map, or Record


* `db:save(Table, Keyfield, Data)`: Run an update or insert query on the
Table provided with the specified Data as the row data. `Data` can be
either a proplist, a map, or a record (See *Workering with Records* below).
Expand All @@ -375,6 +374,16 @@ they only return a single row. They all start with `fr` for "first record"
* `db:save(Table, Data)`: Like `db:save(Table, Keyfield, Data)` except
`Keyfield` is deduced with `list_to_atom(atom_to_list(Table) ++ "id")`

* `db:update(Table, KeyField, Data)`: Like `save/3` but will always use an
`update` even if the value for the keyfield is `0` or `undefined`.

* `db:update(Table, Data)`: Like `save/2`, but uses an `update` operation.

* `db:insert(Table, Data)`: Like `save/2` but uses an `insert` operation.
**Special Note::** This does not strip out any Keyfields, it inserts the data
exactly as it is, so if the key field has a value of `0`, that's the value that
will be inserted.

#### Working with Records

SQL_Bridge can work with records, however, since records are done at compile time, there are some additional steps that must be performed by you in order to accomplish this. The simplest is to use the `save_record()` functions:
Expand Down
26 changes: 15 additions & 11 deletions src/sql_bridge.erl
Expand Up @@ -31,9 +31,9 @@
-type return_value() :: insert_id() | affected_rows()
| [list() | tuple() | t_dict() | proplist()].
-ifdef(has_maps).
-type proplist_or_map() :: proplist() | map().
-type proplist_or_map() :: tuple() | proplist() | map().
-else.
-type proplist_or_map() :: proplist().
-type proplist_or_map() :: tuple() | proplist().
-endif.

-export_type([
Expand Down Expand Up @@ -71,10 +71,13 @@ dicts(Q) -> dq(Q).
dicts(Q, P) -> dq(Q, P).
dict(Q) -> dfr(Q).
dict(Q, P) -> dfr(Q, P).
update(Q) -> qu(Q).
update(Q, P) -> qu(Q, P).
insert(Q) -> qi(Q).
insert(Q, P) -> qi(Q, P).
qupdate(Q) -> qu(Q).
qupdate(Q, P) -> qu(Q, P).
qinsert(Q) -> qi(Q).
qinsert(Q, P) -> qi(Q, P).
update(Table, Obj) -> plu(Table, Obj).
update(Table, KeyField, Obj) -> plu(Table, KeyField, Obj).
insert(Table, Obj) -> pli(Table, Obj).

-spec lookup() -> db().
% @doc Checks the configuration for how we determine the database we're using
Expand Down Expand Up @@ -281,12 +284,12 @@ plq(Q,ParamList) ->
db_q(proplist,Db,Q,ParamList).


%% TODO: This relies on mysql's insert...set syntax, which isn't valid.
-spec pli(Table :: table(), PropList :: proplist()) -> insert_id().
-spec pli(Table :: table(), Data :: proplist_or_map()) -> insert_id().
%% @doc Inserts a proplist into the table
pli(Table,PropList) when is_atom(Table) ->
pli(atom_to_list(Table),PropList);
pli(Table,InitPropList) ->
pli(Table,InitPropList0) ->
InitPropList = ensure_proplist(InitPropList0),
PropList = filter_fields(Table,InitPropList),
Fields0 = [atom_to_list(F) || {F,_} <- PropList],
Fields = iolist_join(Fields0, ","),
Expand All @@ -296,11 +299,12 @@ pli(Table,InitPropList) ->
SQL = ["insert into ",Table,"(",Fields,") values(",PlaceholderString,");"],
qi(SQL, Values).

-spec plu(Table :: table(), PropList :: proplist()) -> affected_rows().
-spec plu(Table :: table(), PropList :: proplist_or_map()) -> affected_rows().
%% @doc Updates a row from the proplist based on the key `Table ++ "id"` in the Table
plu(Table,PropList) when is_atom(Table) ->
plu(atom_to_list(Table),PropList);
plu(Table,PropList) ->
plu(Table,PropList0) ->
PropList = ensure_proplist(PropList0),
KeyField = list_to_atom(Table ++ "id"),
plu(Table,KeyField,PropList).

Expand Down

0 comments on commit fa5f977

Please sign in to comment.